null-ls.lua 4.1 KB

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