From 234f4c22578bd19c36572a165b9f3cac83d4ceaa Mon Sep 17 00:00:00 2001 From: Moshe Avni Date: Thu, 18 Apr 2024 11:02:05 +0300 Subject: [PATCH] refactor-lsp --- .config/nvim/lua/plugins/lsp/init.lua | 220 +++++--------------------- .config/nvim/lua/user/lsp/actions.lua | 54 +++++++ .config/nvim/lua/user/lsp/config.lua | 109 +++++++++++++ .config/nvim/lua/user/lsp/servers.lua | 124 +++++++-------- 4 files changed, 262 insertions(+), 245 deletions(-) create mode 100644 .config/nvim/lua/user/lsp/actions.lua create mode 100644 .config/nvim/lua/user/lsp/config.lua diff --git a/.config/nvim/lua/plugins/lsp/init.lua b/.config/nvim/lua/plugins/lsp/init.lua index 61201751..3a74c9e6 100644 --- a/.config/nvim/lua/plugins/lsp/init.lua +++ b/.config/nvim/lua/plugins/lsp/init.lua @@ -1,81 +1,8 @@ -local actions = function() - return { - ['Format (lp)'] = function() - require('user.lsp.formatting').format() - end, - ['Code Actions (la)'] = function() - vim.lsp.buf.code_action() - end, - ['Code Lens (lx)'] = function() - vim.lsp.codelens.run() - end, - ['Show Definition (gd)'] = function() - vim.cmd 'Lspsaga peek_definition' - end, - ['Show Declaration (gD)'] = function() - vim.lsp.buf.declaration() - end, - ['Show Type Definition (gy)'] = function() - vim.lsp.buf.type_definition() - end, - ['Show Implementation (gi)'] = function() - vim.lsp.buf.implementation() - end, - ['Find References (gr)'] = function() - vim.cmd 'Lspsaga finder' - end, - ['Signature Help (lk)'] = function() - vim.lsp.buf.signature_help() - end, - ['Signature Documentation (K)'] = function() - -- vim.lsp.buf.hover() - vim.cmd 'Lspsaga hover_doc' - end, - ['Rename symbol (lrn)'] = function() - vim.cmd 'Lspsaga rename ++project' - end, - ['Diagnostics quickfix list (lq)'] = function() - vim.diagnostic.setqflist() - end, - ['Clear Diagnostics'] = function() - vim.diagnostic.reset() - end, - ['Delete Log'] = function() - vim.system { 'rm', '-rf', vim.lsp.get_log_path() } - end, - } -end - local M = { 'neovim/nvim-lspconfig', event = { 'BufReadPre', 'BufNewFile' }, opts = { inlay_hints = { enabled = true }, - capabilities = { - textDocument = { - completion = { - completionItem = { - snippetSupport = true, - }, - }, - -- codeAction = { - -- dynamicRegistration = true, - -- codeActionLiteralSupport = { - -- codeActionKind = { - -- valueSet = (function() - -- local res = vim.tbl_values(vim.lsp.protocol.CodeActionKind) - -- table.sort(res) - -- return res - -- end)(), - -- }, - -- }, - -- }, - foldingRange = { - dynamicRegistration = false, - lineFoldingOnly = true, - }, - }, - }, setup = { tsserver = function(_, opts) require('typescript').setup { @@ -84,19 +11,6 @@ local M = { return true end, - yamlls = function(_, opts) - local yaml_cfg = require('yaml-companion').setup { - schemas = opts.settings.yaml.schemas or {}, - builtin_matchers = { - -- Detects Kubernetes files based on content - kubernetes = { enabled = true }, - }, - lspconfig = opts, - } - require('lspconfig')['yamlls'].setup(yaml_cfg) - return true - end, - helm_ls = function() local configs = require 'lspconfig.configs' local util = require 'lspconfig.util' @@ -118,100 +32,50 @@ local M = { }, } -M.init = function() - local start_ls = function() - _G.tmp_write { should_delete = false, new = false } - -- load lsp - require 'lspconfig' - end - vim.keymap.set('n', 'ls', start_ls) - require('user.menu').add_actions('LSP', { - ['Start LSP (ls)'] = function() - start_ls() - end, - }) -end - -M.config = function(_, opts) - require('user.menu').add_actions('LSP', actions()) - require('user.lsp.handlers').setup() - - require('lspconfig.ui.windows').default_options.border = require('user.utils').borders.single_rounded - - -- Set formatting of lsp log - require('vim.lsp.log').set_format_func(vim.inspect) - - local servers = require 'user.lsp.servers' - ------------------ - -- Capabilities -- - ------------------ - local capabilities = vim.tbl_deep_extend( - 'force', - {}, - vim.lsp.protocol.make_client_capabilities(), - has_cmp and cmp_nvim_lsp.default_capabilities() or {}, - opts.capabilities or {} - ) - - ----------------- - -- Diagnostics -- - ----------------- - -- show icons in the sidebar - local signs = { - [vim.diagnostic.severity.ERROR] = '✘', - [vim.diagnostic.severity.WARN] = '', - [vim.diagnostic.severity.HINT] = ' ', - [vim.diagnostic.severity.INFO] = ' ', - } - vim.diagnostic.config { - signs = { text = signs }, - update_in_insert = false, - virtual_text = { - severity = { min = vim.diagnostic.severity.WARN }, - }, - float = { border = require('user.utils').float_border }, - } - - local function setup(server) - local server_opts = vim.tbl_deep_extend('force', { - capabilities = vim.deepcopy(capabilities), - }, servers[server] or {}) - - if opts.setup[server] then - if opts.setup[server](server, server_opts) then - return - end - elseif opts.setup['*'] then - if opts.setup['*'](server, server_opts) then - return - end - end - require('lspconfig')[server].setup(server_opts) - end +M.init = require('user.lsp.config').init + +M.config = function() + require('user.lsp.config').setup() + -- local function setup(server) + -- local server_opts = vim.tbl_deep_extend('force', { + -- capabilities = vim.deepcopy(capabilities), + -- }, servers[server] or {}) + -- + -- if opts.setup[server] then + -- if opts.setup[server](server, server_opts) then + -- return + -- end + -- elseif opts.setup['*'] then + -- if opts.setup['*'](server, server_opts) then + -- return + -- end + -- end + -- require('lspconfig')[server].setup(server_opts) + -- end -- get all the servers that are available thourgh mason-lspconfig - local have_mason, mlsp = pcall(require, 'mason-lspconfig') - local all_mslp_servers = {} - if have_mason then - all_mslp_servers = vim.tbl_keys(require('mason-lspconfig.mappings.server').lspconfig_to_package) - end - - local ensure_installed = {} ---@type string[] - for server, server_opts in pairs(servers) do - if server_opts then - server_opts = server_opts == true and {} or server_opts - -- run manual setup if mason=false or if this is a server that cannot be installed with mason-lspconfig - if server_opts.mason == false or not vim.tbl_contains(all_mslp_servers, server) then - setup(server) - else - ensure_installed[#ensure_installed + 1] = server - end - end - end - - if have_mason then - mlsp.setup { ensure_installed = ensure_installed, handlers = { setup } } - end + -- local have_mason, mlsp = pcall(require, 'mason-lspconfig') + -- local all_mslp_servers = {} + -- if have_mason then + -- all_mslp_servers = vim.tbl_keys(require('mason-lspconfig.mappings.server').lspconfig_to_package) + -- end + -- + -- local ensure_installed = {} ---@type string[] + -- for server, server_opts in pairs(servers) do + -- if server_opts then + -- server_opts = server_opts == true and {} or server_opts + -- -- run manual setup if mason=false or if this is a server that cannot be installed with mason-lspconfig + -- if server_opts.mason == false or not vim.tbl_contains(all_mslp_servers, server) then + -- setup(server) + -- else + -- ensure_installed[#ensure_installed + 1] = server + -- end + -- end + -- end + -- + -- if have_mason then + -- mlsp.setup { ensure_installed = ensure_installed, handlers = { setup } } + -- end end M.dependencies = { diff --git a/.config/nvim/lua/user/lsp/actions.lua b/.config/nvim/lua/user/lsp/actions.lua new file mode 100644 index 00000000..c03985df --- /dev/null +++ b/.config/nvim/lua/user/lsp/actions.lua @@ -0,0 +1,54 @@ +local M = {} +M.actions = function() + return { + ['Format (lp)'] = function() + require('user.lsp.formatting').format() + end, + ['Code Actions (la)'] = function() + vim.lsp.buf.code_action() + end, + ['Code Lens (lx)'] = function() + vim.lsp.codelens.run() + end, + ['Show Definition (gd)'] = function() + vim.cmd 'Lspsaga peek_definition' + end, + ['Show Declaration (gD)'] = function() + vim.lsp.buf.declaration() + end, + ['Show Type Definition (gy)'] = function() + vim.lsp.buf.type_definition() + end, + ['Show Implementation (gi)'] = function() + vim.lsp.buf.implementation() + end, + ['Find References (gr)'] = function() + vim.cmd 'Lspsaga finder' + end, + ['Signature Help (lk)'] = function() + vim.lsp.buf.signature_help() + end, + ['Signature Documentation (K)'] = function() + -- vim.lsp.buf.hover() + vim.cmd 'Lspsaga hover_doc' + end, + ['Rename symbol (lrn)'] = function() + vim.cmd 'Lspsaga rename ++project' + end, + ['Diagnostics quickfix list (lq)'] = function() + vim.diagnostic.setqflist() + end, + ['Clear Diagnostics'] = function() + vim.diagnostic.reset() + end, + ['Delete Log'] = function() + vim.system { 'rm', '-rf', vim.lsp.get_log_path() } + end, + } +end + +M.setup = function() + require('user.menu').add_actions('LSP', M.actions()) +end + +return M diff --git a/.config/nvim/lua/user/lsp/config.lua b/.config/nvim/lua/user/lsp/config.lua new file mode 100644 index 00000000..67a5803e --- /dev/null +++ b/.config/nvim/lua/user/lsp/config.lua @@ -0,0 +1,109 @@ +local M = { + capabilities = { + textDocument = { + completion = { + completionItem = { + snippetSupport = true, + }, + }, + -- codeAction = { + -- dynamicRegistration = true, + -- codeActionLiteralSupport = { + -- codeActionKind = { + -- valueSet = (function() + -- local res = vim.tbl_values(vim.lsp.protocol.CodeActionKind) + -- table.sort(res) + -- return res + -- end)(), + -- }, + -- }, + -- }, + foldingRange = { + dynamicRegistration = false, + lineFoldingOnly = true, + }, + }, + }, + all_mason_lsp_servers = {}, +} + +M.setup_capabilities = function() + ------------------ + -- Capabilities -- + ------------------ + M.capabilities = vim.tbl_deep_extend( + 'force', + {}, + vim.lsp.protocol.make_client_capabilities(), + -- TODO: fix cmp capabilities + has_cmp and cmp_nvim_lsp.default_capabilities() or {}, + M.capabilities or {} + ) +end + +M.diagnostics = function() + ----------------- + -- Diagnostics -- + ----------------- + -- show icons in the sidebar + local signs = { + [vim.diagnostic.severity.ERROR] = '✘', + [vim.diagnostic.severity.WARN] = '', + [vim.diagnostic.severity.HINT] = ' ', + [vim.diagnostic.severity.INFO] = ' ', + } + vim.diagnostic.config { + signs = { text = signs }, + update_in_insert = false, + virtual_text = { + severity = { min = vim.diagnostic.severity.WARN }, + }, + float = { border = require('user.utils').float_border }, + } +end + +M.get_mason_lspconfig = function() + local have_mason, _ = pcall(require, 'mason-lspconfig') + if have_mason then + M.all_mason_lsp_servers = vim.tbl_keys(require('mason-lspconfig.mappings.server').lspconfig_to_package) + end +end + +M.init = function() + local start_ls = function() + _G.tmp_write { should_delete = false, new = false } + -- load lsp + require 'lspconfig' + end + vim.keymap.set('n', 'ls', start_ls) + require('user.menu').add_actions('LSP', { + ['Start LSP (ls)'] = function() + start_ls() + end, + }) +end + +M.setup = function() + require('user.lsp.actions').setup() + require('user.lsp.handlers').setup() + + -- set lsp window border style + require('lspconfig.ui.windows').default_options.border = require('user.utils').borders.single_rounded + + -- Set formatting of lsp log + require('vim.lsp.log').set_format_func(vim.inspect) + + -- set up capabilities + M.setup_capabilities() + + -- set up diagnostics configuration + M.diagnostics() + + -- get all the servers that are available thourgh mason-lspconfig + M.get_mason_lspconfig() + + -- setup lsp servers + require('user.lsp.servers').setup() +end + +return M diff --git a/.config/nvim/lua/user/lsp/servers.lua b/.config/nvim/lua/user/lsp/servers.lua index 6a458f65..b1c01987 100644 --- a/.config/nvim/lua/user/lsp/servers.lua +++ b/.config/nvim/lua/user/lsp/servers.lua @@ -1,40 +1,40 @@ -local on_attaches = require 'user.lsp.on-attach' -local default_on_attach = on_attaches.default - -local M = { - ansiblels = { +local M = {} +M.setup = function() + local on_attaches = require 'user.lsp.on-attach' + local default_on_attach = on_attaches.default + require('lspconfig')['ansiblels'].setup { on_attach = default_on_attach, - }, + } - bashls = { + require('lspconfig')['bashls'].setup { on_attach = default_on_attach, - }, + } - cssls = { + require('lspconfig')['cssls'].setup { on_attach = default_on_attach, - }, + } - cssmodules_ls = { + require('lspconfig')['cssmodules_ls'].setup { on_attach = default_on_attach, - }, + } - dockerls = { + require('lspconfig')['dockerls'].setup { on_attach = default_on_attach, - }, + } - docker_compose_language_service = { + require('lspconfig')['docker_compose_language_service'].setup { on_attach = default_on_attach, - }, + } - groovyls = { + require('lspconfig')['groovyls'].setup { on_attach = default_on_attach, - }, + } - html = { + require('lspconfig')['html'].setup { on_attach = default_on_attach, - }, + } - jsonls = { + require('lspconfig')['jsonls'].setup { on_attach = default_on_attach, settings = { json = { @@ -45,18 +45,18 @@ local M = { validate = { enable = true }, }, }, - }, + } - pyright = { + require('lspconfig')['pyright'].setup { on_attach = default_on_attach, settings = { organizeimports = { provider = 'isort', }, }, - }, + } - lua_ls = { + require('lspconfig')['lua_ls'].setup { on_attach = default_on_attach, settings = { Lua = { @@ -82,17 +82,17 @@ local M = { -- telemetry = { enable = false }, }, }, - }, + } - terraformls = { + require('lspconfig')['terraformls'].setup { on_attach = function(c, b) require('treesitter-terraform-doc').setup {} default_on_attach(c, b) c.server_capabilities.semanticTokensProvider = {} end, - }, + } - tsserver = { + require('lspconfig')['tsserver'].setup { settings = { preferences = { allowRenameOfImportPath = true, @@ -128,13 +128,13 @@ local M = { }, }, on_attach = default_on_attach, - }, + } - vimls = { + require('lspconfig')['vimls'].setup { on_attach = default_on_attach, - }, + } - jdtls = { + require('lspconfig')['jdtls'].setup { on_attach = function(c, b) require('jdtls').setup_dap() default_on_attach(c, b) @@ -143,43 +143,33 @@ local M = { filetypes = { 'kotlin', 'java' }, workspace = { checkThirdParty = false }, }, - }, + } - helm_ls = { + require('lspconfig')['helm_ls'].setup { on_attach = default_on_attach, - }, + } - yamlls = { - on_attach = function(c, b) - local filetype = vim.api.nvim_get_option_value('filetype', { buf = b }) - local buftype = vim.api.nvim_get_option_value('buftype', { buf = b }) - local disabled_fts = { 'helm', 'yaml.gotexttmpl', 'gotmpl' } - if buftype ~= '' or filetype == '' or vim.tbl_contains(disabled_fts, filetype) then - vim.diagnostic.disable(b) - vim.defer_fn(function() - vim.diagnostic.reset(nil, b) - end, 1000) - end - default_on_attach(c, b) - end, - settings = { - redhat = { telemetry = { enabled = false } }, - yaml = { - validate = true, - format = { enable = true }, - hover = true, - trace = { server = 'debug' }, - completion = true, - schemaStore = { - enable = true, - url = 'https://www.schemastore.org/api/json/catalog.json', - }, - schemas = { - kubernetes = '', - }, - }, + local yaml_cfg = require('yaml-companion').setup { + builtin_matchers = { + -- Detects Kubernetes files based on content + kubernetes = { enabled = true }, + }, + lspconfig = { + on_attach = function(c, b) + local filetype = vim.api.nvim_get_option_value('filetype', { buf = b }) + local buftype = vim.api.nvim_get_option_value('buftype', { buf = b }) + local disabled_fts = { 'helm', 'yaml.gotexttmpl', 'gotmpl' } + if buftype ~= '' or filetype == '' or vim.tbl_contains(disabled_fts, filetype) then + vim.diagnostic.disable(b) + vim.defer_fn(function() + vim.diagnostic.reset(nil, b) + end, 1000) + end + default_on_attach(c, b) + end, }, - }, -} + } + require('lspconfig')['yamlls'].setup(yaml_cfg) +end return M