Disappointed not being able to access Standardize Format from the menu, and the fact that it doesn’t clean up quite a bit, I decided to write a code formatter for Xojo in XojoScript so that it could be called from within the IDE. I asked in another thread what others do with XojoScript and IDE Scripting hoping that someone already has done a project like this, but no response as such. Thus, I quickly hacked this together in literally an hour. I am going to be refining it as time goes on but I am really hoping that this will become a community project and that many of you will contribute to it, bug reports, bug fixes and new features! It is already very helpful to me. An example:
If your code looks like:
DIM i as Integer= 10
iF i<18 then
SayHello ("Howdy" , "World" )
Run the Format Code script from the IDE and it will then look like:
Dim i As Integer = 10
If i < 18 Then
Notice how it not only does capitalization like Standardize Format, but it also cleans up whitespace and adds whitespace where it should be.
I have created a GitHub repository for the project. Please try it out, report bugs and more importantly submit bug fixes and new features. As I said, I use this already and will be enhancing it as time goes on but I really hope you will join in too.
If you run into a bug and it does something wrong, don’t try to fix your code, just press Ctrl/Cmd+Z to undo… It’ll revert your code back to what it looked like before the formatter did its thing. I’m not yet finding any place where it is messing up, but just a FYI. Also, it formats just the current editor, so it’s not going to do major damage, especially with Undo. Pretty safe to give it a go.
OK, I’ve largely rewritten it to not be so much of a hack. It now has two main classes: Tokenizer, which simply parses Xojo Code into a stream of tokens. It could be used in other projects as well, and StringWriter: (not happy with the name) which simply takes an array of tokens and writes them to a string. It also now has a Main() method which then integrates the classes into Xojo IDE. Better documentation, easier hacking, a few bugs fixed and it now operates on a selection, if given, or the entire method if no selection.
Looks pretty good, but there are a few problems that I encounter.
This is my code before your script (shortened):
Select Case caption
If Not Me.CurrentPage IsA wp_Sondervan Then
If not me.CurrentPage IsA wp_Nefercon Then
The same code after your script:
Select Case caption
If Not Me.CurrentPage IsA wp _ Sondervan Then
wp _ Sondervan.Show
If not me.CurrentPage IsA wp _ Nefercon Then
wp _ Nefercon.Show
My page names wp_Sondervan and wp_Nefercon are now wp _ Sondervan and wp _ Nefercon (a space before and after the _ (underscore))
The If and End If are no longer ‘connected’ (see image)
However what I’d love to see is to change names of identifiers to the capitalization of it’s declaration. This however requires a much deeper parsing of the semantics of the code. See feedback://showreport?report_id=2171
And of course having the possibility to run this script over more then just the current method, say the full project.
Hope to find the time for some deeper tests and contributions soon.
[quote=14035:@Heiko Hellmer]Holy cow, this is a great contribution! Thanks Jeremy!
Paul, I’ve seen this while just copy/paste an If/End If construct in the editor. This might be a Xojo bug. Workaround: Delete the last ‘f’ in ‘End If’ and retype it.[/quote]
@Heiko, It is due to the _ being a continuation line entry in Xojo, i.e. the next line will be indented some… Thus due to it separating wp_abc to wp _ abc, it is getting many indent requests and only a few outdent requests.
[quote=14005:@Paul Sondervan]Looks pretty good, but there are a few problems that I encounter.
Ah, yes, I see. But anyway, I had sometimes indentation errors by just copy/paste an If/End If construct (with no underscores in it), which has nothing to do with your script. Thanks for clarification.
@Tobias, Thanks! I actually see it as a replacement for the internal Standardize Format, at least until it does quite a bit more than just change case of keywords. I do coding by voice quite a bit and thus extra spaces are inserted here and there between keywords, parens, etc… Standardize Format helps with these types of entry errors but ignores many others, such as:
It could also be seen as a partial word arounds for:
In almost all cases, though, I like Code Format better
When I was creating this I was debating between an external program that could be run via the command line. It could easily learn to parse .xojo_code files. However, Xojo does not yet reload files that have been modified externally and from what I can see, in the IDE Scripting, there are not methods given to traverse an entire project. I do see ProjectItem, SelectProjectItem, and Sublocations which may give us the ability to traverse the current module/class/window, but I have not played with those commands.
[quote=14050:@Jeremy Cowgar]@Heiko, It is due to the _ being a continuation line entry in Xojo, i.e. the next line will be indented some… Thus due to it separating wp_abc to wp _ abc, it is getting many indent requests and only a few outdent requests.
_ is a line continuation IF and only if it appears at the end of the line (only followed by whitespace or a comment
Any other usage should not be altered