2021-04-26 15:24:12 +00:00
|
|
|
local util = require "navigator.util"
|
|
|
|
local log = util.log
|
|
|
|
|
2021-09-05 22:34:26 +00:00
|
|
|
--- navigator signature
|
2021-04-26 15:24:12 +00:00
|
|
|
local match_parameter = function(result)
|
|
|
|
local signatures = result.signatures
|
|
|
|
if #signatures < 1 then
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
local signature = signatures[1]
|
|
|
|
local activeParameter = result.activeParameter or signature.activeParameter
|
|
|
|
if activeParameter == nil then
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2021-05-30 02:25:05 +00:00
|
|
|
if signature.parameters == nil then
|
|
|
|
return
|
|
|
|
end
|
2021-04-27 08:32:19 +00:00
|
|
|
|
2021-04-26 15:24:12 +00:00
|
|
|
if #signature.parameters < 2 or activeParameter + 1 > #signature.parameters then
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
local nextParameter = signature.parameters[activeParameter + 1]
|
|
|
|
|
|
|
|
local label = signature.label
|
|
|
|
if type(nextParameter.label) == "table" then -- label = {2, 4} c style
|
|
|
|
local range = nextParameter.label
|
2021-05-30 02:25:05 +00:00
|
|
|
label = label:sub(1, range[1]) .. [[`]] .. label:sub(range[1] + 1, range[2]) .. [[`]]
|
|
|
|
.. label:sub(range[2] + 1, #label + 1)
|
2021-04-26 15:24:12 +00:00
|
|
|
signature.label = label
|
|
|
|
else
|
|
|
|
if type(nextParameter.label) == "string" then -- label = 'par1 int'
|
|
|
|
local i, j = label:find(nextParameter.label, 1, true)
|
|
|
|
if i ~= nil then
|
2021-11-12 09:06:47 +00:00
|
|
|
label = label:sub(1, i - 1) .. [[`]] .. label:sub(i, j) .. [[`]] .. label:sub(j + 1, #label + 1)
|
2021-04-26 15:24:12 +00:00
|
|
|
signature.label = label
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-15 13:13:01 +00:00
|
|
|
local signature_handler = function(err, result, ctx, config)
|
2021-07-23 00:19:19 +00:00
|
|
|
if config == nil then
|
|
|
|
log("config nil")
|
|
|
|
end
|
2021-09-14 00:41:13 +00:00
|
|
|
if err then
|
2021-12-29 04:50:41 +00:00
|
|
|
vim.notify("signature help error: ".. vim.inspect(err) .. vim.inspect(result), ctx, config, vim.lsp.log_levels.WARN)
|
2021-09-14 00:41:13 +00:00
|
|
|
end
|
|
|
|
config = config or {}
|
|
|
|
if config.border == nil then
|
|
|
|
config.border = _NgConfigValues.border
|
|
|
|
end
|
|
|
|
|
2021-04-26 15:24:12 +00:00
|
|
|
if not (result and result.signatures and result.signatures[1]) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
match_parameter(result)
|
|
|
|
local lines = vim.lsp.util.convert_signature_help_to_markdown_lines(result)
|
|
|
|
if vim.tbl_isempty(lines) then
|
|
|
|
return
|
|
|
|
end
|
2021-05-30 02:25:05 +00:00
|
|
|
|
|
|
|
local syntax = vim.lsp.util.try_trim_markdown_code_blocks(lines)
|
2021-09-05 22:34:26 +00:00
|
|
|
config.focus_id = ctx.bufnr .. "lsp_signature"
|
2021-05-30 02:25:05 +00:00
|
|
|
vim.lsp.util.open_floating_preview(lines, syntax, config)
|
2022-05-15 13:13:01 +00:00
|
|
|
end
|
2021-04-26 15:24:12 +00:00
|
|
|
return {signature_handler = signature_handler}
|