top of page
Search

Code formatting

  • Writer: Ivaylo Fiziev
    Ivaylo Fiziev
  • 1 day ago
  • 2 min read

Code formatting is a difficult task. It should support incomplete or partial code.

This means that you cannot rely on the language parser. Instead, you rely on the lexer. The lexer provides you with all the recognized tokens. Then you create an algorithm that beautifies the code by analyzing the tokens.

To beautify the code you:


1.      Fix indentation (nested statements should be indented properly)

2.      Fix spacing (binary operators should be separated from the operands)

3.      Fix line breaks (every statement should start on a new line)

4.      Preserve the comments (they are handled with care since they are intentionally put on a different channel when recognizing the code)


This is the bare minimum.

Eventually these corrections make the code easier to read and debug.

Also, it unifies the style across different authors.


Despite being simple to define the algorithm is not easy to do. There are many corner cases that should be covered since each statement is different and statements can be combined in multiple ways. For quite some time the idea of code formatting (in the context of SCL) was chasing me but I was faster ... :)

Recently I tried to solve this problem with the help of AI ... in particular the GPT 5.4 model ... and it worked quite well. I had some initial implementation. It used to work only in simple cases. The AI could understand and build on top of my logic using my own coding style. I didn't expect it but it solved many of the corner cases that I was struggling with. Despite the complexity prompting was all I did. Eventually code formatting is still not perfect but works nicely in most cases.

Now you can format the SCL code with a popup menu or a hotkey.


And as with everything we do - we would like to collect customer feedback first. Then we can probably enforce it by formatting the code as you type or when you save your changes. Maybe make this an option. Maybe allow formatting only the selected code ... remember - it works on partial code.


Version 2606 will have this feature!


Needless to say the GPT 5.4 model is amazing. It excels at complex coding and structured data analysis. Produces production-level code.

Most importantly - I can trust it!


 
 
 

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating

Subscribe Form

Thanks for submitting!

bottom of page