diff --git a/dot-config/vim/init.vim b/dot-config/vim/init.vim index e625c12..1362504 100644 --- a/dot-config/vim/init.vim +++ b/dot-config/vim/init.vim @@ -131,13 +131,3 @@ statusline.Init() # necessary. import "./lsp.vim" lsp.Configure() - -set formatexpr=lsp#lsp#FormatExpr() -set keywordprg=:LspHover -nnoremap gD LspGotoDeclaration -nnoremap gd LspGotoDefinition -nnoremap gi LspGotoImpl -nnoremap LspShowSignature -nnoremap gr LspShowReferences -xnoremap e LspSelectionExpand -xnoremap s LspSelectionShrink diff --git a/dot-config/vim/lsp.vim b/dot-config/vim/lsp.vim index 447c1e1..bf07868 100644 --- a/dot-config/vim/lsp.vim +++ b/dot-config/vim/lsp.vim @@ -162,7 +162,47 @@ def ListMissingServers(argLead: string, cmdLine: string, cursorPos: number): lis return MissingServers()->mapnew((_, server): string => server.name) enddef +def ServerHas(feature: string): bool + return lsp#buffer#CurbufGetServer(feature) != {} +enddef + +def LspBufferSettings(): void + if ServerHas('documentFormatting') + setlocal formatexpr=lsp#lsp#FormatExpr() + endif + + if ServerHas('hover') + setlocal keywordprg=:LspHover + endif + + if ServerHas('declaration') + nnoremap gD LspGotoDeclaration + endif + + if ServerHas('definition') + nnoremap gd LspGotoDefinition + endif + + if ServerHas('implementation') + nnoremap gi LspGotoImpl + endif + + if ServerHas('references') + nnoremap gr LspShowReferences + endif + + if ServerHas('selectionRange') + xnoremap e LspSelectionExpand + xnoremap s LspSelectionShrink + endif +enddef + export def Configure(): void + augroup dot/vim/lsp.vim + autocmd! + autocmd User LspAttached LspBufferSettings() + augroup END + # We have to use final rather than const because LspAddServer() assumes it can # modify the dicts it gets, rather than making a fresh copy to mess with. final installedServers = InstalledServers() @@ -173,7 +213,6 @@ export def Configure(): void const missingCount = len(lspServers) - len(installedServers) const warn = $'{missingCount} language server{missingCount > 1 ? "s are" : " is"} configured, but not installed. You may want to run :LspInstall.' augroup dot/vim/lsp.vim - autocmd! exe $'autocmd VimEnter * ++once echo {strings.Quote(warn)}' augroup END endif