Tree-sitter support for SuperCollider

in looking around at some other grammar files in Atom - for instance crucialfelix’s SuperCollider for Atom implementation, it seems that these headers and naming conventions are the way their other system works… so maybe what they’re calling integration or compatibility is actually closer to conversion.

hmm, I just found this: Guide to writing your first Tree-sitter grammar · GitHub - it has a section on setting up a tree-sitter language in Atom. I will explore it.

According to that guide, while it will be necessary to have a grammars folder, and tree-sitter-supercollider.cson file, that file, and the packages.json file pretty much will work to link to the originally installed tree-sitter language elsewhere. So updating the tree-sitter files shouldn’t require doing all of these steps again.

These instructions are for how to graft an existing language into an existing package in Atom, while the document that Sam linked to seems like it’s meant more for how to build a package around the tree-sitter stuff.

Will report back!

I poked at this for an hour today. Since the documentation online is basically non-existent, I think it must be so obvious and simple that it doesn’t even need to be explained. I tried to install Atom NPM and then run “npm i tree-sitter-supercollider” to install Mad’s grammar, but that didn’t work.

No luck for me either. The guide I linked above didn’t work. It didn’t break anything, but the grammar dependency was still on the default supercollider.cson file, not the new one I made tree-sitter-supercollider.cson, which according to that guide was supposed to supersede any other conflicting grammars.

There is a subtle and unexplained line in that guide that says after setting up all these files, run npm install. Navigating to the directory of crucialfelix’s SC package in the terminal - where package.json is saved - and running npm install did an initial error that Atom threw when I restarted it after making the dependency addition and creating that other cson file. But it still doesn’t recognize tree-sitter.

I’m also bummed that there’s nearly no practical documentation, but I dunno, I think obvious should mean that it works right away.

I may try my luck at getting this to work in vim.

Are these other editors really that much faster and better than scide? It seems pretty good to me, and ‘it just works’.

Well I was able to finally install neovim, scnvim (i think…), treesitter, and tree-sitter-for-supercollider!

Now I need to figure out how to actually use vim…

1 Like

After using treesitter syntax highlighting in neovim I couldn’t live without it! Updating just now it was broken (I had to rebuild nvim from source) and I realized how much I depend on it!

so i felt a bit bad for getting frustrated at not being able to get treesitter to work with atom yesterday, so i figured i’d try to take another look today on a fresher head.

i still couldn’t get it working, however, this may be more about not correctly instantiating a package for atom - as i don’t actually know how to do that, and was hoping to just brute force it by making a folder and then running npm install in the directory when the files were ready. i’m also pretty sure there are some mistakes in my code, and maybe a few things left out, but i think i sort of understand the workflow.

it seems like a very manual process though: /queries/highlights.scm and /queries/folds.scm need to get translated to the cson file. this might only matter the first time, unless there are large rewrites or modification to some of the syntax classification. maybe there’s a simpler way to go about it as well, but it’s a bit more than pure syntax conversion.

anyway, my findings:

basically, a directory needs to get made, this is the atom package:

supercollider-mysupercolllider // (package name)
├── LICENSE
├── README.md
├── grammars
│   └── tree-sitter-supercollider.cson
└── package.json

tree-sitter-supercollider.cson is where the magic really needs to happen. there’s a header that’s added that helps associate the language source etc, and then, yeah the rest of that file needs the same content found in folds.scm and highlights.scm. those are written in the folds: section and scopes: section in the cson file, respectively.

scopes is the heavier lift, and in rewriting it, basically each class that Mads called in highlights, gets associated with syntax classes in atom.

part of the trouble was i couldn’t actually find documentation for what the syntax classes are. this is likely my ignorance, but aside from the examples on that atom grammars page, i had to go hunting through various other languages, comparing their tree-sitter language repositories to their atom grammar repositories to try to find the pattern, and i pretty much had to take my best guesses at which classes to associate, and/or the order of association.

anyway, like i said, i couldn’t actually get this working - not sure why. i couldn’t decipher the error that atom threw when trying to parse the cson file.

if anyone wants to take a stab at it, i’m happy to share the file i made. …i need to figure out how to actually use github for sharing and versioning… once i do that, i may also paste it there if anyone would find it useful :slight_smile: til then i can just paste the text in a message or upload the file somewhere else and share a link.

I just pushed another batch of updates that now offer support for most of the stufff you need for writing classes.

With this, it is now down to bugs (see issues) and the most esoteric parts of the language (see todo list) and then it’s done.

I’ve experienced myself that the grammar now is precise enough to catch syntax errors in code before I evaluate it, which I think is pretty cool!

2 Likes

Thanks Mads! is the right way to update without breaking anything in NeoVim :TSUpdate supercollider ?

I have also noticed this error catching behavior which is fantastic

Yes it is but it won’t be included until the next time nvim tree sitter is updated (that’s their update procedure). Every time they update, the latest commit is pulled and put in the lock file, so keep an eye on that one. Hhtat daid they update daily. Best

Fantastic work, I just began to use Neovim a couple weeks ago…quite the timing!

Unfortunately, after installing it using TSUpdate supercollider no highlighted syntax shows up when I open an ‘.scd’ file. I even did a :checkhealth and found that the supercollider parser is present and installed correctly. Any tips?

did you enable TS highlighting in your init.vim file?

lua <<EOF
require'nvim-treesitter.configs'.setup {
  highlight = {
    enable = true,
    custom_captures = {
      -- Highlight the @foo.bar capture group with the "Identifier" highlight group.
      ["foo.bar"] = "Identifier",
    },
  },
}
EOF
1 Like

FYI: nvim-treesitter has not updated it’s supercollider parser for a while (and thus does not contain all of the work done for supporting class writing etc) because of a strange bug. I’m working on it though

1 Like

Thank you! I will follow this thread

Should be fixed now and also include the latest stuff. Update your treesitter plugin (if using nvim) and then don’t forget to :TSUpdate supercollider

4 Likes

HELP WANTED

Hello everyone. I really need some help in maintaining the tree-sitter grammar. Most of the language is implemented but there are constantly issues to resolve and it’s honestly too big of a project to do on my own. Please help out if you can.

4 Likes

The tree-sitter grammar has been updated significantly recently. In fact, all of the language has been implemented now (to my knowledge) except for two (tricky) things: List comprehensions and method names as binary operators.

nvim-treesitter should be updated soon to implement all of the new bug fixes and changes. Also, there’s a changelog now!

Playground
NeoVim users who are curious how your supercollider code is parsed by TS may use the playground plugin. The plugin has a bug for SC( see https://github.com/nvim-treesitter/playground/issues/55) , but I’ve made a fork that works for SuperCollider here:

2 Likes

Here is a live demo where you can see the code being parsed live. The tree-sitter tree is seen on the left and the SuperCollider code on the right.

playground2

2 Likes

Super Mads! You are a hero :slight_smile:
Eirik

Amazing work as usual!! I finally have some time to set up a proper vim environment for SC, looking forward to testing out SCNVim and your plugins