diff --git a/config/nvim/lazy-lock.json b/config/nvim/lazy-lock.json index df789e52..ea66f043 100644 --- a/config/nvim/lazy-lock.json +++ b/config/nvim/lazy-lock.json @@ -14,6 +14,7 @@ "cmp-nvim-lsp": { "branch": "main", "commit": "39e2eda76828d88b773cc27a3f61d2ad782c922d" }, "cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" }, "cmp_luasnip": { "branch": "master", "commit": "05a9ab28b53f71d1aece421ef32fee2cb857a843" }, + "conform.nvim": { "branch": "master", "commit": "62eba813b7501b39612146cbf29cd07f1d4ac29c" }, "copilot-cmp": { "branch": "master", "commit": "b6e5286b3d74b04256d0a7e3bd2908eabec34b44" }, "copilot.lua": { "branch": "master", "commit": "86537b286f18783f8b67bccd78a4ef4345679625" }, "diffview.nvim": { "branch": "main", "commit": "4516612fe98ff56ae0415a259ff6361a89419b0a" }, diff --git a/config/nvim/lua/core/utils.lua b/config/nvim/lua/core/utils.lua index be1a47a8..18be5e0c 100644 --- a/config/nvim/lua/core/utils.lua +++ b/config/nvim/lua/core/utils.lua @@ -88,37 +88,6 @@ function M.signal_handler(signum, callback) end) end ----@param client vim.lsp.Client vim.lsp.Client ----@param bufnr integer buffer number -function M.setup_formatting(client, bufnr) - if not client.supports_method("textDocument/formatting") then - return - end - - local format = function() - if vim.g.lsp_formatting then - vim.lsp.buf.format() - end - end - - local augroup = vim.api.nvim_create_augroup("FormatOnSave", { clear = false }) - vim.api.nvim_clear_autocmds { group = augroup, buffer = bufnr } - - vim.api.nvim_create_autocmd("BufWritePre", { - group = augroup, - buffer = bufnr, - callback = function() - format() - end, - }) - - vim.keymap.set("n", "lf", function() - vim.g.lsp_formatting = not vim.g.lsp_formatting - vim.notify("Format on save: " .. (vim.g.lsp_formatting and "on" or "off")) - format() - end, { buffer = bufnr, desc = "toggle auto formatting" }) -end - function M.lazy_file() -- This autocmd will only trigger when a file was loaded from the cmdline. -- It will render the file as quickly as possible. diff --git a/config/nvim/lua/plugins/formatting.lua b/config/nvim/lua/plugins/formatting.lua new file mode 100644 index 00000000..674c37ac --- /dev/null +++ b/config/nvim/lua/plugins/formatting.lua @@ -0,0 +1,43 @@ +return { + { + "stevearc/conform.nvim", + dependencies = { "mason.nvim" }, + event = "BufWritePre", + cmd = "ConformInfo", + + keys = { + { + "lf", + function() + vim.g.autoformat = not vim.g.autoformat + vim.notify("Format on save: " .. (vim.g.autoformat and "on" or "off")) + if vim.g.autoformat then + require("conform").format() + end + end, + desc = "toggle auto formatting", + }, + }, + + ---@type conform.setupOpts + opts = { + formatters_by_ft = { + lua = { "stylua", lsp_format = "never" }, + sh = { "shfmt" }, + sql = { "sqlfluff" }, + }, + default_format_opts = { + timeout_ms = 3000, + async = false, + quiet = false, + lsp_format = "fallback", + }, + format_on_save = function() + if not vim.g.autoformat then + return + end + return {} + end, + }, + }, +} diff --git a/config/nvim/lua/plugins/leetcode.lua b/config/nvim/lua/plugins/leetcode.lua index 0d3b84ac..16aceb24 100644 --- a/config/nvim/lua/plugins/leetcode.lua +++ b/config/nvim/lua/plugins/leetcode.lua @@ -18,7 +18,7 @@ return { ["enter"] = { function() require("copilot.command").disable() - vim.g.lsp_formatting = true + vim.g.autoformat = true vim.keymap.set("n", "cc", "Leet run", { desc = "Leetcode run testcase" }) vim.keymap.set("n", "cp", "Leet submit", { desc = "Leetcode submit" }) vim.keymap.set("n", [[]], "Leet console", { desc = "Leetcode console" }) diff --git a/config/nvim/lua/plugins/lsp/init.lua b/config/nvim/lua/plugins/lsp/init.lua index d7e3ac63..085951de 100644 --- a/config/nvim/lua/plugins/lsp/init.lua +++ b/config/nvim/lua/plugins/lsp/init.lua @@ -32,8 +32,6 @@ function M.config() vim.lsp.inlay_hint.enable(not enabled) vim.notify("LSP inlay hint: " .. (not enabled and "on" or "off")) end, opts("toggle inlay hists")) - - require("core.utils").setup_formatting(client, bufnr) end local capabilities = require('cmp_nvim_lsp').default_capabilities() diff --git a/config/nvim/lua/plugins/lsp/null-ls.lua b/config/nvim/lua/plugins/lsp/null-ls.lua index e3dc74bc..2ea50844 100644 --- a/config/nvim/lua/plugins/lsp/null-ls.lua +++ b/config/nvim/lua/plugins/lsp/null-ls.lua @@ -3,9 +3,6 @@ return { "nvimtools/none-ls.nvim", opts = { border = "rounded", - on_attach = function(client, bufnr) - require("core.utils").setup_formatting(client, bufnr) - end, }, },