nvim-treesitter / nvim-treesitter-context

Show code context
MIT License
2.48k stars 197 forks source link

Index out of bounds #496

Closed kapral18 closed 3 weeks ago

kapral18 commented 3 weeks ago

Description

When opening NeoTree treesitter-context fails and I am not sure if it's their bug or your bug.

Neovim version

NVIM v0.10.1 Build type: Release LuaJIT 2.1.1727870382 Run "nvim -V1 -v" for more info

Expected behavior

no error

Actual behavior

image

Minimal config

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.minimal", ":p")

-- set stdpaths to use .minimal
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
    "folke/tokyonight.nvim",
    -- add any other plugins here
}

local neotree_config = {
    "nvim-neo-tree/neo-tree.nvim",
    dependencies = { "MunifTanjim/nui.nvim", "nvim-tree/nvim-web-devicons", "nvim-lua/plenary.nvim" },
    cmd = { "Neotree" },
    keys = {
        {
            "<leader>e",
            function()
                require("neo-tree.command").execute({ toggle = true })
            end,
            desc = "Explorer NeoTree (Root Dir)",
        },
    },
    opts = {
        -- Your config here
        -- ...
    },
}

local treesitter = {
    {
        "nvim-treesitter/nvim-treesitter",
        build = ":TSUpdate",
        version = false, -- last release is way too old and doesn't work on Windows
        lazy = false,
        cmd = { "TSUpdateSync", "TSUpdate", "TSInstall" },
        init = function(plugin)
            require("lazy.core.loader").add_to_rtp(plugin)
            require("nvim-treesitter.query_predicates")
        end,
        opts = {
            highlight = { enable = true },
            indent = { enable = true },
            ensure_installed = {
                "javascript",
                "tsx",
                "typescript",
            },
        },
        config = function(_, opts)
            require("nvim-treesitter.configs").setup(opts)
        end,
    },
}

local treesitter_context = {
    "nvim-treesitter/nvim-treesitter-context",
    event = "VeryLazy",
    dependencies = { "nvim-treesitter/nvim-treesitter" },
    opts = { mode = "cursor", max_lines = 3 },
}

table.insert(plugins, neotree_config)
table.insert(plugins, treesitter_context)
table.insert(plugins, treesitter)

require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")

Steps to reproduce

  1. nvim --clean -u minimal.lua
  2. open nvim with sample.tsx file I attached (it's txt because of github restrictions, you can rename it to tsx before opening)
  3. go to line 334 for example (for nested context)
  4. toggle neotree 2-4 times
  5. get the error sample.txt
kapral18 commented 3 weeks ago

Linking to the other ticket for visibility https://github.com/nvim-neo-tree/neo-tree.nvim/issues/1574

OliverGuy commented 3 weeks ago

For me this happens without neo-tree being even loaded (as per :Lazy). Here's my error output:

   Error  16:29:36 msg_show.lua_error Error executing vim.schedule lua callback: ...vim-treesitter-context/lua/treesitter-context/render.lua:107: Could not apply exmtark to { 0, 0, 0, 6 }: Invalid 'end_col': out of range
stack traceback:
    [C]: in function 'error'
    ...vim-treesitter-context/lua/treesitter-context/render.lua:107: in function 'add_extmark'
    ...vim-treesitter-context/lua/treesitter-context/render.lua:159: in function 'fn'
    ...1/share/nvim/runtime/lua/vim/treesitter/languagetree.lua:491: in function 'for_each_tree'
    ...vim-treesitter-context/lua/treesitter-context/render.lua:127: in function 'highlight_contexts'
    ...vim-treesitter-context/lua/treesitter-context/render.lua:417: in function 'open'
    .../lazy/nvim-treesitter-context/lua/treesitter-context.lua:101: in function 'f'
    .../lazy/nvim-treesitter-context/lua/treesitter-context.lua:32: in function <.../lazy/nvim-treesitter-context/lua/treesitter-context.lua:32>
TomLebeda commented 3 weeks ago

I get the same error, but not with NeoTree, for me it's when opening recently closed files with Telescope's "Oldfiles" picker.

The error pops up when the picker is opened. I guess it's either due to change of focus from the buffer to the picker, or when trying to get the context in the Telescope preview.

Also it doesn't happen always, sometimes the picker opens without the error appearing at all. I would say 1 out of 5 times it's okay.

apollo1321 commented 3 weeks ago

Sorry, the bug was introduced in my PR. https://github.com/nvim-treesitter/nvim-treesitter-context/pull/481

I have created another PR that fixes the problem. @lewis6991, can you review it, please? https://github.com/nvim-treesitter/nvim-treesitter-context/pull/497