summaryrefslogtreecommitdiff
path: root/.config/nvim/lua/lsp/null-ls/code_actions.lua
blob: 70cecda26a40d0fb736f80b37c3364b82c38d3b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
local M = {}

local null_ls = require "null-ls"
local services = require "lsp.null-ls.services"
local Log = require "core.log"

local METHOD = null_ls.methods.CODE_ACTION

local is_registered = function(name)
  local query = {
    name = name,
    method = METHOD,
  }
  return require("null-ls.sources").is_registered(query)
end

function M.list_registered_providers(filetype)
  local registered_providers = services.list_registered_providers_names(filetype)
  return registered_providers[METHOD] or {}
end

function M.list_available(filetype)
  local availables = require("null-ls.sources").get_available(filetype, METHOD)
  local actors = vim.tbl_map(function(src)
    return src.name
  end, availables)
  table.sort(actors)
  return actors
end

function M.list_configured(actions_configs)
  local actors, errors = {}, {}

  for _, config in ipairs(actions_configs) do
    vim.validate {
      ["config.name"] = { config.name, "string" },
    }

    local name = config.name:gsub("-", "_")
    local actor = null_ls.builtins.code_actions[name]

    if not actor then
      Log:error("Not a valid code_actions: " .. config.name)
      errors[name] = {} -- Add data here when necessary
    elseif is_registered(config.name) then
      Log:trace "Skipping registering  the source more than once"
    else
      local command
      if actor._opts.command then
        command = services.find_command(actor._opts.command)
      end
      if not command and actor._opts.command ~= nil then
        Log:warn("Not found: " .. actor._opts.command)
        errors[name] = {} -- Add data here when necessary
      else
        Log:debug("Using code_actions: " .. (command or config.name))
        table.insert(
          actors,
          actor.with {
            command = command, -- could be nil
            extra_args = config.args,
            filetypes = config.filetypes,
          }
        )
      end
    end
  end

  return { supported = actors, unsupported = errors }
end

function M.setup(actions_configs)
  if vim.tbl_isempty(actions_configs) then
    return
  end

  local actions = M.list_configured(actions_configs)
  null_ls.register { sources = actions.supported }
end

return M