koka icon indicating copy to clipboard operation
koka copied to clipboard

Code Formatter

Open TimWhiting opened this issue 3 months ago • 4 comments

An autoformatter would be good to have, to keep style consistent (at least within projects).

Koka touts flexibility in syntax. However, despite the flexibility, it is remarkably consistent, and makes it easy to stay consistent.

Obviously there are points where braces are either required (multiple trailing lambdas), or just cleaner (suspenders for creating unit functions). Both of these situations for example happen to occur in the while function.

So there would need to be some rules obviously - you cannot translate all braces to indentation, while you can go the other way.

Additionally, I don't think we should get rid of any with statements that the user introduces, while we could automatically add with statements if the rest of the body is under the lambda.

The biggest things that a formatter would be helpful for though is to

  • Reformat code to a consistent indentation (2 spaces or 4 spaces - which could then allow for tabs - since the formatter could guarantee that there wouldn't be a mix of spaces and tabs: https://github.com/koka-lang/koka/issues/516).
  • Reformat newlines between functions and at the end of the file
  • Reformat parameter and argument lists to wrap at certain lengths, and indent nicely at that point.

I'm of the opinion at this point that the autoformatter should be written in Koka itself, in order to avoid adding more to the Haskell implementation which still struggles sometimes with memory usage due to laziness.

TimWhiting avatar May 17 '24 16:05 TimWhiting