null-ls.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. 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. u.lvim_log "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. u.lvim_log(string.format("checking [%s] for local node module: [%s]", local_nodejs_command, 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. u.lvim_log(string.format("checking in global path instead for node module: [%s]", provider._opts.command))
  59. command_path = provider._opts.command
  60. else
  61. u.lvim_log(string.format("Unable to find node module: [%s]", 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. u.lvim_log(string.format("Unable to find the path for: [%s]", vim.inspect(provider)))
  75. return
  76. end
  77. return provider._opts.command
  78. end
  79. -- TODO: for linters and formatters with spaces and '-' replace with '_'
  80. function M.setup(filetype)
  81. for _, formatter in pairs(lvim.lang[filetype].formatters) do
  82. local builtin_formatter = null_ls.builtins.formatting[formatter.exe]
  83. if not vim.tbl_contains(M.requested_providers, builtin_formatter) then
  84. -- FIXME: why doesn't this work?
  85. -- builtin_formatter._opts.args = formatter.args or builtin_formatter._opts.args
  86. -- builtin_formatter._opts.to_stdin = formatter.stdin or builtin_formatter._opts.to_stdin
  87. local resolved_path = validate_provider_request(builtin_formatter)
  88. if resolved_path then
  89. builtin_formatter._opts.command = resolved_path
  90. table.insert(M.requested_providers, builtin_formatter)
  91. u.lvim_log(string.format("Using format provider: [%s]", builtin_formatter.name))
  92. else
  93. -- mark it here to avoid re-doing the lookup again
  94. register_failed_request(filetype, formatter.exe, "formatters")
  95. end
  96. end
  97. end
  98. for _, linter in pairs(lvim.lang[filetype].linters) do
  99. local builtin_diagnoser = null_ls.builtins.diagnostics[linter.exe]
  100. -- special case: fallback to "eslint"
  101. -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint
  102. -- if provider.exe
  103. if linter.exe == "eslint_d" then
  104. builtin_diagnoser = null_ls.builtins.diagnostics.eslint.with { command = "eslint_d" }
  105. end
  106. if not vim.tbl_contains(M.requested_providers, builtin_diagnoser) then
  107. -- FIXME: why doesn't this work?
  108. -- builtin_diagnoser._opts.args = linter.args or builtin_diagnoser._opts.args
  109. -- builtin_diagnoser._opts.to_stdin = linter.stdin or builtin_diagnoser._opts.to_stdin
  110. local resolved_path = validate_provider_request(builtin_diagnoser)
  111. if resolved_path then
  112. builtin_diagnoser._opts.command = resolved_path
  113. table.insert(M.requested_providers, builtin_diagnoser)
  114. u.lvim_log(string.format("Using linter provider: [%s]", builtin_diagnoser.name))
  115. else
  116. -- mark it here to avoid re-doing the lookup again
  117. register_failed_request(filetype, linter.exe, "linters")
  118. end
  119. end
  120. end
  121. null_ls.register { sources = M.requested_providers }
  122. end
  123. return M