code_actions.lua 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_providers(filetype)
  14. local registered_providers = services.list_registered_providers_names(filetype)
  15. return registered_providers[METHOD] or {}
  16. end
  17. function M.list_available(filetype)
  18. local availables = require("null-ls.sources").get_available(filetype, METHOD)
  19. local actors = vim.tbl_map(function(src)
  20. return src.name
  21. end, availables)
  22. table.sort(actors)
  23. return actors
  24. end
  25. function M.list_configured(actions_configs)
  26. local actors, errors = {}, {}
  27. for _, config in ipairs(actions_configs) do
  28. vim.validate {
  29. ["config.name"] = { config.name, "string" },
  30. }
  31. local name = config.name:gsub("-", "_")
  32. local actor = null_ls.builtins.code_actions[name]
  33. if not actor then
  34. Log:error("Not a valid code_actions: " .. config.name)
  35. errors[name] = {} -- Add data here when necessary
  36. elseif is_registered(config.name) then
  37. Log:trace "Skipping registering the source more than once"
  38. else
  39. local command
  40. if actor._opts.command then
  41. command = services.find_command(actor._opts.command)
  42. end
  43. if not command and actor._opts.command ~= nil then
  44. Log:warn("Not found: " .. actor._opts.command)
  45. errors[name] = {} -- Add data here when necessary
  46. else
  47. Log:debug("Using code_actions: " .. (command or config.name))
  48. table.insert(
  49. actors,
  50. actor.with {
  51. command = command, -- could be nil
  52. extra_args = config.args,
  53. filetypes = config.filetypes,
  54. }
  55. )
  56. end
  57. end
  58. end
  59. return { supported = actors, unsupported = errors }
  60. end
  61. function M.setup(actions_configs)
  62. if vim.tbl_isempty(actions_configs) then
  63. return
  64. end
  65. local actions = M.list_configured(actions_configs)
  66. null_ls.register { sources = actions.supported }
  67. end
  68. return M