treesitter

URL: https://github.com/nvim-treesitter/nvim-treesitter/

Maintainers: Austin Horstman


Provides an interface to tree-sitter

note

This plugin defaults to all functionality disabled.

Please explicitly enable the features you would like to use in plugins.treesitter.settings. For example, to enable syntax highlighting use the plugins.treesitter.settings.highlight.enable option.

Installing tree-sitter grammars from Nixpkgs

By default, all available grammars packaged in the nvim-treesitter package are installed.

If you'd like more control, you could instead specify which packages to install. For example:

  plugins.treesitter = {
    enable = true;

    grammarPackages = with pkgs.vimPlugins.nvim-treesitter.builtGrammars; [
      bash
      json
      lua
      make
      markdown
      nix
      regex
      toml
      vim
      vimdoc
      xml
      yaml
    ];
  };

Installing tree-sitter grammars from nvim-treesitter

The default behavior is not to install any grammars through the plugin. We usually recommend installing grammars through nixpkgs instead (see above).

If you'd like to install a grammar through nvim-treesitter, you can run :TSInstall <grammar> within vim or use the plugins.treesitter.settings.ensure_installed option to specify grammars you want the plugin to fetch and install.

  plugins.treesitter = {
    enable = true;

    settings = {
      # NOTE: You can set whether `nvim-treesitter` should automatically install the grammars.
      auto_install = false;
      ensure_installed = [
        "git_config"
        "git_rebase"
        "gitattributes"
        "gitcommit"
        "gitignore"
      ];
    };
  };

NOTE: You can combine the functionality of plugins.treesitter.nixGrammars and plugins.treesitter.settings.ensure_installed. This may be useful if a grammar isn't available from nixpkgs or you prefer to have specific grammars managed by nvim-treesitter.

Installing Your Own Grammars with Nixvim

The grammars you want will usually be included in nixGrammars by default. But, in the rare case it isn't, you can build your own and use it with Nixvim like so:

{ pkgs, ... }:
let
  # Example of building your own grammar
  treesitter-nu-grammar = pkgs.tree-sitter.buildGrammar {
    language = "nu";
    version = "0.0.0+rev=0bb9a60";
    src = pkgs.fetchFromGitHub {
      owner = "nushell";
      repo = "tree-sitter-nu";
      rev = "0bb9a602d9bc94b66fab96ce51d46a5a227ab76c";
      hash = "sha256-A5GiOpITOv3H0wytCv6t43buQ8IzxEXrk3gTlOrO0K0=";
    };
    meta.homepage = "https://github.com/nushell/tree-sitter-nu";
  };

  # or you can yoink any grammars in tree-sitter.grammars.${grammar-name}
  # treesitter-nu-grammar = pkgs.tree-sitter-grammars.tree-sitter-nu;
in
{

  programs.nixvim.plugins = {
    treesitter = {
      enable = true;
      settings.indent.enable = true;
      grammarPackages = pkgs.vimPlugins.nvim-treesitter.passthru.allGrammars ++ [
        treesitter-nu-grammar
      ];
    };

    extraConfigLua =
      ''
        do
          local parser_config = require("nvim-treesitter.parsers").get_parser_configs()
          -- change the following as needed
          parser_config.nu = {
            install_info = {
              url = "${treesitter-nu-grammar}", -- local path or git repo
              files = {"src/parser.c"}, -- note that some parsers also require src/scanner.c or src/scanner.cc
              -- optional entries:
              --  branch = "main", -- default branch in case of git repo if different from master
              -- generate_requires_npm = false, -- if stand-alone parser without npm dependencies
              -- requires_generate_from_grammar = false, -- if folder contains pre-generated src/parser.c
            },
            filetype = "nu", -- if filetype does not match the parser name
          }
        end
      '';

    # Add as extra plugins so that their `queries/{language}/*.scm` get
    # installed and can be picked up by `tree-sitter`
    extraPlugins = [
      treesitter-nu-grammar
    ];

  };

}

The queries for the grammar should be added to one of the runtime directories under queries/{language} but sometimes plugins do not conform to this structure.

In such cases, you can override the source derivation (or the grammar derivation) to move the queries to the appropriate folder:

(
  (pkgs.fetchFromGitLab {
    owner = "joncoole";
    repo = "tree-sitter-nginx";
    rev = "b4b61db443602b69410ab469c122c01b1e685aa0";
    hash = "sha256-Sa7audtwH8EgrHJ5XIUKTdveZU2pDPoUq70InQ6qcKA=";
  }).overrideAttrs
  (drv: {
    fixupPhase = ''
      mkdir -p $out/queries/nginx
      mv $out/queries/*.scm $out/queries/nginx/
    '';
  })
)

Verify if the queries were picked up by running :TSModuleInfo.

plugins.treesitter.enable

Whether to enable nvim-treesitter.

Type: boolean

Default: false

Example: true

Declared by:

plugins.treesitter.package

The nvim-treesitter package to use.

Type: package

Default: pkgs.vimPlugins.nvim-treesitter

Declared by:

plugins.treesitter.folding

Whether to enable tree-sitter based folding.

Type: boolean

Default: false

Example: true

Declared by:

plugins.treesitter.gccPackage

The gcc package to use. This is required to build grammars if you are not using nixGrammars.

Type: null or package

Default: pkgs.gcc

Example: pkgs.gcc14

Declared by:

plugins.treesitter.grammarPackages

Grammar packages to install

Type: list of package

Default: config.plugins.treesitter.package.passthru.allGrammars

Example: pkgs.vimPlugins.nvim-treesitter.passthru.allGrammars

Declared by:

plugins.treesitter.languageRegister

This is a wrapping of the vim.treesitter.language.register function.

Register specific parsers to one or several filetypes.

The keys are the parser names and the values are either one or several filetypes.

Type: attribute set of ((list of string) or string convertible to it)

Default: { }

Example:

{
  cpp = "onelab";
  python = [
    "foo"
    "bar"
  ];
}

Declared by:

plugins.treesitter.nixGrammars

Whether to install grammars defined in grammarPackages.

Type: boolean

Default: true

Example: false

Declared by:

plugins.treesitter.nixvimInjections

Whether to enable Nixvim injections, e.g. highlighting extraConfigLua as lua.

Type: boolean

Default: true

Example: false

Declared by:

plugins.treesitter.nodejsPackage

The nodejs package to use. This is required to build grammars if you are not using nixGrammars.

Type: null or package

Default: pkgs.nodejs

Example: pkgs.nodejs_22

Declared by:

plugins.treesitter.treesitterPackage

The tree-sitter package to use. This is required to build grammars if you are not using nixGrammars.

Type: null or package

Default: pkgs.tree-sitter

Declared by: