Skip to content

Commit

Permalink
fix: keep signature help open as long as the last non-whitespace char…
Browse files Browse the repository at this point in the history
…acter is a trigger character
  • Loading branch information
folke committed Oct 27, 2022
1 parent 419640e commit 5f4544f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
8 changes: 6 additions & 2 deletions lua/noice/source/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ function M.close(message)
end

---@param message NoiceMessage
function M.auto_close(message)
---@param keep? fun():boolean
function M.auto_close(message, keep)
message.opts.timeout = 100
message.opts.keep = function()
return true
Expand All @@ -90,6 +91,9 @@ function M.auto_close(message)
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI", "InsertCharPre" }, {
group = group,
callback = function()
if keep and keep() then
return
end
if not message:on_buf(vim.api.nvim_get_current_buf()) then
M.close(message)
end
Expand Down Expand Up @@ -123,7 +127,7 @@ function M.signature(_, result, ctx, config)
result.ft = vim.bo[ctx.bufnr].filetype
result.message = message
Signature.new(result):format()
M.auto_close(message)
M.auto_close(message, config.keep)
Manager.add(message)
end
end
Expand Down
33 changes: 21 additions & 12 deletions lua/noice/source/lsp/signature.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,47 @@ M.trigger_kind = {
-- TODO: horz line for Hover should overlap end of code block
-- TODO: positioning of the signature help window

function M.get_char(buf)
local win = vim.fn.bufwinid(buf)
local cursor = vim.api.nvim_win_get_cursor(win == -1 and 0 or win)
local row = cursor[1] - 1
local col = cursor[2]
local _, lines = pcall(vim.api.nvim_buf_get_text, buf, row, 0, row, col, {})
local line = vim.trim(lines and lines[1] or "")
return line:sub(-1, -1)
end

function M.setup()
vim.api.nvim_create_autocmd("LspAttach", {
callback = function(args)
local bufnr = args.buf
local buf = args.buf
local client = vim.lsp.get_client_by_id(args.data.client_id)
if client.server_capabilities.signatureHelpProvider then
local chars = client.server_capabilities.signatureHelpProvider.triggerCharacters
if #chars > 0 then
vim.api.nvim_create_autocmd({ "TextChangedI", "TextChangedP", "InsertEnter" }, {
buffer = bufnr,
buffer = buf,
callback = function()
if vim.api.nvim_get_current_buf() ~= bufnr then
if vim.api.nvim_get_current_buf() ~= buf then
return
end
local message = Lsp.get(Lsp.kinds.signature)
if message:win() then
-- no need to fetch signature when signature is already shown
return
end
local cursor = vim.api.nvim_win_get_cursor(0)
local row = cursor[1] - 1
local col = cursor[2]
local _, lines = pcall(vim.api.nvim_buf_get_text, bufnr, row, 0, row, col, {})
local line = vim.trim(lines and lines[1] or "")
local char = line:sub(-1, -1)
if vim.tbl_contains(chars, char) then
if vim.tbl_contains(chars, M.get_char(buf)) then
local params = vim.lsp.util.make_position_params(0, client.offset_encoding)
vim.lsp.buf_request(
bufnr,
buf,
"textDocument/signatureHelp",
params,
vim.lsp.with(require("noice.source.lsp").signature, { trigger = true })
vim.lsp.with(require("noice.source.lsp").signature, {
trigger = true,
keep = function()
return vim.tbl_contains(chars, M.get_char(buf))
end,
})
)
end
end,
Expand Down

0 comments on commit 5f4544f

Please sign in to comment.