null-ls.lua 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 is_nodejs_provider(provider) then
  32. vim.cmd "let root_dir = FindRootDirectory()"
  33. local root_dir = vim.api.nvim_get_var "root_dir"
  34. local local_provider_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command
  35. if vim.fn.executable(local_provider_command) == 1 then
  36. return true, local_provider_command
  37. end
  38. end
  39. if vim.fn.executable(provider._opts.command) == 1 then
  40. return false, provider._opts.command
  41. end
  42. return retval.is_local, retval.path
  43. end
  44. local function validate_provider(provider)
  45. local is_local, provider_path = is_provider_found(provider)
  46. if not provider_path then
  47. u.lvim_log(string.format("Unable to find the path for: [%s]", vim.inspect(provider)))
  48. return false
  49. end
  50. if is_local then
  51. provider._opts.command = provider_path
  52. end
  53. return true
  54. end
  55. -- TODO: for linters and formatters with spaces and '-' replace with '_'
  56. function M.setup(filetype)
  57. for _, formatter in pairs(lvim.lang[filetype].formatters) do
  58. local builtin_formatter = null_ls.builtins.formatting[formatter.exe]
  59. -- FIXME: why doesn't this work?
  60. -- builtin_formatter._opts.args = formatter.args or builtin_formatter._opts.args
  61. -- builtin_formatter._opts.to_stdin = formatter.stdin or builtin_formatter._opts.to_stdin
  62. table.insert(M.requested_providers, builtin_formatter)
  63. u.lvim_log(string.format("Using format provider: [%s]", formatter.exe))
  64. end
  65. for _, linter in pairs(lvim.lang[filetype].linters) do
  66. local builtin_diagnoser = null_ls.builtins.diagnostics[linter.exe]
  67. -- FIXME: why doesn't this work?
  68. -- builtin_diagnoser._opts.args = linter.args or builtin_diagnoser._opts.args
  69. -- builtin_diagnoser._opts.to_stdin = linter.stdin or builtin_diagnoser._opts.to_stdin
  70. -- NOTE: validate before inserting to table
  71. -- special case: fallback to "eslint"
  72. -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint
  73. if linter.exe == "eslint_d" then
  74. builtin_diagnoser = null_ls.builtins.diagnostics.eslint.with { command = "eslint_d" }
  75. end
  76. if validate_provider(builtin_diagnoser) then
  77. table.insert(M.requested_providers, builtin_diagnoser)
  78. end
  79. u.lvim_log(string.format("Using linter provider: [%s]", linter.exe))
  80. end
  81. null_ls.register { sources = M.requested_providers }
  82. end
  83. return M