nvim-treesitter / nvim-treesitter

Nvim Treesitter configurations and abstraction layer
Apache License 2.0
10.88k stars 907 forks source link

query_predicates.lua:264: Index out of bounds #4846

Open jalvesaq opened 1 year ago

jalvesaq commented 1 year ago

Describe the bug

When editing Quarto documents, if I delete a Visual Selection that spans to the next line, I frequently get the error:

E5108: Error executing lua ...nvim-treesitter/lua/nvim-treesitter/query_predicates.lua:264: Index out of bounds

To Reproduce

  1. In Normal mode, press v in the middle of a line and, then, press j to extend the selection to the next line.
  2. Press d to delete the line.

Expected behavior

No error.

Output of :checkhealth nvim-treesitter

nvim-treesitter: require("nvim-treesitter.health").check()

Installation
- OK tree-sitter found 0.20.7 (parser generator, only needed for :TSInstallFromGrammar)
- OK node found v18.13.0 (only needed for :TSInstallFromGrammar)
- OK git executable found.
- OK cc executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl", "zig" }
  Version: cc (Ubuntu 12.2.0-17ubuntu1) 12.2.0
- OK Neovim was compiled with tree-sitter runtime ABI version 14 (required >=13). Parsers must be compatible with runtime ABI.

OS Info:
{
  machine = "x86_64",
  release = "6.2.0-20-generic",
  sysname = "Linux",
  version = "#20-Ubuntu SMP PREEMPT_DYNAMIC Thu Apr  6 07:48:48 UTC 2023"
}

Parser/Features         H L F I J
  - c                   ✓ ✓ ✓ ✓ ✓
  - lua                 ✓ ✓ ✓ ✓ ✓
  - markdown            ✓ . ✓ . ✓
  - query               ✓ ✓ ✓ ✓ ✓
  - r                   ✓ ✓ . ✓ ✓
  - vim                 ✓ ✓ ✓ . ✓
  - vimdoc              ✓ . . . ✓

  Legend: H[ighlight], L[ocals], F[olds], I[ndents], In[j]ections
         +) multiple parsers found, only one will be used
         x) errors found in the query, try to run :TSUpdate {lang}

### Output of `nvim --version`

```text
NVIM v0.10.0-dev-372+gcb34d0ddd
Build type: Release
LuaJIT 2.1.0-beta3

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/local/share/nvim"

Run :checkhealth for more info

Additional context

The bug is fixed with the following change:

diff --git a/lua/nvim-treesitter/query_predicates.lua b/lua/nvim-treesitter/query_predicates.lua
index 545def1..c534f34 100644
--- a/lua/nvim-treesitter/query_predicates.lua
+++ b/lua/nvim-treesitter/query_predicates.lua
@@ -259,6 +259,7 @@ query.add_directive("trim!", function(match, _, bufnr, pred, metadata)
       return
     end

+    if end_row < vim.api.nvim_buf_line_count(bufnr) then
       while true do
         -- As we only care when end_col == 0, always inspect one line above end_row.
         local end_line = vim.api.nvim_buf_get_lines(bufnr, end_row - 1, end_row, true)[1]
@@ -269,6 +270,7 @@ query.add_directive("trim!", function(match, _, bufnr, pred, metadata)

         end_row = end_row - 1
       end
+    end

     -- If this produces an invalid range, we just skip it.
     if start_row < end_row or (start_row == end_row and start_col <= end_col) then

But I don't know if this change introduces new bugs...

clason commented 1 year ago

PR welcome; we can test from there!