Skip to content

tree-sitter: add schema to grammar definition, support multi-grammar repos#200028

Closed
Profpatsch wants to merge 14 commits intoNixOS:masterfrom
Profpatsch:tree-sitter-grammar-json-add-location-support
Closed

tree-sitter: add schema to grammar definition, support multi-grammar repos#200028
Profpatsch wants to merge 14 commits intoNixOS:masterfrom
Profpatsch:tree-sitter-grammar-json-add-location-support

Conversation

@Profpatsch
Copy link
Member

Description of changes

based on #199566

WIP

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 22.11 Release Notes (or backporting 22.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
    • (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • Fits CONTRIBUTING.md.

The allGrammars had to be patched at both use-sites, so let’s pull
that into the original definition.

Adds a few more docstrings.
`repo` and `orga` are not required to print the `default.nix`. This
will be relevant because `gitlab` is going to be added, which does not
have these two fields.
We want to be able to download tree-sitter grammars from other
platforms. The first, easiest target is gitlab.
Gitlab uses a `projectId` instead of orga/repo though, so we need to
implement it differently (also checking the latest version of a repo
works differently).
This uses the gitlab API to download the jsonc grammar.

Currently the gitlab support only allows for gitlab.com, but that
could be changed quite easily.

I opted for copying a lot of the github code, since it’s too early to
factor out shared code.
The `language` attr is only used for the grammar name, changing it
makes no sense at all, and makes the output names irregular.

I read something about nvim inferring the language from the filepath
or something, I seriously hope that’s not A Thing, and if it is, we
should find a different way of solving that.

The tree-sitter integration is not specific to nvim, so it shouldn’t
have nvim hacks.
Some of these repos define multiple grammars, so we need to be able to
specify them, in order to remove the hack in the default.nix
The toml quickly got out of hand when I wanted to add more fields to
the list of known grammars, since then you need something like

```
[[knownTreeSitterOrgGrammarRepos.foo]]
repo = "foo"
[[knownTreeSitterOrgGrammarRepos.foo.grammars]]
name = "foo-grammar-bar"
location = "bar"
```

Just to express

```
{ knownTreeSitterOrgGrammarRepos = [
  { foo = {
      repo = "foo"
      grammars = [
        { name = "foo-grammar-bar"
          location = "bar"
        }
      ]
    }
  }
]}
```

Which translates to a *very* verbose config file.

Instead, let’s convert to json. Of course that means no comments are
possible, but there is a good solution that every modern editor
supports: json schema.
This also gives auto-completion and documentation for free.
Looks like this is just an elaborate linkFarm command.
This argument was extremely underspecified and hacked into the rest of
the code. I’d rather remove it for now and re-do support later if
necessary.

Ideally we’d expose the building blocks in a defined & documented
fashion.
@github-actions github-actions bot added the 6.topic: vim Advanced text editor label Nov 7, 2022
@ofborg ofborg bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Nov 7, 2022
@wegank wegank added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Mar 19, 2024
@uncenter
Copy link
Member

I think now that grammar sources and updating has been reworked in #408414, this can be closed?

@uncenter uncenter closed this Dec 17, 2025
@uncenter uncenter added the 6.topic: tree-sitter Tree-sitter is a parser generator tool and an incremental parsing library. label Dec 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.status: merge conflict This PR has merge conflicts with the target branch 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 6.topic: tree-sitter Tree-sitter is a parser generator tool and an incremental parsing library. 6.topic: vim Advanced text editor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants