Problems with TreeSitter in lua files (and in general)

Hello,

just recently, my nvim developed a new problem: When opening a large lua file, nvim becomes slow and unresponsive to the point of unusability. Individual keystrokes take seconds to be processed. I have the following suspicions about the problems nature:

  • It’s probably not the LSP, since a) turning of the LSP does nothing, and b) the problem occurs in the Telescope preview window (takes multiple seconds for the preview to appear), where the LSP does not run.
  • It has something to do with the comments. I deleted all the comments in the file (in another editor) and nvim behaved again.

It seems to only affect lua, a large file of julia code was unaffected. I assume it has something to do with the way documentation is extracted from the lua files. I tried to TSDiable all, TSInstall lua and TSInstall luadoc. None of these helped. I kind of need my lua documentations though, so any help would be appreciated.

1 Like

if you have TreeSitter enabled for lua try to disable and see the differences, i have the same behavior with markdown files

Hi.
Thanks for the hint. I did a :TSDisable lua before, thinking that this would stop treesitter, but apparently it doesn’t. Google’d quickly and did a lua vim.treesitter.stop(), which immediately fixed the Problem. This confirms at least where the problem is.

Now a short-term workaround would be to just disable treesitter in any and all lua buffers, but ideally I would like TS to work properly again. So maybe I should give some background:
Some time ago my treesitter (for no reason I can point to) just completely broke. In every buffer that had some associated treesitter (not just lua, every buffer, including the nvim command line and Telescope previews) I would constantly get obnoxious errors about some ‘lua callback error’ because of a ‘invalid node type at position XXX’. These errors would be rethrown at pretty much any action, including moving around and writing in insert mode.
I tried to uninstall and reinstall all parsers via :TSUninstall all, but it kept telling me
Tried to uninstall parser for XXX! But the parser is still installed (not by nvim-treesitter):/snap/nvim/2809/usr/share/nvim/runtime/parser/XXX.so. So I tried to manually delete all the files in the given directory, but I kept getting the error ‘cannot remove XXX.so: Read-only file system.’. Both sudo and rm -f did not help either. I am not quite an expert in Linux so I did not know how to handle the situaltion. I google’d around a bit longer and found an old forum entry that suggested to do
:lua require("nvim-treesitter.install").prefer_git = true
followed by
:TSInstall all
That did the trick and (appaently) fixed treesitter again. But some time later I noticed how slow lua files had become now.

So thats the story. I tried uninstalling and reinstalling all parsers again, but then I keep running into the old ‘invalid node type’ and ’ can’t uninstall parser XXX’ problem. A simple :TSInstall lua/luadoc and then confirming to reinstall does not fix it either. Is this a kown problem? Can someone tell me what’s going on here?

you can disable from the config setup for example i have:

local status, ts = pcall(require, "nvim-treesitter.configs")
if (not status) then return end

ts.setup {
  highlight = {
    enable = true,
    disable = {"rust", "python", "lua", "markdown"},
  },
  indent = {
    enable = true,
    disable = {},
  },
  ensure_installed = {
    "markdown",
    "markdown_inline",
    "tsx",
    "typescript",
    "javascript",
    "toml",
    "json",
    "yaml",
    "css",
    "html",
    "lua",
    "rust",
    "python",
    "haskell",
    "ocaml",
  },
  autotag = {
    enable = true,
  },
  context_commentstring = {
    enable         = true,
    enable_autocmd = false,
  }
}

1 Like

Also helpful! I just tried to disable TS with a autocmd on filetype lua, but that seems to not work as intended.