null-ls.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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(provider)
  22. local command_path
  23. local root_dir
  24. if lvim.builtin.rooter.active then
  25. --- use vim-rooter to set root_dir
  26. vim.cmd "let root_dir = FindRootDirectory()"
  27. root_dir = vim.api.nvim_get_var "root_dir"
  28. else
  29. --- use LSP to set root_dir
  30. local ts_client = require("utils").get_active_client_by_ft "typescript"
  31. if ts_client == nil then
  32. u.lvim_log "Unable to determine root directory since tsserver didn't start correctly"
  33. return
  34. end
  35. root_dir = ts_client.config.root_dir
  36. end
  37. local local_nodejs_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command
  38. u.lvim_log(string.format("checking [%s] for local node module: [%s]", local_nodejs_command, vim.inspect(provider)))
  39. if vim.fn.executable(local_nodejs_command) == 1 then
  40. command_path = local_nodejs_command
  41. elseif vim.fn.executable(provider._opts.command) == 1 then
  42. u.lvim_log(string.format("checking in global path instead for node module: [%s]", provider._opts.command))
  43. command_path = provider._opts.command
  44. else
  45. u.lvim_log(string.format("Unable to find node module: [%s]", provider._opts.command))
  46. end
  47. return command_path
  48. end
  49. local function validate_provider_request(provider)
  50. if provider == "" or provider == nil then
  51. return
  52. end
  53. -- NOTE: we can't use provider.name because eslint_d uses eslint name
  54. if vim.tbl_contains(nodejs_local_providers, provider._opts.command) then
  55. return validate_nodejs_provider(provider)
  56. end
  57. if vim.fn.executable(provider._opts.command) ~= 1 then
  58. u.lvim_log(string.format("Unable to find the path for: [%s]", vim.inspect(provider)))
  59. return
  60. end
  61. return provider._opts.command
  62. end
  63. -- TODO: for linters and formatters with spaces and '-' replace with '_'
  64. function M.setup(filetype)
  65. for _, formatter in pairs(lvim.lang[filetype].formatters) do
  66. local builtin_formatter = null_ls.builtins.formatting[formatter.exe]
  67. if not vim.tbl_contains(M.requested_providers, builtin_formatter) then
  68. -- FIXME: why doesn't this work?
  69. -- builtin_formatter._opts.args = formatter.args or builtin_formatter._opts.args
  70. -- builtin_formatter._opts.to_stdin = formatter.stdin or builtin_formatter._opts.to_stdin
  71. local resolved_path = validate_provider_request(builtin_formatter)
  72. if resolved_path then
  73. builtin_formatter._opts.command = resolved_path
  74. table.insert(M.requested_providers, builtin_formatter)
  75. u.lvim_log(string.format("Using format provider: [%s]", builtin_formatter.name))
  76. end
  77. end
  78. end
  79. for _, linter in pairs(lvim.lang[filetype].linters) do
  80. local builtin_diagnoser = null_ls.builtins.diagnostics[linter.exe]
  81. -- special case: fallback to "eslint"
  82. -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint
  83. -- if provider.exe
  84. if linter.exe == "eslint_d" then
  85. builtin_diagnoser = null_ls.builtins.diagnostics.eslint.with { command = "eslint_d" }
  86. end
  87. if not vim.tbl_contains(M.requested_providers, builtin_diagnoser) then
  88. -- FIXME: why doesn't this work?
  89. -- builtin_diagnoser._opts.args = linter.args or builtin_diagnoser._opts.args
  90. -- builtin_diagnoser._opts.to_stdin = linter.stdin or builtin_diagnoser._opts.to_stdin
  91. local resolved_path = validate_provider_request(builtin_diagnoser)
  92. if resolved_path then
  93. builtin_diagnoser._opts.command = resolved_path
  94. table.insert(M.requested_providers, builtin_diagnoser)
  95. u.lvim_log(string.format("Using linter provider: [%s]", builtin_diagnoser.name))
  96. end
  97. end
  98. end
  99. null_ls.register { sources = M.requested_providers }
  100. end
  101. return M