null-ls.lua 3.2 KB

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