Browse Source

refactor null-ls (#1202)

kylo252 4 năm trước cách đây
mục cha
commit
e2dd993ce7
1 tập tin đã thay đổi với 32 bổ sung35 xóa
  1. 32 35
      lua/lsp/null-ls.lua

+ 32 - 35
lua/lsp/null-ls.lua

@@ -23,40 +23,37 @@ function M.get_registered_providers_by_filetype(ft)
   return matches
 end
 
-local function is_nodejs_provider(provider)
-  for _, local_provider in ipairs(nodejs_local_providers) do
-    if local_provider == provider._opts.command then
-      return true
-    end
+local function validate_nodejs_provider(requests, provider)
+  vim.cmd "let root_dir = FindRootDirectory()"
+  local root_dir = vim.api.nvim_get_var "root_dir"
+  local local_nodejs_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command
+  u.lvim_log(string.format("checking for local node module: [%s]", vim.inspect(provider)))
+  if vim.fn.executable(local_nodejs_command) == 1 then
+    provider._opts.command = local_nodejs_command
+    table.insert(requests, provider)
+  elseif vim.fn.executable(provider._opts.command) == 1 then
+    u.lvim_log(string.format("checking in global path instead for node module: [%s]", provider._opts.command))
+    table.insert(requests, provider)
+  else
+    u.lvim_log(string.format("Unable to find node module: [%s]", provider._opts.command))
+    return false
   end
-  return false
+  return true
 end
 
-local function is_provider_found(provider)
-  local retval = { is_local = false, path = nil }
-  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
-      return true, local_provider_command
-    end
+local function validate_provider_request(requests, provider)
+  if provider == "" or provider == nil then
+    return false
   end
-  if vim.fn.executable(provider._opts.command) == 1 then
-    return false, provider._opts.command
+  -- NOTE: we can't use provider.name because eslint_d uses eslint name
+  if vim.tbl_contains(nodejs_local_providers, provider._opts.command) then
+    return validate_nodejs_provider(requests, provider)
   end
-  return retval.is_local, retval.path
-end
-
-local function validate_provider(provider)
-  local is_local, provider_path = is_provider_found(provider)
-  if not provider_path then
+  if vim.fn.executable(provider._opts.command) ~= 1 then
     u.lvim_log(string.format("Unable to find the path for: [%s]", vim.inspect(provider)))
     return false
   end
-  if is_local then
-    provider._opts.command = provider_path
-  end
+  table.insert(requests, provider)
   return true
 end
 
@@ -67,25 +64,25 @@ function M.setup(filetype)
     -- 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))
+    if validate_provider_request(M.requested_providers, builtin_formatter) then
+      u.lvim_log(string.format("Using format provider: [%s]", formatter.exe))
+    end
   end
 
   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
-    -- NOTE: validate before inserting to table
     -- special case: fallback to "eslint"
     -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint
+    -- if provider.exe
     if linter.exe == "eslint_d" then
       builtin_diagnoser = null_ls.builtins.diagnostics.eslint.with { command = "eslint_d" }
     end
-    if validate_provider(builtin_diagnoser) then
-      table.insert(M.requested_providers, builtin_diagnoser)
+    -- 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
+    if validate_provider_request(M.requested_providers, builtin_diagnoser) then
+      u.lvim_log(string.format("Using linter provider: [%s]", linter.exe))
     end
-    u.lvim_log(string.format("Using linter provider: [%s]", linter.exe))
   end
 
   null_ls.register { sources = M.requested_providers }