diff --git a/lua/sighelp/helper.lua b/lua/sighelp/helper.lua index 9b68353..bbf92f2 100644 --- a/lua/sighelp/helper.lua +++ b/lua/sighelp/helper.lua @@ -109,12 +109,11 @@ local function make_signature_lines(signature_help, maxlen) vim.list_extend(lines, vim.split(signature.label, "\n", { plain = true, trimempty = true })) local wrapped_lines = {} - for _, ll in ipairs(lines) do - vim.list_extend(wrapped_lines, _wrap_lines(ll, maxlen)) + for _, l in ipairs(lines) do + vim.list_extend(wrapped_lines, _wrap_lines(l, maxlen)) end return wrapped_lines end - local function make_doc_lines(result, ft) local active_signature = result.activeSignature or 0 local signature = result.signatures[active_signature + 1] @@ -319,17 +318,24 @@ local function create_float_windows(result, syntax, opts) syntax = syntax or "text" vim.bo[sig_bufnr].syntax = syntax vim.bo[sig_bufnr].ft = syntax + vim.bo[sig_bufnr].bufhidden = "hide" vim.treesitter.start(sig_bufnr) + vim.bo[doc_bufnr].syntax = "markdown" vim.bo[doc_bufnr].ft = "markdown" + vim.bo[doc_bufnr].bufhidden = "hide" vim.treesitter.start(doc_bufnr) local sig_content = make_signature_lines(result, opts.max_width) local doc_content = make_doc_lines(result, syntax) + ---@diagnostic disable-next-line: param-type-mismatch api.nvim_buf_set_lines(sig_bufnr, 0, -1, true, sig_content) + vim.bo[sig_bufnr].modifiable = false + ---@diagnostic disable-next-line: param-type-mismatch api.nvim_buf_set_lines(doc_bufnr, 0, -1, true, doc_content) + vim.bo[doc_bufnr].modifiable = false local sig_float_options, doc_float_options = _make_floating_popup_options(sig_content, doc_content, opts) return function() @@ -337,8 +343,6 @@ local function create_float_windows(result, syntax, opts) vim.wo[sig_winnr].conceallevel = 2 vim.wo[sig_winnr].foldenable = false vim.wo[sig_winnr].winblend = opts.winblend - vim.bo[sig_bufnr].modifiable = false - vim.bo[sig_bufnr].bufhidden = "hide" -- save focus_id api.nvim_buf_set_var(bufnr, "lsp_sig_win", sig_winnr) return sig_bufnr, sig_winnr, sig_content @@ -347,8 +351,6 @@ local function create_float_windows(result, syntax, opts) vim.wo[doc_winnr].conceallevel = 2 vim.wo[doc_winnr].foldenable = false vim.wo[doc_winnr].winblend = opts.winblend - vim.bo[doc_bufnr].modifiable = false - vim.bo[doc_bufnr].bufhidden = "hide" -- save focus_id api.nvim_buf_set_var(doc_bufnr, "lsp_doc_win", doc_winnr) diff --git a/lua/sighelp/init.lua b/lua/sighelp/init.lua index 8e676ea..d471ccc 100644 --- a/lua/sighelp/init.lua +++ b/lua/sighelp/init.lua @@ -19,7 +19,6 @@ function SignatureHelp.new() markid = nil, timer = nil, current_signatures = nil, - enabled = false, current_signature_idx = nil, doc_winnr = nil, @@ -179,12 +178,18 @@ function SignatureHelp:display_doc_win() if self.doc_win_showing then return else + self.active_winnr = vim.api.nvim_get_current_win() self.doc_bufnr, self.doc_winnr = self._display_doc_win() self.doc_win_showing = true self.doc_win_focused = true vim.api.nvim_set_current_win(self.doc_winnr) - exit_docs = function() + -- set window variable that can be checked externally for i.e. setting keymaps + local w = vim.w + w.sighelp_doc_win = true + vim.w = w + + local exit_docs = function() vim.api.nvim_set_current_win(self.active_winnr) vim.fn.feedkeys("i", "t") --put us back into insert mode self.doc_win_focused = false @@ -208,7 +213,7 @@ function SignatureHelp:display_doc_win() end function SignatureHelp:trigger() - if not self.enabled then + if not vim.b.signatureHelp then return end @@ -239,74 +244,80 @@ function SignatureHelp:trigger() end function SignatureHelp:check_capability() - local clients = vim.lsp.get_clients() + local bufnr = vim.api.nvim_get_current_buf() + local clients = vim.lsp.get_clients({ bufnr = bufnr }) for _, client in ipairs(clients) do if client.server_capabilities.signatureHelpProvider then - self.enabled = true + vim.b.signatureHelp = true return end end - self.enabled = false end function SignatureHelp:setup_autocmds() - local group = api.nvim_create_augroup("LspSignatureHelp", { clear = true }) - - local function debounced_trigger() - if self.timer then - vim.fn.timer_stop(self.timer) - end - self.timer = vim.fn.timer_start(self.debounce_time, function() - self:trigger() - end) - end - - api.nvim_create_autocmd({ "InsertEnter", "CursorMovedI" }, { - group = group, - callback = function() - debounced_trigger() - end, - }) - - -- make sure signature help windows get closed if we switch out of the doc window - -- instead of exiting it using the exit keymaps - api.nvim_create_autocmd({ "WinEnter" }, { - group = group, - callback = function() - vim.defer_fn(function() - if self.active_winnr == vim.api.nvim_get_current_win() and vim.api.nvim_get_mode().mode == "n" then - self:hide_sig_win() - self:hide_doc_win() - end - end, 30) - end, - }) - - api.nvim_create_autocmd({ "CursorMoved" }, { - group = group, - callback = function() - if self.normal_mode_active then - debounced_trigger() - end - end, - }) - - api.nvim_create_autocmd({ "InsertLeave" }, { - group = group, - callback = function() - if self.doc_win_focused == false then - self:hide_sig_win() - self:hide_doc_win() - self.normal_mode_active = false - end - end, - }) - - api.nvim_create_autocmd("LspAttach", { - group = group, + api.nvim_create_autocmd({ "LspAttach", "BufEnter" }, { + -- group = group, callback = function() vim.defer_fn(function() + local group = api.nvim_create_augroup("LspSignatureHelp", { clear = true }) self:check_capability() + + if not vim.b.signatureHelp then + return + end + + local function debounced_trigger() + if self.timer then + vim.fn.timer_stop(self.timer) + end + self.timer = vim.fn.timer_start(self.debounce_time, function() + self:trigger() + end) + end + + api.nvim_create_autocmd({ "InsertEnter", "CursorMovedI" }, { + group = group, + callback = function() + debounced_trigger() + end, + }) + + -- make sure signature help windows get closed if we switch out of the doc window + -- instead of exiting it using the exit keymaps + api.nvim_create_autocmd({ "WinEnter" }, { + group = group, + callback = function() + vim.defer_fn(function() + if + self.active_winnr == vim.api.nvim_get_current_win() + and vim.api.nvim_get_mode().mode == "n" + then + self:hide_sig_win() + self:hide_doc_win() + end + end, 30) + end, + }) + + api.nvim_create_autocmd({ "CursorMoved" }, { + group = group, + callback = function() + if self.normal_mode_active then + debounced_trigger() + end + end, + }) + + api.nvim_create_autocmd({ "InsertLeave" }, { + group = group, + callback = function() + if self.doc_win_focused == false then + self:hide_sig_win() + self:hide_doc_win() + self.normal_mode_active = false + end + end, + }) end, 100) end, }) @@ -389,7 +400,6 @@ function M.setup(opts) group = vim.api.nvim_create_augroup("LspSignatureColors", { clear = true }), callback = setup_highlights, }) - -- Setup autocmds signature_help:setup_autocmds()