diff options
author | kylo252 <[email protected]> | 2021-07-31 06:06:08 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2021-07-31 00:06:08 -0400 |
commit | 9d89929d9bb47d1f78c2d3945b761da2f24a5643 (patch) | |
tree | 7f5d281ddb4bbdcecffe01c0234d02be32ede1f7 /lua/lsp/null-ls.lua | |
parent | 6f4dd8471b24f71ed6712fb6b9acc0ac79b9137a (diff) |
Enable querying lang-server formatting capabilities (#1078)
Diffstat (limited to 'lua/lsp/null-ls.lua')
-rw-r--r-- | lua/lsp/null-ls.lua | 113 |
1 files changed, 58 insertions, 55 deletions
diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index 6a31de26..d2222602 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -1,76 +1,79 @@ local M = {} +local u = require "utils" +local null_ls = require "null-ls" -local _, null_ls = pcall(require, "null-ls") -local utils = require "utils" -local sources = {} +local nodejs_local_providers = { "prettier", "prettierd", "prettier_d_slim", "eslint_d", "eslint" } -local local_executables = { "prettier", "prettierd", "prettier_d_slim", "eslint_d", "eslint" } +M.requested_providers = {} -local find_local_exe = function(exe) - vim.cmd "let root_dir = FindRootDirectory()" - local root_dir = vim.api.nvim_get_var "root_dir" - local local_exe = root_dir .. "/node_modules/.bin/" .. exe - return local_exe +local function is_nodejs_provider(provider) + for _, local_provider in ipairs(nodejs_local_providers) do + if local_provider == provider.exe then + return true + end + end + return false end --- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint -local get_normalized_exe = function(exe, type) - if type == "diagnostics" and exe == "eslint_d" then - return "eslint" +local function is_provider_found(provider) + -- special case: fallback to "eslint" + -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint + provider._opts.command = provider._opts.command == "eslint_d" and "eslint" or provider._opts.command + + local retval = { is_local = false, path = nil } + if vim.fn.executable(provider._opts.command) == 1 then + return false, provider._opts.command end - return exe + if is_nodejs_provider(provider) then + vim.cmd "let root_dir = FindRootDirectory()" + local root_dir = vim.api.nvim_get_var "root_dir" + local local_provider_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command + if vim.fn.executable(local_provider_command) == 1 then + retval.is_local = true + retval.path = local_provider_command + end + end + return retval.is_local, retval.path end -local function setup_ls(exe, type) - if utils.has_value(local_executables, exe) then - local normalized_exe = get_normalized_exe(exe, type) - local smart_executable = null_ls.builtins[type][normalized_exe] - local local_executable = find_local_exe(exe) - if vim.fn.executable(local_executable) == 1 then - smart_executable._opts.command = local_executable - table.insert(sources, smart_executable) - else - if vim.fn.executable(exe) == 1 then - smart_executable._opts.command = exe - table.insert(sources, smart_executable) - end - end - else - if null_ls.builtins[type][exe] and vim.fn.executable(null_ls.builtins[type][exe]._opts.command) then - table.insert(sources, null_ls.builtins[type][exe]) - end +local function validate_provider(provider) + local is_local, provider_path = is_provider_found(provider) + if not provider_path then + u.lvim_log(string.format("Unable to find the path for: [%s]", provider)) + return false end - null_ls.register { sources = sources } + if is_local then + provider._opts.command = provider_path + end + return true end -- TODO: for linters and formatters with spaces and '-' replace with '_' -local function setup(filetype, type) - local executables = nil - if type == "diagnostics" then - executables = lvim.lang[filetype].linters +function M.setup(filetype) + for _, formatter in pairs(lvim.lang[filetype].formatters) do + local builtin_formatter = null_ls.builtins.formatting[formatter.exe] + -- FIXME: why doesn't this work? + -- builtin_formatter._opts.args = formatter.args or builtin_formatter._opts.args + -- builtin_formatter._opts.to_stdin = formatter.stdin or builtin_formatter._opts.to_stdin + table.insert(M.requested_providers, builtin_formatter) + u.lvim_log(string.format("Using format provider: [%s]", formatter.exe)) end - if type == "formatting" then - executables = lvim.lang[filetype].formatter.exe + + for _, linter in pairs(lvim.lang[filetype].linters) do + local builtin_diagnoser = null_ls.builtins.diagnostics[linter.exe] + -- FIXME: why doesn't this work? + -- builtin_diagnoser._opts.args = linter.args or builtin_diagnoser._opts.args + -- builtin_diagnoser._opts.to_stdin = linter.stdin or builtin_diagnoser._opts.to_stdin + table.insert(M.requested_providers, builtin_diagnoser) + u.lvim_log(string.format("Using linter provider: [%s]", linter.exe)) end - if utils.is_table(executables) then - for _, exe in pairs(executables) do - if exe ~= "" then - setup_ls(exe, type) - end + for idx, provider in pairs(M.requested_providers) do + if not validate_provider(provider) then + table.remove(M.requested_providers, idx) end end - if utils.is_string(executables) and executables ~= "" then - setup_ls(executables, type) - end -end - --- TODO: return the formatter if one was registered, then turn off the builtin formatter -function M.setup(filetype) - setup(filetype, "formatting") - setup(filetype, "diagnostics") - lvim.sources = sources - return sources + null_ls.register { sources = M.requested_providers } end return M |