From 9ff0dcb60cf0878c44bcafc3eec7844ce84106f6 Mon Sep 17 00:00:00 2001 From: Snoweuph Date: Thu, 23 Apr 2026 22:51:01 +0200 Subject: [PATCH] treesitter: add filetype mappings --- docs/manual/release-notes/rl-0.9.md | 4 ++ modules/plugins/treesitter/config.nix | 87 ++++++++++++----------- modules/plugins/treesitter/treesitter.nix | 15 +++- 3 files changed, 65 insertions(+), 41 deletions(-) diff --git a/docs/manual/release-notes/rl-0.9.md b/docs/manual/release-notes/rl-0.9.md index 04ba7acd..9c399f7a 100644 --- a/docs/manual/release-notes/rl-0.9.md +++ b/docs/manual/release-notes/rl-0.9.md @@ -283,6 +283,10 @@ - Added {option}`vim.languages.tera.treesitter.injection` to configure, what language the content is. +- Added {option}`vim.treesitter.filetypeMappings` to support mappings similar to + . + This is mostly use full for Markdown code block injections. + - Added `vim.lsp.presets.` to contain LSP configurations. This allows for more flexibility in nvf and reuse of LSPs across languages. Dropped `deprecatedSingleOrListOf` in favor of `listOf` for the affected LSP options. diff --git a/modules/plugins/treesitter/config.nix b/modules/plugins/treesitter/config.nix index 862a5ed8..985a363d 100644 --- a/modules/plugins/treesitter/config.nix +++ b/modules/plugins/treesitter/config.nix @@ -24,49 +24,56 @@ in { treesitter.grammars = optionals cfg.addDefaultGrammars cfg.defaultGrammars; - pluginRC.treesitter-autocommands = entryAfter ["basic"] '' - vim.api.nvim_create_augroup("nvf_treesitter", { clear = true }) + pluginRC = { + treesitter-autocommands = entryAfter ["basic"] '' + vim.api.nvim_create_augroup("nvf_treesitter", { clear = true }) - ${lib.optionalString cfg.highlight.enable '' - -- Enable treesitter highlighting for all filetypes - vim.api.nvim_create_autocmd("FileType", { - group = "nvf_treesitter", - pattern = "*", - callback = function() - pcall(vim.treesitter.start) - end, - }) - ''} - - ${lib.optionalString cfg.indent.enable '' - -- Enable treesitter highlighting for all filetypes - vim.api.nvim_create_autocmd("FileType", { - group = "nvf_treesitter", - 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 + ${lib.optionalString cfg.highlight.enable '' + -- Enable treesitter highlighting for all filetypes + vim.api.nvim_create_autocmd("FileType", { + group = "nvf_treesitter", + pattern = "*", + callback = function() + pcall(vim.treesitter.start) + end, + }) ''} - vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()" - end, - }) - ''} - ${lib.optionalString cfg.fold '' - -- Enable treesitter folding for all filetypes - vim.api.nvim_create_autocmd("FileType", { - group = "nvf_treesitter", - pattern = "*", - callback = function() - vim.wo[0][0].foldmethod = "expr" - vim.wo[0][0].foldexpr = "v:lua.vim.treesitter.foldexpr()" - end, - }) - ''} - ''; + ${lib.optionalString cfg.indent.enable '' + -- Enable treesitter highlighting for all filetypes + vim.api.nvim_create_autocmd("FileType", { + group = "nvf_treesitter", + 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, + }) + ''} + + ${lib.optionalString cfg.fold '' + -- Enable treesitter folding for all filetypes + vim.api.nvim_create_autocmd("FileType", { + group = "nvf_treesitter", + pattern = "*", + callback = function() + vim.wo[0][0].foldmethod = "expr" + vim.wo[0][0].foldexpr = "v:lua.vim.treesitter.foldexpr()" + end, + }) + ''} + ''; + treesitter-filetype-mappings = entryAfter ["basic"] '' + for lang, ft in pairs(${toLuaObject cfg.filetypeMappings}) do + vim.treesitter.language.register(lang, ft) + end + ''; + }; additionalRuntimePaths = mkIf (cfg.queries != []) [ (let diff --git a/modules/plugins/treesitter/treesitter.nix b/modules/plugins/treesitter/treesitter.nix index 9d543621..11dda33e 100644 --- a/modules/plugins/treesitter/treesitter.nix +++ b/modules/plugins/treesitter/treesitter.nix @@ -4,7 +4,7 @@ ... }: let inherit (lib.options) mkOption mkEnableOption literalExpression; - inherit (lib.types) listOf nullOr package bool str lines enum submodule oneOf; + inherit (lib.types) listOf nullOr package bool str lines enum submodule oneOf attrsOf; queriesType = submodule { options = { @@ -111,5 +111,18 @@ in { default = []; description = "A list of Neovim treesitter queries to be registered."; }; + + filetypeMappings = mkOption { + type = attrsOf (listOf str); + default = {}; + example = { + "sh" = ["ash" "dash"]; + }; + description = '' + Register alternative parser names for a filetype. + For more information see `:h vim.treesitter.language.register()`. + See treesitter builtin mappings here: + ''; + }; }; }