|
| 1 | +local require = require("noice.util.lazy") |
| 2 | + |
| 3 | +local NoiceText = require("noice.text") |
| 4 | +local Format = require("noice.source.lsp.format") |
| 5 | +local Markdown = require("noice.text.markdown") |
| 6 | +local Lsp = require("noice.source.lsp") |
| 7 | + |
| 8 | +---@class SignatureInformation |
| 9 | +---@field label string |
| 10 | +---@field documentation? string|MarkupContent |
| 11 | +---@field parameters? ParameterInformation[] |
| 12 | +---@field activeParameter? integer |
| 13 | + |
| 14 | +---@class ParameterInformation |
| 15 | +---@field label string|{[1]:integer, [2]:integer} |
| 16 | +---@field documentation? string|MarkupContent |
| 17 | + |
| 18 | +---@class SignatureHelpContext |
| 19 | +---@field triggerKind SignatureHelpTriggerKind |
| 20 | +---@field triggerCharacter? string |
| 21 | +---@field isRetrigger boolean |
| 22 | +---@field activeSignatureHelp? SignatureHelp |
| 23 | + |
| 24 | +---@class SignatureHelp |
| 25 | +---@field signatures SignatureInformation[] |
| 26 | +---@field activeSignature? integer |
| 27 | +---@field activeParameter? integer |
| 28 | +---@field ft? string |
| 29 | +---@field message NoiceMessage |
| 30 | +local M = {} |
| 31 | +M.__index = M |
| 32 | + |
| 33 | +---@enum SignatureHelpTriggerKind |
| 34 | +M.trigger_kind = { |
| 35 | + invoked = 1, |
| 36 | + trigger_character = 2, |
| 37 | + content_change = 3, |
| 38 | +} |
| 39 | + |
| 40 | +-- TODO: add scroll up/down |
| 41 | +-- TODO: horz line for Hover should overlap end of code block |
| 42 | +-- TODO: markdown links |
| 43 | + |
| 44 | +function M.setup() |
| 45 | + vim.api.nvim_create_autocmd("LspAttach", { |
| 46 | + callback = function(args) |
| 47 | + local bufnr = args.buf |
| 48 | + local client = vim.lsp.get_client_by_id(args.data.client_id) |
| 49 | + if client.server_capabilities.signatureHelpProvider then |
| 50 | + local chars = client.server_capabilities.signatureHelpProvider.triggerCharacters |
| 51 | + if #chars > 0 then |
| 52 | + vim.api.nvim_create_autocmd({ "TextChangedI", "TextChangedP", "InsertEnter" }, { |
| 53 | + buffer = bufnr, |
| 54 | + callback = function() |
| 55 | + if vim.api.nvim_get_current_buf() ~= bufnr then |
| 56 | + return |
| 57 | + end |
| 58 | + local message = Lsp.get(Lsp.kinds.signature) |
| 59 | + if message:win() then |
| 60 | + -- no need to fetch signature when signature is already shown |
| 61 | + return |
| 62 | + end |
| 63 | + local cursor = vim.api.nvim_win_get_cursor(0) |
| 64 | + local row = cursor[1] - 1 |
| 65 | + local col = cursor[2] |
| 66 | + local _, lines = pcall(vim.api.nvim_buf_get_text, bufnr, row, 0, row, col, {}) |
| 67 | + local line = vim.trim(lines and lines[1] or "") |
| 68 | + local char = line:sub(-1, -1) |
| 69 | + if vim.tbl_contains(chars, char) then |
| 70 | + local params = vim.lsp.util.make_position_params(0, client.offset_encoding) |
| 71 | + vim.lsp.buf_request( |
| 72 | + bufnr, |
| 73 | + "textDocument/signatureHelp", |
| 74 | + params, |
| 75 | + vim.lsp.with(require("noice.source.lsp").signature, { trigger = true }) |
| 76 | + ) |
| 77 | + end |
| 78 | + end, |
| 79 | + }) |
| 80 | + end |
| 81 | + end |
| 82 | + end, |
| 83 | + }) |
| 84 | +end |
| 85 | + |
| 86 | +---@param help SignatureHelp |
| 87 | +function M.new(help) |
| 88 | + return setmetatable(help, M) |
| 89 | +end |
| 90 | + |
| 91 | +function M:active_parameter(sig_index) |
| 92 | + if self.activeSignature and self.signatures[self.activeSignature + 1] and sig_index ~= self.activeSignature + 1 then |
| 93 | + return |
| 94 | + end |
| 95 | + local sig = self.signatures[sig_index] |
| 96 | + if sig.activeParameter and sig.parameters[sig.activeParameter + 1] then |
| 97 | + return sig.parameters[sig.activeParameter + 1] |
| 98 | + end |
| 99 | + if self.activeParameter and sig.parameters[self.activeParameter + 1] then |
| 100 | + return sig.parameters[self.activeParameter + 1] |
| 101 | + end |
| 102 | + return sig.parameters[1] |
| 103 | +end |
| 104 | + |
| 105 | +---@param sig SignatureInformation |
| 106 | +---@param param ParameterInformation |
| 107 | +function M:format_active_parameter(sig, param) |
| 108 | + local label = param.label |
| 109 | + if type(label) == "string" then |
| 110 | + local from = sig.label:find(label, 1, true) |
| 111 | + if from then |
| 112 | + self.message:append(NoiceText("", { |
| 113 | + hl_group = "LspSignatureActiveParameter", |
| 114 | + col = from - 1, |
| 115 | + length = vim.fn.strlen(label), |
| 116 | + })) |
| 117 | + end |
| 118 | + else |
| 119 | + self.message:append(NoiceText("", { |
| 120 | + hl_group = "LspSignatureActiveParameter", |
| 121 | + col = label[1], |
| 122 | + length = label[2] - label[1], |
| 123 | + })) |
| 124 | + end |
| 125 | +end |
| 126 | + |
| 127 | +---@param sig SignatureInformation |
| 128 | +function M:format_signature(sig_index, sig) |
| 129 | + if sig_index ~= 1 then |
| 130 | + self.message:append("\n\n") |
| 131 | + self.message:append(sig) |
| 132 | + end |
| 133 | + self.message:append("```" .. (self.ft or "")) |
| 134 | + if sig_index ~= 1 then |
| 135 | + Markdown.horizontal_line(self.message) |
| 136 | + end |
| 137 | + self.message:newline() |
| 138 | + self.message:append(sig.label) |
| 139 | + local param = self:active_parameter(sig_index) |
| 140 | + if param then |
| 141 | + self:format_active_parameter(sig, param) |
| 142 | + end |
| 143 | + self.message:append("\n```") |
| 144 | + if sig.documentation then |
| 145 | + Markdown.horizontal_line(self.message) |
| 146 | + self.message:newline() |
| 147 | + Format.format(self.message, sig.documentation) |
| 148 | + end |
| 149 | +end |
| 150 | + |
| 151 | +function M:format() |
| 152 | + for s, sig in ipairs(self.signatures) do |
| 153 | + self:format_signature(s, sig) |
| 154 | + end |
| 155 | +end |
| 156 | + |
| 157 | +return M |
0 commit comments