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.
This commit is contained in:
ViViDboarder 2022-01-12 14:35:07 -08:00
parent aef826f76b
commit 0c05f9db2a
2 changed files with 31 additions and 20 deletions

View File

@ -186,19 +186,13 @@ function M.config_lsp()
utils.try_require("lspconfig", function(lsp_config) utils.try_require("lspconfig", function(lsp_config)
local capabilities = merged_capabilities() 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 -- Configure each server
lsp_config.bashls.setup({ capabilities = capabilities, on_attach = get_default_attach() }) 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.gopls.setup({ capabilities = capabilities, on_attach = get_default_attach() })
lsp_config.pyright.setup({ capabilities = capabilities, on_attach = get_default_attach(no_format) }) lsp_config.pyright.setup({ capabilities = capabilities, on_attach = get_default_attach() })
lsp_config.rls.setup({ lsp_config.rls.setup({
capabilities = capabilities, capabilities = capabilities,
on_attach = get_default_attach(no_format), on_attach = get_default_attach(),
settings = { settings = {
rust = { rust = {
build_on_save = false, 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)
end end

View File

@ -3,12 +3,19 @@ local utils = require("utils")
function M.configure(options) function M.configure(options)
utils.try_require("null-ls", function(null_ls) utils.try_require("null-ls", function(null_ls)
-- Load newer versions of plugins -- Aggregate filetypes with language servers
local alex = require("plugins.null-ls.linters").alex local server_filetypes = {}
local ansiblelint = require("plugins.null-ls.linters").ansiblelint 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 -- Remove filetypes for Language servers I want to override
-- null_ls.builtins.diagnostics.ansiblelint.filetypes = { "yaml.ansible" } local override_filetypes = { "python" }
server_filetypes = vim.tbl_filter(function(ft)
return not vim.tbl_contains(override_filetypes, ft)
end, server_filetypes)
local sources = { local sources = {
-- Generic -- Generic
@ -23,20 +30,15 @@ function M.configure(options)
null_ls.builtins.diagnostics.mypy, null_ls.builtins.diagnostics.mypy,
-- Go -- Go
null_ls.builtins.diagnostics.golangci_lint, null_ls.builtins.diagnostics.golangci_lint,
null_ls.builtins.formatting.gofmt,
-- Text -- Text
null_ls.builtins.code_actions.proselint, null_ls.builtins.code_actions.proselint,
null_ls.builtins.diagnostics.proselint, null_ls.builtins.diagnostics.proselint,
null_ls.builtins.diagnostics.write_good, null_ls.builtins.diagnostics.write_good,
-- null_ls.builtins.diagnostics.alex -- null_ls.builtins.diagnostics.alex
alex,
-- Ansible -- Ansible
-- null_ls.builtins.diagnostics.ansiblelint, -- null_ls.builtins.diagnostics.ansiblelint.with({filetypes={"yaml.ansible"}}),
ansiblelint,
-- Shell -- Shell
null_ls.builtins.diagnostics.shellcheck, null_ls.builtins.diagnostics.shellcheck,
-- Rust
null_ls.builtins.formatting.rustfmt,
-- Lua -- Lua
null_ls.builtins.diagnostics.luacheck, null_ls.builtins.diagnostics.luacheck,
null_ls.builtins.formatting.stylua, null_ls.builtins.formatting.stylua,
@ -44,6 +46,18 @@ function M.configure(options)
null_ls.builtins.diagnostics.hadolint, 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 if null_ls["setup"] ~= nil then
options.sources = sources options.sources = sources
null_ls.setup(options) null_ls.setup(options)