From 026623ab7e86f2b063131602b8bbf0381f4f994e Mon Sep 17 00:00:00 2001 From: Yegappan Lakshmanan Date: Sat, 29 Oct 2022 17:41:24 -0700 Subject: [PATCH] Use autocmd_add() function to add autocmds --- autoload/lsp/lsp.vim | 77 +++++++++++++++++++++++++------------- autoload/lsp/outline.vim | 34 +++++++++++++---- autoload/lsp/signature.vim | 4 +- 3 files changed, 79 insertions(+), 36 deletions(-) diff --git a/autoload/lsp/lsp.vim b/autoload/lsp/lsp.vim index 919df56..13807a2 100644 --- a/autoload/lsp/lsp.vim +++ b/autoload/lsp/lsp.vim @@ -245,7 +245,7 @@ def g:LspDiagExpr(): any enddef # Called after leaving insert mode. Used to process diag messages (if any) -def g:LspLeftInsertMode() +def LspLeftInsertMode() if !exists('b:LspDiagsUpdatePending') return endif @@ -259,6 +259,51 @@ def g:LspLeftInsertMode() diag.UpdateDiags(lspserver, bnr) enddef +# Add buffer-local autocmds when attaching a LSP server to a buffer +def AddBufLocalAutocmds(lspserver: dict, bnr: number): void + var acmds: list> = [] + + # file saved notification handler + acmds->add({bufnr: bnr, + event: 'BufWritePost', + group: 'LSPBufferAutocmds', + cmd: 'LspSavedFile()'}) + + # Update the diagnostics when insert mode is stopped + acmds->add({bufnr: bnr, + event: 'InsertLeave', + group: 'LSPBufferAutocmds', + cmd: 'LspLeftInsertMode()'}) + + # Insert-mode completion autocmds (if configured) + if opt.lspOptions.autoComplete + # Trigger 24x7 insert mode completion when text is changed + acmds->add({bufnr: bnr, + event: 'TextChangedI', + group: 'LSPBufferAutocmds', + cmd: 'LspComplete()'}) + if lspserver.completionLazyDoc + # resolve additional documentation for a selected item + acmds->add({bufnr: bnr, + event: 'CompleteChanged', + group: 'LSPBufferAutocmds', + cmd: 'LspResolve()'}) + endif + endif + + # Auto highlight all the occurrences of the current keyword + if opt.lspOptions.autoHighlight && + lspserver.caps->get('documentHighlightProvider', false) + acmds->add({bufnr: bnr, + event: 'CursorMoved', + group: 'LSPBufferAutocmds', + cmd: 'call LspDocHighlightClear() | call LspDocHighlight()'}) + endif + + autocmd_add(acmds) + +enddef + def BufferInit(bnr: number): void var lspserver: dict = buf.BufLspServerGet(bnr) if lspserver->empty() || !lspserver.running @@ -295,33 +340,11 @@ def BufferInit(bnr: number): void # initialize signature help signature.SignatureInit(lspserver) - # Set buffer local autocmds - augroup LSPBufferAutocmds - # file saved notification handler - exe $'autocmd BufWritePost call LspSavedFile()' - - if opt.lspOptions.autoComplete - # Trigger 24x7 insert mode completion when text is changed - exe $'autocmd TextChangedI call LspComplete()' - if lspserver.completionLazyDoc - exe $'autocmd CompleteChanged call LspResolve()' - endif - endif - - # Update the diagnostics when insert mode is stopped - exe $'autocmd InsertLeave call LspLeftInsertMode()' - - if opt.lspOptions.autoHighlight && - lspserver.caps->get('documentHighlightProvider', false) - # Highlight all the occurrences of the current keyword - exe $'autocmd CursorMoved call LspDocHighlightClear() | call LspDocHighlight()' - endif - augroup END + AddBufLocalAutocmds(lspserver, bnr) if exists('#User#LspAttached') doautocmd User LspAttached endif - enddef # A new buffer is opened. If LSP is supported for this buffer, then add it @@ -356,7 +379,7 @@ export def AddFile(bnr: number): void BufferInit(bnr) else augroup LSPBufferAutocmds - exe $'autocmd User LspServerReady{lspserver.name} ++once call BufferInit({bnr})' + exe $'autocmd User LspServerReady{lspserver.name} ++once BufferInit({bnr})' augroup END endif @@ -578,7 +601,7 @@ enddef # Insert mode completion handler. Used when 24x7 completion is enabled # (default). -def g:LspComplete() +def LspComplete() var lspserver: dict = buf.CurbufGetServer() if lspserver->empty() || !lspserver.running || !lspserver.ready return @@ -617,7 +640,7 @@ def g:LspComplete() enddef # Lazy complete documentation handler -def g:LspResolve() +def LspResolve() var lspserver: dict = CurbufGetServerChecked() if lspserver->empty() return diff --git a/autoload/lsp/outline.vim b/autoload/lsp/outline.vim index e34c73f..26dcae1 100644 --- a/autoload/lsp/outline.vim +++ b/autoload/lsp/outline.vim @@ -212,7 +212,7 @@ enddef # when the outline window is closed, do the cleanup def OutlineCleanup() # Remove the outline autocommands - :silent! autocmd! LSPOutline + silent! autocmd_delete([{group: 'LSPOutline'}]) :silent! syntax clear LSPTitle enddef @@ -261,13 +261,31 @@ export def OpenOutlineWindow() prop_type_add('LspOutlineHighlight', {bufnr: bufnr(), highlight: 'Search'}) - augroup LSPOutline - au! - autocmd BufEnter * call g:LspRequestDocSymbols() - # when the outline window is closed, do the cleanup - autocmd BufUnload LSP-Outline call OutlineCleanup() - autocmd CursorHold * call OutlineHighlightCurrentSymbol() - augroup END + try + autocmd_delete([{group: 'LSPOutline', event: '*'}]) + catch /E367:/ + endtry + var acmds: list> + + # Refresh or add the symbols in a buffer to the outline window + acmds->add({event: 'BufEnter', + group: 'LSPOutline', + pattern: '*', + cmd: 'call g:LspRequestDocSymbols()'}) + + # when the outline window is closed, do the cleanup + acmds->add({event: 'BufUnload', + group: 'LSPOutline', + pattern: 'LSP-Outline', + cmd: 'OutlineCleanup()'}) + + # Highlight the current symbol when the cursor is not moved for sometime + acmds->add({event: 'CursorHold', + group: 'LSPOutline', + pattern: '*', + cmd: 'OutlineHighlightCurrentSymbol()'}) + + autocmd_add(acmds) prevWinID->win_gotoid() enddef diff --git a/autoload/lsp/signature.vim b/autoload/lsp/signature.vim index c5c0c84..57bd65d 100644 --- a/autoload/lsp/signature.vim +++ b/autoload/lsp/signature.vim @@ -35,7 +35,9 @@ export def SignatureInit(lspserver: dict) exe $"inoremap {ch} {ch}=LspShowSignature()" endfor # close the signature popup when leaving insert mode - autocmd InsertLeave call CloseCurBufSignaturePopup() + autocmd_add([{bufnr: bufnr(), + event: 'InsertLeave', + cmd: 'CloseCurBufSignaturePopup()'}]) enddef # process the 'textDocument/signatureHelp' reply from the LSP server and -- 2.48.1