null-ls.lua 3.1 KB

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