From 29a97b4aa3063953ddcef8195eca72cb2194ed38 Mon Sep 17 00:00:00 2001 From: Daniel Carrillo Date: Sat, 8 Jun 2024 15:09:31 +0200 Subject: [PATCH] [neovim] Use conform plugin for auto formatting --- .config/nvim/lazy-lock.json | 13 +++++------- .config/nvim/lua/core/keymaps.lua | 9 +++----- .config/nvim/lua/core/lazy.lua | 7 ++++++- .config/nvim/lua/plugins/cmp.lua | 6 +++--- .config/nvim/lua/plugins/dap.lua | 1 + .config/nvim/lua/plugins/go.lua | 12 ----------- .config/nvim/lua/plugins/lsp/conform.lua | 26 ++++++++++++++++++++++++ .config/nvim/lua/plugins/lsp/init.lua | 1 + .config/nvim/lua/plugins/lsp/none-ls.lua | 7 ------- 9 files changed, 45 insertions(+), 37 deletions(-) create mode 100644 .config/nvim/lua/plugins/lsp/conform.lua diff --git a/.config/nvim/lazy-lock.json b/.config/nvim/lazy-lock.json index 26369bd..b5dd092 100644 --- a/.config/nvim/lazy-lock.json +++ b/.config/nvim/lazy-lock.json @@ -1,20 +1,17 @@ { "Comment.nvim": { "branch": "master", "commit": "e51f2b142d88bb666dcaa77d93a07f4b419aca70" }, "FixCursorHold.nvim": { "branch": "master", "commit": "1900f89dc17c603eec29960f57c00bd9ae696495" }, - "LuaSnip": { "branch": "master", "commit": "03c8e67eb7293c404845b3982db895d59c0d1538" }, "auto-save.nvim": { "branch": "main", "commit": "4b59610e1318f8a89501cee9d47a0e8650f0a4d5" }, "b64.nvim": { "branch": "main", "commit": "e93d1a7e065f6eaf5d3bae9efb9bc2c30907f471" }, "bufferline.nvim": { "branch": "main", "commit": "99337f63f0a3c3ab9519f3d1da7618ca4f91cffe" }, "cmp-buffer": { "branch": "main", "commit": "3022dbc9166796b644a841a02de8dd1cc1d311fa" }, "cmp-nvim-lsp": { "branch": "main", "commit": "39e2eda76828d88b773cc27a3f61d2ad782c922d" }, "cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" }, - "cmp_luasnip": { "branch": "master", "commit": "05a9ab28b53f71d1aece421ef32fee2cb857a843" }, "copilot-cmp": { "branch": "master", "commit": "72fbaa03695779f8349be3ac54fa8bd77eed3ee3" }, "copilot-lualine": { "branch": "main", "commit": "4cca52f4d4d6c7439c51227d8054e346ef5ff0e0" }, "copilot.lua": { "branch": "master", "commit": "f7612f5af4a7d7615babf43ab1e67a2d790c13a6" }, "copilotchat.nvim": { "branch": "canary", "commit": "82923efe22b604cf9c0cad0bb2a74aa9247755ab" }, "diffview.nvim": { "branch": "main", "commit": "3afa6a053f680e9f1329c4a151db988a482306cd" }, - "friendly-snippets": { "branch": "main", "commit": "e11b09bf10706bb74e16e4c3d11b2274d62e687f" }, "fzy-lua-native": { "branch": "master", "commit": "820f745b7c442176bcc243e8f38ef4b985febfaf" }, "git-blame.nvim": { "branch": "master", "commit": "408d5487d908dfe5d48e5645d8b27ddcc16b11e0" }, "github-nvim-theme": { "branch": "main", "commit": "d832925e77cef27b16011a8dfd8835f49bdcd055" }, @@ -44,14 +41,14 @@ "none-ls.nvim": { "branch": "main", "commit": "8691504118b252d64fc5023a104aedd100ab754a" }, "nui.nvim": { "branch": "main", "commit": "322978c734866996274467de084a95e4f9b5e0b1" }, "nvim-autopairs": { "branch": "master", "commit": "c15de7e7981f1111642e7e53799e1211d4606cb9" }, - "nvim-cmp": { "branch": "main", "commit": "5260e5e8ecadaf13e6b82cf867a909f54e15fd07" }, + "nvim-cmp": { "branch": "main", "commit": "a110e12d0b58eefcf5b771f533fc2cf3050680ac" }, "nvim-dap": { "branch": "master", "commit": "5ba8ceace596360321cf33fa4b56d9d46e057ce9" }, "nvim-dap-python": { "branch": "master", "commit": "ae0225d0d4a46e18e6057ab3701ef87bbbd6aaad" }, "nvim-dap-ui": { "branch": "master", "commit": "f7d75cca202b52a60c520ec7b1ec3414d6e77b0f" }, "nvim-dap-virtual-text": { "branch": "master", "commit": "d7c695ea39542f6da94ee4d66176f5d660ab0a77" }, "nvim-lspconfig": { "branch": "master", "commit": "92166b89ab4b3d60f24e58170cac53b7141fd032" }, "nvim-nio": { "branch": "master", "commit": "632024157d01e8bc48fd7df6a7de8ffe3fdd4f3a" }, - "nvim-treesitter": { "branch": "master", "commit": "c5cbd3ec74f6f5ddbac939e6f24b99fe78262b4c" }, + "nvim-treesitter": { "branch": "master", "commit": "c1e1e24b6433d4539bad9f5daa207df39633071b" }, "nvim-ts-context-commentstring": { "branch": "main", "commit": "cb064386e667def1d241317deed9fd1b38f0dc2e" }, "nvim-ufo": { "branch": "main", "commit": "aa2e676af592b4e99c105d80d6eafd1afc215d99" }, "nvim-web-devicons": { "branch": "master", "commit": "b4b302d6ae229f67df7a87ef69fa79473fe788a9" }, @@ -61,11 +58,11 @@ "statuscol.nvim": { "branch": "main", "commit": "2eaca29ef3079dedc484dbd7824ad723e8d35676" }, "telescope-fzf-native.nvim": { "branch": "main", "commit": "9ef21b2e6bb6ebeaf349a0781745549bbb870d27" }, "telescope-ui-select.nvim": { "branch": "master", "commit": "6e51d7da30bd139a6950adf2a47fda6df9fa06d2" }, - "telescope.nvim": { "branch": "master", "commit": "dfa230be84a044e7f546a6c2b0a403c739732b86" }, + "telescope.nvim": { "branch": "master", "commit": "3a743491e5c6be0ed0aa8c31c6905df8f66179ba" }, "tokyonight.nvim": { "branch": "main", "commit": "02e9028fe3560f38363c2d38f1c87e45eb04fdb3" }, - "trouble.nvim": { "branch": "main", "commit": "c11dc2777d52da2c8da25836817e43608ec951a5" }, + "trouble.nvim": { "branch": "main", "commit": "806c50491078b66daf13c408042f2e74da46d0ff" }, "typescript-tools.nvim": { "branch": "master", "commit": "c43d9580c3ff5999a1eabca849f807ab33787ea7" }, - "venv-selector.nvim": { "branch": "regexp", "commit": "b691d0fc88f51aa901e2a1889d5a2ea5f7d777a9" }, + "venv-selector.nvim": { "branch": "regexp", "commit": "203c9046e1c0787ec00edfd456b30e4381afbfd1" }, "vim-bbye": { "branch": "master", "commit": "25ef93ac5a87526111f43e5110675032dbcacf56" }, "vim-illuminate": { "branch": "master", "commit": "5eeb7951fc630682c322e88a9bbdae5c224ff0aa" }, "vim-kitty-navigator": { "branch": "master", "commit": "20abf8613aa228a5def1ae02cd9da0f2d210352a" }, diff --git a/.config/nvim/lua/core/keymaps.lua b/.config/nvim/lua/core/keymaps.lua index 58cdb20..514b302 100644 --- a/.config/nvim/lua/core/keymaps.lua +++ b/.config/nvim/lua/core/keymaps.lua @@ -125,12 +125,9 @@ keymap( "Lspsaga peek_definition", vim.tbl_extend("force", opts, { desc = "Peek the definition of the directive under the cursor" }) ) -keymap( - { "n", "v" }, - "lf", - "lua vim.lsp.buf.format{ async=true }", - vim.tbl_extend("force", opts, { desc = "Format the current buffer or selection" }) -) +keymap({ "n", "v" }, "lf", function() + require("conform").format({ async = true, lsp_fallback = true }) +end, vim.tbl_extend("force", opts, { desc = "Format the current buffer or selection" })) -- Neotest keymap( diff --git a/.config/nvim/lua/core/lazy.lua b/.config/nvim/lua/core/lazy.lua index d4ad417..fd72c60 100644 --- a/.config/nvim/lua/core/lazy.lua +++ b/.config/nvim/lua/core/lazy.lua @@ -114,7 +114,7 @@ require("lazy").setup({ { "numToStr/Comment.nvim", version = "v0.*" }, { "JoosepAlviste/nvim-ts-context-commentstring" }, { "nvim-tree/nvim-web-devicons", lazy = true }, - { "akinsho/bufferline.nvim", event = "VeryLazy", branch = "main" }, -- version = "v4.*" }, + { "akinsho/bufferline.nvim", event = "VeryLazy", version = "v4.*" }, { "moll/vim-bbye" }, { "nvim-lualine/lualine.nvim", event = "VeryLazy" }, { "lukas-reineke/indent-blankline.nvim", event = "BufReadPost", main = "ibl" }, @@ -185,6 +185,11 @@ require("lazy").setup({ { "folke/trouble.nvim" }, { "glepnir/lspsaga.nvim", event = "BufRead" }, { "arkav/lualine-lsp-progress" }, + { + "stevearc/conform.nvim", + event = { "BufWritePre" }, + cmd = { "ConformInfo" }, + }, -- Telescope { "nvim-telescope/telescope.nvim", cmd = "Telescope" }, diff --git a/.config/nvim/lua/plugins/cmp.lua b/.config/nvim/lua/plugins/cmp.lua index dddf9a3..45b8538 100644 --- a/.config/nvim/lua/plugins/cmp.lua +++ b/.config/nvim/lua/plugins/cmp.lua @@ -45,9 +45,9 @@ cmp.setup({ [""] = cmp.mapping.complete(), [""] = cmp.mapping.abort(), [""] = cmp.mapping.confirm(), - [""] = cmp.mapping(function(fallback) - cmp.select_next_item() - end), + -- [""] = cmp.mapping(function(fallback) + -- cmp.select_next_item() + -- end), [""] = function(fallback) cmp.abort() fallback() diff --git a/.config/nvim/lua/plugins/dap.lua b/.config/nvim/lua/plugins/dap.lua index 53f4926..cb20652 100644 --- a/.config/nvim/lua/plugins/dap.lua +++ b/.config/nvim/lua/plugins/dap.lua @@ -28,6 +28,7 @@ dapui.setup({ }) vim.fn.sign_define("DapBreakpoint", { text = "", texthl = "DiagnosticSignError", linehl = "", numhl = "" }) +vim.fn.sign_define("DapStopped", { text = "󰔰", texthl = "DiagnosticSignWarn", linehl = "", numhl = "" }) dap.listeners.after.event_initialized.dapui_config = function() dapui.open() diff --git a/.config/nvim/lua/plugins/go.lua b/.config/nvim/lua/plugins/go.lua index eff7dc4..b0fa621 100644 --- a/.config/nvim/lua/plugins/go.lua +++ b/.config/nvim/lua/plugins/go.lua @@ -8,15 +8,3 @@ require("go").setup({ require("guihua.maps").setup({ maps = { close_view = "" }, }) - -vim.cmd("autocmd FileType go nmap gf :lua require('go.format').goimport()") - -local format_sync_grp = vim.api.nvim_create_augroup("GoFormat", {}) -vim.api.nvim_create_autocmd("BufWritePre", { - pattern = "*.go", - callback = function() - require('go.format').goimports() - end, - group = format_sync_grp, -}) - diff --git a/.config/nvim/lua/plugins/lsp/conform.lua b/.config/nvim/lua/plugins/lsp/conform.lua new file mode 100644 index 0000000..cd21ab5 --- /dev/null +++ b/.config/nvim/lua/plugins/lsp/conform.lua @@ -0,0 +1,26 @@ +require("conform").setup({ + formatters_by_ft = { + go = { "goimports", "gofumpt" }, + javascript = { "prettier" }, + json = { "prettier" }, + lua = { "stylua" }, + markdown = { "prettier" }, + python = { "isort", "black" }, + typescript = { "prettier" }, + yaml = { "prettier" }, + }, + formatters = { + { + command = "black", + args = { "--line-length", "100" }, + }, + }, + format_on_save = function(bufnr) + local filetypes = { "go", "typescript", "lua" } + if not vim.tbl_contains(filetypes, vim.bo[bufnr].filetype) then + return + end + + return { timeout_ms = 500, lsp_fallback = false } + end, +}) diff --git a/.config/nvim/lua/plugins/lsp/init.lua b/.config/nvim/lua/plugins/lsp/init.lua index 429d5e2..478a888 100644 --- a/.config/nvim/lua/plugins/lsp/init.lua +++ b/.config/nvim/lua/plugins/lsp/init.lua @@ -1,6 +1,7 @@ require("plugins.lsp.mason") require("plugins.lsp.handlers").setup() require("plugins.lsp.none-ls") +require("plugins.lsp.conform") require("plugins.lsp.lsp-saga") local win = require("lspconfig.ui.windows") diff --git a/.config/nvim/lua/plugins/lsp/none-ls.lua b/.config/nvim/lua/plugins/lsp/none-ls.lua index 83c67d5..d3e00f5 100644 --- a/.config/nvim/lua/plugins/lsp/none-ls.lua +++ b/.config/nvim/lua/plugins/lsp/none-ls.lua @@ -1,6 +1,4 @@ local none_ls = require("null-ls") --- https://github.com/nvimtools/none-ls.nvim/tree/main/lua/null-ls/builtins/formatting -local formatting = none_ls.builtins.formatting -- https://github.com/nvimtools/none-ls.nvim/tree/main/lua/null-ls/builtins/diagnostics local diagnostics = none_ls.builtins.diagnostics @@ -13,11 +11,6 @@ end none_ls.setup({ debug = false, sources = { - formatting.black.with({ - extra_args = { "--fast", "--line-length", "100" }, - }), - formatting.stylua, - formatting.prettier, diagnostics.hadolint, diagnostics.markdownlint, diagnostics.revive.with({