]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Add count to Lsp{Goto,Peek}-commands
authorAndreas Louv <andreas@louv.dk>
Sun, 16 Apr 2023 21:50:17 +0000 (23:50 +0200)
committerAndreas Louv <andreas@louv.dk>
Sun, 16 Apr 2023 22:10:38 +0000 (00:10 +0200)
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.

autoload/lsp/lsp.vim
autoload/lsp/lspserver.vim
doc/lsp.txt
plugin/lsp.vim

index 83ee3900a0f9c2ac719e238d8cc345fb3ef8e7ca..7f715fab7c005aaefae9261958532c8b4b01279b 100644 (file)
@@ -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<any> = 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<any> = 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<any> = 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<any> = 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
index f37b8b5fede078aacbb7beda9df165905bb51cd4..27168fb323425fe485bb38e2b39c60ada14f0ec9 100644 (file)
@@ -663,7 +663,7 @@ enddef
 #
 # Result: Location | Location[] | LocationLink[] | null
 def GotoSymbolLoc(lspserver: dict<any>, 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<any>, msg: string, peekSymbol: bool,
 
   var location: dict<any>
   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<any>, peek: bool, cmdmods: string)
+def GotoDefinition(lspserver: dict<any>, 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<any>, 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<any>, peek: bool, cmdmods: string)
+def GotoDeclaration(lspserver: dict<any>, 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<any>, 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<any>, peek: bool, cmdmods: string)
+def GotoTypeDef(lspserver: dict<any>, 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<any>, 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<any>, peek: bool, cmdmods: string)
+def GotoImplementation(lspserver: dict<any>, 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<any>, 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"
index 0e47cf21bab19ad5c3cf4e58ebadf06507c57b53..bce3d0f218d6acff7b58664b3f926334728b813e 100644 (file)
@@ -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 <buffer> <C-W>gd <Cmd>topleft LspGotoDefinition<CR>
 <
                                                *: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 <buffer> gi <Cmd>LspGotoImpl<CR>
 <
                                                *: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
                        <Esc> 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
index 1e282da4a240efdaca9175b208042a028fddb142..1dbc26dce08cf24821ad3161aec3994b88ec25b7 100644 (file)
@@ -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(<range>, <line1>, <line2>)
-command! -nargs=0 -bar LspGotoDeclaration lsp.GotoDeclaration(v:false, <q-mods>)
-command! -nargs=0 -bar LspGotoDefinition lsp.GotoDefinition(v:false, <q-mods>)
-command! -nargs=0 -bar LspGotoImpl lsp.GotoImplementation(v:false, <q-mods>)
-command! -nargs=0 -bar LspGotoTypeDef lsp.GotoTypedef(v:false, <q-mods>)
+command! -nargs=0 -bar -count LspGotoDeclaration lsp.GotoDeclaration(v:false, <q-mods>, <count>)
+command! -nargs=0 -bar -count LspGotoDefinition lsp.GotoDefinition(v:false, <q-mods>, <count>)
+command! -nargs=0 -bar -count LspGotoImpl lsp.GotoImplementation(v:false, <q-mods>, <count>)
+command! -nargs=0 -bar -count LspGotoTypeDef lsp.GotoTypedef(v:false, <q-mods>, <count>)
 command! -nargs=0 -bar LspHighlight call LspDocHighlight(<q-mods>)
 command! -nargs=0 -bar LspHighlightClear call LspDocHighlightClear()
 command! -nargs=0 -bar LspHover lsp.Hover(<q-mods>)
 command! -nargs=0 -bar LspIncomingCalls lsp.IncomingCalls()
 command! -nargs=0 -bar LspOutgoingCalls lsp.OutgoingCalls()
 command! -nargs=0 -bar -count LspOutline lsp.Outline(<q-mods>, <count>)
-command! -nargs=0 -bar LspPeekDeclaration lsp.GotoDeclaration(v:true, <q-mods>)
-command! -nargs=0 -bar LspPeekDefinition lsp.GotoDefinition(v:true, <q-mods>)
-command! -nargs=0 -bar LspPeekImpl lsp.GotoImplementation(v:true, <q-mods>)
+command! -nargs=0 -bar -count LspPeekDeclaration lsp.GotoDeclaration(v:true, <q-mods>, <count>)
+command! -nargs=0 -bar -count LspPeekDefinition lsp.GotoDefinition(v:true, <q-mods>, <count>)
+command! -nargs=0 -bar -count LspPeekImpl lsp.GotoImplementation(v:true, <q-mods>, <count>)
 command! -nargs=0 -bar LspPeekReferences lsp.ShowReferences(v:true)
-command! -nargs=0 -bar LspPeekTypeDef lsp.GotoTypedef(v:true, <q-mods>)
+command! -nargs=0 -bar -count LspPeekTypeDef lsp.GotoTypedef(v:true, <q-mods>, <count>)
 command! -nargs=? -bar LspRename lsp.Rename(<q-args>)
 command! -nargs=0 -bar LspSelectionExpand lsp.SelectionExpand()
 command! -nargs=0 -bar LspSelectionShrink lsp.SelectionShrink()