From: Andreas Louv Date: Sun, 16 Apr 2023 21:50:17 +0000 (+0200) Subject: Add count to Lsp{Goto,Peek}-commands X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=c788f03691e519b626cadf469641ddf6cec22a06;p=vim-lsp.git Add count to Lsp{Goto,Peek}-commands By providing a count the commands works as if there was only one found location, instead of how currently a location-list and popup is created for multiple results. --- diff --git a/autoload/lsp/lsp.vim b/autoload/lsp/lsp.vim index 83ee390..7f715fa 100644 --- a/autoload/lsp/lsp.vim +++ b/autoload/lsp/lsp.vim @@ -254,43 +254,43 @@ export def ServerRunning(ftype: string): bool enddef # Go to a definition using "textDocument/definition" LSP request -export def GotoDefinition(peek: bool, cmdmods: string) +export def GotoDefinition(peek: bool, cmdmods: string, count: number) var lspserver: dict = buf.CurbufGetServerChecked('definition') if lspserver->empty() return endif - lspserver.gotoDefinition(peek, cmdmods) + lspserver.gotoDefinition(peek, cmdmods, count) enddef # Go to a declaration using "textDocument/declaration" LSP request -export def GotoDeclaration(peek: bool, cmdmods: string) +export def GotoDeclaration(peek: bool, cmdmods: string, count: number) var lspserver: dict = buf.CurbufGetServerChecked('declaration') if lspserver->empty() return endif - lspserver.gotoDeclaration(peek, cmdmods) + lspserver.gotoDeclaration(peek, cmdmods, count) enddef # Go to a type definition using "textDocument/typeDefinition" LSP request -export def GotoTypedef(peek: bool, cmdmods: string) +export def GotoTypedef(peek: bool, cmdmods: string, count: number) var lspserver: dict = buf.CurbufGetServerChecked('typeDefinition') if lspserver->empty() return endif - lspserver.gotoTypeDef(peek, cmdmods) + lspserver.gotoTypeDef(peek, cmdmods, count) enddef # Go to a implementation using "textDocument/implementation" LSP request -export def GotoImplementation(peek: bool, cmdmods: string) +export def GotoImplementation(peek: bool, cmdmods: string, count: number) var lspserver: dict = buf.CurbufGetServerChecked('implementation') if lspserver->empty() return endif - lspserver.gotoImplementation(peek, cmdmods) + lspserver.gotoImplementation(peek, cmdmods, count) enddef # Switch source header using "textDocument/switchSourceHeader" LSP request diff --git a/autoload/lsp/lspserver.vim b/autoload/lsp/lspserver.vim index f37b8b5..27168fb 100644 --- a/autoload/lsp/lspserver.vim +++ b/autoload/lsp/lspserver.vim @@ -663,7 +663,7 @@ enddef # # Result: Location | Location[] | LocationLink[] | null def GotoSymbolLoc(lspserver: dict, msg: string, peekSymbol: bool, - cmdmods: string) + cmdmods: string, count: number) var reply = lspserver.rpc(msg, GetLspTextDocPosition(true), false) if reply->empty() || reply.result->empty() var emsg: string @@ -683,26 +683,32 @@ def GotoSymbolLoc(lspserver: dict, msg: string, peekSymbol: bool, var location: dict if reply.result->type() == v:t_list - # When there are multiple symbol locations, display the locations in a - # location list. - if reply.result->len() > 1 - var title: string = '' - if msg ==# 'textDocument/declaration' - title = 'Declarations' - elseif msg ==# 'textDocument/typeDefinition' - title = 'Type Definitions' - elseif msg ==# 'textDocument/implementation' - title = 'Implementations' - else - title = 'Definitions' + if count == 0 + # When there are multiple symbol locations, and a specific one isn't + # requested with 'count', display the locations in a location list. + if reply.result->len() > 1 + var title: string = '' + if msg ==# 'textDocument/declaration' + title = 'Declarations' + elseif msg ==# 'textDocument/typeDefinition' + title = 'Type Definitions' + elseif msg ==# 'textDocument/implementation' + title = 'Implementations' + else + title = 'Definitions' + endif + + symbol.ShowLocations(lspserver, reply.result, peekSymbol, title) + return endif - - symbol.ShowLocations(lspserver, reply.result, peekSymbol, title) - return endif - # Only one location - location = reply.result[0] + # Select the location requsted in 'count' + var idx = count - 1 + if idx >= reply.result->len() + idx = reply.result->len() - 1 + endif + location = reply.result[idx] else location = reply.result endif @@ -712,7 +718,7 @@ enddef # Request: "textDocument/definition" # Param: DefinitionParams -def GotoDefinition(lspserver: dict, peek: bool, cmdmods: string) +def GotoDefinition(lspserver: dict, peek: bool, cmdmods: string, count: number) # Check whether LSP server supports jumping to a definition if !lspserver.isDefinitionProvider util.ErrMsg('Jumping to a symbol definition is not supported') @@ -721,12 +727,12 @@ def GotoDefinition(lspserver: dict, peek: bool, cmdmods: string) # interface DefinitionParams # interface TextDocumentPositionParams - GotoSymbolLoc(lspserver, 'textDocument/definition', peek, cmdmods) + GotoSymbolLoc(lspserver, 'textDocument/definition', peek, cmdmods, count) enddef # Request: "textDocument/declaration" # Param: DeclarationParams -def GotoDeclaration(lspserver: dict, peek: bool, cmdmods: string) +def GotoDeclaration(lspserver: dict, peek: bool, cmdmods: string, count: number) # Check whether LSP server supports jumping to a declaration if !lspserver.isDeclarationProvider util.ErrMsg('Jumping to a symbol declaration is not supported') @@ -735,12 +741,12 @@ def GotoDeclaration(lspserver: dict, peek: bool, cmdmods: string) # interface DeclarationParams # interface TextDocumentPositionParams - GotoSymbolLoc(lspserver, 'textDocument/declaration', peek, cmdmods) + GotoSymbolLoc(lspserver, 'textDocument/declaration', peek, cmdmods, count) enddef # Request: "textDocument/typeDefinition" # Param: TypeDefinitionParams -def GotoTypeDef(lspserver: dict, peek: bool, cmdmods: string) +def GotoTypeDef(lspserver: dict, peek: bool, cmdmods: string, count: number) # Check whether LSP server supports jumping to a type definition if !lspserver.isTypeDefinitionProvider util.ErrMsg('Jumping to a symbol type definition is not supported') @@ -749,12 +755,12 @@ def GotoTypeDef(lspserver: dict, peek: bool, cmdmods: string) # interface TypeDefinitionParams # interface TextDocumentPositionParams - GotoSymbolLoc(lspserver, 'textDocument/typeDefinition', peek, cmdmods) + GotoSymbolLoc(lspserver, 'textDocument/typeDefinition', peek, cmdmods, count) enddef # Request: "textDocument/implementation" # Param: ImplementationParams -def GotoImplementation(lspserver: dict, peek: bool, cmdmods: string) +def GotoImplementation(lspserver: dict, peek: bool, cmdmods: string, count: number) # Check whether LSP server supports jumping to a implementation if !lspserver.isImplementationProvider util.ErrMsg('Jumping to a symbol implementation is not supported') @@ -763,7 +769,7 @@ def GotoImplementation(lspserver: dict, peek: bool, cmdmods: string) # interface ImplementationParams # interface TextDocumentPositionParams - GotoSymbolLoc(lspserver, 'textDocument/implementation', peek, cmdmods) + GotoSymbolLoc(lspserver, 'textDocument/implementation', peek, cmdmods, count) enddef # Request: "textDocument/switchSourceHeader" diff --git a/doc/lsp.txt b/doc/lsp.txt index 0e47cf2..bce3d0f 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -603,19 +603,20 @@ can map these commands to keys and make it easier to invoke them. file using the language server. *:LspGotoDeclaration* -:LspGotoDeclaration Jumps to the declaration of the symbol under the +:[count]LspGotoDeclaration + Jumps to the declaration of the symbol under the cursor. The behavior of this command is similar to the |:LspGotoDefinition| command. *:LspGotoDefinition* -:LspGotoDefinition Jumps to the definition of the symbol under the - cursor. +:[count]LspGotoDefinition + Jumps to the [count] definition of the symbol under the + cursor. If there are multiple matches and [count] isn't + specified, then a location list will be created with the + list of locations. - If there are multiple matches, then a location list will - be created with the list of locations. - - If there is only one location then the following will - apply: + If there is only one location, or [count] is provided + then the following will apply: If the file is already present in a window, then jumps to that window. Otherwise, opens the file in a new @@ -645,7 +646,7 @@ can map these commands to keys and make it easier to invoke them. nnoremap gd topleft LspGotoDefinition < *:LspGotoImpl* -:LspGotoImpl Jumps to the implementation of the symbol under the +:[count]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 language servers support this feature. @@ -655,7 +656,7 @@ can map these commands to keys and make it easier to invoke them. nnoremap gi LspGotoImpl < *:LspGotoTypeDef* -:LspGotoTypeDef Jumps to the type definition of the symbol under the +:[count]LspGotoTypeDef Jumps to the type definition of the symbol under the cursor. The behavior of this command is similar to the |:LspGotoDefinition| command. Note that not all the language servers support this feature. @@ -734,19 +735,28 @@ can map these commands to keys and make it easier to invoke them. :vert aboveleft 50LspOutline < *:LspPeekDeclaration* -:LspPeekDeclaration Displays the line where the symbol under the +:[count]LspPeekDeclaration + Displays the line where the symbol under the cursor is declared in a popup window. The behavior of this command is similar to the |:LspPeekDefinition| command. *:LspPeekDefinition* -:LspPeekDefinition Displays the line where the symbol under the cursor is +:[count]LspPeekDefinition + Displays the line where the symbol under the cursor is defined in a popup window. The symbol is highlighted in the popup window. Moving the cursor or pressing will close the popup window. + When more than one symbol is found all of them will be + shown. The corresponding file for the symbol is + displayed in another popup window. As the selection in + the symbol popup menu changes, the file in the popup is + updated. + When [count] is provided only the [count] symbol will be + shown. *:LspPeekImpl* -:LspPeekImpl Displays the implementation of the symbol under the +:[count]LspPeekImpl Displays the implementation of the symbol under the cursor in a popup window. The behavior of this command is similar to the |:LspPeekDefinition| command. Note that not all the language servers @@ -760,7 +770,7 @@ can map these commands to keys and make it easier to invoke them. the file in the popup is updated. *:LspPeekTypeDef* -:LspPeekTypeDef Displays the line where the type of the symbol under +:[count]LspPeekTypeDef Displays the line where the type of the symbol under the cursor is defined in a popup window. The behavior of this command is similar to the |:LspPeekDefinition| command. Note that not all the diff --git a/plugin/lsp.vim b/plugin/lsp.vim index 1e282da..1dbc26d 100644 --- a/plugin/lsp.vim +++ b/plugin/lsp.vim @@ -69,21 +69,21 @@ command! -nargs=0 -bar LspDiagShow lsp.ShowDiagnostics() command! -nargs=0 -bar LspDiagHere lsp.JumpToDiag('here') command! -nargs=0 -bar LspFold lsp.FoldDocument() command! -nargs=0 -bar -range=% LspFormat lsp.TextDocFormat(, , ) -command! -nargs=0 -bar LspGotoDeclaration lsp.GotoDeclaration(v:false, ) -command! -nargs=0 -bar LspGotoDefinition lsp.GotoDefinition(v:false, ) -command! -nargs=0 -bar LspGotoImpl lsp.GotoImplementation(v:false, ) -command! -nargs=0 -bar LspGotoTypeDef lsp.GotoTypedef(v:false, ) +command! -nargs=0 -bar -count LspGotoDeclaration lsp.GotoDeclaration(v:false, , ) +command! -nargs=0 -bar -count LspGotoDefinition lsp.GotoDefinition(v:false, , ) +command! -nargs=0 -bar -count LspGotoImpl lsp.GotoImplementation(v:false, , ) +command! -nargs=0 -bar -count LspGotoTypeDef lsp.GotoTypedef(v:false, , ) command! -nargs=0 -bar LspHighlight call LspDocHighlight() command! -nargs=0 -bar LspHighlightClear call LspDocHighlightClear() command! -nargs=0 -bar LspHover lsp.Hover() command! -nargs=0 -bar LspIncomingCalls lsp.IncomingCalls() command! -nargs=0 -bar LspOutgoingCalls lsp.OutgoingCalls() command! -nargs=0 -bar -count LspOutline lsp.Outline(, ) -command! -nargs=0 -bar LspPeekDeclaration lsp.GotoDeclaration(v:true, ) -command! -nargs=0 -bar LspPeekDefinition lsp.GotoDefinition(v:true, ) -command! -nargs=0 -bar LspPeekImpl lsp.GotoImplementation(v:true, ) +command! -nargs=0 -bar -count LspPeekDeclaration lsp.GotoDeclaration(v:true, , ) +command! -nargs=0 -bar -count LspPeekDefinition lsp.GotoDefinition(v:true, , ) +command! -nargs=0 -bar -count LspPeekImpl lsp.GotoImplementation(v:true, , ) command! -nargs=0 -bar LspPeekReferences lsp.ShowReferences(v:true) -command! -nargs=0 -bar LspPeekTypeDef lsp.GotoTypedef(v:true, ) +command! -nargs=0 -bar -count LspPeekTypeDef lsp.GotoTypedef(v:true, , ) command! -nargs=? -bar LspRename lsp.Rename() command! -nargs=0 -bar LspSelectionExpand lsp.SelectionExpand() command! -nargs=0 -bar LspSelectionShrink lsp.SelectionShrink()