Announcing tree-sitter-nix

I wrote a tree-sitter grammar for Nix: GitHub - nix-community/tree-sitter-nix: Nix grammar for tree-sitter [maintainer=@cstrahan]

Down the road, we could do some fun stuff, like provide syntax highlighting in nix repl (Tree Sitter tries its best to come up with a sensible parse tree, even if there are errors).

19 Likes

I was thinking about it for a long time, is there a plan to have it included in Atom? Maybe including it in @wmertens extnsion?

I would love to use this instead of my Nix grammar. The Nix grammar can’t express the full language correctly because it’s only regular expressions.
However, that would mean having tree-sitter support in editors, something that doesn’t exist yet it seems :frowning:

However, that would mean having tree-sitter support in editors, something that doesn’t exist yet it seems :frowning:

Atom has support, but I don’t know about any other editors. There’s some ongoing work to support tree-sitter in Neovim, though.

I was thinking about it for a long time, is there a plan to have it included in Atom?

Yeah, that’s something I’d like to do eventually.

I’ll note that I’ve pushed some changes that add a pretty decent test suite, fixes a couple bugs, and overhauls the shape of the resulting parse tree (it was pretty verbose before, but now it’s very clean).

I think at this point it should be very robust, and I don’t expect much churn. I’d love for some people to play around with it and report back. If no one can find some improvements / faults, I’ll cut a 1.0.0 release.

Does it support escaping of dollar in strings ā€˜ā€™$ ?

Nice catch. I just pushed a fix for that in the custom scanner (I’d link to the commit, but it looks like we can’t link to github on this setup of Discourse :man_shrugging:).

1 Like

:thinking: just ftr

logged out
on the first post on this thread i see

This post was flagged by the community and is temporarily hidden.
View hidden content.

logged in
i see

This is the first time cstrahan has posted — let’s welcome them to our community!

and

This post was flagged by the community and is temporarily hidden.
View hidden content.

/ftr

:confused:

Oh yeah. That’s kinda weird. I saw that my first post, and a subsequent post here, are showing up as slightly grayed out. Bizarre.

I also saw these strange things… on another post too. Something fishy with discourse.

Discourse just plainly hates URLs. It flags posts of new users with them, it loses them in the plain-text parts of emails…

I started putting together a formatter using this grammar here: GitHub - justinwoo/format-nix: A simple formatter for Nix using tree-sitter-nix.

It’s a simple PureScript Node application using the node tree-sitter bindings.

6 Likes

That’s awesome! Glad to see the grammar is already proving useful.

Thanks for your contributions and feedback, btw.

1 Like

Hey, while looking at the status of language server implementations for nix, and stumbling upon this project, I think it’d be worth mentioning for reference GitHub - bash-lsp/bash-language-server: A language server for Bash which implements a very fast and rather performent language server for bash using a tree-sitter grammer as well. I assume the same could be leveraged for a language server implementation for nix as well.

Current LSP alternatives:

I’ll also note that using a nodejs based language server is easier for Editors to include. Atom’s and vscode’s configuration ecosystems are based on Javascript AFAIK and one of Vim’s popular LSP client plugins is GitHub - neoclide/coc.nvim: Nodejs extension host for vim & neovim, load extensions like VSCode and host language servers. which uses a configuration system very much like VSCode’s. For example it’s sh extension is easily installed with just :CocInstall coc-sh.

2 Likes

I am attempting to package (at least for myself) the neovim version that supports tree-sitter (see Tree-sitter step 1: vendor runtime lib + add lua API by bfredl Ā· Pull Request #10124 Ā· neovim/neovim Ā· GitHub and [WIP] Tree-sitter integration by bfredl Ā· Pull Request #9219 Ā· neovim/neovim Ā· GitHub).
There seems to be a package on npmjs but as GitHub - tree-sitter/tree-sitter: An incremental parsing system for programming tools looked cargo-based, I was expecting a rust package. I also would need GitHub - tree-sitter/tree-sitter-c: C grammar for tree-sitter . I wonder if you had packaged any of this for your tests ? or tell me which one I should use in nixpkgs.

2 Likes

I would like to load your grammar into neovim. I’ve started following Guide to writing your first Tree-sitter grammar Ā· GitHub and trial by error led me to this command line nix run nixpkgs.tree-sitter nixpkgs:nodePackages.node-gyp nixpkgs:python27 but then it doesn’t find /bin/sh . Do you have some recipe on how to generate a module onx nixos from your grammar ? the CI configuration was not very information-heavy either xD

2 Likes

related

2 Likes

Do you know how can we use tree-sitter-nix with syntax-highlighter?

something like add tree-sitter-nix Ā· milahu/syntax-highlighter@617abbf Ā· GitHub
the ā€œgrammarā€ file maps node types to token categories
todo: update package-lock.json, use npmlock2nix or nix-npm-buildpackage,
use tree-sitter from nixpkgs instead of tree-sitter-cli from npm

Assume you know GitHub - nix-community/rnix-lsp: WIP Language Server for Nix! [maintainer=@aaronjanse] but just for others who don’t know it to check out :grin: