Xojo Code Formatter - Written in XojoScript for IDE access

Howdy,

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" ) end if

Run the Format Code script from the IDE and it will then look like:

Dim i As Integer = 10 If i < 18 Then SayHello("Howdy", "World") End If

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.

https://github.com/jcowgar/xojo-format-code

Very cool. Thanks, Jeremy!

That’s awesome!!

Nice!

just try this… work very well…

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.

Good stuff Jeremy!

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.

Not going to keep updating the messages here, if you are interested in keeping up on the development of it, please follow the project on GitHub, https://github.com/jcowgar/xojo-format-code

“Emitter”. <-- Better name.

Works like a charm! Well done. All Xojo have to do is add the possibility to add a hotkey to a specific script…

Looks pretty good, but there are a few problems that I encounter.
This is my code before your script (shortened):

  Select Case caption 
  Case kSegmentSondervan 
    If Not Me.CurrentPage IsA wp_Sondervan Then 
      wp_Sondervan.Show 
    End If 
  Case kSegmentNefercon 
    If not me.CurrentPage IsA wp_Nefercon Then 
      wp_Nefercon.Show 
    End If 
  End Select

The same code after your script:

  Select Case caption 
  Case kSegmentSondervan 
    If Not Me.CurrentPage IsA wp _ Sondervan Then 
      wp _ Sondervan.Show 
      End If 
  Case kSegmentNefercon 
    If not me.CurrentPage IsA wp _ Nefercon Then 
      wp _ Nefercon.Show 
      End If 
  End Select
  1. My page names wp_Sondervan and wp_Nefercon are now wp _ Sondervan and wp _ Nefercon (a space before and after the _ (underscore))
  2. The If and End If are no longer ‘connected’ (see image)

very nice work!
This script could be seen as a workaround for <https://xojo.com/issue/3898>

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 <https://xojo.com/issue/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.

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=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.

[/quote]

@Paul, Working on it now.

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.

[quote=14021:@Tobias Bußmann]very nice work!
This script could be seen as a workaround for <https://xojo.com/issue/3898>
[/quote]

@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:

SayHello( "Hello") a=33

It could also be seen as a partial word arounds for:

<https://xojo.com/issue/27090> - Standardize Format should have a menu command
<https://xojo.com/issue/16202> - Add main menu item for “Standardize Format”
<https://xojo.com/issue/9844> - Standardize Format is only available after Text Selection
<https://xojo.com/issue/3898> - Standardize Format in IDE Script
<https://xojo.com/issue/2171> - Standardize Format only working for keywords

In almost all cases, though, I like Code Format better :slight_smile:

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.

Fantastic!

This is fixed. Do a git pull.

[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.
[/quote]

_ 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

[quote=14077:@Norman Palardy]_ 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[/quote]

Yup, I fixed that in v0.2.1 of the script. I treat _ as a line continuation if it is followed by a space, newline or comment. Just an oversight in the initial parsing code.

two little fixes:

avoid space before end of line:

After line 306 add the following:

ElseIf nextTok.Type = Token.Newline Then ' Do nothing

handle // comments:

add after the addition above also:

ElseIf Tok.Value = "/" And nextTok.Value = "/" Then ' Do nothing
or, depending on the desired behaviour for //////// comments :

ElseIf Tok.Value = "/" And nextTok.Value = "/" And lastTok.Value <> "/" Then ' Do nothing