adventuregamestudio / ags

AGS editor and engine source code
Other
704 stars 160 forks source link

Script editor can auto-format code #2016

Open edmundito opened 1 year ago

edmundito commented 1 year ago

Describe the problem Formatting code is tedious, and many developers want to avoid it. There have been many improvements to code formatting in other editors and other languages, it would be nice to have the same in the scripting.

Suggested change An checkable option to automatically format the code when saving. It would format the code in the most readable way possible. Because there's different philosophies on where brackets go, that should probably be another option as well.

ericoporto commented 1 year ago

I have taken a look at this, if this was just figuring it out the indentations, it's possible to parse and use the fold marks as a way to figuring it out, and go from line 1 to the last and adjust to put the "insides" of the folds with an upper level of indentation from the parent, and this can be done entirely in Scintilla commands. Let's call strategy A.

But, there is no guarantee the start of a block is right in the next line of something that requires an open block. This would mean either adding or removing lines at these places. After this is done, if there is a way, a second pass with strategy A works. Let's call this the strategy BA.

Now the other approach I can think is to parse the code and store the contents in some simplified AST, that doesn't contain the details, begin an undo action in Scintilla, clean all lines and print from the AST with the right format and the end the undo action. This looks more certain to work. Let's call this strategy C.

I think strategy A is similar to what we have while typing so I get that is something that can maybe be always on. C looks easier to write tests.

ivan-mogilko commented 1 year ago

"Format the code in the most readable way possible" does not sound like a well defined feature. Everybody will have their own opinion on what it is.

I honestly think that we should aim towards finding a library or some kind of an advanced text editor control that allows to configure the autoformat settings in accordance to contemporary standards and format code at real-time, and not have to write or maintain this ourselves.

ericoporto commented 1 year ago

I looked around and I couldn't find something really good for this yet

I plan to look more into Uncrustify and see if it's there any strategy to add a new language there.