Skip to content

Commit

Permalink
feat: apply_first
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed Sep 29, 2023
1 parent 3ca9269 commit f531e4e
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ The default configuration:
-- If there's only one code action, it gets automatically applied.
-- The values can either be a string or a string tuple (with description)
-- example: "<leader>aq" | { "<leader>aq", "Quickfix" }
apply_first = nil, -- applies first code action
quickfix = nil, -- can be filtered with the `quickfix_filter` option bellow
quickfix_next = nil, -- tries to fix the next diagnostic
quickfix_prev = nil, -- tries to fix the previous diagnostic
Expand Down
25 changes: 25 additions & 0 deletions lua/clear-action/actions.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local utils = require("clear-action.utils")
local M = {}

local code_action = vim.lsp.buf.code_action
Expand All @@ -22,6 +23,30 @@ M.apply = function(prefix)
})
end

---@param client lsp.Client
M.apply_first = function(client)
local bufnr = vim.api.nvim_get_current_buf()
local params = vim.lsp.util.make_range_params()

local function on_result(results)
local action = results[1]
local ctx = { bufnr = bufnr }

if not action then
vim.notify("No code actions available", vim.log.levels.INFO)
return
end

utils.handle_action(action, client, ctx)
end

params.context = {
triggerKind = vim.lsp.protocol.CodeActionTriggerKind.Invoked,
diagnostics = vim.lsp.diagnostic.get_line_diagnostics(),
}
utils.code_action_request(bufnr, params, on_result)
end

---@param filters table<string, string> | nil
M.quickfix = function(filters)
code_action({
Expand Down
1 change: 1 addition & 0 deletions lua/clear-action/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ local defaults = {
},
},
mappings = {
apply_first = nil,
quickfix = nil,
quickfix_next = nil,
quickfix_prev = nil,
Expand Down
9 changes: 7 additions & 2 deletions lua/clear-action/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@ M.on_attach = function(bufnr, client)
vim.keymap.set(mode, lhs, rhs, {
silent = true,
buffer = bufnr,
desc = desc
desc = desc,
})
end

for name, value in pairs(mappings) do
local key, desc = parse_value(value)
if name ~= "actions" and key and actions[name] then
local arg = vim.startswith(name, "quickfix") and quickfix_filters
local arg
if vim.startswith(name, "quickfix") then
arg = quickfix_filters
elseif name == "apply_first" then
arg = client
end
set("n", key, function() actions[name](arg) end, desc)
end
end
Expand Down
16 changes: 4 additions & 12 deletions lua/clear-action/signs.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
local M = {}

local config = require("clear-action.config")
local utils = require("clear-action.utils")

local is_sending = false
local clear_extmark = function() vim.api.nvim_buf_clear_namespace(0, config.ns, 0, -1) end

local function on_result(results, context)
is_sending = false
local virt_text = {}
local opts = config.options.signs

Expand Down Expand Up @@ -64,18 +66,8 @@ local function code_action_request()
triggerKind = vim.lsp.protocol.CodeActionTriggerKind.Automatic,
diagnostics = vim.lsp.diagnostic.get_line_diagnostics(),
}

is_sending = true

vim.lsp.buf_request(bufnr, "textDocument/codeAction", params, function(error, results, context)
is_sending = false

if error then
local message = type(error) == "string" and error or error.message
vim.notify("code action: " .. message, vim.log.levels.WARN)
end
if results then on_result(results, context) end
end)
utils.code_action_request(bufnr, params, on_result)
end

local function update()
Expand All @@ -86,8 +78,8 @@ local function update()
end

M.on_attach = function(bufnr)
local events = { "CursorMoved", "TextChanged" }
is_sending = false
local events = { "CursorMoved", "TextChanged" }

if config.options.signs.update_on_insert then
vim.list_extend(events, { "CursorMovedI, TextChangedI" })
Expand Down
41 changes: 41 additions & 0 deletions lua/clear-action/utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
local M = {}

local function apply_action(action, client, ctx)
if action.edit then
vim.lsp.util.apply_workspace_edit(action.edit, client.offset_encoding)
elseif action.command then
local command = type(action.command) == "table" and action.command or action
client._exec_cmd(command, ctx)
end
end

M.code_action_request = function(bufnr, params, on_result)
vim.lsp.buf_request(bufnr, "textDocument/codeAction", params, function(error, results, context)
if error then
local message = type(error) == "string" and error or error.message
vim.notify("code action: " .. message, vim.log.levels.WARN)
end
if results then on_result(results, context) end
end)
end

M.handle_action = function(action, client, ctx)
local dyn_cap = client.dynamic_capabilities
local reg = dyn_cap and dyn_cap:get("textDocument/codeAction", { bufnr = ctx.bufnr })
local supports_resolve = vim.tbl_get(reg or {}, "registerOptions", "resolveProvider")
or client.supports_method("codeAction/resolve")

if not action.edit and client and supports_resolve then
client.request("codeAction/resolve", action, function(err, resolved_action)
if err then
vim.notify(err.code .. ': ' .. err.message, vim.log.levels.ERROR)
return
end
apply_action(resolved_action, client, ctx)
end, ctx.bufnr)
else
apply_action(action, client, ctx)
end
end

return M

0 comments on commit f531e4e

Please sign in to comment.