null-ls.lua 5.4 KB

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