Format's the code in the current editor of the Xojo IDE. For example, badly formatted code:
DIM i as Integer= 10
iF i<18 then
SayHello ("Howdy" , "World" )
end if
Automagically becomes:
Dim i As Integer = 10
If i < 18 Then
SayHello("Howdy", "World")
End If
It also does some other semi-format tasks, for example it expands common short-hand assignments:
appleCount += applesInDeliveryTruck
appleCount -= applesAteWhileUnloading
appleCount *= bushelsInTruck
appleCount /= peopleHelpingToUnload
Automagically becomes:
appleCount = appleCount + applesInDeliveryTruck
appleCount = appleCount - applesAteWhileUnloading
appleCount = appleCount * bushelsInTruck
appleCount = appleCount / peopleHelpingToUnload
Copy Scripts/Format Code.xojo_script
to the Scripts
directory of your Xojo
Installation. To run unit testing, simply open Format Code Unit Test.xojo_project
and click the menu item File > IDE Scripts > Format Code - Test.xojo_script.
If you are keeping up with the bleeding edge Format Code development via git, you may wish to clone the repository, then create symbolic links instead of copying the script each update. An example for OS X at the time of this writing:
$ cd $HOME/development
$ git clone https://github.com/jcowgar/xojo-format-code.git
$ cd xojo-format-code
$ git checkout develop
$ ln -s Format\ Code.xojo_script /Applications/Xojo\ 2015\ Release\ 2.2/Scripts/
Now when a new commit is pushed or release made, simply:
$ cd $HOME/development/xojo-format-code
$ git pull
and you will be running the bleeding edge.
On OS X you can use the System Preferences to create a shortcut to execute the Format Code script.
- Load System Preferences
- Select Keyboard
- Select the Shortcuts tab
- Select App Shortcuts
- Click the + button
- Choose Application Xojo
- Enter File->IDE Scripts->Format Code.xojo_script into the Menu Title
- Choose a keyboard shortcut (I use Ctrl+F for Format)
Users can set options to control various aspects of Format Code. Options can be set one of two ways,
- Change configuration settings at the top of
Format Code.xojo_script
: This will enable your preferences to be applied across multiple projects w/o change. It does not allow for custom settings per project and each time you updateFormat Code.xojo_script
you will have to re-apply your custom settings. - Create a module named
FormatCodePreferences
and add constants with the same names as the variables in the script. This allows you to have per-project settings and be able to updateFormat Code.xojo_script
easily. This module does not have to contain all, or any, of the preferences but any preference that is found in the module will override any preference set manually in theFormat Code.xojo_script
file.
You can also use both options, #1 to provide defaults to simple projects and #2 to override those defaults for specific projects.
When setting options via the FormatCodePreferences
, the CaseConversion constant can
be numeric (1, 2, 3) or a string, "kTitleCase", "kLowerCase", "kUpperCase" and boolean
constants can be a boolean value or a string, "Yes" or "True"
byREF
Would become:
ByRef // If CaseConversion = kTitleCase
byref // If CaseConversion = kLowerCase
BYREF // If CaseConversion = kUpperCase
Add ( 1 , Abs(something ) )
Would become:
Add(1, Abs(something)) // If PadParens = False
Add( 1, Abs( something ) ) // If PadParens = True
Add ( 1 , Abs ( something ) )
Would become:
Add(1, Abs(something)) // If PadParensOuter = False
Add (1, Abs (something)) // If PadParensOuter = True
a = 10 + 5
Would become:
a=10+5 // If PadOperators = False
a = 10 + 5 // If PadOperators = True
Add(10, 5)
Would become:
Add(10,5) // If PadComma = False
Add(10, 5) // If PadComma = True
A user definable array lists all the keywords that you wish to have case conversion performed on. The array comes with all the keywords of the Xojo language already defined.
A user definable array defined in the FormatCodePreferences
module delimited by commas.
All items will be converted to TitleCase
regardless of any case conversion setting.
A user definable array defined in the FormatCodePreferences
module delimited by commas.
All items will be converted to UPPERCASE
regardless of any case conversion setting.
A user definable array defined in the FormatCodePreferences
module delimited by commas.
All items will be converted to lowercase
regardless of any case conversion setting.