-
-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(recipes): add recipe for automatically triggering lsp signature …
…help
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
lua/astrocommunity/recipes/astrolsp-auto-signature-help/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# AstroLSP - Automatically show signature help | ||
|
||
**Website:** <https://docs.astronvim.com/recipes/advanced_lsp/#automatic-signature-help> | ||
|
||
This plugin specification configures AstroLSP to automatically trigger the LSP signature help when necessary and relevant. |
72 changes: 72 additions & 0 deletions
72
lua/astrocommunity/recipes/astrolsp-auto-signature-help/init.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
return { | ||
"AstroNvim/astrolsp", | ||
specs = { | ||
{ | ||
"AstroNvim/astrocore", | ||
---@type AstroCoreOpts | ||
opts = { | ||
autocmds = { | ||
signature_help_triggers = { | ||
{ | ||
event = "LspAttach", | ||
desc = "Add signature help triggers as language servers attach", | ||
callback = function(args) | ||
local client = vim.lsp.get_client_by_id(args.data.client_id) | ||
if client and client.supports_method "textDocument/signatureHelp" then | ||
vim.b[args.buf].signature_help_trigger = require("astrocore").list_insert_unique( | ||
vim.b[args.buf].signature_help_trigger, | ||
client.server_capabilities.signatureHelpProvider.triggerCharacters or {} | ||
) | ||
end | ||
end, | ||
}, | ||
{ | ||
event = "LspDetach", | ||
desc = "Safely remove LSP signature help triggers when language servers detach", | ||
callback = vim.schedule_wrap(function(args) | ||
if not vim.api.nvim_buf_is_valid(args.buf) then return end | ||
local signature_help_trigger = {} | ||
for _, client in pairs((vim.lsp.get_clients or vim.lsp.get_active_clients) { bufnr = args.buf }) do | ||
if client.id ~= args.data.client_id and client.supports_method "textDocument/signatureHelp" then | ||
require("astrocore").list_insert_unique( | ||
signature_help_trigger, | ||
client.server_capabilities.signatureHelpProvider.triggerCharacters or {} | ||
) | ||
end | ||
end | ||
vim.b[args.buf].signature_help_trigger = signature_help_trigger | ||
end), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
---@type AstroLSPOpts | ||
opts = { | ||
autocmds = { | ||
auto_signature_help = { | ||
cond = "textDocument/signatureHelp", | ||
{ | ||
event = "TextChangedI", | ||
desc = "Automatically trigger signature help when a trigger character is typed", | ||
callback = function(args) | ||
local signature_help_trigger = vim.b[args.buf].signature_help_trigger | ||
if signature_help_trigger then | ||
local cur_line = vim.api.nvim_get_current_line():gsub("%s+$", "") -- rm trailing spaces | ||
local pos = vim.api.nvim_win_get_cursor(0)[2] | ||
local cur_char = cur_line:sub(pos, pos) | ||
|
||
for _, char in ipairs(signature_help_trigger) do | ||
if cur_char == char then | ||
vim.lsp.buf.signature_help() | ||
return | ||
end | ||
end | ||
end | ||
end, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} |