README.md | 6 +++--- doc/lsp.txt | 15 +++++++++++---- plugin/lsp.vim | 10 +++++++++- diff --git a/README.md b/README.md index b489cafcb8a0ba84cbc86e4c11d28ceb14ea2b68..011bfe26fb5324b12fa08bf856831dc0dd2707d4 100644 --- a/README.md +++ b/README.md @@ -157,10 +157,10 @@ \ filterCompletionDuplicates: v:false, \ }) ``` -If you used [vim-plug](https://github.com/junegunn/vim-plug) to install the LSP plugin, then you need to use the VimEnter autocmd to initialize the LSP server and to set the LSP server options. For example: +If you used [vim-plug](https://github.com/junegunn/vim-plug) to install the LSP plugin, then you need to use the LspSetup User autocmd to initialize the LSP server and to set the LSP server options. For example: ```viml let lspOpts = #{autoHighlightDiags: v:true} -autocmd VimEnter * call LspOptionsSet(lspOpts) +autocmd User LspSetup call LspOptionsSet(lspOpts) let lspServers = [#{ \ name: 'clang', @@ -168,7 +168,7 @@ \ filetype: ['c', 'cpp'], \ path: '/usr/local/bin/clangd', \ args: ['--background-index'] \ }] -autocmd VimEnter * call LspAddServer(lspServers) +autocmd User LspSetup call LspAddServer(lspServers) ``` ## Supported Commands diff --git a/doc/lsp.txt b/doc/lsp.txt index 7db10965a201809e557c01aba05281f7ac41dd19..3c1e3fc9aba83881ea6901b63c8189b80af8d463 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -3,7 +3,7 @@ Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com) For Vim version 9.0 and above -Last change: Feb 7, 2024 +Last change: Feb 13, 2024 ============================================================================== CONTENTS *lsp-contents* @@ -412,13 +412,13 @@ The language servers are added using the LspAddServer() function. This function accepts a list of language servers with the above information. If you used [vim-plug](https://github.com/junegunn/vim-plug) to install the -LSP plugin, then you need to use the VimEnter autocmd to initialize the +LSP plugin, then you need to use the LspSetup User autocmd to initialize the language server and to set the language server options. For example: > vim9script var lspOpts = {autoHighlightDiags: true} - autocmd VimEnter * LspOptionsSet(lspOpts) + autocmd User LspSetup LspOptionsSet(lspOpts) var lspServers = [ { @@ -428,7 +428,7 @@ path: '/usr/local/bin/clangd', args: ['--background-index'] } ] - autocmd VimEnter * LspAddServer(lspServers) + autocmd User LspSetup LspAddServer(lspServers) < *lsp-options* *LspOptionsSet()* *g:LspOptionsSet()* @@ -1580,6 +1580,13 @@ refreshed to display the outgoing calls. ============================================================================== 11. Autocommands *lsp-autocmds* + + *LspSetup* +LspSetup A |User| autocommand fired when the LSP plugin + is loaded. Can be used to add language + servers using the |LspAddServer()| function + and to set plugin options using the + |LspOptionsSet()| function. *LspAttached* LspAttached A |User| autocommand fired when the LSP client diff --git a/plugin/lsp.vim b/plugin/lsp.vim index 49d667710609d60ddb853e0610e007852c28812b..9125b6eb65f5306d625eb1863b43a83058d30153 100644 --- a/plugin/lsp.vim +++ b/plugin/lsp.vim @@ -3,10 +3,13 @@ " Needs Vim version 9.0 and above finish endif -vim9script +vim9script noclear # Language Server Protocol (LSP) plugin for vim +if get(g:, 'loaded_lsp', false) + finish +endif g:loaded_lsp = true import '../autoload/lsp/options.vim' @@ -148,6 +151,11 @@ \ :LspHighlight anoremenu PopUp.L&sp.Highlight\ Clear \ :LspHighlightClear endif +endif + +# Invoke autocmd to register LSP servers and to set LSP options +if exists('#User#LspSetup') + :doautocmd User LspSetup endif # vim: shiftwidth=2 softtabstop=2