]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Use getbufoneline() where applicable
authorMagnus Groß <magnus@mggross.com>
Wed, 31 May 2023 21:44:24 +0000 (23:44 +0200)
committerMagnus Groß <magnus@mggross.com>
Wed, 31 May 2023 21:49:39 +0000 (23:49 +0200)
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
autoload/lsp/symbol.vim
autoload/lsp/textedit.vim
test/clangd_tests.vim

index 7a337b3c5f6ddb25b09d77624fe404caf858294e..4665131904ae47223d679cc7a11f327c03db5e02 100644 (file)
@@ -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()})
index 597160656cf0893c7695a1180327dcb288a9fd7a..38725e1cd569dfff4bf7d064943b9bdd68a52ad3 100644 (file)
@@ -349,8 +349,8 @@ def PeekLocations(lspserver: dict<any>, locations: list<dict<any>>,
       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<any>, locations: list<dict<any>>,
     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,
index 7a30137ae398c9f279fe82499bd3d4123d545c83..02859dec25712ab1d507df2c812697ebcb163347 100644 (file)
@@ -173,7 +173,7 @@ export def ApplyTextEdits(bnr: number, text_edits: list<dict<any>>): 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
 
index 2d58fc70195cb0047c4c8d94719e614eb9f14f87..7c29d3a2c233a9a2155738ed42396715c3d528fb 100644 (file)
@@ -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