Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to configure code actions that trigger sign/virtual text #311

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lua/navigator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ _NgConfigValues = {
sign_priority = 40,
virtual_text = true,
virtual_text_icon = true,
exclude = {},
},
rename = {
enable = true,
Expand Down
19 changes: 14 additions & 5 deletions lua/navigator/codeAction.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,11 @@ local action_virtual_call_back = function(line, diagnostics)
return code_action:render_action_virtual_text(line, diagnostics)
end

local code_action_req = function(_call_back_fn, diagnostics)
local context = { diagnostics = diagnostics }
local code_action_req = function(_call_back_fn, context)
local params = vim.lsp.util.make_range_params()
params.context = context
local line = params.range.start.line
local callback = _call_back_fn(line, diagnostics)
local callback = _call_back_fn(line, context.diagnostics)
vim.lsp.buf_request(0, 'textDocument/codeAction', params, callback)
end

Expand Down Expand Up @@ -203,7 +202,7 @@ code_action.range_code_action = function(startpos, endpos)
end, 1000)
end

code_action.code_action_prompt = function(bufnr)
code_action.code_action_prompt = function(bufnr, only)
if special_buffers[vim.bo.filetype] then
log('skip buffer', vim.bo.filetype)
return
Expand All @@ -218,10 +217,20 @@ code_action.code_action_prompt = function(bufnr)
diagnostics = diagnostic.get(vim.api.nvim_get_current_buf(), { lnum = lnum })
end

local context = {
diagnostics = diagnostics,
}

if next(only) then
context.only = only
end

log('using code action context for prompt', context)

local winid = get_current_winid()
code_action[winid] = code_action[winid] or {}
code_action[winid].lightbulb_line = code_action[winid].lightbulb_line or 0
code_action_req(action_virtual_call_back, diagnostics)
code_action_req(action_virtual_call_back, context)
end

return code_action
12 changes: 11 additions & 1 deletion lua/navigator/lspclient/attach.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,22 @@ M.on_attach = function(client, bufnr)

if _NgConfigValues.lsp.code_action.enable then
if client.server_capabilities.codeActionProvider and client.name ~= 'null-ls' then

local kinds = {}
if client.server_capabilities.codeActionProvider.codeActionKinds then
for _, kind in ipairs(client.server_capabilities.codeActionProvider.codeActionKinds) do
if not vim.tbl_contains(_NgConfigValues.lsp.code_action.exclude, kind) then
table.insert(kinds, kind)
end
end
end

trace('code action enabled for client', client.server_capabilities.codeActionProvider)
api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
group = api.nvim_create_augroup('NGCodeActGroup_' .. tostring(bufnr), {}),
buffer = bufnr,
callback = function()
require('navigator.codeAction').code_action_prompt(bufnr)
require('navigator.codeAction').code_action_prompt(bufnr, kinds)
end,
})
end
Expand Down
Loading