]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Handle LocationLink type properly and update the test for :LspPeekReferences
authorYegappan Lakshmanan <yegappan@yahoo.com>
Sun, 12 Mar 2023 19:36:18 +0000 (12:36 -0700)
committerYegappan Lakshmanan <yegappan@yahoo.com>
Sun, 12 Mar 2023 19:36:18 +0000 (12:36 -0700)
autoload/lsp/lspserver.vim
autoload/lsp/symbol.vim
autoload/lsp/util.vim
test/unit_tests.vim

index 7d9fc77ead282a37316f391465215ab3227c6103..f0397c8dfd90321090bfdf35e9ebb855388df9fa 100644 (file)
@@ -890,11 +890,6 @@ def GotoSymbolLoc(lspserver: dict<any>, msg: string, peekSymbol: bool,
     location = reply.result
   endif
 
-  # Convert to 'LocationLink'
-  if !location->has_key('targetUri')
-    location = util.LspLocationToLocationLink(location)
-  endif
-
   symbol.GotoSymbol(lspserver, location, peekSymbol, cmdmods)
 enddef
 
@@ -1595,14 +1590,6 @@ def TagFunc(lspserver: dict<any>, pat: string, flags: string, info: dict<any>):
     taglocations = [reply.result]
   endif
 
-  taglocations = taglocations->map((key, location) => {
-    # Already a 'LocationLink'
-    if location->has_key('targetUri')
-      return location
-    endif
-    return util.LspLocationToLocationLink(location)
-  })
-
   return symbol.TagFunc(lspserver, taglocations, pat)
 enddef
 
index 89e498f21a3d82c4dad35bdf7c98dc90a493290a..5dcf66c1f13e5d14a8b56483a5c0da88f1e7a716 100644 (file)
@@ -259,6 +259,7 @@ def UpdatePeekFilePopup(lspserver: dict<any>, refs: list<dict<any>>)
 
   lspserver.peekSymbolFilePopup = popup_create(bnr, popupAttrs)
   var cmds =<< trim eval END
+    setlocal number
     [{refs[n].range.start.line + 1}, 1]->cursor()
     normal! z.
   END
@@ -397,7 +398,8 @@ enddef
 # Display the file specified by LSP 'LocationLink' in a popup window and
 # highlight the range in 'location'.
 def PeekSymbolLocation(lspserver: dict<any>, location: dict<any>)
-  var fname = util.LspUriToFile(location.targetUri)
+  var [uri, range] = util.LspLocationParse(location)
+  var fname = util.LspUriToFile(uri)
   var bnum = fname->bufadd()
   if bnum == 0
     # Failed to create or find a buffer
@@ -431,13 +433,13 @@ def PeekSymbolLocation(lspserver: dict<any>, location: dict<any>)
   var pos: list<number> = []
   var start_col: number
   var end_col: number
-  start_col = util.GetLineByteFromPos(pwbuf, location.targetSelectionRange.start) + 1
-  end_col = util.GetLineByteFromPos(pwbuf, location.targetSelectionRange.end) + 1
-  pos->add(location.targetSelectionRange.start.line + 1)
+  start_col = util.GetLineByteFromPos(pwbuf, range.start) + 1
+  end_col = util.GetLineByteFromPos(pwbuf, range.end) + 1
+  pos->add(range.start.line + 1)
   pos->extend([start_col, end_col - start_col])
   matchaddpos('Search', [pos], 10, 101, {window: pwid})
   var cmds =<< trim eval END
-    [{location.targetSelectionRange.start.line + 1}, 1]->cursor()
+    [{range.start.line + 1}, 1]->cursor()
     normal! z.
   END
   win_execute(pwid, cmds, 'silent!')
@@ -468,8 +470,9 @@ export def TagFunc(lspserver: dict<any>,
     var tagitem = {}
     tagitem.name = pat
 
-    tagitem.filename = util.LspUriToFile(tagloc.targetUri)
-    tagitem.cmd = $"/\\%{tagloc.targetSelectionRange.start.line + 1}l\\%{tagloc.targetSelectionRange.start.character + 1}c"
+    var [uri, range] = util.LspLocationParse(tagloc)
+    tagitem.filename = util.LspUriToFile(uri)
+    tagitem.cmd = $"/\\%{range.start.line + 1}l\\%{range.start.character + 1}c"
 
     retval->add(tagitem)
   endfor
index 091c8d221dfff36249ef2ba0896318a38ffee8a3..0e18dcf44b08120f1defb5d263d9195d5570a3b6 100644 (file)
@@ -50,15 +50,16 @@ export def ClearTraceLogs()
   writefile([], $'{lsp_log_dir}lsp-server.err')
 enddef
 
-# Convert a 'Location' to a dict that looks a lot like a 'LocationLink' but with
-# the caveat that 'targetRange' is null.
-export def LspLocationToLocationLink(location: dict<any>): dict<any>
-  return {
-    targetUri: location.uri,
-    targetSelectionRange: location.range,
-    targetRange: null,
-    originSelectionRange: null
-  }
+# Parse a LSP Location or LocationLink type and return a List with two items.
+# The first item is the DocumentURI and the second item is the Range.
+export def LspLocationParse(lsploc: dict<any>): list<any>
+  if lsploc->has_key('targetUri')
+    # LocationLink
+    return [lsploc.targetUri, lsploc.targetSelectionRange]
+  else
+    # Location
+    return [lsploc.uri, lsploc.range]
+  endif
 enddef
 
 # Convert a LSP file URI (file://<absolute_path>) to a Vim file name
@@ -165,7 +166,8 @@ enddef
 # number and character number. The user specified window command modifiers
 # (e.g. topleft) are in 'cmdmods'.
 export def JumpToLspLocation(location: dict<any>, cmdmods: string)
-  var fname = LspUriToFile(location.targetUri)
+  var [uri, range] = LspLocationParse(location)
+  var fname = LspUriToFile(uri)
 
   # jump to the file and line containing the symbol
   if cmdmods == ''
@@ -202,8 +204,7 @@ export def JumpToLspLocation(location: dict<any>, cmdmods: string)
   # Set the previous cursor location mark. Instead of using setpos(), m' is
   # used so that the current location is added to the jump list.
   normal m'
-  setcursorcharpos(location.targetSelectionRange.start.line + 1,
-                       location.targetSelectionRange.start.character + 1)
+  setcursorcharpos(range.start.line + 1, range.start.character + 1)
 enddef
 
 # 'indexof' is to new to use it, use this instead.
index c07b273e4d5c0230ec0898a6660879903f41d4e9..cbdf008db332050611553aea260ca1ca9a7e85fa 100644 (file)
@@ -234,7 +234,7 @@ def Test_LspShowReferences()
   # write since last change" error message.  To disable this message, mark the
   # buffer as not modified.
   setlocal nomodified
-  cursor(1, 5)
+  cursor(10, 6)
   :LspPeekReferences
   var ids = popup_list()
   assert_equal(2, ids->len())
@@ -242,7 +242,11 @@ def Test_LspShowReferences()
   var refPopupAttrs = ids[1]->popup_getoptions()
   assert_match('Xtest', filePopupAttrs.title)
   assert_equal('References', refPopupAttrs.title)
-  assert_equal(1, line('.', ids[0]))
+  assert_equal(10, line('.', ids[0]))
+  assert_equal(3, line('$', ids[1]))
+  feedkeys("jj\<CR>", 'xt')
+  assert_equal(12, line('.'))
+  assert_equal([], popup_list())
   popup_clear()
 
   bw!