From acc5b101d68d97630f492df42a4a5ca0ecd6908f Mon Sep 17 00:00:00 2001 From: Yegappan Lakshmanan Date: Sat, 8 Oct 2022 08:25:31 -0700 Subject: [PATCH] Use sync RPC for text selection. Update the help text. --- README.md | 27 +++++++++++++++++++++++-- autoload/lsp/handlers.vim | 12 ----------- autoload/lsp/lspserver.vim | 14 +++++++++---- doc/lsp.txt | 41 ++++++++++++++++++++++++++++---------- 4 files changed, 66 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 7fa2bc8..8a94468 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,8 +23,28 @@ You can also install and manage this plugin using any one of the Vim plugin mana 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): ``` let lspServers = [ @@ -83,7 +103,7 @@ To register a LSP server, add the following lines to your .vimrc file (use only 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 @@ args|a list of command-line arguments passed to the LSP server. Each argument is 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 85f41ee..79efc0a 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 @@ def ProcessCodeActionReply(lspserver: dict, req: dict, reply: 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 @@ export def ProcessReply(lspserver: dict, req: dict, reply: dict): '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 aa55459..dfa4886 100644 --- a/autoload/lsp/lspserver.vim +++ b/autoload/lsp/lspserver.vim @@ -920,13 +920,19 @@ def SelectionRange(lspserver: dict, fname: string) # 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 99cb875..3d07de7 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -2,7 +2,7 @@ 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 @@ diagnostic messages, you can add the following line to your .vimrc file: 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 @@ diagnostic messages, you can add the following line to your .vimrc file: |: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 @@ diagnostic messages, you can add the following line to your .vimrc file: 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. + + 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 le LspSelectionExpand + 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. -- 2.48.1