Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit fd0c7b6

Browse files
committed
refactor(autocomplete): simplify LSP signature help
Use Neovim's built-in vim.lsp.buf.signature_help() instead of custom implementation to reduce code complexity and maintenance burden while maintaining the same functionality. The changes include: - Replace custom signature handler with native Neovim function - Improve trigger character detection using vim.iter() - Remove namespace for highlighting as it's handled internally now - Streamline autocmd group creation
1 parent 32c113d commit fd0c7b6

File tree

1 file changed

+21
-73
lines changed

1 file changed

+21
-73
lines changed

lua/autocomplete/signature.lua

+21-73
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,8 @@ local M = {}
55

66
local state = {
77
entry = nil,
8-
ns = nil,
98
}
109

11-
local function signature_handler(client, result, bufnr)
12-
local triggers = client.server_capabilities.signatureHelpProvider.triggerCharacters
13-
local ft = vim.bo[bufnr].filetype
14-
if not result.signatures or #result.signatures == 0 then
15-
return
16-
end
17-
local lines, hl = vim.lsp.util.convert_signature_help_to_markdown_lines(result, ft, triggers)
18-
if not lines or #lines == 0 then
19-
return
20-
end
21-
22-
local fbuf = vim.lsp.util.open_floating_preview(lines, 'markdown', {
23-
focusable = false,
24-
close_events = { 'CursorMoved', 'CursorMovedI', 'BufLeave', 'BufWinLeave' },
25-
border = M.config.border,
26-
max_width = M.config.width,
27-
max_height = M.config.height,
28-
anchor_bias = 'above',
29-
})
30-
31-
-- Highlight the active parameter.
32-
if hl then
33-
if vim.fn.has('nvim-0.11.0') == 1 then
34-
vim.highlight.range(
35-
fbuf,
36-
state.ns,
37-
'LspSignatureActiveParameter',
38-
{ hl[1], hl[2] },
39-
{ hl[3], hl[4] }
40-
)
41-
else
42-
vim.api.nvim_buf_add_highlight(
43-
fbuf,
44-
state.ns,
45-
'PmenuSel',
46-
vim.startswith(lines[1], '```') and 1 or 0,
47-
unpack(hl)
48-
)
49-
end
50-
end
51-
end
52-
5310
local function cursor_moved(args)
5411
local line = vim.api.nvim_get_current_line()
5512
local col = vim.api.nvim_win_get_cursor(0)[2]
@@ -63,35 +20,28 @@ local function cursor_moved(args)
6320
end
6421

6522
local before_line = line:sub(1, col)
23+
local has_trigger_char = vim.iter(
24+
client.server_capabilities.signatureHelpProvider.triggerCharacters or {}
25+
)
26+
:filter(function(c)
27+
return string.find(before_line, '[' .. c .. ']') ~= nil
28+
end)
29+
:next() ~= nil
6630

67-
-- Try to find signature help trigger character in current line
68-
for _, c in ipairs(client.server_capabilities.signatureHelpProvider.triggerCharacters or {}) do
69-
if string.find(before_line, '[' .. c .. ']') then
70-
local params = vim.lsp.util.make_position_params(
71-
vim.api.nvim_get_current_win(),
72-
client.offset_encoding
73-
)
74-
params.context = {
75-
isRetrigger = true,
76-
triggerKind = vim.lsp.protocol.CompletionTriggerKind.TriggerCharacter,
77-
triggerCharacter = c,
78-
}
79-
80-
util.debounce(state.entry, M.config.debounce_delay, function()
81-
return util.request(
82-
client,
83-
methods.textDocument_signatureHelp,
84-
params,
85-
function(result)
86-
signature_handler(client, result, args.buf)
87-
end,
88-
args.buf
89-
)
90-
end)
91-
92-
return
93-
end
31+
if not has_trigger_char then
32+
return
9433
end
34+
35+
util.debounce(state.entry, M.config.debounce_delay, function()
36+
vim.lsp.buf.signature_help({
37+
focusable = false,
38+
close_events = { 'CursorMoved', 'CursorMovedI', 'BufLeave', 'BufWinLeave' },
39+
border = M.config.border,
40+
max_width = M.config.width,
41+
max_height = M.config.height,
42+
anchor_bias = 'above',
43+
})
44+
end)
9545
end
9646

9747
M.config = {
@@ -103,13 +53,11 @@ M.config = {
10353

10454
function M.setup(config)
10555
M.config = vim.tbl_deep_extend('force', M.config, config or {})
106-
state.ns = vim.api.nvim_create_namespace('LspSignatureHelp')
10756
state.entry = util.entry()
108-
local group = vim.api.nvim_create_augroup('LspSignatureHelp', {})
10957

11058
vim.api.nvim_create_autocmd({ 'CursorMovedI', 'InsertEnter' }, {
11159
desc = 'Auto show LSP signature help',
112-
group = group,
60+
group = vim.api.nvim_create_augroup('LspSignatureHelp', {}),
11361
callback = cursor_moved,
11462
})
11563
end

0 commit comments

Comments
 (0)