treesitter: allow setting of filetype pattern and excludes for indent

This commit is contained in:
Matthias Putz 2026-03-24 22:19:48 +01:00
commit d30ba87023
3 changed files with 41 additions and 4 deletions

View file

@ -4,8 +4,10 @@
...
}: let
inherit (lib.modules) mkIf;
inherit (lib.strings) optionalString;
inherit (lib.lists) optionals;
inherit (lib.nvim.dag) entryAfter;
inherit (lib.nvim.lua) toLuaObject;
cfg = config.vim.treesitter;
in {
@ -39,8 +41,14 @@ in {
-- Enable treesitter highlighting for all filetypes
vim.api.nvim_create_autocmd("FileType", {
group = "nvf_treesitter",
pattern = "*",
callback = function()
pattern = ${toLuaObject cfg.indent.pattern},
callback = function(args)
${optionalString (builtins.length cfg.indent.excludes > 0) ''
local ft = vim.bo[args.buf].filetype
if vim.tbl_contains(${toLuaObject cfg.indent.excludes}, ft) then
return
end
''}
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
end,
})

View file

@ -4,7 +4,7 @@
...
}: let
inherit (lib.options) mkOption mkEnableOption literalExpression;
inherit (lib.types) listOf nullOr package bool;
inherit (lib.types) listOf nullOr package bool str oneOf;
in {
options.vim.treesitter = {
enable = mkEnableOption "treesitter, also enabled automatically through language options";
@ -64,7 +64,28 @@ in {
'';
};
indent = {enable = mkEnableOption "indentation with treesitter" // {default = true;};};
indent = {
enable = mkEnableOption "indentation with treesitter" // {default = true;};
pattern = mkOption {
type = oneOf [str (listOf str)];
default = "*";
example = literalExpression ''["lua" "nix"]'';
description = ''
Specify the filetype pattern(s) for which the treesitter indentation should be used.
See {command}`:h autocmd-pattern`.
'';
};
excludes = mkOption {
type = listOf str;
default = [];
example = literalExpression ''["haskell", "purescript"]'';
description = ''
Exclude the listed filetypes from using treesitter indentation.
'';
};
};
highlight = {enable = mkEnableOption "highlighting with treesitter" // {default = true;};};
};
}