1 { lib, callPackage, tree-sitter, neovim, neovimUtils, runCommand, vimPlugins, tree-sitter-grammars }:
6 inherit (neovimUtils) grammarToPlugin;
7 generatedGrammars = callPackage ./generated.nix {
8 inherit (tree-sitter) buildGrammar;
11 generatedDerivations = lib.filterAttrs (_: lib.isDerivation) generatedGrammars;
13 # add aliases so grammars from `tree-sitter` are overwritten in `withPlugins`
14 # for example, for ocaml_interface, the following aliases will be added
16 # tree-sitter-ocaml-interface
17 # tree-sitter-ocaml_interface
18 builtGrammars = generatedGrammars // lib.concatMapAttrs
21 replaced = lib.replaceStrings [ "_" ] [ "-" ] k;
24 "tree-sitter-${k}" = v;
25 } // lib.optionalAttrs (k != replaced) {
27 "tree-sitter-${replaced}" = v;
31 allGrammars = lib.attrValues generatedDerivations;
34 # pkgs.vimPlugins.nvim-treesitter.withPlugins (p: [ p.c p.java ... ])
35 # or for all grammars:
36 # pkgs.vimPlugins.nvim-treesitter.withAllGrammars
38 f: self.nvim-treesitter.overrideAttrs {
39 passthru.dependencies = map grammarToPlugin
40 (f (tree-sitter.builtGrammars // builtGrammars));
43 withAllGrammars = withPlugins (_: allGrammars);
51 passthru = (super.nvim-treesitter.passthru or { }) // {
52 inherit builtGrammars allGrammars grammarToPlugin withPlugins withAllGrammars;
54 grammarPlugins = lib.mapAttrs (_: grammarToPlugin) generatedDerivations;
59 nvimWithAllGrammars = neovim.override {
60 configure.packages.all.start = [ withAllGrammars ];
63 runCommand "nvim-treesitter-check-queries"
65 nativeBuildInputs = [ nvimWithAllGrammars ];
70 export HOME=$(mktemp -d)
71 ln -s ${withAllGrammars}/CONTRIBUTING.md .
73 nvim --headless "+luafile ${withAllGrammars}/scripts/check-queries.lua" | tee log
75 if grep -q Warning log; then
76 echo "Error: warnings were emitted by the check"
81 tree-sitter-queries-are-present-for-custom-grammars =
85 (grammar: grammarToPlugin grammar)
86 # true is here because there is `recurseForDerivations = true`
88 (lib.attrValues tree-sitter-grammars)
91 runCommand "nvim-treesitter-test-queries-are-present-for-custom-grammars"
94 function check_grammar {
95 EXPECTED_FILES="$2/parser/$1.so `ls $2/queries/$1/*.scm`"
98 echo expected files for $1:
101 # the derivation has only symlinks, and `find` doesn't count them as files
102 # so we cannot use `-type f`
103 for file in `find $2 -not -type d`; do
105 # see https://stackoverflow.com/a/8063284
106 if ! echo "$EXPECTED_FILES" | grep -wqF "$file"; then
107 echo $file is unexpected, exiting
116 (g: "check_grammar \"${g.grammarName}\" \"${g}\"")
122 no-queries-for-official-grammars =
125 # true is here because there is `recurseForDerivations = true`
127 (lib.attrValues vimPlugins.nvim-treesitter-parsers)
130 runCommand "nvim-treesitter-test-no-queries-for-official-grammars"
135 function check_grammar {
137 if [ -d $2/queries ]; then
138 echo Queries dir exists in $1
139 echo This is unexpected, see https://github.com/NixOS/nixpkgs/pull/344849#issuecomment-2381447839
147 (g: "check_grammar \"${g.grammarName}\" \"${g}\"")
154 meta = with lib; (super.nvim-treesitter.meta or { }) // {
155 license = licenses.asl20;
156 maintainers = with maintainers; [ figsoda ];
158 nvimRequireCheck = "nvim-treesitter";