diff options
Diffstat (limited to 'lua/lvim/lsp/null-ls')
-rw-r--r-- | lua/lvim/lsp/null-ls/formatters.lua | 66 | ||||
-rw-r--r-- | lua/lvim/lsp/null-ls/init.lua | 38 | ||||
-rw-r--r-- | lua/lvim/lsp/null-ls/linters.lua | 66 | ||||
-rw-r--r-- | lua/lvim/lsp/null-ls/services.lua | 63 |
4 files changed, 233 insertions, 0 deletions
diff --git a/lua/lvim/lsp/null-ls/formatters.lua b/lua/lvim/lsp/null-ls/formatters.lua new file mode 100644 index 00000000..87583abd --- /dev/null +++ b/lua/lvim/lsp/null-ls/formatters.lua @@ -0,0 +1,66 @@ +local M = {} + +local null_ls = require "null-ls" +local services = require "lvim.lsp.null-ls.services" +local Log = require "lvim.core.log" + +function M.list_registered_providers(filetype) + local null_ls_methods = require "null-ls.methods" + local formatter_method = null_ls_methods.internal["FORMATTING"] + local registered_providers = services.list_registered_providers_names(filetype) + return registered_providers[formatter_method] or {} +end + +function M.list_available(filetype) + local formatters = {} + local tbl = require "lvim.utils.table" + for _, provider in pairs(null_ls.builtins.formatting) do + if tbl.contains(provider.filetypes or {}, function(ft) + return ft == "*" or ft == filetype + end) then + table.insert(formatters, provider.name) + end + end + + return formatters +end + +function M.list_configured(formatter_configs) + local formatters, errors = {}, {} + + for _, fmt_config in ipairs(formatter_configs) do + local formatter_name = fmt_config.exe:gsub("-", "_") + local formatter = null_ls.builtins.formatting[formatter_name] + + if not formatter then + Log:error("Not a valid formatter: " .. fmt_config.exe) + errors[fmt_config.exe] = {} -- Add data here when necessary + else + local formatter_cmd = services.find_command(formatter._opts.command) + if not formatter_cmd then + Log:warn("Not found: " .. formatter._opts.command) + errors[fmt_config.exe] = {} -- Add data here when necessary + else + Log:debug("Using formatter: " .. formatter_cmd) + formatters[fmt_config.exe] = formatter.with { + command = formatter_cmd, + extra_args = fmt_config.args, + filetypes = fmt_config.filetypes, + } + end + end + end + + return { supported = formatters, unsupported = errors } +end + +function M.setup(formatter_configs) + if vim.tbl_isempty(formatter_configs) then + return + end + + local formatters_by_ft = M.list_configured(formatter_configs) + null_ls.register { sources = formatters_by_ft.supported } +end + +return M diff --git a/lua/lvim/lsp/null-ls/init.lua b/lua/lvim/lsp/null-ls/init.lua new file mode 100644 index 00000000..ef4a1844 --- /dev/null +++ b/lua/lvim/lsp/null-ls/init.lua @@ -0,0 +1,38 @@ +local M = {} + +local Log = require "lvim.core.log" +local formatters = require "lvim.lsp.null-ls.formatters" +local linters = require "lvim.lsp.null-ls.linters" + +function M:setup() + local status_ok, null_ls = pcall(require, "null-ls") + if not status_ok then + Log:error "Missing null-ls dependency" + return + end + + null_ls.config() + local default_opts = require("lvim.lsp").get_common_opts() + + if vim.tbl_isempty(lvim.lsp.null_ls.setup or {}) then + lvim.lsp.null_ls.setup = default_opts + end + + require("lspconfig")["null-ls"].setup(lvim.lsp.null_ls.setup) + for filetype, config in pairs(lvim.lang) do + if not vim.tbl_isempty(config.formatters) then + vim.tbl_map(function(c) + c.filetypes = { filetype } + end, config.formatters) + formatters.setup(config.formatters) + end + if not vim.tbl_isempty(config.linters) then + vim.tbl_map(function(c) + c.filetypes = { filetype } + end, config.formatters) + linters.setup(config.linters) + end + end +end + +return M diff --git a/lua/lvim/lsp/null-ls/linters.lua b/lua/lvim/lsp/null-ls/linters.lua new file mode 100644 index 00000000..1069ac58 --- /dev/null +++ b/lua/lvim/lsp/null-ls/linters.lua @@ -0,0 +1,66 @@ +local M = {} + +local null_ls = require "null-ls" +local services = require "lvim.lsp.null-ls.services" +local Log = require "lvim.core.log" + +function M.list_registered_providers(filetype) + local null_ls_methods = require "null-ls.methods" + local linter_method = null_ls_methods.internal["DIAGNOSTICS"] + local registered_providers = services.list_registered_providers_names(filetype) + return registered_providers[linter_method] or {} +end + +function M.list_available(filetype) + local linters = {} + local tbl = require "lvim.utils.table" + for _, provider in pairs(null_ls.builtins.diagnostics) do + if tbl.contains(provider.filetypes or {}, function(ft) + return ft == "*" or ft == filetype + end) then + table.insert(linters, provider.name) + end + end + + return linters +end + +function M.list_configured(linter_configs) + local linters, errors = {}, {} + + for _, lnt_config in pairs(linter_configs) do + local linter_name = lnt_config.exe:gsub("-", "_") + local linter = null_ls.builtins.diagnostics[linter_name] + + if not linter then + Log:error("Not a valid linter: " .. lnt_config.exe) + errors[lnt_config.exe] = {} -- Add data here when necessary + else + local linter_cmd = services.find_command(linter._opts.command) + if not linter_cmd then + Log:warn("Not found: " .. linter._opts.command) + errors[lnt_config.exe] = {} -- Add data here when necessary + else + Log:debug("Using linter: " .. linter_cmd) + linters[lnt_config.exe] = linter.with { + command = linter_cmd, + extra_args = lnt_config.args, + filetypes = lnt_config.filetypes, + } + end + end + end + + return { supported = linters, unsupported = errors } +end + +function M.setup(linter_configs) + if vim.tbl_isempty(linter_configs) then + return + end + + local linters = M.list_configured(linter_configs) + null_ls.register { sources = linters.supported } +end + +return M diff --git a/lua/lvim/lsp/null-ls/services.lua b/lua/lvim/lsp/null-ls/services.lua new file mode 100644 index 00000000..9cb29f49 --- /dev/null +++ b/lua/lvim/lsp/null-ls/services.lua @@ -0,0 +1,63 @@ +local M = {} + +local function find_root_dir() + local util = require "lspconfig/util" + local lsp_utils = require "lvim.lsp.utils" + + local ts_client = lsp_utils.is_client_active "typescript" + if ts_client then + return ts_client.config.root_dir + end + local dirname = vim.fn.expand "%:p:h" + return util.root_pattern "package.json"(dirname) +end + +local function from_node_modules(command) + local root_dir = find_root_dir() + + if not root_dir then + return nil + end + + return root_dir .. "/node_modules/.bin/" .. command +end + +local local_providers = { + prettier = { find = from_node_modules }, + prettierd = { find = from_node_modules }, + prettier_d_slim = { find = from_node_modules }, + eslint_d = { find = from_node_modules }, + eslint = { find = from_node_modules }, + stylelint = { find = from_node_modules }, +} + +function M.find_command(command) + if local_providers[command] then + local local_command = local_providers[command].find(command) + if local_command and vim.fn.executable(local_command) == 1 then + return local_command + end + end + + if vim.fn.executable(command) == 1 then + return command + end + return nil +end + +function M.list_registered_providers_names(filetype) + local u = require "null-ls.utils" + local c = require "null-ls.config" + local registered = {} + for method, source in pairs(c.get()._methods) do + for name, filetypes in pairs(source) do + if u.filetype_matches(filetypes, filetype) then + registered[method] = registered[method] or {} + table.insert(registered[method], name) + end + end + end + return registered +end + +return M |