From f7c84d58f8fc8ebee3abd322d4d65702f3176845 Mon Sep 17 00:00:00 2001 From: "asep.komarudin" Date: Mon, 24 Jun 2024 12:05:25 +0700 Subject: [PATCH] enc: cpp confug default --- lua/custom/default.lua | 2 +- lua/plugins/cppcfg.lua | 27 --------------- lua/plugins/java.lua | 4 +-- lua/plugins/themes/_globalvar.lua | 1 + lua/user/format_onsave.lua | 49 +++++++++++++++++++++++----- lua/user/utils/lualine_component.lua | 2 +- 6 files changed, 46 insertions(+), 39 deletions(-) diff --git a/lua/custom/default.lua b/lua/custom/default.lua index 931cb8e..27798a6 100644 --- a/lua/custom/default.lua +++ b/lua/custom/default.lua @@ -161,6 +161,6 @@ pcode.active_golang_config = false pcode.active_python_config = false pcode.active_cpp_config = true pcode.active_java_config = { - active = true, + active = false, project = "gradle", -- gradle or maven } diff --git a/lua/plugins/cppcfg.lua b/lua/plugins/cppcfg.lua index 6c267cd..5bb7318 100644 --- a/lua/plugins/cppcfg.lua +++ b/lua/plugins/cppcfg.lua @@ -43,33 +43,6 @@ if pcode.active_cpp_config then { "rc", "terminalg++ --debug main.cpp -o main", desc = "Compile Debug main.cpp" }, }, }, - { - "stevearc/conform.nvim", - event = { "BufReadPre", "BufNewFile" }, - opts = function(_, opts) - local psave = pcode.format_on_save or 0 - opts.formatters_by_ft = opts.formatters_by_ft or {} - opts.formatters_by_ft.cpp = { "clang-format" } - if psave == 1 then - opts.format_on_save = { - timeout_ms = pcode.format_timeout_ms or 500, - lsp_fallback = true, - } - end - return opts - end, - config = function(_, opts) - local conform = require("conform") - conform.setup(opts) - vim.keymap.set({ "n", "v" }, "lF", function() - conform.format({ - lsp_fallback = true, - async = false, - timeout_ms = 500, - }) - end, { desc = "Format file or range (in visual mode)" }) - end, - }, } end return M diff --git a/lua/plugins/java.lua b/lua/plugins/java.lua index d520a6e..e5c49d0 100644 --- a/lua/plugins/java.lua +++ b/lua/plugins/java.lua @@ -52,7 +52,7 @@ if pcode.active_java_config.active then opts = function(_, opts) local psave = pcode.format_on_save or 0 opts.formatters_by_ft = opts.formatters_by_ft or {} - opts.formatters_by_ft.java = { "google-java-format" } + opts.formatters_by_ft.java = { "lsp_fmt" } if psave == 1 then opts.format_on_save = { timeout_ms = pcode.format_timeout_ms or 500, @@ -68,7 +68,7 @@ if pcode.active_java_config.active then conform.format({ lsp_fallback = true, async = false, - timeout_ms = 500, + timeout_ms = pcode.format_timeout_ms or 500, }) end, { desc = "Format file or range (in visual mode)" }) end, diff --git a/lua/plugins/themes/_globalvar.lua b/lua/plugins/themes/_globalvar.lua index 8074ffc..37bd3e1 100644 --- a/lua/plugins/themes/_globalvar.lua +++ b/lua/plugins/themes/_globalvar.lua @@ -79,6 +79,7 @@ if pcode.active_cpp_config then table.insert(pcode.treesitter_ensure_installed, "cpp") table.insert(pcode.treesitter_ensure_installed, "c") table.insert(pcode.mason_ensure_installed, "clangd") + table.insert(pcode.null_ls_ensure_installed, "clang_format") table.insert(pcode.dap_ensure_installed, "codelldb") pcode.nvim_dap = true end diff --git a/lua/user/format_onsave.lua b/lua/user/format_onsave.lua index 254ec42..48e15cb 100644 --- a/lua/user/format_onsave.lua +++ b/lua/user/format_onsave.lua @@ -12,6 +12,34 @@ if next(buf_clients) == nil then end if run == 1 then + -- function FORMAT_FILTER(client) + -- local filetype = vim.bo.filetype + -- local n = require("null-ls") + -- local s = require("null-ls.sources") + -- local method = n.methods.FORMATTING + -- local available_formatters = s.get_available(filetype, method) + -- + -- if #available_formatters > 0 then + -- return client.name == "null-ls" + -- elseif client.supports_method("textDocument/formatting") then + -- return true + -- else + -- return false + -- end + -- end + -- + -- vim.cmd([[ + -- augroup _lsp + -- autocmd! + -- " autocmd BufWritePre * lua vim.lsp.buf.format{timeout_ms =200, filter=format_filter} + -- autocmd BufWritePre * lua vim.lsp.buf.format{timeout_ms=pcode.format_timeout_ms or 5000 ,filter=FORMAT_FILTER} + -- augroup end + -- ]]) + + ---filter passed to vim.lsp.buf.format + ---always selects null-ls if it's available and caches the value per buffer + ---@param client table client attached to a buffer + ---@return boolean if client matches function FORMAT_FILTER(client) local filetype = vim.bo.filetype local n = require("null-ls") @@ -27,12 +55,17 @@ if run == 1 then return false end end - - vim.cmd([[ - augroup _lsp - autocmd! - " autocmd BufWritePre * lua vim.lsp.buf.format{timeout_ms =200, filter=format_filter} - autocmd BufWritePre * lua vim.lsp.buf.format{timeout_ms=pcode.format_timeout_ms or 5000 ,filter=FORMAT_FILTER} - augroup end - ]]) + vim.api.nvim_create_autocmd("BufWritePre", { + group = "lsp_format_on_save", + pattern = "*", + callback = function() + vim.lsp.buf.format({ timeout_ms = pcode.format_timeout_ms or 5000, filter = FORMAT_FILTER }) + end, + }) +else + vim.schedule(function() + pcall(function() + vim.api.nvim_clear_autocmds({ group = "lsp_format_on_save" }) + end) + end) end diff --git a/lua/user/utils/lualine_component.lua b/lua/user/utils/lualine_component.lua index 5de4785..b293efb 100644 --- a/lua/user/utils/lualine_component.lua +++ b/lua/user/utils/lualine_component.lua @@ -108,7 +108,7 @@ return { -- add conform.nvim local status, conform = pcall(require, "conform") - if status then + if status and supported_formatters and #supported_formatters == 0 then local formatters = conform.list_formatters_for_buffer() if formatters and #formatters > 0 then vim.list_extend(buf_client_names, formatters)