From: Yegappan Lakshmanan Date: Sun, 12 Mar 2023 19:36:18 +0000 (-0700) Subject: Handle LocationLink type properly and update the test for :LspPeekReferences X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=3e39e8202981bd095d31717388e9058d27d929bc;p=vim-lsp.git Handle LocationLink type properly and update the test for :LspPeekReferences --- diff --git a/autoload/lsp/lspserver.vim b/autoload/lsp/lspserver.vim index 7d9fc77..f0397c8 100644 --- a/autoload/lsp/lspserver.vim +++ b/autoload/lsp/lspserver.vim @@ -890,11 +890,6 @@ def GotoSymbolLoc(lspserver: dict, 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, pat: string, flags: string, info: dict): 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 diff --git a/autoload/lsp/symbol.vim b/autoload/lsp/symbol.vim index 89e498f..5dcf66c 100644 --- a/autoload/lsp/symbol.vim +++ b/autoload/lsp/symbol.vim @@ -259,6 +259,7 @@ def UpdatePeekFilePopup(lspserver: dict, refs: list>) 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, location: dict) - 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, location: dict) var pos: list = [] 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, 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 diff --git a/autoload/lsp/util.vim b/autoload/lsp/util.vim index 091c8d2..0e18dcf 100644 --- a/autoload/lsp/util.vim +++ b/autoload/lsp/util.vim @@ -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): dict - 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): list + 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://) 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, 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, 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. diff --git a/test/unit_tests.vim b/test/unit_tests.vim index c07b273..cbdf008 100644 --- a/test/unit_tests.vim +++ b/test/unit_tests.vim @@ -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\", 'xt') + assert_equal(12, line('.')) + assert_equal([], popup_list()) popup_clear() bw!