# Format Code - Xojo Script to nicely format your source

**URL:** <https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823>\
**Category:** Extras\
**Created:** [July 25, 2013, 12:51pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823 "2013-07-25T12:51:57Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [July 25, 2013, 12:51pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/1 "2013-07-25T12:51:57Z")

</div>

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](https://github.com/jcowgar/xojo-format-code) page for more complete docs and [Format Code Releases on GitHub](https://github.com/jcowgar/xojo-format-code/releases) 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!

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [July 25, 2013, 1:07pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/2 "2013-07-25T13:07:37Z")

</div>

> [@22477:@Jeremy Cowgar](#):
>
> Please be careful as this script does alter your code.

Just to _Bold_ this part.

---

<div class="post-metadata">

**Author:** ![Massimo\_Valle](https://forum.xojo.com/user_avatar/forum.xojo.com/massimo_valle/32/23049_2.png) [@Massimo\_Valle](https://forum.xojo.com/u/Massimo_Valle)\
**Post date:** [July 25, 2013, 1:54pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/3 "2013-07-25T13:54:45Z")

</div>

Very nice, Jeremy.  
Do you prefer feedback by email or commenting this topic (so others can comment)?

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [July 25, 2013, 1:56pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/4 "2013-07-25T13:56:52Z")

</div>

Doesn’t matter, if the information is pertinent to others, forum may be better, but don’t want to clutter it up with on going support as that’s not its purpose. Your call. If you have a github account, bug reports can be added there if that is the feedback, otherwise I’ll take them any way I can get them.

---

<div class="post-metadata">

**Author:** ![Massimo\_Valle](https://forum.xojo.com/user_avatar/forum.xojo.com/massimo_valle/32/23049_2.png) [@Massimo\_Valle](https://forum.xojo.com/u/Massimo_Valle)\
**Post date:** [July 25, 2013, 2:06pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/5 "2013-07-25T14:06:12Z")

</div>

Ok, As I said this could be very welcomed to me, nice work. I will test more but for now a couple of feature requests and a missing thing.

1. About capitalization I’d like to have also a semi-titleCase option. So then `Myclass` will become `myClass`

2. Self, Me are always capitalized, while I’d like to have them following the capitalization preferences. I can change the code myself, but perhaps someone else could prefer this.

3. You handle Self, Me, but not Super.

Keep up the good work! 😃

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [July 25, 2013, 2:42pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/6 "2013-07-25T14:42:55Z")

</div>

I wi take a look at this too, but I wonder if a companion module with constants would be a better place to keep the preferences for the script? That way, one wouldn’t have to change their prefs back with every bug fix.

---

<div class="post-metadata">

**Author:** ![Massimo\_Valle](https://forum.xojo.com/user_avatar/forum.xojo.com/massimo_valle/32/23049_2.png) [@Massimo\_Valle](https://forum.xojo.com/u/Massimo_Valle)\
**Post date:** [July 25, 2013, 2:52pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/7 "2013-07-25T14:52:03Z")

</div>

> [@22505:@Kem Tekinay](#):
>
> I wi take a look at this too, but I wonder if a companion module with constants would be a better place to keep the preferences for the script? That way, one wouldn’t have to change their prefs back with every bug fix.

I agree, it would be a nice change.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [July 25, 2013, 3:52pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/8 "2013-07-25T15:52:29Z")

</div>

> [@22505:@Kem Tekinay](#):
>
> I wi take a look at this too, but I wonder if a companion module with constants would be a better place to keep the preferences for the script? That way, one wouldn’t have to change their prefs back with every bug fix.

I wanted to do that but not sure how to make those variables in the companion module be available to my script? Or are you speaking of a module that you have to add to your project?

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [July 25, 2013, 3:53pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/9 "2013-07-25T15:53:25Z")

</div>

Right, you’d add the module to your project.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [July 25, 2013, 4:57pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/10 "2013-07-25T16:57:59Z")

</div>

OK, 1.0.0b2 has been released… It includes: super. support, case conversion for Me., Self. and Super, and support for setting preferences in a module in your project named `FormatCodePreferences`.

The pro to setting configuration in the script is that you can format any project without setting up a particular module for it. The con is having to reset your configuration each new release of Format Code and that the setting is global, i.e. all projects will be formatted the same way.

The pro to setting configuration by the module is: No update hassles and each project can have its own preferences, for example contribute to an open source project w/a particular formatting scheme vs. your own private scheme.

Now, once Format Code settles down, you could use both… setup your default in the script and then when something varies from the default, create a module for it in the project itself.

Please see: [Format Code - GitHub Releases](https://github.com/jcowgar/xojo-format-code/releases/tag/v1.0.0b2) and [Format Code - README.md](https://github.com/jcowgar/xojo-format-code/blob/master/README.md) for more info on this release.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [April 25, 2014, 3:39pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/11 "2014-04-25T15:39:08Z")

</div>

This is a really nice job, especially adding the prefs module as optional. Brilliant.

I tend to mix case. For example, I prefer certain keywords to be title case and others lowercase. Why? Dunno, I just do. What about adding groups for KeywordsToUppercase, KeywordsToLowercase, and KeywordsToTitleCase? Not all groups would have to be filled in, so if a keyword doesn’t appear on any of the new lists, it will be treated to the default behavior. In other words, if you want all your keywords as title case except for if/then/else, you’d set CaseConversion to kTitleCase, but add those three exceptions to KeywordsToLowercase.

These three additional arrays could be maintained in the pref module as comma-delimited strings.

What do you think?

(I’ll clone the repo and make the change if you agree. I’m not looking to create work for you.)

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [April 25, 2014, 3:40pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/12 "2014-04-25T15:40:31Z")

</div>

Kem, that sounds great! Yeah, clone and I’ll happily pull!

BTW… I should add in my `README.md` that you can turn off the module for Web, Console and Desktop applications so it’ll never be compiled in your code wasting space/time.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [April 25, 2014, 4:56pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/13 "2014-04-25T16:56:43Z")

</div>

I can’t imagine that matters.

Anyway, I made the changes and sent a Pull Request.

I haven’t dived into it, but the tokens are always one word, yes? For example, `end if` would be considered two tokens?

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [April 25, 2014, 5:41pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/14 "2014-04-25T17:41:06Z")

</div>

Yeah, end if is turned into two tokens.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [April 25, 2014, 5:58pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/15 "2014-04-25T17:58:18Z")

</div>

As I thought. I was wondering if it made sense to turn End If, End Select, and Select Case into one token each, but the more I think about the reasoning behind that, the less useful it seems. If you want If and Select capitalized, you probably want the End capitalized too.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [April 25, 2014, 6:01pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/16 "2014-04-25T18:01:46Z")

</div>

I debated that also. When I wrote the tokenizer, I made it as generic as I could but still be able to understand Xojo code. In Xojo, however, “Select Case” and “End If” could be counted as a single token. As it is right now, if we ever want to do some more complex fixes, we may need to know if this “If” is part of an If statement or an “End If”. We can do it by checking the previous token, but… was something I debated. I think in the end I flipped a coin to decide.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [April 25, 2014, 6:07pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/17 "2014-04-25T18:07:52Z")

</div>

Upon further consideration, I think it’s fine.

Does it make sense to add an “AdditionalTokens” property to the module? For example, I may always want Str and Format to title cased.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [April 25, 2014, 6:11pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/18 "2014-04-25T18:11:30Z")

</div>

That’s a good idea, I wonder though if we should just make a `MethodsToCapitalize` array. One would then wonder, do we need a `VariablesToCapitalize` array. I actually think both is good and an `AdditionalTokens` array, which users can fill in items from their own libraries if they wish

With your idea of a comma list in the Module, it would be very nice. One could also use an External module to share settings amongst many projects.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [April 25, 2014, 6:13pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/19 "2014-04-25T18:13:10Z")

</div>

I got a bit confused by that. Are you proposing adding three new arrays to the prefs?

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [April 25, 2014, 7:49pm UTC](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823/20 "2014-04-25T19:49:34Z")

</div>

> [@82685:@Kem Tekinay](#):
>
> I got a bit confused by that. Are you proposing adding three new arrays to the prefs?

I’ll have to look again, but I was thinking MethodsToCapitalize and VariablesToCapitalize could be populated with those from the Xojo library. AdditionalTokens could be for users to add support for their own projects and libraries.

Thoughts?

So, MethodsToCapitalize could contain things like Str, Format, UBound, GetFolderItem, etc… I guess there really are not global variables. Maybe VariablesToCapitalize isn’t needed.

[Next page](https://forum.xojo.com/t/format-code-xojo-script-to-nicely-format-your-source/9823.md?page=2)
