null-ls.lua 4.0 KB

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