Skip to content

Commit

Permalink
Add support for the LspSetup user autocmd to add language servers and…
Browse files Browse the repository at this point in the history
… to set options after the plugin is loaded
  • Loading branch information
yegappan committed Feb 14, 2024
1 parent 705a4b2 commit 87189fa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,18 @@ 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',
\ filetype: ['c', 'cpp'],
\ path: '/usr/local/bin/clangd',
\ args: ['--background-index']
\ }]
autocmd VimEnter * call LspAddServer(lspServers)
autocmd User LspSetup call LspAddServer(lspServers)
```

## Supported Commands
Expand Down
15 changes: 11 additions & 4 deletions doc/lsp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down Expand Up @@ -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 = [
{
Expand All @@ -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()*
Expand Down Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion plugin/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 <nomodeline> User LspSetup
endif

# vim: shiftwidth=2 softtabstop=2

0 comments on commit 87189fa

Please sign in to comment.