From 42f4ab623ebea15bb8a044ad3e939ca4dc650b86 Mon Sep 17 00:00:00 2001 From: Oscar Wallberg Date: Mon, 4 Sep 2023 04:19:44 +0200 Subject: [PATCH] Refactor lsp --- lua/lsp/config/diagnosticls.lua | 98 ---------------- lua/lsp/config/jedi_language_server.lua | 55 --------- lua/lsp/init.lua | 30 +++-- lua/lsp/servers.lua | 94 --------------- .../{config/clangd.lua => servers/bashls.lua} | 18 ++- .../{config/bashls.lua => servers/clangd.lua} | 21 +++- lua/lsp/{config => servers}/cmake.lua | 14 ++- lua/lsp/servers/diagnosticls.lua | 109 ++++++++++++++++++ lua/lsp/{config => servers}/groovyls.lua | 14 ++- lua/lsp/servers/init.lua | 18 +++ lua/lsp/servers/jedi_language_server.lua | 61 ++++++++++ lua/lsp/{config => servers}/lemminx.lua | 34 ++++-- lua/lsp/{config => servers}/lua_ls.lua | 30 +++-- 13 files changed, 292 insertions(+), 304 deletions(-) delete mode 100644 lua/lsp/config/diagnosticls.lua delete mode 100644 lua/lsp/config/jedi_language_server.lua delete mode 100644 lua/lsp/servers.lua rename lua/lsp/{config/clangd.lua => servers/bashls.lua} (66%) rename lua/lsp/{config/bashls.lua => servers/clangd.lua} (62%) rename lua/lsp/{config => servers}/cmake.lua (71%) create mode 100644 lua/lsp/servers/diagnosticls.lua rename lua/lsp/{config => servers}/groovyls.lua (69%) create mode 100644 lua/lsp/servers/init.lua create mode 100644 lua/lsp/servers/jedi_language_server.lua rename lua/lsp/{config => servers}/lemminx.lua (50%) rename lua/lsp/{config => servers}/lua_ls.lua (60%) diff --git a/lua/lsp/config/diagnosticls.lua b/lua/lsp/config/diagnosticls.lua deleted file mode 100644 index 8cc1400..0000000 --- a/lua/lsp/config/diagnosticls.lua +++ /dev/null @@ -1,98 +0,0 @@ ---[[ - Copyright 2023 Oscar Wallberg - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -]] - -return { - cmd = { "diagnostic-languageserver", "--stdio", }, - single_file_support = true, - -- For more info see https://github.com/iamcco/diagnostic-languageserver - init_options = { - filetypes = { python = "flake8", lua = "luaFormatter", }, - linters = { - -- some help from this: - -- https://github.com/creativenull/diagnosticls-configs-nvim/blob/main/lua/diagnosticls-configs/linters/flake8.lua - flake8 = { - command = "flake8", - args = { - "--config", - "tox.ini", - "--format", - "%(row)d,%(col)d,%(code).1s,%(code)s: %(text)s", - "-", - }, - rootPatterns = { "Pipfile", ".git", "tox.ini", }, - isStdout = true, - isStderr = false, - debounce = 100, - offsetLine = 0, - offsetColumn = 0, - sourceName = "flake8", - formatLines = 1, - formatPattern = { - [[(\d+),(\d+),([A-Z]),(.*)(\r|\n)*$]], - { line = 1, column = 2, security = 3, message = { 4, }, }, - }, - securities = { - -- Available securities are { 'error', 'warning', 'hint', 'info' } - E = "error", - W = "warning", - B = "hint", - F = "error", - D = "info", - }, - }, - }, - formatFiletypes = { - python = { "black", "isort", }, - sh = { "shfmt", }, - }, - formatters = { - black = { - sourceName = "black", - command = "black", - args = { - "--stdin-filename", - "%filename", - "-t", - "py39", - "--quiet", - "-", - }, - rootPatterns = { "Pipfile", ".git", "tox.ini", }, - isStdout = true, - isStderr = false, - ignoreExitCode = false, - }, - isort = { - sourceName = "isort", - command = "isort", - args = { "--quiet", "-", }, - rootPatterns = { "Pipfile", ".git", "tox.ini", }, - isStdout = true, - isStderr = false, - ignoreExitCode = false, - }, - shfmt = { - sourceName = "shfmt", - command = "shfmt", - args = { "-s", "-i", "4", "-ci", }, - rootPatterns = { "Pipfile", ".git", "tox.ini", }, - isStdout = true, - isStderr = false, - ignoreExitCode = false, - }, - }, - }, -} diff --git a/lua/lsp/config/jedi_language_server.lua b/lua/lsp/config/jedi_language_server.lua deleted file mode 100644 index 7bf4889..0000000 --- a/lua/lsp/config/jedi_language_server.lua +++ /dev/null @@ -1,55 +0,0 @@ ---[[ - Copyright 2023 Oscar Wallberg - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -]] - -return { - cmd = { "jedi-language-server", }, - single_file_support = true, - -- For more info see: - -- - https://github.com/pappasam/jedi-language-server#configuration - -- - https://github.com/pappasam/coc-jedi#configuration (good descriptions) - init_options = { - -- Built-in diagnostics seem to be very basic, - -- to the point where you are wondering if it's even active. - -- Will use iamcco/diagnostic-languageserver instead. - diagnostics = { - -- Enables (or disables) diagnostics provided by Jedi - -- type: boolean - -- default: true - enable = false, - }, - -- The preferred MarkupKind for all jedi-language-server messages that take MarkupContent. - -- type: string - -- accepted values: "markdown", "plaintext" - -- If omitted, jedi-language-server defaults to the client-preferred configuration. - -- If there is no client-preferred configuration, jedi language server users "plaintext". - -- markupKindPreferred = "markdown", - workspace = { - symbols = { - -- Performance optimization that sets names of folders that are ignored for workspace/symbols. - -- type: string[] - -- default: { ".nox", ".tox", ".venv", "__pycache__", "venv" } - -- If you manually set this option, it overrides the default. - -- Setting it to an empty array will result in no ignored folders. - ignoreFolders = { - ".nox", ".tox", ".venv", "__pycache__", "venv", - "artifacts", "config", ".vscode", ".pytest_cache", - "build", "scripts", "incoax_tests.egg-info", - "node_modules", - }, - }, - }, - }, -} diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index ad23ef6..58b5304 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -221,7 +221,7 @@ end function P.reload_server_buf(self, name) local server = self.servers[name] local ft_map = {} - for _, ft in ipairs(server.filetypes) do + for _, ft in ipairs(server.lspconfig.filetypes) do ft_map[ft] = true end for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do @@ -241,8 +241,8 @@ function P.filetypes(self) if not self._filetypes then self._filetypes = {} local unique = {} - for _, cfg in pairs(self.servers) do - for _, ft in ipairs(cfg.filetypes) do + for _, server in pairs(self.servers) do + for _, ft in ipairs(server.lspconfig.filetypes) do if not unique[ft] then table.insert(self._filetypes, ft) unique[ft] = true @@ -257,22 +257,21 @@ end function P.language_servers(self) if not self._language_servers then self._language_servers = {} - for name, opts in pairs(self.servers) do - if opts.dependencies ~= nil then - for _, dep in ipairs(opts.dependencies) do + for name, server in pairs(self.servers) do + if server.dependencies ~= nil then + for _, dep in ipairs(server.dependencies) do if not utils.is_available(dep) then utils.warn( "Disabling " .. name .. " because " .. dep .. " is required but not installed", module_name ) - opts.enabled = false + server.enabled = false goto next_server end end end - if opts.enabled == true then - opts.config = require("lsp.config." .. name) + if server.enabled == true then table.insert(self._language_servers, name) end ::next_server:: @@ -283,18 +282,17 @@ function P.language_servers(self) end function P.setup_server(self, name) - local opts = self.servers[name] + local server = self.servers[name] - if opts.enabled ~= true then + if server.enabled ~= true then return end local lspconfig = require("lspconfig") - opts.config.filetypes = opts.filetypes - opts.config.root_dir = lspconfig.util.find_git_ancestor - opts.config.capabilities = self.capabilities - opts.config.on_attach = self.on_attach - lspconfig[name].setup(opts.config) + server.lspconfig.root_dir = lspconfig.util.find_git_ancestor + server.lspconfig.capabilities = self.capabilities + server.lspconfig.on_attach = self.on_attach + lspconfig[name].setup(server.lspconfig) self:reload_server_buf(name) end diff --git a/lua/lsp/servers.lua b/lua/lsp/servers.lua deleted file mode 100644 index 106fa7b..0000000 --- a/lua/lsp/servers.lua +++ /dev/null @@ -1,94 +0,0 @@ ---[[ - Copyright 2023 Oscar Wallberg - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -]] - -local servers = { - clangd = { - enabled = true, - filetypes = { - "c", - "cpp", - "objc", - "objcpp", - "cuda", - "proto", - }, - config = {}, - }, - cmake = { - enabled = true, - filetypes = { - "cmake", - }, - config = {}, - }, - diagnosticls = { - enabled = true, - filetypes = { - "python", - "lua", - "sh", - }, - dependencies = { - "npm", - }, - config = {}, - }, - lua_ls = { - enabled = true, - filetypes = { - "lua", - }, - config = {}, - }, - lemminx = { - enabled = true, - filetypes = { - "xml", - "xsd", - "xsl", - "xslt", - "svg", - }, - config = {}, - }, - bashls = { - enabled = true, - filetypes = { - "sh", - }, - dependencies = { - "npm", - "shellcheck" - }, - config = {}, - }, - groovyls = { - enabled = true, - filetypes = { - "groovy", - }, - config = {}, - }, - jedi_language_server = { - enabled = true, - filetypes = { - "python", - }, - config = {}, - }, -} - -return servers diff --git a/lua/lsp/config/clangd.lua b/lua/lsp/servers/bashls.lua similarity index 66% rename from lua/lsp/config/clangd.lua rename to lua/lsp/servers/bashls.lua index be10a56..bc65384 100644 --- a/lua/lsp/config/clangd.lua +++ b/lua/lsp/servers/bashls.lua @@ -15,11 +15,17 @@ ]] return { - cmd = { - "clangd", - "--clang-tidy", - "--enable-config", - "--log=verbose", + enabled = true, + dependencies = { + "npm", + "shellcheck", + }, + lspconfig = { + filetypes = { + "sh", + }, + cmd = { "bash-language-server", "start", }, + cmd_env = { GLOB_PATTERN = "*@(.sh|.inc|.bash|.command)", }, + single_file_support = true, }, - single_file_support = true, } diff --git a/lua/lsp/config/bashls.lua b/lua/lsp/servers/clangd.lua similarity index 62% rename from lua/lsp/config/bashls.lua rename to lua/lsp/servers/clangd.lua index 933496d..caa855e 100644 --- a/lua/lsp/config/bashls.lua +++ b/lua/lsp/servers/clangd.lua @@ -15,7 +15,22 @@ ]] return { - cmd = { "bash-language-server", "start", }, - cmd_env = { GLOB_PATTERN = "*@(.sh|.inc|.bash|.command)", }, - single_file_support = true, + enabled = true, + lspconfig = { + filetypes = { + "c", + "cpp", + "objc", + "objcpp", + "cuda", + "proto", + }, + cmd = { + "clangd", + "--clang-tidy", + "--enable-config", + "--log=verbose", + }, + single_file_support = true, + }, } diff --git a/lua/lsp/config/cmake.lua b/lua/lsp/servers/cmake.lua similarity index 71% rename from lua/lsp/config/cmake.lua rename to lua/lsp/servers/cmake.lua index cbaa666..a913f2e 100644 --- a/lua/lsp/config/cmake.lua +++ b/lua/lsp/servers/cmake.lua @@ -15,9 +15,15 @@ ]] return { - cmd = { "cmake-language-server", }, - single_file_support = true, - init_options = { - buildDirectory = "build", + enabled = true, + lspconfig = { + filetypes = { + "cmake", + }, + cmd = { "cmake-language-server", }, + single_file_support = true, + init_options = { + buildDirectory = "build", + }, }, } diff --git a/lua/lsp/servers/diagnosticls.lua b/lua/lsp/servers/diagnosticls.lua new file mode 100644 index 0000000..010e42e --- /dev/null +++ b/lua/lsp/servers/diagnosticls.lua @@ -0,0 +1,109 @@ +--[[ + Copyright 2023 Oscar Wallberg + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +]] + +return { + enabled = true, + dependencies = { + "npm", + }, + lspconfig = { + filetypes = { + "python", + "lua", + "sh", + }, + cmd = { "diagnostic-languageserver", "--stdio", }, + single_file_support = true, + -- For more info see https://github.com/iamcco/diagnostic-languageserver + init_options = { + filetypes = { python = "flake8", lua = "luaFormatter", }, + linters = { + -- some help from this: + -- https://github.com/creativenull/diagnosticls-configs-nvim/blob/main/lua/diagnosticls-configs/linters/flake8.lua + flake8 = { + command = "flake8", + args = { + "--config", + "tox.ini", + "--format", + "%(row)d,%(col)d,%(code).1s,%(code)s: %(text)s", + "-", + }, + rootPatterns = { "Pipfile", ".git", "tox.ini", }, + isStdout = true, + isStderr = false, + debounce = 100, + offsetLine = 0, + offsetColumn = 0, + sourceName = "flake8", + formatLines = 1, + formatPattern = { + [[(\d+),(\d+),([A-Z]),(.*)(\r|\n)*$]], + { line = 1, column = 2, security = 3, message = { 4, }, }, + }, + securities = { + -- Available securities are { 'error', 'warning', 'hint', 'info' } + E = "error", + W = "warning", + B = "hint", + F = "error", + D = "info", + }, + }, + }, + formatFiletypes = { + python = { "black", "isort", }, + sh = { "shfmt", }, + }, + formatters = { + black = { + sourceName = "black", + command = "black", + args = { + "--stdin-filename", + "%filename", + "-t", + "py39", + "--quiet", + "-", + }, + rootPatterns = { "Pipfile", ".git", "tox.ini", }, + isStdout = true, + isStderr = false, + ignoreExitCode = false, + }, + isort = { + sourceName = "isort", + command = "isort", + args = { "--quiet", "-", }, + rootPatterns = { "Pipfile", ".git", "tox.ini", }, + isStdout = true, + isStderr = false, + ignoreExitCode = false, + }, + shfmt = { + sourceName = "shfmt", + command = "shfmt", + args = { "-s", "-i", "4", "-ci", }, + rootPatterns = { "Pipfile", ".git", "tox.ini", }, + isStdout = true, + isStderr = false, + ignoreExitCode = false, + }, + }, + }, + }, +} diff --git a/lua/lsp/config/groovyls.lua b/lua/lsp/servers/groovyls.lua similarity index 69% rename from lua/lsp/config/groovyls.lua rename to lua/lsp/servers/groovyls.lua index 0498630..8e64c91 100644 --- a/lua/lsp/config/groovyls.lua +++ b/lua/lsp/servers/groovyls.lua @@ -15,9 +15,15 @@ ]] return { - cmd = { - "java", "-jar", - "/usr/share/java/groovy-language-server/groovy-language-server-all.jar", + enabled = true, + lspconfig = { + filetypes = { + "groovy", + }, + cmd = { + "java", "-jar", + "/usr/share/java/groovy-language-server/groovy-language-server-all.jar", + }, + single_file_support = true, }, - single_file_support = true, } diff --git a/lua/lsp/servers/init.lua b/lua/lsp/servers/init.lua new file mode 100644 index 0000000..2f0aa1b --- /dev/null +++ b/lua/lsp/servers/init.lua @@ -0,0 +1,18 @@ +local servers = { + "bashls", + "clangd", + "cmake", + "diagnosticls", + "groovyls", + "jedi_language_server", + "lemminx", + "lua_ls", +} + +local manifest = {} + +for _, name in ipairs(servers) do + manifest[name] = require("lsp.servers." .. name) +end + +return manifest diff --git a/lua/lsp/servers/jedi_language_server.lua b/lua/lsp/servers/jedi_language_server.lua new file mode 100644 index 0000000..4ee4801 --- /dev/null +++ b/lua/lsp/servers/jedi_language_server.lua @@ -0,0 +1,61 @@ +--[[ + Copyright 2023 Oscar Wallberg + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +]] + +return { + enabled = true, + lspconfig = { + filetypes = { + "python", + }, + cmd = { "jedi-language-server", }, + single_file_support = true, + -- For more info see: + -- - https://github.com/pappasam/jedi-language-server#configuration + -- - https://github.com/pappasam/coc-jedi#configuration (good descriptions) + init_options = { + -- Built-in diagnostics seem to be very basic, + -- to the point where you are wondering if it's even active. + -- Will use iamcco/diagnostic-languageserver instead. + diagnostics = { + -- Enables (or disables) diagnostics provided by Jedi + -- type: boolean + -- default: true + enable = false, + }, + -- The preferred MarkupKind for all jedi-language-server messages that take MarkupContent. + -- type: string + -- accepted values: "markdown", "plaintext" + -- If omitted, jedi-language-server defaults to the client-preferred configuration. + -- If there is no client-preferred configuration, jedi language server users "plaintext". + -- markupKindPreferred = "markdown", + workspace = { + symbols = { + -- Performance optimization that sets names of folders that are ignored for workspace/symbols. + -- type: string[] + -- default: { ".nox", ".tox", ".venv", "__pycache__", "venv" } + -- If you manually set this option, it overrides the default. + -- Setting it to an empty array will result in no ignored folders. + ignoreFolders = { + ".nox", ".tox", ".venv", "__pycache__", "venv", + "artifacts", "config", ".vscode", ".pytest_cache", + "build", "scripts", "incoax_tests.egg-info", + "node_modules", + }, + }, + }, + }, + }, +} diff --git a/lua/lsp/config/lemminx.lua b/lua/lsp/servers/lemminx.lua similarity index 50% rename from lua/lsp/config/lemminx.lua rename to lua/lsp/servers/lemminx.lua index 86aec36..842b9e7 100644 --- a/lua/lsp/config/lemminx.lua +++ b/lua/lsp/servers/lemminx.lua @@ -15,18 +15,28 @@ ]] return { - cmd = { "lemminx", }, - single_file_support = true, - init_options = { - settings = { - xml = { - format = { - splitAttributes = true, - joinCDATALines = false, - joinContentLines = true, - joinCommentLines = false, - spaceBeforeEmptyCloseLine = false, - enabled = true, + enabled = true, + lspconfig = { + filetypes = { + "xml", + "xsd", + "xsl", + "xslt", + "svg", + }, + cmd = { "lemminx", }, + single_file_support = true, + init_options = { + settings = { + xml = { + format = { + splitAttributes = true, + joinCDATALines = false, + joinContentLines = true, + joinCommentLines = false, + spaceBeforeEmptyCloseLine = false, + enabled = true, + }, }, }, }, diff --git a/lua/lsp/config/lua_ls.lua b/lua/lsp/servers/lua_ls.lua similarity index 60% rename from lua/lsp/config/lua_ls.lua rename to lua/lsp/servers/lua_ls.lua index a07e8a7..ca405be 100644 --- a/lua/lsp/config/lua_ls.lua +++ b/lua/lsp/servers/lua_ls.lua @@ -21,19 +21,25 @@ table.insert(runtime_path, "lua/?/init.lua") -- spec: https://luals.github.io/wiki/settings/ return { - cmd = { "lua-language-server", }, - single_file_support = true, - settings = { - Lua = { - runtime = { - version = "LuaJIT", - path = runtime_path, + enabled = true, + lspconfig = { + filetypes = { + "lua", + }, + cmd = { "lua-language-server", }, + single_file_support = true, + settings = { + Lua = { + runtime = { + version = "LuaJIT", + path = runtime_path, + }, + workspace = { + library = vim.api.nvim_get_runtime_file("", true), + checkThirdParty = false, + }, + telemetry = { enable = false, }, }, - workspace = { - library = vim.api.nvim_get_runtime_file("", true), - checkThirdParty = false, - }, - telemetry = { enable = false, }, }, }, }