From 704f331a71057f62296f720de79b98bd02162c86 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Magnus=20Gro=C3=9F?= Date: Wed, 31 May 2023 23:44:24 +0200 Subject: [PATCH] Use getbufoneline() where applicable This has better performance than getbufline() and as a sideeffect also fixes a rare issue where the LSP sends invalid data such that getbufline() reads an empty list and causes the array access to get out of bounds. With getbufoneline() this neatly falls back to returning the empty string. --- autoload/lsp/outline.vim | 2 +- autoload/lsp/symbol.vim | 4 ++-- autoload/lsp/textedit.vim | 2 +- test/clangd_tests.vim | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/autoload/lsp/outline.vim b/autoload/lsp/outline.vim index 7a337b3..4665131 100644 --- a/autoload/lsp/outline.vim +++ b/autoload/lsp/outline.vim @@ -194,7 +194,7 @@ def OutlineHighlightCurrentSymbol() # Highlight the selected symbol var col: number = - bnr->getbufline(symbolTable[mid].outlineLine)[0]->match('\S') + 1 + bnr->getbufoneline(symbolTable[mid].outlineLine)->match('\S') + 1 prop_add(symbolTable[mid].outlineLine, col, {bufnr: bnr, type: 'LspOutlineHighlight', length: symbolTable[mid].name->len()}) diff --git a/autoload/lsp/symbol.vim b/autoload/lsp/symbol.vim index 5971606..38725e1 100644 --- a/autoload/lsp/symbol.vim +++ b/autoload/lsp/symbol.vim @@ -349,8 +349,8 @@ def PeekLocations(lspserver: dict, locations: list>, bnr->bufload() endif - var text: string = bnr->getbufline(range.start.line + 1)[0] var lnum = range.start.line + 1 + var text: string = bnr->getbufoneline(lnum) menuItems->add($'{lnum}: {text}') endfor @@ -392,7 +392,7 @@ export def ShowLocations(lspserver: dict, locations: list>, if !bnr->bufloaded() bnr->bufload() endif - var text: string = bnr->getbufline(range.start.line + 1)[0]->trim("\t ", 1) + var text: string = bnr->getbufoneline(range.start.line + 1)->trim("\t ", 1) qflist->add({filename: fname, lnum: range.start.line + 1, col: util.GetLineByteFromPos(bnr, range.start) + 1, diff --git a/autoload/lsp/textedit.vim b/autoload/lsp/textedit.vim index 7a30137..02859de 100644 --- a/autoload/lsp/textedit.vim +++ b/autoload/lsp/textedit.vim @@ -173,7 +173,7 @@ export def ApplyTextEdits(bnr: number, text_edits: list>): void # lines. var dellastline: bool = false if start_line == 0 && bnr->getbufinfo()[0].linecount == 1 && - bnr->getbufline(1)[0] == '' + bnr->getbufoneline(1) == '' dellastline = true endif diff --git a/test/clangd_tests.vim b/test/clangd_tests.vim index 2d58fc7..7c29d3a 100644 --- a/test/clangd_tests.vim +++ b/test/clangd_tests.vim @@ -464,14 +464,14 @@ def g:Test_LspDiag_Multi() assert_equal([1, 5], [line('.'), col('.')]) var ids = popup_list() assert_equal(1, ids->len()) - assert_match('Incompatible pointer to integer', getbufline(ids[0]->winbufnr(), 1, '$')[0]) + assert_match('Incompatible pointer to integer', getbufoneline(ids[0]->winbufnr(), 1, '$')) popup_clear() cursor(1, 6) :LspDiagHere assert_equal([1, 9], [line('.'), col('.')]) ids = popup_list() assert_equal(1, ids->len()) - assert_match('Initializer element is not', getbufline(ids[0]->winbufnr(), 1, '$')[0]) + assert_match('Initializer element is not', getbufoneline(ids[0]->winbufnr(), 1, '$')) popup_clear() # Line without diagnostics -- 2.48.1