Skip to content

Commit 41311b6

Browse files
committed
let LS detect workspace root after first file opened
Only set workspace root once the first file of that file type is opened; otherwise the workspace root often is $HOME (say when using Gvim, or opening a file in a Git repo) and the LS scans too many files Partially resolves [0] as ideally this workspace root would automatically change with the CWD, in particular after loading a session (:h mksession) Links: [0]: #512
1 parent 04b5b04 commit 41311b6

File tree

2 files changed

+83
-39
lines changed

2 files changed

+83
-39
lines changed

README.md

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ $ vim -u NONE -c "helptags $HOME/.vim/pack/downloads/opt/lsp/doc" -c q
1414
```
1515

1616
After installing the plugin using the above steps, add the following line to
17-
your $HOME/.vimrc file:
17+
your `$HOME/.vimrc` file:
1818

1919
```viml
2020
packadd lsp
@@ -55,41 +55,55 @@ To use the plugin features with a particular file type(s), you need to first reg
5555
The LSP servers are registered using the `LspAddServer()` function. This function accepts a list of LSP servers.
5656

5757
To register a LSP server, add the following lines to your .vimrc file (use only the LSP servers that you need from the below list). If you used [vim-plug](https://github.com/junegunn/vim-plug) to install the LSP plugin, the steps are described later in this section.
58+
5859
```viml
60+
g:lspServers = []
61+
62+
# Clangd language server
63+
g:lspServers->add({
64+
name: 'clangd',
65+
filetype: ['c', 'cpp'],
66+
path: '/usr/local/bin/clangd',
67+
args: ['--background-index']
68+
})
69+
70+
# Javascript/Typescript language server
71+
g:lspServers->add({
72+
name: 'typescriptlang',
73+
filetype: ['javascript', 'typescript'],
74+
path: '/usr/local/bin/typescript-language-server',
75+
args: ['--stdio'],
76+
})
77+
78+
# Go language server
79+
g:lspServers->add({
80+
name: 'golang',
81+
filetype: ['go', 'gomod'],
82+
path: '/usr/local/bin/gopls',
83+
args: ['serve'],
84+
syncInit: true
85+
})
5986
60-
" Clangd language server
61-
call LspAddServer([#{
62-
\ name: 'clangd',
63-
\ filetype: ['c', 'cpp'],
64-
\ path: '/usr/local/bin/clangd',
65-
\ args: ['--background-index']
66-
\ }])
67-
68-
" Javascript/Typescript language server
69-
call LspAddServer([#{
70-
\ name: 'typescriptlang',
71-
\ filetype: ['javascript', 'typescript'],
72-
\ path: '/usr/local/bin/typescript-language-server',
73-
\ args: ['--stdio'],
74-
\ }])
75-
76-
" Go language server
77-
call LspAddServer([#{
78-
\ name: 'golang',
79-
\ filetype: ['go', 'gomod'],
80-
\ path: '/usr/local/bin/gopls',
81-
\ args: ['serve'],
82-
\ syncInit: v:true
83-
\ }])
84-
85-
" Rust language server
86-
call LspAddServer([#{
87-
\ name: 'rustlang',
88-
\ filetype: ['rust'],
89-
\ path: '/usr/local/bin/rust-analyzer',
90-
\ args: [],
91-
\ syncInit: v:true
92-
\ }])
87+
# Rust language server
88+
g:lspServers->add({
89+
name: 'rustlang',
90+
filetype: ['rust'],
91+
path: '/usr/local/bin/rust-analyzer',
92+
args: [],
93+
syncInit: true
94+
})
95+
96+
# Only set Workspace Root once the first file of that file type is openend
97+
# otherwise often defaults to $HOME and scans too many files
98+
augroup LspSetup
99+
autocmd!
100+
augroup END
101+
for i in range(len(g:lspServers))
102+
for ft in g:lspServers[i].filetype
103+
# echoerr 'autocmd LspSetup FileType' ft 'call LspAddServer([g:lspServers[' .. i .. ']]) | autocmd! LspSetup FileType' ft
104+
exe 'autocmd LspSetup FileType' ft 'autocmd BufWinEnter <buffer> call LspAddServer([g:lspServers[' .. i .. ']]) | autocmd! LspSetup FileType' ft
105+
endfor
106+
endfor
93107
```
94108

95109
The above lines register the language servers for C/C++, Javascript/Typescript, Go and Rust file types. Refer to the [Wiki](https://github.com/yegappan/lsp/wiki) page for various language server specific configuration.
@@ -110,6 +124,7 @@ The `LspAddServer()` function accepts a list of LSP servers with the above infor
110124

111125
Some of the LSP plugin features can be enabled or disabled by using the `LspOptionsSet()` function, detailed in `:help lsp-options`.
112126
Here is an example of configuration with default values:
127+
113128
```viml
114129
call LspOptionsSet(#{
115130
\ aleSupport: v:false,
@@ -159,17 +174,25 @@ call LspOptionsSet(#{
159174
```
160175

161176
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:
177+
162178
```viml
163179
let lspOpts = #{autoHighlightDiags: v:true}
164180
autocmd User LspSetup call LspOptionsSet(lspOpts)
165181
166-
let lspServers = [#{
182+
let g:lspServers = [#{
167183
\ name: 'clang',
168184
\ filetype: ['c', 'cpp'],
169185
\ path: '/usr/local/bin/clangd',
170186
\ args: ['--background-index']
171187
\ }]
172-
autocmd User LspSetup call LspAddServer(lspServers)
188+
augroup LspSetup
189+
autocmd!
190+
augroup END
191+
for i in range(len(g:lspServers))
192+
for ft in g:lspServers[i].filetype
193+
exe 'autocmd LspSetup FileType' ft 'autocmd BufWinEnter <buffer> call LspAddServer([g:lspServers[' .. i .. ']]) | autocmd! LspSetup FileType' ft
194+
endfor
195+
endfor
173196
```
174197

175198
## Supported Commands

doc/lsp.txt

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,14 @@ LSP plugin, the steps are described later in this section: >
183183
args: ['--check-parent-process', '-v']
184184
}
185185
]
186-
LspAddServer(lspServers)
186+
augroup LspSetup
187+
autocmd!
188+
augroup END
189+
for i in range(len(g:lspServers))
190+
for ft in g:lspServers[i].filetype
191+
exe 'autocmd LspSetup FileType' ft 'autocmd BufWinEnter <buffer> call LspAddServer([g:lspServers[' .. i .. ']]) | autocmd! LspSetup FileType' ft
192+
endfor
193+
endfor
187194
<
188195
Depending on the location of the typescript and python pyls language servers
189196
installed in your system, update the "path" in the above snippet
@@ -237,7 +244,14 @@ Shell script, Vim script and PHP file types: >
237244
}
238245
}
239246
]
240-
LspAddServer(lspServers)
247+
augroup LspSetup
248+
autocmd!
249+
augroup END
250+
for i in range(len(g:lspServers))
251+
for ft in g:lspServers[i].filetype
252+
exe 'autocmd LspSetup FileType' ft 'autocmd BufWinEnter <buffer> call LspAddServer([g:lspServers[' .. i .. ']]) | autocmd! LspSetup FileType' ft
253+
endfor
254+
endfor
241255
<
242256
To add a language server, the following information is needed:
243257

@@ -437,7 +451,14 @@ language server and to set the language server options. For example: >
437451
args: ['--background-index']
438452
}
439453
]
440-
autocmd User LspSetup LspAddServer(lspServers)
454+
augroup LspSetup
455+
autocmd!
456+
augroup END
457+
for i in range(len(g:lspServers))
458+
for ft in g:lspServers[i].filetype
459+
exe 'autocmd LspSetup FileType' ft 'autocmd BufWinEnter <buffer> call LspAddServer([g:lspServers[' .. i .. ']]) | autocmd! LspSetup FileType' ft
460+
endfor
461+
endfor
441462
<
442463
*lsp-options* *LspOptionsSet()*
443464
*g:LspOptionsSet()*

0 commit comments

Comments
 (0)