From: Yegappan Lakshmanan Date: Wed, 14 Feb 2024 06:47:56 +0000 (-0800) Subject: Add support for the LspSetup user autocmd to add language servers and to set options... X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=87189faf0bd4a2f974408df2d8c261dbfc3348dd;p=vim-lsp.git Add support for the LspSetup user autocmd to add language servers and to set options after the plugin is loaded --- diff --git a/README.md b/README.md index b489caf..011bfe2 100644 --- a/README.md +++ b/README.md @@ -157,10 +157,10 @@ call LspOptionsSet(#{ \ }) ``` -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 @@ let lspServers = [#{ \ 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 7db1096..3c1e3fc 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 @@ language server and to set the language server options. For example: > args: ['--background-index'] } ] - autocmd VimEnter * LspAddServer(lspServers) + autocmd User LspSetup LspAddServer(lspServers) < *lsp-options* *LspOptionsSet()* *g:LspOptionsSet()* @@ -1581,6 +1581,13 @@ In the call hierarchy tree window, the following commands are supported: ============================================================================== 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 attaches to a buffer. Can be used to configure diff --git a/plugin/lsp.vim b/plugin/lsp.vim index 49d6677..9125b6e 100644 --- a/plugin/lsp.vim +++ b/plugin/lsp.vim @@ -3,10 +3,13 @@ if !has('vim9script') || v:version < 900 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' @@ -150,4 +153,9 @@ if has('gui_running') 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