From: Yegappan Lakshmanan Date: Tue, 27 Jun 2023 04:11:48 +0000 (-0700) Subject: Select the current symbol when opening the peek symbol popup menu. Open closed folds... X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=579c6a1ca5a598043c38efcd527dd3922273fbfb;p=vim-lsp.git Select the current symbol when opening the peek symbol popup menu. Open closed folds when jumping to a symbol location --- diff --git a/autoload/lsp/diag.vim b/autoload/lsp/diag.vim index 30b1b7d..05dd44e 100644 --- a/autoload/lsp/diag.vim +++ b/autoload/lsp/diag.vim @@ -638,6 +638,7 @@ def JumpDiag(diag: dict) var startPos: dict = diag.range.start setcursorcharpos(startPos.line + 1, util.GetCharIdxWithoutCompChar(bufnr(), startPos) + 1) + :normal! zv if !opt.lspOptions.showDiagWithVirtualText :redraw DisplayDiag(diag) diff --git a/autoload/lsp/symbol.vim b/autoload/lsp/symbol.vim index b4d92e2..0be5300 100644 --- a/autoload/lsp/symbol.vim +++ b/autoload/lsp/symbol.vim @@ -142,6 +142,7 @@ def JumpToWorkspaceSymbol(cmdmods: string, popupID: number, result: number): voi setcursorcharpos(symTbl[result - 1].pos.line + 1, util.GetCharIdxWithoutCompChar(bufnr(), symTbl[result - 1].pos) + 1) + :normal! zv catch # ignore exceptions endtry @@ -360,6 +361,9 @@ def PeekLocations(lspserver: dict, locations: list>, var w: number = &columns var fnamelen = float2nr(w * 0.4) + var curlnum = line('.') + var symIdx = 1 + var curSymIdx = 1 var menuItems: list = [] for loc in locations var [uri, range] = util.LspLocationParse(loc) @@ -373,6 +377,11 @@ def PeekLocations(lspserver: dict, locations: list>, var lnum = range.start.line + 1 var text: string = bnr->getbufline(lnum)->get(0, '') menuItems->add($'{lnum}: {text}') + + if lnum == curlnum + curSymIdx = symIdx + endif + symIdx += 1 endfor var popupAttrs = { @@ -391,6 +400,11 @@ def PeekLocations(lspserver: dict, locations: list>, callback: function(LocPopupCallback, [lspserver, locations]) } lspserver.peekSymbolPopup = popup_menu(menuItems, popupAttrs) + # Select the current symbol in the menu + var cmds =<< trim eval END + [{curSymIdx}, 1]->cursor() + END + win_execute(lspserver.peekSymbolPopup, cmds, 'silent!') UpdatePeekFilePopup(lspserver, locations) enddef @@ -765,8 +779,13 @@ def SymbolMenuItemSelected(symPopupMenu: number, # Jump to the selected symbol location var r = symTblFiltered[result - 1].selectionRange + if r->empty() + # SymbolInformation doesn't have the selectionRange field + r = symTblFiltered[result - 1].range + endif setcursorcharpos(r.start.line + 1, - util.GetCharIdxWithoutCompChar(bufnr(), r.start) + 1) + util.GetCharIdxWithoutCompChar(bufnr(), r.start) + 1) + :normal! zv endif symInputPopup->popup_close() prop_remove({type: 'LspSymbolNameProp', all: true}) diff --git a/autoload/lsp/util.vim b/autoload/lsp/util.vim index c7b0d79..aefe766 100644 --- a/autoload/lsp/util.vim +++ b/autoload/lsp/util.vim @@ -303,6 +303,7 @@ export def JumpToLspLocation(location: dict, cmdmods: string) endif setcursorcharpos(range.start.line + 1, GetCharIdxWithoutCompChar(bufnr(), range.start) + 1) + :normal! zv enddef # indexof() function is not present in older Vim 9 versions. So use this diff --git a/test/clangd_tests.vim b/test/clangd_tests.vim index 443ff98..2ee69e0 100644 --- a/test/clangd_tests.vim +++ b/test/clangd_tests.vim @@ -237,12 +237,24 @@ def g:Test_LspShowReferences() assert_match('XshowRefs', filePopupAttrs.title) assert_equal('Symbol References', refPopupAttrs.title) assert_equal(10, line('.', ids[0])) + assert_equal(1, line('.', ids[1])) assert_equal(3, line('$', ids[1])) feedkeys("jj\", 'xt') assert_equal(12, line('.')) assert_equal([], popup_list()) popup_clear() + # LspShowReferences should start with the current symbol + cursor(12, 6) + :LspPeekReferences + sleep 50m + ids = popup_list() + assert_equal(2, ids->len()) + assert_equal(12, line('.', ids[0])) + assert_equal(3, line('.', ids[1])) + feedkeys("\", 'xt') + popup_clear() + bw! # empty file