Does a code formatter exist?

Not anything crazy - i.e. traversing the tree - but something in the vein of black or clang-format ?
Would be great to have all my code look the same, I find it much easier to understand thing when they’re consistently formatted.



I’d also like to have a code-formatting feature in the SuperCollider-IDE, but that wouldn’t be possible because SuperCollider supports a wide range of code styles:

SuperCollider lends itself to MANY different coding styles - that’s one of it’s biggest benefits, and biggest hazards.
(Some notes on writing SuperCollider code for non-programmers)

Yeah, I saw that, and I figured there’d be at least something for an opinionated format.
I futzed with clang-format and it seems that i’ll be able to make it work with a few rule tweaks.

The SuperCollider ide already auto-formats all code? In fact, it’s not possible to turn it off, and there are some non-standard style things it does, so it’s a little annoying :slight_smile:

oh haha I’ve never used the SCIde! I’ve always just used vim bc I’m so used to it.
Does anyone know what snippet is called when the file is saved ? I likely can use that instead of clang-format.

I’d be really interested in using something like that with vim but I haven’t been able to figure out yet how to adapt clang-format to do that for me - would clang-format be able to add ; at the end of lines?

Afaik, clang-formatter is only going to move your code around, not add additional sigils (except for when it creates a comment block).

@madskjeldgaard has added TreeSitter support for sclang - I think this could be used to do formatting in neovim…

I just had the opportunity to look at the tree-sitter today.
This looks exceptionally promising! Having well-formatted code in self-contained projects is almost a pre-requisite for building larger scale software and this looks like a good avenue to achieve that. (My attempts were clang-format were, to put it midly, unsuccessful.)

For some reason I though the IDE would do mild formatting for you, but I never use it so I may be mistaken. I was poking through the SC code and I couldn’t find where that might live so I may be mistaken. Does anyone know if this does exist and if so, what class those live in ?

If it exists it may be straightforward to modify it into treesitter friendly code. If not, it may be time to think about what a standard format should look like, not necessarily the standard format. (I understand there are many different coding styles, so a formatter should account for that, like clang-format does.)

The core indent logic is here:

This is pretty dependent on some QT data structures, and also assumes that the code is already tokenized, so it’s only iterating through tokens. Also, it’s worth noting that this indentation algorithm is not correct, and does a lot of incorrect indentation to sclang code sadly - but at least it’s a place to start!

I’ve ported the algorithm to sclang before (just to experiment with) - it’s not so complex if you don’t have to also do the tokenization (I’m assuming that treesitter will give you that for free).

FWIW, another approach would be to actually store the sclang AST as a data structure during parsing/compilation. This would have the benefit of re-using the existing sclang parser code rather than trying to recreate the parser yourself - this is a pretty straightforward C++ task, basically making PyrParseNode and it’s subclasses into something serializable (e.g. JSON). There’s code that ALMOST does this already (supercollider/DumpParseNode.cpp at develop · supercollider/supercollider · GitHub) - I haven’t looked closely at what it outputs, and there’s probably more work required to make this digestible, but it’s a good starting point.

Awesome! Thank you. I’ll take a read and see what I can get from this.