null-ls.lua 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. local M = {}
  2. local u = require "utils"
  3. local null_ls = require "null-ls"
  4. local nodejs_local_providers = { "prettier", "prettierd", "prettier_d_slim", "eslint_d", "eslint" }
  5. M.requested_providers = {}
  6. local function is_nodejs_provider(provider)
  7. for _, local_provider in ipairs(nodejs_local_providers) do
  8. if local_provider == provider.exe then
  9. return true
  10. end
  11. end
  12. return false
  13. end
  14. local function is_provider_found(provider)
  15. -- special case: fallback to "eslint"
  16. -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint
  17. provider._opts.command = provider._opts.command == "eslint_d" and "eslint" or provider._opts.command
  18. local retval = { is_local = false, path = nil }
  19. if vim.fn.executable(provider._opts.command) == 1 then
  20. return false, provider._opts.command
  21. end
  22. if is_nodejs_provider(provider) then
  23. vim.cmd "let root_dir = FindRootDirectory()"
  24. local root_dir = vim.api.nvim_get_var "root_dir"
  25. local local_provider_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command
  26. if vim.fn.executable(local_provider_command) == 1 then
  27. retval.is_local = true
  28. retval.path = local_provider_command
  29. end
  30. end
  31. return retval.is_local, retval.path
  32. end
  33. local function validate_provider(provider)
  34. local is_local, provider_path = is_provider_found(provider)
  35. if not provider_path then
  36. u.lvim_log(string.format("Unable to find the path for: [%s]", provider))
  37. return false
  38. end
  39. if is_local then
  40. provider._opts.command = provider_path
  41. end
  42. return true
  43. end
  44. -- TODO: for linters and formatters with spaces and '-' replace with '_'
  45. function M.setup(filetype)
  46. for _, formatter in pairs(lvim.lang[filetype].formatters) do
  47. local builtin_formatter = null_ls.builtins.formatting[formatter.exe]
  48. -- FIXME: why doesn't this work?
  49. -- builtin_formatter._opts.args = formatter.args or builtin_formatter._opts.args
  50. -- builtin_formatter._opts.to_stdin = formatter.stdin or builtin_formatter._opts.to_stdin
  51. table.insert(M.requested_providers, builtin_formatter)
  52. u.lvim_log(string.format("Using format provider: [%s]", formatter.exe))
  53. end
  54. for _, linter in pairs(lvim.lang[filetype].linters) do
  55. local builtin_diagnoser = null_ls.builtins.diagnostics[linter.exe]
  56. -- FIXME: why doesn't this work?
  57. -- builtin_diagnoser._opts.args = linter.args or builtin_diagnoser._opts.args
  58. -- builtin_diagnoser._opts.to_stdin = linter.stdin or builtin_diagnoser._opts.to_stdin
  59. table.insert(M.requested_providers, builtin_diagnoser)
  60. u.lvim_log(string.format("Using linter provider: [%s]", linter.exe))
  61. end
  62. for idx, provider in pairs(M.requested_providers) do
  63. if not validate_provider(provider) then
  64. table.remove(M.requested_providers, idx)
  65. end
  66. end
  67. null_ls.register { sources = M.requested_providers }
  68. end
  69. return M