null-ls.lua 3.6 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. 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 validate_nodejs_provider(requests, provider)
  22. vim.cmd "let root_dir = FindRootDirectory()"
  23. local root_dir = vim.api.nvim_get_var "root_dir"
  24. local local_nodejs_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command
  25. u.lvim_log(string.format("checking for local node module: [%s]", vim.inspect(provider)))
  26. if vim.fn.executable(local_nodejs_command) == 1 then
  27. provider._opts.command = local_nodejs_command
  28. table.insert(requests, provider)
  29. elseif vim.fn.executable(provider._opts.command) == 1 then
  30. u.lvim_log(string.format("checking in global path instead for node module: [%s]", provider._opts.command))
  31. table.insert(requests, provider)
  32. else
  33. u.lvim_log(string.format("Unable to find node module: [%s]", provider._opts.command))
  34. return false
  35. end
  36. return true
  37. end
  38. local function validate_provider_request(requests, provider)
  39. if provider == "" or provider == nil then
  40. return false
  41. end
  42. -- NOTE: we can't use provider.name because eslint_d uses eslint name
  43. if vim.tbl_contains(nodejs_local_providers, provider._opts.command) then
  44. return validate_nodejs_provider(requests, provider)
  45. end
  46. if vim.fn.executable(provider._opts.command) ~= 1 then
  47. u.lvim_log(string.format("Unable to find the path for: [%s]", vim.inspect(provider)))
  48. return false
  49. end
  50. table.insert(requests, provider)
  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. if validate_provider_request(M.requested_providers, builtin_formatter) then
  61. u.lvim_log(string.format("Using format provider: [%s]", formatter.exe))
  62. end
  63. end
  64. for _, linter in pairs(lvim.lang[filetype].linters) do
  65. local builtin_diagnoser = null_ls.builtins.diagnostics[linter.exe]
  66. -- special case: fallback to "eslint"
  67. -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint
  68. -- if provider.exe
  69. if linter.exe == "eslint_d" then
  70. builtin_diagnoser = null_ls.builtins.diagnostics.eslint.with { command = "eslint_d" }
  71. end
  72. -- FIXME: why doesn't this work?
  73. -- builtin_diagnoser._opts.args = linter.args or builtin_diagnoser._opts.args
  74. -- builtin_diagnoser._opts.to_stdin = linter.stdin or builtin_diagnoser._opts.to_stdin
  75. if validate_provider_request(M.requested_providers, builtin_diagnoser) then
  76. u.lvim_log(string.format("Using linter provider: [%s]", linter.exe))
  77. end
  78. end
  79. null_ls.register { sources = M.requested_providers }
  80. end
  81. return M