From ce6237688220b7d36de3c7a170b2afd18a9e2ff4 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Fri, 8 Nov 2024 18:03:08 +0300 Subject: [PATCH] plugins/nvim-treesitter: add extraQueries --- modules/plugins/style/nvim-treesitter.nix | 54 ++++++++++++++++++----- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/modules/plugins/style/nvim-treesitter.nix b/modules/plugins/style/nvim-treesitter.nix index 719c722..c7a5b84 100644 --- a/modules/plugins/style/nvim-treesitter.nix +++ b/modules/plugins/style/nvim-treesitter.nix @@ -14,8 +14,33 @@ let if cfg.grammars == null then cfg.package.withAllGrammars else cfg.package.withPlugins cfg.grammars; + + postPatchExtraGrammars = lib.concatLines (lib.flip + lib.mapAttrsToList + cfg.extraGrammars + (k: { src, language, ... }: + '' + if [ -d ${src}/queries ]; then + ln -s ${src}/queries queries/${language} + fi + '' + ) + ); + + postPatchExtraQueries = lib.concatLines (lib.flatten + (lib.flip lib.mapAttrsToList cfg.extraQueries ( + lang: + lib.mapAttrsToList (queryKind: queries: + '' + mkdir -p queries/${lang} + echo "${queries}" >> queries/${lang}/${queryKind}.scm + '' + ) + )) + ); + finalNvimTreeSitter = - if cfg.extraGrammars == { } then nvimTreeSitterWithBuiltinGrammars + if cfg.extraGrammars == { } && cfg.extraQueries == { } then nvimTreeSitterWithBuiltinGrammars else nvimTreeSitterWithBuiltinGrammars.overrideAttrs (oldAttrs: { passthru.dependencies = oldAttrs.passthru.dependencies @@ -26,15 +51,9 @@ let '' )); - postPatch = oldAttrs.postPatch + (lib.concatLines - (lib.flip lib.mapAttrsToList cfg.extraGrammars (k: { src, language, ... }: - '' - if [ -d ${src}/queries ]; then - ln -s ${src}/queries queries/${language} - fi - '' - )) - ); + postPatch = oldAttrs.postPatch + + postPatchExtraGrammars + + postPatchExtraQueries; }); in { @@ -62,6 +81,21 @@ in default = { }; }; + extraQueries = mkOption { + type = with types; attrsOf (submodule { + options = { + injections = mkOption { + type = either str (listOf str); + default = [ ]; + }; + }; + }); + default = { }; + example = { + javascript.injections = ''(comment) @comment''; + }; + }; + settings = mkOption { type = types.attrs; default = { };