From 6013b67dc9ae84ca20a250d5c159efbc5dd242fc Mon Sep 17 00:00:00 2001 From: Austin Horstman Date: Mon, 15 Dec 2025 22:00:53 -0600 Subject: [PATCH] plugins/treesitter: folding use autocmd Follow upstream recommendation for enabling folding. --- plugins/by-name/treesitter/default.nix | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/plugins/by-name/treesitter/default.nix b/plugins/by-name/treesitter/default.nix index fd21ce58..056e0a60 100644 --- a/plugins/by-name/treesitter/default.nix +++ b/plugins/by-name/treesitter/default.nix @@ -11,7 +11,6 @@ let literalExpression optionalString mkIf - mkDefault ; buildGrammarDeps = [ "gcc" @@ -312,6 +311,22 @@ lib.nixvim.plugins.mkNeovimPlugin { pkg: pkg.withPlugins (_: cfg.grammarPackages) ); + # NOTE: This autoCmd is declared outside of Lua while the autogroup is created in luaConfig.content. + # This is fragile - if module generation order changes, the autocmd might be created before the + # autogroup exists (causing failure), or the autogroup's clear=true might clear this autocmd. + # The current order happens to work, but changes to nixvim's module system could break this. + autoCmd = lib.optional cfg.folding.enable { + event = "FileType"; + group = "nixvim_treesitter"; + pattern = "*"; + callback.__raw = '' + function() + vim.wo[0][0].foldexpr = 'v:lua.vim.treesitter.foldexpr()' + vim.wo[0][0].foldmethod = 'expr' + end + ''; + }; + warnings = lib.nixvim.mkWarnings "plugins.treesitter" ( lib.map (packageName: { when = !cfg.nixGrammars && !config.dependencies.${packageName}.enable; @@ -321,10 +336,5 @@ lib.nixvim.plugins.mkNeovimPlugin { ''; }) buildGrammarDeps ); - - opts = mkIf cfg.folding.enable { - foldmethod = mkDefault "expr"; - foldexpr = mkDefault "v:lua.vim.treesitter.foldexpr()"; - }; }; }