From 7d3109f60eaea889686a2036dbd42c225d6a000e Mon Sep 17 00:00:00 2001 From: Evie Litherland-Smith Date: Sat, 20 May 2023 12:44:21 +0100 Subject: [PATCH] Separate LSP config and nvim-cmp Make nvim-cmp standalone, lazy lone on InsertEnter and be used by neorg to enable treesitter integration (no LSP available) Move LSP config into lspconfig.lua Optimise some startup conditions --- lua/lsp/attach.lua | 31 ----------- lua/lsp/cmp.lua | 53 ------------------ lua/lsp/config.lua | 39 -------------- lua/lsp/flags.lua | 5 -- lua/lsp/null_ls_sources.lua | 19 ------- lua/plugins/comment.lua | 1 - lua/plugins/lspconfig.lua | 105 ++++++++++++++++++++++++++++++------ lua/plugins/luasnip.lua | 9 ++++ lua/plugins/magma_nvim.lua | 2 +- lua/plugins/neogit.lua | 4 -- lua/plugins/notify.lua | 2 - lua/plugins/nvim_cmp.lua | 66 +++++++++++++++++++++++ lua/plugins/nvim_neorg.lua | 28 ++++++---- 13 files changed, 184 insertions(+), 180 deletions(-) delete mode 100644 lua/lsp/attach.lua delete mode 100644 lua/lsp/cmp.lua delete mode 100644 lua/lsp/config.lua delete mode 100644 lua/lsp/flags.lua delete mode 100644 lua/lsp/null_ls_sources.lua create mode 100644 lua/plugins/luasnip.lua create mode 100644 lua/plugins/nvim_cmp.lua diff --git a/lua/lsp/attach.lua b/lua/lsp/attach.lua deleted file mode 100644 index 3ae2970..0000000 --- a/lua/lsp/attach.lua +++ /dev/null @@ -1,31 +0,0 @@ --- Use an on_attach function to only map the following keys --- after the language server attaches to the current buffer -local on_attach = function(client, bufnr) - -- Mappings. - -- See `:help vim.lsp.*` for documentation on any of the below functions - local bufopts = { noremap = true, silent = true, buffer = bufnr } - vim.keymap.set("n", "gd", vim.lsp.buf.definition, bufopts) - vim.keymap.set("n", "gr", vim.lsp.buf.references, bufopts) - vim.keymap.set("n", "gi", vim.lsp.buf.implementation, bufopts) - vim.keymap.set("n", "K", vim.lsp.buf.hover, bufopts) - vim.keymap.set("n", "D", vim.lsp.buf.type_definition, bufopts) - vim.keymap.set("n", "rn", vim.lsp.buf.rename, bufopts) - vim.keymap.set("n", "ca", vim.lsp.buf.code_action, bufopts) - vim.keymap.set( - "n", - "i", - function() vim.lsp.buf.format { bufnr = bufnr, timeout_ms = 2000, async = true } end, - bufopts - ) - - -- specific client tweaks - if client.name ~= "null-ls" then client.server_capabilities.documentFormattingProvider = false end - if client.name == "ruff_lsp" then client.server_capabilities.hoverProvider = false end - - -- Attach navic and navbuddy if applicable - if client.server_capabilities.documentSymbolProvider then - require("nvim-navic").attach(client, bufnr) - require("nvim-navbuddy").attach(client, bufnr) - end -end -return on_attach diff --git a/lua/lsp/cmp.lua b/lua/lsp/cmp.lua deleted file mode 100644 index 553a2c4..0000000 --- a/lua/lsp/cmp.lua +++ /dev/null @@ -1,53 +0,0 @@ -local cmp = require "cmp" -local luasnip = require "luasnip" -local cmp_autopairs = require "nvim-autopairs.completion.cmp" - -cmp.setup { - snippet = { - expand = function(args) - luasnip.lsp_expand(args.body) -- For `luasnip` users. - end, - }, - mapping = cmp.mapping.preset.insert { - [""] = cmp.mapping.scroll_docs(-4), -- Up - [""] = cmp.mapping.scroll_docs(4), -- Down - [""] = cmp.mapping.complete(), - [""] = cmp.mapping.confirm { - behavior = cmp.ConfirmBehavior.Replace, - select = true, - }, - [""] = cmp.mapping(function(fallback) - if cmp.visible() then - cmp.select_next_item() - elseif luasnip.expand_or_jumpable() then - luasnip.expand_or_jump() - else - fallback() - end - end, { "i", "s" }), - [""] = cmp.mapping(function(fallback) - if cmp.visible() then - cmp.select_prev_item() - elseif luasnip.jumpable(-1) then - luasnip.jump(-1) - else - fallback() - end - end, { "i", "s" }), - }, - sources = { - { name = "nvim_lsp" }, - { name = "luasnip" }, - }, -} - --- Set configuration for specific filetype. --- cmp.setup.filetype("python", { sources = { { name = "nvim_lsp" } } }) --- cmp.setup.filetype("nix", { sources = { { name = "nvim_lsp" } } }) -cmp.setup.cmdline("/", { mapping = cmp.mapping.preset.cmdline(), sources = { { name = "buffer" } } }) -cmp.setup.cmdline(":", { - mapping = cmp.mapping.preset.cmdline(), - sources = { { name = "cmdline" }, { name = "path" } }, -}) - -cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done()) diff --git a/lua/lsp/config.lua b/lua/lsp/config.lua deleted file mode 100644 index 53d2977..0000000 --- a/lua/lsp/config.lua +++ /dev/null @@ -1,39 +0,0 @@ -local M = {} - --- Mappings. --- See `:help vim.diagnostic.*` for documentation on any of the below functions -local opts = { noremap = true, silent = true } -vim.keymap.set("n", "e", vim.diagnostic.open_float, opts) -vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, opts) -vim.keymap.set("n", "]d", vim.diagnostic.goto_next, opts) -vim.keymap.set("n", "q", vim.diagnostic.setloclist, opts) - -local signs = { Error = " ", Warn = " ", Hint = " ", Info = " " } -for type, icon in pairs(signs) do - local hl = "DiagnosticSign" .. type - vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl }) -end - -require("neodev").setup() - -M.on_attach = require "lsp.attach" -M.lsp_flags = require "lsp.flags" -M.capabilities = require("cmp_nvim_lsp").default_capabilities() - -local servers = { "pyright", "ruff_lsp", "nil_ls", "lua_ls", "fortls", "yamlls", "vimls", "bashls" } -for _, name in ipairs(servers) do - require("lspconfig")[name].setup { - on_attach = M.on_attach, - flags = M.lsp_flags, - capabilities = M.capabilities, - } -end - -require("null-ls").setup { - sources = require "lsp.null_ls_sources", - on_attach = M.on_attach, - flags = M.lsp_flags, - capabilities = M.capabilities, -} - -return M diff --git a/lua/lsp/flags.lua b/lua/lsp/flags.lua deleted file mode 100644 index 3f61fca..0000000 --- a/lua/lsp/flags.lua +++ /dev/null @@ -1,5 +0,0 @@ -local lsp_flags = { - -- This is the default in Nvim 0.7+ - debounce_text_changes = 150, -} -return lsp_flags diff --git a/lua/lsp/null_ls_sources.lua b/lua/lsp/null_ls_sources.lua deleted file mode 100644 index c827eaa..0000000 --- a/lua/lsp/null_ls_sources.lua +++ /dev/null @@ -1,19 +0,0 @@ -local null_ls = require "null-ls" -return { - null_ls.builtins.code_actions.gitsigns, - - null_ls.builtins.formatting.alejandra, - null_ls.builtins.formatting.autoflake, - null_ls.builtins.formatting.beautysh, - null_ls.builtins.formatting.black, - null_ls.builtins.formatting.fixjson, - -- null_ls.builtins.formatting.fprettify, - null_ls.builtins.formatting.isort, - null_ls.builtins.formatting.mdformat, - -- null_ls.builtins.formatting.nixfmt, - -- null_ls.builtins.formatting.nixpkgs_fmt, - null_ls.builtins.formatting.shellharden, - null_ls.builtins.formatting.stylua, - - null_ls.builtins.hover.dictionary, -} diff --git a/lua/plugins/comment.lua b/lua/plugins/comment.lua index babc58b..6cd3fe0 100644 --- a/lua/plugins/comment.lua +++ b/lua/plugins/comment.lua @@ -1,7 +1,6 @@ return { "numToStr/Comment.nvim", keys = { "gcc", "gcb" }, - event = { "BufReadPre" }, config = true, opts = { ignore = "^$" }, } diff --git a/lua/plugins/lspconfig.lua b/lua/plugins/lspconfig.lua index 507695b..8f2746e 100644 --- a/lua/plugins/lspconfig.lua +++ b/lua/plugins/lspconfig.lua @@ -1,18 +1,93 @@ +local on_attach = function(client, bufnr) + -- Mappings. + -- See `:help vim.lsp.*` for documentation on any of the below functions + local bufopts = { noremap = true, silent = true, buffer = bufnr } + vim.keymap.set("n", "gd", vim.lsp.buf.definition, bufopts) + vim.keymap.set("n", "gr", vim.lsp.buf.references, bufopts) + vim.keymap.set("n", "gi", vim.lsp.buf.implementation, bufopts) + vim.keymap.set("n", "K", vim.lsp.buf.hover, bufopts) + vim.keymap.set("n", "D", vim.lsp.buf.type_definition, bufopts) + vim.keymap.set("n", "rn", vim.lsp.buf.rename, bufopts) + vim.keymap.set("n", "ca", vim.lsp.buf.code_action, bufopts) + vim.keymap.set( + "n", + "i", + function() vim.lsp.buf.format { bufnr = bufnr, timeout_ms = 2000, async = true } end, + bufopts + ) + + -- specific client tweaks + if client.name ~= "null-ls" then client.server_capabilities.documentFormattingProvider = false end + if client.name == "ruff_lsp" then client.server_capabilities.hoverProvider = false end + + -- Attach navic and navbuddy if applicable + if client.server_capabilities.documentSymbolProvider then + require("nvim-navic").attach(client, bufnr) + require("nvim-navbuddy").attach(client, bufnr) + end +end + +local config = function() + require("neodev").setup() + + -- Mappings. + -- See `:help vim.diagnostic.*` for documentation on any of the below functions + local opts = { noremap = true, silent = true } + vim.keymap.set("n", "e", vim.diagnostic.open_float, opts) + vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, opts) + vim.keymap.set("n", "]d", vim.diagnostic.goto_next, opts) + vim.keymap.set("n", "q", vim.diagnostic.setloclist, opts) + + local signs = { Error = " ", Warn = " ", Hint = " ", Info = " " } + for type, icon in pairs(signs) do + local hl = "DiagnosticSign" .. type + vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl }) + end + + local lsp_flags = { debounce_text_changes = 150 } + local capabilities = require("cmp_nvim_lsp").default_capabilities() + local servers = { + "pyright", + "ruff_lsp", + "nil_ls", + "lua_ls", + "fortls", + "yamlls", + "vimls", + "bashls", + } + for _, name in ipairs(servers) do + require("lspconfig")[name].setup { + on_attach = on_attach, + flags = lsp_flags, + capabilities = capabilities, + } + end + + local builtins = require "null-ls.builtins" + require("null-ls").setup { + sources = { + builtins.code_actions.gitsigns, + builtins.formatting.alejandra, + builtins.formatting.autoflake, + builtins.formatting.beautysh, + builtins.formatting.black, + builtins.formatting.fixjson, + builtins.formatting.isort, + builtins.formatting.mdformat, + builtins.formatting.shellharden, + builtins.formatting.stylua, + builtins.hover.dictionary, + }, + on_attach = on_attach, + flags = lsp_flags, + capabilities = capabilities, + } +end + return { "neovim/nvim-lspconfig", - event = { "BufReadPre", "BufNewFile", "CmdlineEnter" }, - dependencies = { - { "hrsh7th/cmp-nvim-lsp", dependencies = { "hrsh7th/nvim-cmp" } }, - { "hrsh7th/cmp-buffer", dependencies = { "hrsh7th/nvim-cmp" } }, - { "hrsh7th/cmp-path", dependencies = { "hrsh7th/nvim-cmp" } }, - { "hrsh7th/cmp-cmdline", dependencies = { "hrsh7th/nvim-cmp" } }, - { "L3MON4D3/LuaSnip", dependencies = { "hrsh7th/nvim-cmp", "saadparwaiz1/cmp_luasnip" } }, - { "windwp/nvim-autopairs", dependencies = { "hrsh7th/nvim-cmp" } }, - { "folke/neodev.nvim", dependencies = { "hrsh7th/nvim-cmp" } }, - { "jose-elias-alvarez/null-ls.nvim", dependencies = { "davidmh/cspell.nvim" } }, - }, - config = function() - require "lsp.config" - require "lsp.cmp" - end, + event = { "BufReadPre", "BufNewFile" }, + dependencies = { "cmp-nvim-lsp", "folke/neodev.nvim", "jose-elias-alvarez/null-ls.nvim" }, + config = config, } diff --git a/lua/plugins/luasnip.lua b/lua/plugins/luasnip.lua new file mode 100644 index 0000000..2830d48 --- /dev/null +++ b/lua/plugins/luasnip.lua @@ -0,0 +1,9 @@ +return { + "L3MON4D3/LuaSnip", + lazy = true, + dependencies = { + "rafamadriz/friendly-snippets", + config = function() require("luasnip.loaders.from_vscode").lazy_load() end, + }, + -- build = "make install_jsregexp", +} diff --git a/lua/plugins/magma_nvim.lua b/lua/plugins/magma_nvim.lua index f51d26d..ec88bb5 100644 --- a/lua/plugins/magma_nvim.lua +++ b/lua/plugins/magma_nvim.lua @@ -1,4 +1,4 @@ return { "dccsillag/magma-nvim", - event = { "BufReadPre", "BufNewFile" }, + event = { "FileType python" }, } diff --git a/lua/plugins/neogit.lua b/lua/plugins/neogit.lua index 0513d5d..6a8cf8b 100644 --- a/lua/plugins/neogit.lua +++ b/lua/plugins/neogit.lua @@ -10,10 +10,6 @@ return { opts = { disable_commit_confirmation = true, disable_builtin_notifications = true, - -- kind = "floating", - -- commit_popup = { kind = "floating" }, - -- preview_buffer = { kind = "floating" }, - -- popup = { kind = "floating" }, integrations = { diffview = true }, }, } diff --git a/lua/plugins/notify.lua b/lua/plugins/notify.lua index 08f71c3..8ad0e99 100644 --- a/lua/plugins/notify.lua +++ b/lua/plugins/notify.lua @@ -1,10 +1,8 @@ return { "rcarriga/nvim-notify", lazy = true, - -- dependencies = { "catppuccin" }, config = true, opts = { - -- background_colour = require("catppuccin.palettes").get_palette("macchiato").base, fps = 60, render = "default", -- "minimal", "simple" }, diff --git a/lua/plugins/nvim_cmp.lua b/lua/plugins/nvim_cmp.lua new file mode 100644 index 0000000..b78c376 --- /dev/null +++ b/lua/plugins/nvim_cmp.lua @@ -0,0 +1,66 @@ +local config = function() + local cmp = require "cmp" + local luasnip = require "luasnip" + local cmp_autopairs = require "nvim-autopairs.completion.cmp" + + cmp.setup { + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) -- For `luasnip` users. + end, + }, + mapping = cmp.mapping.preset.insert { + [""] = cmp.mapping.scroll_docs(-4), -- Up + [""] = cmp.mapping.scroll_docs(4), -- Down + [""] = cmp.mapping.complete(), + [""] = cmp.mapping.confirm { + behavior = cmp.ConfirmBehavior.Replace, + select = true, + }, + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + else + fallback() + end + end, { "i", "s" }), + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + elseif luasnip.jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end + end, { "i", "s" }), + }, + sources = { { name = "nvim_lsp" }, { name = "luasnip" } }, + } + + -- Set configuration for specific filetype. + cmp.setup.filetype("norg", { sources = { { name = "neorg" } } }) + cmp.setup.cmdline({ "/", "?" }, { mapping = cmp.mapping.preset.cmdline(), sources = { { name = "buffer" } } }) + cmp.setup.cmdline( + ":", + { mapping = cmp.mapping.preset.cmdline(), sources = { { name = "path" }, { name = "cmdline" } } } + ) + + cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done()) +end + +return { + "hrsh7th/nvim-cmp", + event = { "InsertEnter", "CmdlineEnter" }, + dependencies = { + "hrsh7th/cmp-nvim-lsp", + "hrsh7th/cmp-buffer", + "hrsh7th/cmp-path", + "hrsh7th/cmp-cmdline", + "windwp/nvim-autopairs", + "saadparwaiz1/cmp_luasnip", + "L3MON4D3/LuaSnip", + }, + config = config, +} diff --git a/lua/plugins/nvim_neorg.lua b/lua/plugins/nvim_neorg.lua index 19d38ec..e3cc683 100644 --- a/lua/plugins/nvim_neorg.lua +++ b/lua/plugins/nvim_neorg.lua @@ -1,18 +1,26 @@ return { "nvim-neorg/neorg", build = ":Neorg sync-parsers", - dependencies = { { "nvim-lua/plenary.nvim" } }, + event = { "FileType norg" }, + cmd = "Neorg", + lazy = true, + dependencies = { "nvim-lua/plenary.nvim", "nvim-treesitter/nvim-treesitter" }, + init = function() + vim.api.nvim_create_autocmd({ "BufNewFile", "BufReadPost" }, { + pattern = { "*.norg" }, + command = "set filetype=norg", + }) + vim.api.nvim_create_autocmd({ "BufWritePre" }, { + pattern = { "*.norg" }, + command = "Neorg update-metadata", + }) + end, opts = { load = { - ["core.defaults"] = {}, -- Loads default behaviour - ["core.concealer"] = {}, -- Adds pretty icons to your documents - ["core.dirman"] = { -- Manages Neorg workspaces - config = { - workspaces = { - notes = "~/notes", - }, - }, - }, + ["core.defaults"] = {}, + ["core.concealer"] = {}, + ["core.dirman"] = { config = { workspaces = { notes = "~/notes" } } }, + ["core.completion"] = { config = { engine = "nvim-cmp" } }, }, }, }