Tree-sitter neovim grammar plugins don't forward queries

The logic in tree-sitter.buildGrammar preserves any queries included in the top level of the grammar repo, but neovim’s grammarToPlugin just takes the parser dylib ignoring any query files, which means that any grammars added manually with these helpers won’t give highlights/folds/etc even though :checkhealth reports that you have a valid grammar loaded.

I’ve gotten it to work temporarily by literally symlinking the queries into my neovim runpath: /nix/store/*-tree-sitter-foo-grammar/queries/highlights.scm -> ~/.config/nvim/queries, which is obviously undesirable. I can sidechannel the queries into the runtime dir with something like:

let numbat-grammar = pkgs.tree-sitter.buildGrammar {
    version = "2024-03-19"; language = "numbat";
    src = pkgs.fetchFromGitHub {
      owner = "irevoire"; repo = "tree-sitter-numbat";
      rev = "483aeccc940367073281055499907026a70d9880";
      hash = "sha256-UTKI1tAstM202/ef9O3ipP5ogR3/Gyi19wYrtwQWimI=";
    };
  };
  typst-grammar = pkgs.tree-sitter-grammars.tree-sitter-typst;
in {
  programs.neovim.config = {
    plugins = with pkgs.vimPlugins; [
    ...
      (nvim-treesitter.withPlugins (p: with p; [ rust markdown ... ] ++ [
        typst-grammar numbat-grammar
      ]))
    ];
  ...
  };
  xdg.configFile = {
    "nvim/queries/numbat".source = "${numbat-grammar}/queries";
    "nvim/queries/typst".source = "${pkgs.tree-sitter-grammars.tree-sitter-typst}/queries";
  };
}

but I assume there’s a better way to do that or register them with nvim-treesitter. I haven’t managed to find any relevant docs or examples of people doing so this in the wild, but I know other people are using custom tree-sitter grammars with neovim and nix.

I’m hoping @figsoda or someone else more familiar with the nix + neovim + tree-sitter configuration has already solved this and I’m just looking in the wrong places. I also wonder if updating grammarToPlugin to automatically add any queries to the rtp would be desirable?

1 Like