This started as a project to simply clean up code entered by me via voice dictation, but it has since turned into a full code formatter for Xojo. It installs as an IDE script and runs from the File > IDE Scripts menu. It takes badly formatted code and makes it beautiful doing case conversion on keywords as well as fixing whitespace. It has a few options to accommodate quite a few different code formatting styles. For example you can choose to pad parenthesis characters on the inside or outside, pad a comma, convert keyword case to TitleCase, lowercase or UPPERCASE, etc…
Here is an example using the default formatting (most common I believe), Your code before Format Code:
[code] dim apples AS INTEGER = 18
apples= 8+Abs(38 ) - 44
if apples> 8 then
MsgBox (“You have enough apples for the day”)
ELSE
MsgBox( “You should really buy more apples”)
end if[/code]
After running Format Code, the result is:
[code] Dim apples As Integer = 18
apples = 8 + Abs(38) - 44
If apples > 8 Then
MsgBox(“You have enough apples for the day”)
Else
MsgBox(“You should really buy more apples”)
End If[/code]
By changing various options at the top of the script, you can come up with:
[code] Dim apples As Integer = 18
apples = 8 + Abs ( 38 ) - 44
If apples > 8 Then
MsgBox ( “You have enough apples for the day” )
Else
MsgBox ( “You should really buy more apples” )
End If[/code]
Or even:
[code] DIM apples AS INTEGER=18
apples=8+Abs(38)-44
IF apples>8 THEN
MsgBox(“You have enough apples for the day”)
ELSE
MsgBox(“You should really buy more apples”)
END IF[/code]
if you wish.
Please see Format Code on GitHub page for more complete docs and Format Code Releases on GitHub for .zip and .tar.gz downloads.
Please be careful as this script does alter your code. You can always use Cmd/Ctrl+Z to undo its changes (in one fell swoop) but you must know where it fails and test early. It is working great for me, but with the 1.0.0b1 release, there has been many options added. So, please use it and test your code right after each format to find problems and not loose any code you may have changed after a bad format.
I am looking for feedback in the form of bug reports, feature requests and even simple it works great messages (hopefully only these kind :-D) The amount of feedback received will of course dictate how much effort I put into this for others. No feedback, I’ll just make things simple and use it internally, a lot of feedback, bug reports and new test cases? I’ll keep maintaining it for everyone.
If you do discover a bug, it would be great if you would install the optional Format Code - Test.xojo_script
file and add a test case for the failing condition. Better yet, clone format-code
on github, add the unit test (and bug fix if you see the error) and do a pull request!
Thanks!