README.md | 27 +++++++++++++++++++++++++-- autoload/lsp/handlers.vim | 12 ------------ autoload/lsp/lspserver.vim | 14 ++++++++++---- doc/lsp.txt | 41 +++++++++++++++++++++++++++++++---------- diff --git a/README.md b/README.md index 7fa2bc886235a699063ce86e01fb75e5969afde1..8a9446862f5a861ed24f560f15ec654c63d5686b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ![unit-tests](https://github.com/yegappan/mru/workflows/unit-tests/badge.svg?branch=master) -Language Server Protocol (LSP) plugin for Vim9. You need Vim version 9.0 or above to use this plugin. +Language Server Protocol (LSP) plugin for Vim. You need Vim version 9.0 or above to use this plugin. This plugin is written using only the Vim9 script. ## Installation @@ -23,7 +23,27 @@ You can also install and manage this plugin using any one of the Vim plugin managers (dein.vim, pathogen, vam, vim-plug, volt, Vundle, etc.). You will also need to install one or more language servers corresponding to the programming languages that you are using. Refer to the https://langserver.org/ page for the list of available language servers. +## Features + +The following language server protocol (LSP) features are supported: + +* Code completion +* Jump to definition, declaration, implementation, type definition +* Display warning and error diagnostics +* Find all symbol references +* Workspace symbol search +* Display code outline +* Rename symbol +* Display type and documentation on hover +* Inlay hints +* Code action +* Formatting code +* Folding code +* Visually select symbol block/region + ## Configuration + +To use the plugin features with a particular file type(s), you need to first register a LSP server for that file type(s). 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): ``` @@ -83,7 +103,7 @@ \ ] call LspAddServer(lspServers) ``` -The above lines add the LSP servers for C, C++, Javascript, Typescript, Shell script, Vim script, Go, Rust, Python and Fortran file types. +The above lines add the LSP servers for C, C++, Javascript, Typescript, Shell script, Vim script, Go, Rust, Python and Fortran file types. In addition to the above listed file types, this plugin also supports other file types. To add a LSP server, the following information is needed: @@ -96,6 +116,9 @@ The LSP servers are added using the LspAddServer() function. This function accepts a list of LSP servers with the above information. ## Supported Commands + +The following commands are provided to use the LSP features. + Command|Description -------|----------- :LspShowServers|Display the list of registered LSP servers diff --git a/autoload/lsp/handlers.vim b/autoload/lsp/handlers.vim index 85f41eec452b4cf279759d3518d1e65c2d286819..79efc0aeb4213eb8ef03aaa5c471ca98e173f943 100644 --- a/autoload/lsp/handlers.vim +++ b/autoload/lsp/handlers.vim @@ -12,7 +12,6 @@ import './textedit.vim' import './symbol.vim' import './codeaction.vim' import './callhierarchy.vim' as callhier -import './selection.vim' import './signature.vim' # process the 'initialize' method reply from the LSP server @@ -474,16 +473,6 @@ codeaction.ApplyCodeAction(lspserver, reply.result) enddef -# Reply: 'textDocument/selectionRange' -# Result: SelectionRange[] | null -def ProcessSelectionRangeReply(lspserver: dict, req: dict, reply: dict) - if reply.result->empty() - return - endif - - selection.SelectionStart(lspserver, reply.result) -enddef - # Reply: 'textDocument/foldingRange' # Result: FoldingRange[] | null def ProcessFoldingRangeReply(lspserver: dict, req: dict, reply: dict) @@ -650,7 +639,6 @@ 'textDocument/formatting': ProcessFormatReply, 'textDocument/rangeFormatting': ProcessFormatReply, 'textDocument/rename': ProcessRenameReply, 'textDocument/codeAction': ProcessCodeActionReply, - 'textDocument/selectionRange': ProcessSelectionRangeReply, 'textDocument/foldingRange': ProcessFoldingRangeReply, 'workspace/executeCommand': ProcessWorkspaceExecuteReply, 'workspace/symbol': ProcessWorkspaceSymbolReply, diff --git a/autoload/lsp/lspserver.vim b/autoload/lsp/lspserver.vim index aa5545900c9ce2dc1e0121914290b15913f0633c..dfa48860e6a5c68bc1a1b2d83c0c1425764ee324 100644 --- a/autoload/lsp/lspserver.vim +++ b/autoload/lsp/lspserver.vim @@ -920,13 +920,19 @@ # clear the previous selection reply lspserver.selection = {} - var req = lspserver.createRequest('textDocument/selectionRange') # interface SelectionRangeParams # interface TextDocumentIdentifier - req.params->extend({textDocument: {uri: util.LspFileToUri(fname)}, positions: [GetLspPosition()]}) - lspserver.sendMessage(req) + var param = {} + param.textDocument = {} + param.textDocument.uri = util.LspFileToUri(fname) + param.positions = [GetLspPosition()] + var resp = lspserver.rpc('textDocument/selectionRange', param) - lspserver.waitForResponse(req) + if resp->empty() || resp.result->empty() + return + endif + + selection.SelectionStart(lspserver, resp.result) enddef # Expand the previous selection or start a new one diff --git a/doc/lsp.txt b/doc/lsp.txt index 99cb875941f6293215f184741fb60f149ac10228..3d07de71ce5261fcb33d84ba76f72d17f1c4cabf 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -2,7 +2,7 @@ *lsp.txt* Language Server Protocol (LSP) Plugin for Vim9 Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com) For Vim version 9.0 and above -Last change: Sep 29, 2022 +Last change: Oct 7, 2022 ============================================================================== *lsp-license* @@ -292,6 +292,10 @@ is pushed onto the tag stack. The |CTRL-T| command can be used to go back up the tag stack. Also the |``| mark is set to the position before the jump. + You may want to map a key to invoke this command: > + + nnoremap gd LspGotoDefinition +< *:LspGotoDeclaration* :LspGotoDeclaration Jumps to the declaration of the symbol under the cursor. The behavior of this command is similar to the @@ -303,12 +307,20 @@ cursor. The behavior of this command is similar to the |:LspGotoDefinition| command. Note that not all the LSP servers support this feature. + You may want to map a key to invoke this command: > + + nnoremap gt LspGotoTypeDef +< *:LspGotoImpl* :LspGotoImpl Jumps to the implementation of the symbol under the cursor. The behavior of this command is similar to the |:LspGotoDefinition| command. Note that not all the LSP servers support this feature. + You may want to map a key to invoke this command: > + + nnoremap gi LspGotoImpl +< *:LspPeekDefinition* :LspPeekDefinition Displays the line where the symbol under the cursor is defined in the |preview-window|. The symbol is @@ -502,24 +514,33 @@ < Default is false *:LspSelectionExpand* -:LspSelectionExpand Expand the current symbol range visual selection. It - is useful to create a visual map to use this command. - Example: > +:LspSelectionExpand Visually select the region of the symbol under the + cursor. In visual mode, expands the current symbol + visual region selection to include the next level. - xnoremap le LspSelectionExpand + For example, if the cursor is on a "for" statement, + this command selects the "for" statement and the body + of the "for" statement. + + It is useful to create a visual map to use this + command. Example: > + + xnoremap e LspSelectionExpand < - With the above map, you can press "le" in visual mode - successively to expand the current visual region. + With the above map, you can press "\e" in visual mode + successively to expand the current symbol visual + region. *:LspSelectionShrink* :LspSelectionShrink Shrink the current symbol range visual selection. It is useful to create a visual map to use this command. Example: > - xnoremap ls LspSelectionShrink + xnoremap s LspSelectionShrink < - With the above map, you can press "ls" in visual mode - successively to shrink the current visual region. + With the above map, you can press "\s" in visual mode + successively to shrink the current symbol visual + region. *:LspFold* :LspFold Fold the current file.