mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
fix(lsp): support duplicate params in signature help (#15032)
This commit is contained in:
parent
1c41689287
commit
9132b76da6
@ -329,20 +329,26 @@ M['textDocument/implementation'] = location_handler
|
|||||||
--- - border: (default=nil)
|
--- - border: (default=nil)
|
||||||
--- - Add borders to the floating window
|
--- - Add borders to the floating window
|
||||||
--- - See |vim.api.nvim_open_win()|
|
--- - See |vim.api.nvim_open_win()|
|
||||||
function M.signature_help(_, method, result, _, bufnr, config)
|
function M.signature_help(_, method, result, client_id, bufnr, config)
|
||||||
config = config or {}
|
config = config or {}
|
||||||
config.focus_id = method
|
config.focus_id = method
|
||||||
-- When use `autocmd CompleteDone <silent><buffer> lua vim.lsp.buf.signature_help()` to call signatureHelp handler
|
-- When use `autocmd CompleteDone <silent><buffer> lua vim.lsp.buf.signature_help()` to call signatureHelp handler
|
||||||
-- If the completion item doesn't have signatures It will make noise. Change to use `print` that can use `<silent>` to ignore
|
-- If the completion item doesn't have signatures It will make noise. Change to use `print` that can use `<silent>` to ignore
|
||||||
if not (result and result.signatures and result.signatures[1]) then
|
if not (result and result.signatures and result.signatures[1]) then
|
||||||
print('No signature help available')
|
if config.silent ~= true then
|
||||||
|
print('No signature help available')
|
||||||
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
local client = vim.lsp.get_client_by_id(client_id)
|
||||||
|
local triggers = client.resolved_capabilities.signature_help_trigger_characters
|
||||||
local ft = api.nvim_buf_get_option(bufnr, 'filetype')
|
local ft = api.nvim_buf_get_option(bufnr, 'filetype')
|
||||||
local lines, hl = util.convert_signature_help_to_markdown_lines(result, ft)
|
local lines, hl = util.convert_signature_help_to_markdown_lines(result, ft, triggers)
|
||||||
lines = util.trim_empty_lines(lines)
|
lines = util.trim_empty_lines(lines)
|
||||||
if vim.tbl_isempty(lines) then
|
if vim.tbl_isempty(lines) then
|
||||||
print('No signature help available')
|
if config.silent ~= true then
|
||||||
|
print('No signature help available')
|
||||||
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local fbuf, fwin = util.open_floating_preview(lines, "markdown", config)
|
local fbuf, fwin = util.open_floating_preview(lines, "markdown", config)
|
||||||
|
@ -845,9 +845,10 @@ end
|
|||||||
---
|
---
|
||||||
--@param signature_help Response of `textDocument/SignatureHelp`
|
--@param signature_help Response of `textDocument/SignatureHelp`
|
||||||
--@param ft optional filetype that will be use as the `lang` for the label markdown code block
|
--@param ft optional filetype that will be use as the `lang` for the label markdown code block
|
||||||
|
--@param triggers optional list of trigger characters from the lsp server. used to better determine parameter offsets
|
||||||
--@returns list of lines of converted markdown.
|
--@returns list of lines of converted markdown.
|
||||||
--@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_signatureHelp
|
--@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_signatureHelp
|
||||||
function M.convert_signature_help_to_markdown_lines(signature_help, ft)
|
function M.convert_signature_help_to_markdown_lines(signature_help, ft, triggers)
|
||||||
if not signature_help.signatures then
|
if not signature_help.signatures then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -877,10 +878,16 @@ function M.convert_signature_help_to_markdown_lines(signature_help, ft)
|
|||||||
end
|
end
|
||||||
if signature.parameters and #signature.parameters > 0 then
|
if signature.parameters and #signature.parameters > 0 then
|
||||||
local active_parameter = (signature.activeParameter or signature_help.activeParameter or 0)
|
local active_parameter = (signature.activeParameter or signature_help.activeParameter or 0)
|
||||||
-- If the activeParameter is not inside the valid range, then clip it.
|
if active_parameter < 0
|
||||||
if active_parameter >= #signature.parameters then
|
then active_parameter = 0
|
||||||
active_parameter = 0
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- If the activeParameter is > #parameters, then set it to the last
|
||||||
|
-- NOTE: this is not fully according to the spec, but a client-side interpretation
|
||||||
|
if active_parameter >= #signature.parameters then
|
||||||
|
active_parameter = #signature.parameters - 1
|
||||||
|
end
|
||||||
|
|
||||||
local parameter = signature.parameters[active_parameter + 1]
|
local parameter = signature.parameters[active_parameter + 1]
|
||||||
if parameter then
|
if parameter then
|
||||||
--[=[
|
--[=[
|
||||||
@ -905,8 +912,23 @@ function M.convert_signature_help_to_markdown_lines(signature_help, ft)
|
|||||||
if type(parameter.label) == "table" then
|
if type(parameter.label) == "table" then
|
||||||
active_hl = parameter.label
|
active_hl = parameter.label
|
||||||
else
|
else
|
||||||
local i = signature.label:find(parameter.label)
|
local offset = 1
|
||||||
if i then active_hl = {i - 1, i + #parameter.label - 1} end
|
-- try to set the initial offset to the first found trigger character
|
||||||
|
for _, t in ipairs(triggers or {}) do
|
||||||
|
local trigger_offset = signature.label:find(t, 1, true)
|
||||||
|
if trigger_offset and (offset == 1 or trigger_offset < offset) then
|
||||||
|
offset = trigger_offset
|
||||||
|
end
|
||||||
|
end
|
||||||
|
for p, param in pairs(signature.parameters) do
|
||||||
|
offset = signature.label:find(param.label, offset, true)
|
||||||
|
if not offset then break end
|
||||||
|
if p == active_parameter + 1 then
|
||||||
|
active_hl = {offset - 1, offset + #parameter.label - 1}
|
||||||
|
break
|
||||||
|
end
|
||||||
|
offset = offset + #param.label + 1
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if parameter.documentation then
|
if parameter.documentation then
|
||||||
|
Loading…
Reference in New Issue
Block a user