this post was submitted on 12 Nov 2025
1 points (100.0% liked)

Neovim

11 readers
0 users here now

Neovim is an hyperextensible Vim-based text editor. Learn more at neovim.io.

founded 2 years ago
MODERATORS
 
This is an automated archive made by the Lemmit Bot.

The original was posted on /r/neovim by /u/jessevdp on 2025-11-11 10:40:00+00:00.


I'm working on a new nvim configuration based on the nightly version utilizing vim.pack.add. I noticed that nvim-treesitter has been rewritten on the main branch: "This is a full, incompatible, rewrite."

As part of the rewrite there is no longer a convenient built in way to auto install parsers.

Also, since I'm using vim.pack.add I have to find a way to ensure :TSUpdate is run when the plugin updates.

I came up with the following. How did I do?

vim.pack.add({
  { src = "https://github.com/nvim-treesitter/nvim-treesitter", version = "main" },
}, { confirm = false })

local ts = require("nvim-treesitter")
local augroup = vim.api.nvim_create_augroup("myconfig.treesitter", { clear = true })

vim.api.nvim_create_autocmd("FileType", {
  group = augroup,
  pattern = { "*" },
  callback = function(event)
    local filetype = event.match
    local lang = vim.treesitter.language.get_lang(filetype)
    local is_installed, error = vim.treesitter.language.add(lang)

    if not is_installed then
      local available_langs = ts.get_available()
      local is_available = vim.tbl_contains(available_langs, lang)

      if is_available then
        vim.notify("Installing treesitter parser for " .. lang, vim.log.levels.INFO)
        ts.install({ lang }):wait(30 * 1000)
      end
    end

    local ok, _ = pcall(vim.treesitter.start, event.buf, lang)
    if not ok then return end

    vim.bo[event.buf].indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
    vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
  end
})

vim.api.nvim_create_autocmd("PackChanged", {
  group = augroup,
  pattern = { "nvim-treesitter" },
  callback = function(event)
    vim.notify("Updating treesitter parsers", vim.log.levels.INFO)
    ts.update(nil, { summary = true }):wait(30 * 1000)
  end
})

BTW, I actually like this rewrite and how it forced me to learn a little bit more about how neovim works with treesitter, what parts are built into neovim vs. what is handled by the plugin, etc.

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here