From 1a645ecd7dd5d3f04b475d42fa52e2a89d421727 Mon Sep 17 00:00:00 2001 From: Patrick Auernig Date: Thu, 7 Sep 2023 15:59:57 +0200 Subject: [PATCH] Split up lsp config --- lua/valeth/lazy.lua | 5 +- lua/valeth/plugins/init.lua | 2 + lua/valeth/plugins/lsp.lua | 118 --------------------------- lua/valeth/plugins/lsp/cmp.lua | 40 +++++++++ lua/valeth/plugins/lsp/init.lua | 43 ++++++++++ lua/valeth/plugins/lsp/lspconfig.lua | 49 +++++++++++ lua/valeth/plugins/lsp/mason.lua | 32 ++++++++ lua/valeth/plugins/lsp/ufo.lua | 27 ++++++ 8 files changed, 197 insertions(+), 119 deletions(-) delete mode 100644 lua/valeth/plugins/lsp.lua create mode 100644 lua/valeth/plugins/lsp/cmp.lua create mode 100644 lua/valeth/plugins/lsp/init.lua create mode 100644 lua/valeth/plugins/lsp/lspconfig.lua create mode 100644 lua/valeth/plugins/lsp/mason.lua create mode 100644 lua/valeth/plugins/lsp/ufo.lua diff --git a/lua/valeth/lazy.lua b/lua/valeth/lazy.lua index 52fce86..1f29308 100644 --- a/lua/valeth/lazy.lua +++ b/lua/valeth/lazy.lua @@ -50,4 +50,7 @@ end vim.opt.rtp:prepend(lazypath) -require("lazy").setup("valeth.plugins") +require("lazy").setup({ + { import = "valeth.plugins" }, + { import = "valeth.plugins.lsp" }, +}) diff --git a/lua/valeth/plugins/init.lua b/lua/valeth/plugins/init.lua index e112f88..04cd912 100644 --- a/lua/valeth/plugins/init.lua +++ b/lua/valeth/plugins/init.lua @@ -13,4 +13,6 @@ return { "numToStr/Comment.nvim", opts = {}, }, + + "nvim-lua/plenary.nvim", } diff --git a/lua/valeth/plugins/lsp.lua b/lua/valeth/plugins/lsp.lua deleted file mode 100644 index edc625f..0000000 --- a/lua/valeth/plugins/lsp.lua +++ /dev/null @@ -1,118 +0,0 @@ -local spec = { - "VonHeikemen/lsp-zero.nvim", - branch = "v2.x", -} - -spec.dependencies = { - "neovim/nvim-lspconfig", - -- completion - "hrsh7th/nvim-cmp", - "hrsh7th/cmp-nvim-lsp", - "hrsh7th/cmp-path", - "onsails/lspkind.nvim", - -- formatting - "mhartington/formatter.nvim", - -- snippets - "saadparwaiz1/cmp_luasnip", - "L3MON4D3/LuaSnip", - "rafamadriz/friendly-snippets", - -- lsp manager - "williamboman/mason.nvim", - "williamboman/mason-lspconfig.nvim", - -- code folding - "kevinhwang91/nvim-ufo", - "kevinhwang91/promise-async", -} - -spec.config = function() - local lsp_zero = require("lsp-zero").preset({}) - local ufo = require("ufo") - local tsb = require("telescope.builtin") - - lsp_zero.ensure_installed { - "lua_ls", - "rust_analyzer", - } - - lsp_zero.on_attach(function(_, buffnr) - local opts = { buffer = buffnr, remap = false } - - ufo.attach(buffnr) - - vim.keymap.set("n", "gd", tsb.lsp_definitions, opts) - vim.keymap.set("n", "gr", tsb.lsp_references, opts) - vim.keymap.set("n", "K", vim.lsp.buf.hover, opts) - vim.keymap.set("n", "ca", vim.lsp.buf.code_action, opts) - vim.keymap.set("n", "ci", vim.diagnostic.open_float, opts) - vim.keymap.set("n", "re", vim.lsp.buf.rename, opts) - vim.keymap.set({ "n", "i" }, "", vim.lsp.buf.signature_help, opts) - - vim.keymap.set("n", "fmt", function() - vim.lsp.buf.format({ async = false, timeout_ms = 5000 }) - end) - end) - - - -- NOTE: Format on save does not support async formatting - lsp_zero.format_on_save({ - servers = { - ["rust_analyzer"] = { "rust" } - } - }) - - local lsp_config = require("lspconfig") - - lsp_config.lua_ls.setup(lsp_zero.nvim_lua_ls()) - - lsp_config.rust_analyzer.setup { - settings = { - ["rust-analyzer"] = { - cargo = { - features = "all" - }, - rustfmt = { - -- needs to use nightly channel to use unstable options - extraArgs = { "+nightly" } - } - } - } - } - - ufo.setup { - close_fold_kinds = {"comment", "imports"}, - } - - lsp_zero.setup() - - local cmp = require("cmp") - local lspk = require("lspkind") - - local cmp_select = { behavior = cmp.SelectBehavior.Select } - cmp.setup { - formatting = { - format = lspk.cmp_format({}), - }, - mapping = { - [""] = cmp.mapping.confirm({ select = true }), - [""] = cmp.mapping.select_prev_item(cmp_select), - [""] = cmp.mapping.select_next_item(cmp_select), - [""] = cmp.mapping.complete(), - [""] = nil, - [""] = nil, - } - } - - vim.diagnostic.config({ - virtual_text = false, - }) - - -- Gimme pretty icons - lsp_zero.set_sign_icons({ - error = '✘', - warn = '▲', - hint = '⚑', - info = '»' - }) -end - -return spec diff --git a/lua/valeth/plugins/lsp/cmp.lua b/lua/valeth/plugins/lsp/cmp.lua new file mode 100644 index 0000000..fa726cb --- /dev/null +++ b/lua/valeth/plugins/lsp/cmp.lua @@ -0,0 +1,40 @@ +local spec = { + "hrsh7th/nvim-cmp", + event = "LspAttach", +} + +spec.dependencies = { + "VonHeikemen/lsp-zero.nvim", + "hrsh7th/cmp-nvim-lsp", + "hrsh7th/cmp-path", + "onsails/lspkind.nvim", + "saadparwaiz1/cmp_luasnip", + "L3MON4D3/LuaSnip", + "rafamadriz/friendly-snippets", +} + +spec.config = function() + local cmp = require("cmp") + local lspk = require("lspkind") + + local cmp_select = { behavior = cmp.SelectBehavior.Select } + + cmp.setup { + formatting = { + format = lspk.cmp_format({ + mode = "symbol_text", + }), + }, + mapping = { + [""] = cmp.mapping.confirm({ select = true }), + [""] = cmp.mapping.select_prev_item(cmp_select), + [""] = cmp.mapping.select_next_item(cmp_select), + [""] = cmp.mapping.complete(), + [""] = nil, + [""] = nil, + } + } + +end + +return spec diff --git a/lua/valeth/plugins/lsp/init.lua b/lua/valeth/plugins/lsp/init.lua new file mode 100644 index 0000000..68ed079 --- /dev/null +++ b/lua/valeth/plugins/lsp/init.lua @@ -0,0 +1,43 @@ +-- TODO: figure out how to configure auto formatting and drop lsp-zero +local spec = { + "VonHeikemen/lsp-zero.nvim", + branch = "v2.x", +} + +spec.dependencies = { + "neovim/nvim-lspconfig", + "hrsh7th/nvim-cmp", + "mhartington/formatter.nvim", + "williamboman/mason.nvim", +} + +spec.config = function() + local lsp_zero = require("lsp-zero").preset({}) + + -- NOTE: Format on save does not support async formatting + lsp_zero.format_on_save({ + servers = { + ["rust_analyzer"] = { "rust" } + } + }) + + local lsp_config = require("lspconfig") + + lsp_config.lua_ls.setup(lsp_zero.nvim_lua_ls()) + + lsp_zero.setup() + + vim.diagnostic.config({ + virtual_text = false, + }) + + -- Gimme pretty icons + lsp_zero.set_sign_icons({ + error = '✘', + warn = '▲', + hint = '⚑', + info = '»' + }) +end + +return spec diff --git a/lua/valeth/plugins/lsp/lspconfig.lua b/lua/valeth/plugins/lsp/lspconfig.lua new file mode 100644 index 0000000..d096b4a --- /dev/null +++ b/lua/valeth/plugins/lsp/lspconfig.lua @@ -0,0 +1,49 @@ +local spec = { + "neovim/nvim-lspconfig", +} + +spec.dependencies = { + "nvim-telescope/telescope.nvim", +} + +spec.config = function() + local lsp_config = require("lspconfig") + local tsb = require("telescope.builtin") + local autocmd = vim.api.nvim_create_autocmd + local autogroup = vim.api.nvim_create_augroup + + autocmd("LspAttach", { + group = autogroup("LspAttachConfig", {}), + callback = function(event) + local opts = { buffer = event.buf, remap = false } + + vim.keymap.set("n", "gd", tsb.lsp_definitions, opts) + vim.keymap.set("n", "gr", tsb.lsp_references, opts) + vim.keymap.set("n", "K", vim.lsp.buf.hover, opts) + vim.keymap.set("n", "ca", vim.lsp.buf.code_action, opts) + vim.keymap.set("n", "ci", vim.diagnostic.open_float, opts) + vim.keymap.set("n", "re", vim.lsp.buf.rename, opts) + vim.keymap.set({ "n", "i" }, "", vim.lsp.buf.signature_help, opts) + + vim.keymap.set("n", "fmt", function() + vim.lsp.buf.format({ async = false, timeout_ms = 5000 }) + end) + end + }) + + lsp_config.rust_analyzer.setup { + settings = { + ["rust-analyzer"] = { + cargo = { + features = "all" + }, + rustfmt = { + -- needs to use nightly channel to use unstable options + extraArgs = { "+nightly" } + } + } + } + } +end + +return spec diff --git a/lua/valeth/plugins/lsp/mason.lua b/lua/valeth/plugins/lsp/mason.lua new file mode 100644 index 0000000..532d202 --- /dev/null +++ b/lua/valeth/plugins/lsp/mason.lua @@ -0,0 +1,32 @@ +local spec = { + "williamboman/mason.nvim", +} + +spec.dependencies = { + "williamboman/mason-lspconfig.nvim", + "neovim/nvim-lspconfig", +} + +spec.config = function() + local mason = require("mason") + local mason_lspconfig = require("mason-lspconfig") + + mason.setup({ + ui = { + icons = { + package_installed = "✓", + package_pending = "➜", + package_uninstalled = "✗" + } + } + }) + + mason_lspconfig.setup({ + ensure_installed = { + "lua_ls", + "rust_analyzer", + } + }) +end + +return spec diff --git a/lua/valeth/plugins/lsp/ufo.lua b/lua/valeth/plugins/lsp/ufo.lua new file mode 100644 index 0000000..c5ec163 --- /dev/null +++ b/lua/valeth/plugins/lsp/ufo.lua @@ -0,0 +1,27 @@ +local spec = { + "kevinhwang91/nvim-ufo", + event = "LspAttach", +} + +spec.dependencies = { + "kevinhwang91/promise-async", +} + +spec.config = function() + local ufo = require("ufo") + local autocmd = vim.api.nvim_create_autocmd + local autogroup = vim.api.nvim_create_augroup + + autocmd("LspAttach", { + group = autogroup("LspUfo", {}), + callback = function(event) + ufo.attach(event.buf) + end + }) + + ufo.setup { + close_fold_kinds = {"comment", "imports"}, + } +end + +return spec