null-ls.lua 3.8 KB

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