code_actions.lua 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. local M = {}
  2. local null_ls = require "null-ls"
  3. local services = require "lvim.lsp.null-ls.services"
  4. local Log = require "lvim.core.log"
  5. local METHOD = null_ls.methods.CODE_ACTION
  6. local is_registered = function(name)
  7. local query = {
  8. name = name,
  9. method = METHOD,
  10. }
  11. return require("null-ls.sources").is_registered(query)
  12. end
  13. function M.list_registered(filetype)
  14. local registered_providers = services.list_registered_providers_names(filetype)
  15. return registered_providers[METHOD] or {}
  16. end
  17. function M.list_configured(actions_configs)
  18. local actors, errors = {}, {}
  19. for _, config in ipairs(actions_configs) do
  20. vim.validate {
  21. ["config.name"] = { config.name, "string" },
  22. }
  23. local name = config.name:gsub("-", "_")
  24. local actor = null_ls.builtins.code_actions[name]
  25. if not actor then
  26. Log:error("Not a valid code_actions: " .. config.name)
  27. errors[name] = {} -- Add data here when necessary
  28. elseif is_registered(config.name) then
  29. Log:trace "Skipping registering the source more than once"
  30. else
  31. local command
  32. if actor._opts.command then
  33. command = services.find_command(actor._opts.command)
  34. end
  35. if not command and actor._opts.command ~= nil then
  36. Log:warn("Not found: " .. actor._opts.command)
  37. errors[name] = {} -- Add data here when necessary
  38. else
  39. Log:debug("Using code_actions: " .. (command or config.name))
  40. table.insert(
  41. actors,
  42. actor.with {
  43. command = command, -- could be nil
  44. extra_args = config.args,
  45. filetypes = config.filetypes,
  46. }
  47. )
  48. end
  49. end
  50. end
  51. return { supported = actors, unsupported = errors }
  52. end
  53. function M.setup(actions_configs)
  54. if vim.tbl_isempty(actions_configs) then
  55. return
  56. end
  57. local actions = M.list_configured(actions_configs)
  58. null_ls.register { sources = actions.supported }
  59. end
  60. return M