]> Sergey Matveev's repositories - vim-lsp.git/blob - plugin/lsp.vim
36b7ef4a927b71293b02fee4f71158c9ebe87721
[vim-lsp.git] / plugin / lsp.vim
1 if !has('vim9script') ||  v:version < 900
2   " Needs Vim version 9.0 and above
3   finish
4 endif
5
6 vim9script
7
8 # Language Server Protocol (LSP) plugin for vim
9
10 g:loaded_lsp = true
11
12 import '../autoload/lsp/options.vim'
13 import autoload '../autoload/lsp/lsp.vim'
14
15 # Set LSP plugin options from 'opts'.
16 def g:LspOptionsSet(opts: dict<any>)
17   options.OptionsSet(opts)
18 enddef
19
20 # Return a copy of all the LSP plugin options
21 def g:LspOptionsGet(): dict<any>
22   return options.OptionsGet()
23 enddef
24
25 # Add one or more LSP servers in 'serverList'
26 def g:LspAddServer(serverList: list<dict<any>>)
27   lsp.AddServer(serverList)
28 enddef
29
30 # Register 'Handler' callback function for LSP command 'cmd'.
31 def g:LspRegisterCmdHandler(cmd: string, Handler: func)
32   lsp.RegisterCmdHandler(cmd, Handler)
33 enddef
34
35 # Returns true if the language server for the current buffer is initialized
36 # and ready to accept requests.
37 def g:LspServerReady(): bool
38   return lsp.ServerReady()
39 enddef
40
41 # Returns true if the language server for 'ftype' file type is running
42 def g:LspServerRunning(ftype: string): bool
43   return lsp.ServerRunning(ftype)
44 enddef
45
46 augroup LSPAutoCmds
47   au!
48   autocmd BufNewFile,BufReadPost,FileType * lsp.AddFile(expand('<abuf>')->str2nr())
49   # Note that when BufWipeOut is invoked, the current buffer may be different
50   # from the buffer getting wiped out.
51   autocmd BufWipeOut * lsp.RemoveFile(expand('<abuf>')->str2nr())
52   autocmd BufWinEnter * lsp.BufferLoadedInWin(expand('<abuf>')->str2nr())
53 augroup END
54
55 # TODO: Is it needed to shutdown all the LSP servers when exiting Vim?
56 # This takes some time.
57 # autocmd VimLeavePre * call lsp.StopAllServers()
58
59 # LSP commands
60 command! -nargs=? -bar -range LspCodeAction lsp.CodeAction(<line1>, <line2>, <q-args>)
61 command! -nargs=0 -bar LspCodeLens lsp.CodeLens()
62 command! -nargs=+ -bar -bang -count -complete=customlist,lsp.LspDiagComplete LspDiag lsp.LspDiagCmd(<q-args>, <count>, <bang>false)
63 command! -nargs=0 -bar -bang LspDiagCurrent lsp.LspShowCurrentDiag(<bang>false)
64 command! -nargs=0 -bar LspDiagFirst lsp.JumpToDiag('first')
65 command! -nargs=0 -bar LspDiagLast lsp.JumpToDiag('last')
66 command! -nargs=0 -bar -count=1 LspDiagNext lsp.JumpToDiag('next', <count>)
67 command! -nargs=0 -bar -count=1 LspDiagPrev lsp.JumpToDiag('prev', <count>)
68 command! -nargs=0 -bar LspDiagShow lsp.ShowDiagnostics()
69 command! -nargs=0 -bar LspDiagHere lsp.JumpToDiag('here')
70 command! -nargs=0 -bar LspDocumentSymbol lsp.ShowDocSymbols()
71 command! -nargs=0 -bar LspFold lsp.FoldDocument()
72 command! -nargs=0 -bar -range=% LspFormat lsp.TextDocFormat(<range>, <line1>, <line2>)
73 command! -nargs=0 -bar -count LspGotoDeclaration lsp.GotoDeclaration(v:false, <q-mods>, <count>)
74 command! -nargs=0 -bar -count LspGotoDefinition lsp.GotoDefinition(v:false, <q-mods>, <count>)
75 command! -nargs=0 -bar -count LspGotoImpl lsp.GotoImplementation(v:false, <q-mods>, <count>)
76 command! -nargs=0 -bar -count LspGotoTypeDef lsp.GotoTypedef(v:false, <q-mods>, <count>)
77 command! -nargs=0 -bar LspHighlight call LspDocHighlight(<q-mods>)
78 command! -nargs=0 -bar LspHighlightClear call LspDocHighlightClear()
79 command! -nargs=? -bar LspHover lsp.Hover(<q-mods>)
80 command! -nargs=1 -bar -complete=customlist,lsp.LspInlayHintsComplete LspInlayHints lsp.InlayHints(<q-args>)
81 command! -nargs=0 -bar LspIncomingCalls lsp.IncomingCalls()
82 command! -nargs=0 -bar LspOutgoingCalls lsp.OutgoingCalls()
83 command! -nargs=0 -bar -count LspOutline lsp.Outline(<q-mods>, <count>)
84 command! -nargs=0 -bar -count LspPeekDeclaration lsp.GotoDeclaration(v:true, <q-mods>, <count>)
85 command! -nargs=0 -bar -count LspPeekDefinition lsp.GotoDefinition(v:true, <q-mods>, <count>)
86 command! -nargs=0 -bar -count LspPeekImpl lsp.GotoImplementation(v:true, <q-mods>, <count>)
87 command! -nargs=0 -bar LspPeekReferences lsp.ShowReferences(v:true)
88 command! -nargs=0 -bar -count LspPeekTypeDef lsp.GotoTypedef(v:true, <q-mods>, <count>)
89 command! -nargs=? -bar LspRename lsp.Rename(<q-args>)
90 command! -nargs=0 -bar LspSelectionExpand lsp.SelectionExpand()
91 command! -nargs=0 -bar LspSelectionShrink lsp.SelectionShrink()
92 command! -nargs=+ -bar -complete=customlist,lsp.LspServerComplete LspServer lsp.LspServerCmd(<q-args>)
93 command! -nargs=0 -bar LspShowReferences lsp.ShowReferences(v:false)
94 command! -nargs=0 -bar LspShowAllServers lsp.ShowAllServers()
95 command! -nargs=0 -bar LspShowSignature call LspShowSignature()
96 command! -nargs=0 -bar LspSubTypeHierarchy lsp.TypeHierarchy(0)
97 command! -nargs=0 -bar LspSuperTypeHierarchy lsp.TypeHierarchy(1)
98 # Clangd specifc extension to switch from one C/C++ source file to a
99 # corresponding header file
100 command! -nargs=0 -bar LspSwitchSourceHeader lsp.SwitchSourceHeader()
101 command! -nargs=? -bar LspSymbolSearch lsp.SymbolSearch(<q-args>, <q-mods>)
102 command! -nargs=1 -bar -complete=dir LspWorkspaceAddFolder lsp.AddWorkspaceFolder(<q-args>)
103 command! -nargs=0 -bar LspWorkspaceListFolders lsp.ListWorkspaceFolders()
104 command! -nargs=1 -bar -complete=dir LspWorkspaceRemoveFolder lsp.RemoveWorkspaceFolder(<q-args>)
105
106 # Add the GUI menu entries
107 if has('gui_running')
108   anoremenu <silent> L&sp.Goto.Definition :LspGotoDefinition<CR>
109   anoremenu <silent> L&sp.Goto.Declaration :LspGotoDeclaration<CR>
110   anoremenu <silent> L&sp.Goto.Implementation :LspGotoImpl<CR>
111   anoremenu <silent> L&sp.Goto.TypeDef :LspGotoTypeDef<CR>
112
113   anoremenu <silent> L&sp.Show\ Signature :LspShowSignature<CR>
114   anoremenu <silent> L&sp.Show\ References :LspShowReferences<CR>
115   anoremenu <silent> L&sp.Show\ Detail :LspHover<CR>
116   anoremenu <silent> L&sp.Outline :LspOutline<CR>
117
118   anoremenu <silent> L&sp.Goto\ Symbol :LspDocumentSymbol<CR>
119   anoremenu <silent> L&sp.Symbol\ Search :LspSymbolSearch<CR>
120   anoremenu <silent> L&sp.Outgoing\ Calls :LspOutgoingCalls<CR>
121   anoremenu <silent> L&sp.Incoming\ Calls :LspIncomingCalls<CR>
122   anoremenu <silent> L&sp.Rename :LspRename<CR>
123   anoremenu <silent> L&sp.Code\ Action :LspCodeAction<CR>
124
125   anoremenu <silent> L&sp.Highlight\ Symbol :LspHighlight<CR>
126   anoremenu <silent> L&sp.Highlight\ Clear :LspHighlightClear<CR>
127
128   # Diagnostics
129   anoremenu <silent> L&sp.Diagnostics.Current :LspDiag current<CR>
130   anoremenu <silent> L&sp.Diagnostics.Show\ All :LspDiag show<CR>
131   anoremenu <silent> L&sp.Diagnostics.First :LspDiag first<CR>
132   anoremenu <silent> L&sp.Diagnostics.Last :LspDiag last<CR>
133   anoremenu <silent> L&sp.Diagnostics.Next :LspDiag next<CR>
134   anoremenu <silent> L&sp.Diagnostics.Previous :LspDiag prev<CR>
135   anoremenu <silent> L&sp.Diagnostics.This :LspDiag here<CR>
136
137   if &mousemodel =~ 'popup'
138     anoremenu <silent> PopUp.L&sp.Go\ to\ Definition
139           \ :LspGotoDefinition<CR>
140     anoremenu <silent> PopUp.L&sp.Go\ to\ Declaration
141           \ :LspGotoDeclaration<CR>
142     anoremenu <silent> PopUp.L&sp.Find\ All\ References
143           \ :LspShowReferences<CR>
144     anoremenu <silent> PopUp.L&sp.Show\ Detail
145           \ :LspHover<CR>
146     anoremenu <silent> PopUp.L&sp.Highlight\ Symbol
147           \ :LspHighlight<CR>
148     anoremenu <silent> PopUp.L&sp.Highlight\ Clear
149           \ :LspHighlightClear<CR>
150   endif
151 endif
152
153 # vim: shiftwidth=2 softtabstop=2