From fcb1d4b95feb1b557e9b951e70d96da5fcc6c18e Mon Sep 17 00:00:00 2001 From: Austin Horstman Date: Fri, 24 Apr 2026 00:05:53 -0500 Subject: [PATCH] plugins/treesitter: add highlight disable option --- plugins/by-name/treesitter/default.nix | 54 +++++++++++++++++-- .../plugins/by-name/treesitter/default.nix | 51 ++++++++++++++++++ 2 files changed, 101 insertions(+), 4 deletions(-) diff --git a/plugins/by-name/treesitter/default.nix b/plugins/by-name/treesitter/default.nix index 7ec85f23..14b820ff 100644 --- a/plugins/by-name/treesitter/default.nix +++ b/plugins/by-name/treesitter/default.nix @@ -187,6 +187,23 @@ lib.nixvim.plugins.mkNeovimPlugin { highlight = { enable = lib.mkEnableOption "tree-sitter based syntax highlighting"; + + disable = mkOption { + type = with types; listOf str; + default = [ ]; + example = [ + "latex" + "html" + ]; + description = '' + List of languages or filetypes for which tree-sitter based syntax highlighting should not + be started by Nixvim. + + This option only applies to Nixvim's native tree-sitter highlighting setup for the modern + nvim-treesitter main branch. Legacy nvim-treesitter configuration should continue using + upstream settings under `plugins.treesitter.settings`. + ''; + }; }; indent = { @@ -294,14 +311,31 @@ lib.nixvim.plugins.mkNeovimPlugin { ''} ${optionalString (highlightEnabled || indentEnabled) '' -- Enable features via autocommands for modern nvim-treesitter + ${optionalString highlightEnabled '' + local disabled_highlight = ${lib.nixvim.toLuaObject cfg.highlight.disable} + ''} + vim.api.nvim_create_autocmd('FileType', { group = augroup, pattern = '*', - callback = function() + callback = function(args) ${optionalString highlightEnabled '' - pcall(vim.treesitter.start) + local filetype = vim.bo[args.buf].filetype + local lang = vim.treesitter.language.get_lang(filetype) or filetype + local start_highlight = true + + for _, disabled in ipairs(disabled_highlight) do + if disabled == lang or disabled == filetype then + start_highlight = false + break + end + end + + if start_highlight then + pcall(vim.treesitter.start, args.buf, lang) + end ''}${optionalString indentEnabled '' - vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()" + vim.bo[args.buf].indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()" ''} end, }) @@ -343,7 +377,19 @@ lib.nixvim.plugins.mkNeovimPlugin { }; warnings = lib.nixvim.mkWarnings "plugins.treesitter" ( - lib.map (packageName: { + [ + { + when = + (cfg.settings.highlight.disable or null) != null + && !(lib.hasInfix "nvim-treesitter-legacy" (lib.getName cfg.package)); + message = '' + `plugins.treesitter.settings.highlight.disable` is an upstream legacy nvim-treesitter + option. For Nixvim's native highlighting support with the modern nvim-treesitter main + branch, use `${opt.highlight.disable}` instead. + ''; + } + ] + ++ lib.map (packageName: { when = !cfg.nixGrammars && !config.dependencies.${packageName}.enable; message = '' `${packageName}` is required to build grammars as you are not using `${opt.nixGrammars}`. diff --git a/tests/test-sources/plugins/by-name/treesitter/default.nix b/tests/test-sources/plugins/by-name/treesitter/default.nix index 632992d6..faf95af9 100644 --- a/tests/test-sources/plugins/by-name/treesitter/default.nix +++ b/tests/test-sources/plugins/by-name/treesitter/default.nix @@ -40,6 +40,36 @@ }; }; + disable-highlighting = + { config, lib, ... }: + { + assertions = [ + { + assertion = lib.hasInfix ''local disabled_highlight = { "latex", "html" }'' config.content; + message = "Treesitter highlight disable list should be present in generated lua."; + } + { + assertion = lib.hasInfix "if disabled == lang or disabled == filetype then" config.content; + message = "Treesitter highlight disable check should match language and filetype."; + } + { + assertion = lib.hasInfix "pcall(vim.treesitter.start, args.buf, lang)" config.content; + message = "Treesitter highlighting should start with the resolved language."; + } + ]; + + plugins.treesitter = { + enable = true; + highlight = { + enable = true; + disable = [ + "latex" + "html" + ]; + }; + }; + }; + no-nix-grammars = { plugins.treesitter = { enable = true; @@ -66,6 +96,7 @@ legacy-master = { plugins.treesitter = { enable = true; + package = pkgs.vimPlugins.nvim-treesitter-legacy; settings = { auto_install = false; @@ -100,4 +131,24 @@ }; }; }; + + legacy-highlight-disable-warning = { + test.runNvim = false; + test.warnings = expect: [ + (expect "count" 1) + (expect "any" "`plugins.treesitter.settings.highlight.disable` is an upstream legacy nvim-treesitter") + (expect "any" "use `plugins.treesitter.highlight.disable` instead.") + ]; + + plugins.treesitter = { + enable = true; + settings.highlight = { + enable = true; + disable = [ + "latex" + "html" + ]; + }; + }; + }; }