null-ls.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. function M.get_registered_providers_by_filetype(ft)
  7. local matches = {}
  8. for _, provider in pairs(M.requested_providers) do
  9. if vim.tbl_contains(provider.filetypes, ft) then
  10. local provider_name = provider.name
  11. -- special case: show "eslint_d" instead of eslint
  12. -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint
  13. if string.find(provider._opts.command, "eslint_d") then
  14. provider_name = "eslint_d"
  15. end
  16. table.insert(matches, provider_name)
  17. end
  18. end
  19. return matches
  20. end
  21. local function is_nodejs_provider(provider)
  22. for _, local_provider in ipairs(nodejs_local_providers) do
  23. if local_provider == provider._opts.command then
  24. return true
  25. end
  26. end
  27. return false
  28. end
  29. local function is_provider_found(provider)
  30. local retval = { is_local = false, path = nil }
  31. if vim.fn.executable(provider._opts.command) == 1 then
  32. return false, provider._opts.command
  33. end
  34. if is_nodejs_provider(provider) then
  35. vim.cmd "let root_dir = FindRootDirectory()"
  36. local root_dir = vim.api.nvim_get_var "root_dir"
  37. local local_provider_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command
  38. if vim.fn.executable(local_provider_command) == 1 then
  39. retval.is_local = true
  40. retval.path = local_provider_command
  41. end
  42. end
  43. return retval.is_local, retval.path
  44. end
  45. local function validate_provider(provider)
  46. local is_local, provider_path = is_provider_found(provider)
  47. if not provider_path then
  48. u.lvim_log(string.format("Unable to find the path for: [%s]", vim.inspect(provider)))
  49. return false
  50. end
  51. if is_local then
  52. provider._opts.command = provider_path
  53. end
  54. return true
  55. end
  56. -- TODO: for linters and formatters with spaces and '-' replace with '_'
  57. function M.setup(filetype)
  58. for _, formatter in pairs(lvim.lang[filetype].formatters) do
  59. local builtin_formatter = null_ls.builtins.formatting[formatter.exe]
  60. -- FIXME: why doesn't this work?
  61. -- builtin_formatter._opts.args = formatter.args or builtin_formatter._opts.args
  62. -- builtin_formatter._opts.to_stdin = formatter.stdin or builtin_formatter._opts.to_stdin
  63. table.insert(M.requested_providers, builtin_formatter)
  64. u.lvim_log(string.format("Using format provider: [%s]", formatter.exe))
  65. end
  66. for _, linter in pairs(lvim.lang[filetype].linters) do
  67. local builtin_diagnoser = null_ls.builtins.diagnostics[linter.exe]
  68. -- FIXME: why doesn't this work?
  69. -- builtin_diagnoser._opts.args = linter.args or builtin_diagnoser._opts.args
  70. -- builtin_diagnoser._opts.to_stdin = linter.stdin or builtin_diagnoser._opts.to_stdin
  71. -- NOTE: validate before inserting to table
  72. if validate_provider(builtin_diagnoser) then
  73. table.insert(M.requested_providers, builtin_diagnoser)
  74. end
  75. -- special case: fallback to "eslint"
  76. -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint
  77. if linter.exe == "eslint_d" then
  78. table.insert(M.requested_providers, null_ls.builtins.diagnostics.eslint.with { command = "eslint_d" })
  79. end
  80. u.lvim_log(string.format("Using linter provider: [%s]", linter.exe))
  81. end
  82. -- FIXME: why would we need to remove if we never add?
  83. for idx, provider in pairs(M.requested_providers) do
  84. if not validate_provider(provider) then
  85. table.remove(M.requested_providers, idx)
  86. end
  87. end
  88. null_ls.register { sources = M.requested_providers }
  89. end
  90. return M