Native Treesitter in Neovim
As fate would have it, I make a blog post about returning to Neovim, and for some reason an update to nvim-treesitter breaks my ability to see syntax highlighting for nushell. A very small annoyance, but enough for me to replace it. I remember seeing this awesome post by boltless so I knew it was possible. Turned out to be super simple!
First I needed something to install the parsers. I ended up following boltless’ recommendation of using luarocks and making a small script to automate installing necessary parsers:
def tsi [parser: string] {
let tree = $"($env.HOME)/.local/share/nvim/site"
luarocks --lua-version=5.1 $"--tree=($tree)" install $"tree-sitter-($parser)"
}
Installing them is simple as tsi rust or whichever language I need to grab. Next I needed to add these parsers to my packpath, so I added a new treesitter.lua to core with the following contents.
-- Native treesitter parsers installed via luarocks
local rocks_path = vim.fn.stdpath("data") .. "/site/lib/luarocks/rocks-5.1"
for _, parser_dir in ipairs(vim.fn.glob(rocks_path .. "/tree-sitter-*/*/", true, true)) do
vim.opt.runtimepath:prepend(parser_dir)
end
One of the last steps is an autocmd to start up tree sitter with the open buffer.
vim.api.nvim_create_autocmd("FileType", {
callback = function(ev)
pcall(vim.treesitter.start, ev.buf)
end
})
A nice small bonus: adding the following to treesitter.lua lets me use folds with za.
vim.o.foldenable = true
vim.o.foldlevel = 99
vim.o.foldmethod = "expr"
vim.o.foldexpr = "v:lua.vim.treesitter.foldexpr()"
Now everything works how I expect it to, and I’ve learned more about how treesitter works! Would also highly recommend this post that goes way deeper into the subject. Nothing beats the rush of solving a small problem to make my dev env faster and smoother!