From 1472cfa6d1ce37d7d006a636d5a518a4b6bc830e Mon Sep 17 00:00:00 2001 From: Leonhard Kost Date: Fri, 13 Nov 2026 18:28:35 +0100 Subject: [PATCH] feat(neovim): make treesitter auto_install work --- pkgs/nixvim/plugins/treesitter.nix | 33 ++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/pkgs/nixvim/plugins/treesitter.nix b/pkgs/nixvim/plugins/treesitter.nix index 3945103..a10ad15 100644 --- a/pkgs/nixvim/plugins/treesitter.nix +++ b/pkgs/nixvim/plugins/treesitter.nix @@ -2,11 +2,40 @@ { plugins.treesitter = { enable = true; - highlight.enable = true; - indent.enable = true; + nixGrammars = false; }; + extraConfigLua = '' + local function start(buf, lang) + if not vim.api.nvim_buf_is_valid(buf) then return end + if not pcall(vim.treesitter.start, buf, lang) then return end + + -- indent (nvim-treesitter's indents.scm, experimental on main) + vim.bo[buf].indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()" + + -- folds (built into Neovim, uses folds.scm) + vim.api.nvim_buf_call(buf, function() + vim.wo[0][0].foldmethod = "expr" + vim.wo[0][0].foldexpr = "v:lua.vim.treesitter.foldexpr()" + end) + end + + vim.api.nvim_create_autocmd("FileType", { + callback = function(args) + local lang = vim.treesitter.language.get_lang(args.match) + if not lang then return end + local ts = require("nvim-treesitter") + if vim.tbl_contains(ts.get_installed(), lang) then + start(args.buf, lang) + elseif vim.tbl_contains(ts.get_available(), lang) then + ts.install(lang):await(function() start(args.buf, lang) end) + end + end, + }) + ''; + extraPackages = with pkgs; [ gcc + tree-sitter ]; }