From 0c05f9db2a1ac57d83999c238d613d39fa8ebf06 Mon Sep 17 00:00:00 2001 From: ViViDboarder Date: Wed, 12 Jan 2022 14:35:07 -0800 Subject: [PATCH] Refactor null-ls again Now building a list of filetypes that already have language servers configured and then excluding those filetypes from null-ls. There is an override where they can be forced enabled for language servers that may not have a formatter or where the null-ls formatter is preferred. Eg. Python where I want to format with black. --- neovim/lua/plugins/lsp.lua | 15 +++++------- neovim/lua/plugins/null-ls/init.lua | 36 ++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/neovim/lua/plugins/lsp.lua b/neovim/lua/plugins/lsp.lua index cc83cfe..d863416 100644 --- a/neovim/lua/plugins/lsp.lua +++ b/neovim/lua/plugins/lsp.lua @@ -186,19 +186,13 @@ function M.config_lsp() utils.try_require("lspconfig", function(lsp_config) local capabilities = merged_capabilities() - -- Config null-ls - require("plugins.null-ls").configure({ capabilities = capabilities, on_attach = get_default_attach() }) - - -- Attach options to disable formatting - local no_format = { document_formatting = false, document_range_formatting = false } - -- Configure each server lsp_config.bashls.setup({ capabilities = capabilities, on_attach = get_default_attach() }) - lsp_config.gopls.setup({ capabilities = capabilities, on_attach = get_default_attach(no_format) }) - lsp_config.pyright.setup({ capabilities = capabilities, on_attach = get_default_attach(no_format) }) + lsp_config.gopls.setup({ capabilities = capabilities, on_attach = get_default_attach() }) + lsp_config.pyright.setup({ capabilities = capabilities, on_attach = get_default_attach() }) lsp_config.rls.setup({ capabilities = capabilities, - on_attach = get_default_attach(no_format), + on_attach = get_default_attach(), settings = { rust = { build_on_save = false, @@ -207,6 +201,9 @@ function M.config_lsp() }, }, }) + + -- Config null-ls after lsps so we can disable for languages that have language servers + require("plugins.null-ls").configure({ capabilities = capabilities, on_attach = get_default_attach() }) end) end diff --git a/neovim/lua/plugins/null-ls/init.lua b/neovim/lua/plugins/null-ls/init.lua index 7628d23..697cb7b 100644 --- a/neovim/lua/plugins/null-ls/init.lua +++ b/neovim/lua/plugins/null-ls/init.lua @@ -3,12 +3,19 @@ local utils = require("utils") function M.configure(options) utils.try_require("null-ls", function(null_ls) - -- Load newer versions of plugins - local alex = require("plugins.null-ls.linters").alex - local ansiblelint = require("plugins.null-ls.linters").ansiblelint + -- Aggregate filetypes with language servers + local server_filetypes = {} + utils.try_require("lspconfig", function(lsp_config) + vim.tbl_map(function(server) + vim.list_extend(server_filetypes, lsp_config[server].filetypes) + end, lsp_config.available_servers()) + end) - -- Use ansiblelint for only ansible files - -- null_ls.builtins.diagnostics.ansiblelint.filetypes = { "yaml.ansible" } + -- Remove filetypes for Language servers I want to override + local override_filetypes = { "python" } + server_filetypes = vim.tbl_filter(function(ft) + return not vim.tbl_contains(override_filetypes, ft) + end, server_filetypes) local sources = { -- Generic @@ -23,20 +30,15 @@ function M.configure(options) null_ls.builtins.diagnostics.mypy, -- Go null_ls.builtins.diagnostics.golangci_lint, - null_ls.builtins.formatting.gofmt, -- Text null_ls.builtins.code_actions.proselint, null_ls.builtins.diagnostics.proselint, null_ls.builtins.diagnostics.write_good, -- null_ls.builtins.diagnostics.alex - alex, -- Ansible - -- null_ls.builtins.diagnostics.ansiblelint, - ansiblelint, + -- null_ls.builtins.diagnostics.ansiblelint.with({filetypes={"yaml.ansible"}}), -- Shell null_ls.builtins.diagnostics.shellcheck, - -- Rust - null_ls.builtins.formatting.rustfmt, -- Lua null_ls.builtins.diagnostics.luacheck, null_ls.builtins.formatting.stylua, @@ -44,6 +46,18 @@ function M.configure(options) null_ls.builtins.diagnostics.hadolint, } + -- Map disabled filetypes onto list of sources + vim.tbl_map(function(builtin) + return builtin.with({ disabled_filetypes = server_filetypes }) + end, sources) + + -- Add custom or modified sources + vim.list_extend(sources, { + require("plugins.null-ls.linters").alex, + require("plugins.null-ls.linters").ansiblelint, + }) + + -- Setup or configure null_ls if null_ls["setup"] ~= nil then options.sources = sources null_ls.setup(options)