The formatter for Structured Text is here! I have always been used working with source code formatters like ReSharper or CodeRush for C#. But such is tools is not available for Structured Text. Unfortunately because having a consistent code style enforced by a formatter is one the most important ways of keeping your code maintainable. Which is why I decided to develop my own source code formatter for Structured Text!
In this post I present the main functionalities of “STweep – The formatter for Structered Text”.
Functionalities
STweep is a configurable source code formatter. This means that you are free to set the style you prefer. STweep comes with an integrated options screen where you directly can see the effect of a setting.
The most important features of the formatter are:
- Automatic line breaking
- Aligning in columns
- Managing blank lines
- Automatic indentation
Automatic line breaking
Long lines can cause readability problems. And it’s frustrating if you have to scroll side ways to read your long lines. If you set a line length limit STweep will automatically make an intelligent break in your line to keep your code readable.
The following example shows a pretty unreadable line:
IF Variable1 THEN IF Variable2 THEN Library1.Library2.DoSomething(FunctionInput1 := FunctionArgument1,FunctionInput2 := FunctionArgument2,FunctionInput3 := FunctionArgument3,FunctionInput4 := FunctionArgument4); END_IF END_IF
Setting the line length to 100 STweep will format your code:
IF Variable1 THEN IF Variable2 THEN Library1.Library2.DoSomething(FunctionInput1 := FunctionArgument1, FunctionInput2 := FunctionArgument2, FunctionInput3 := FunctionArgument3, FunctionInput4 := FunctionArgument4); END_IF END_IF
Which makes your Structured Text source code much better readable. Notice that STweep does not make a hard cut after 100 characters but instead it considers at lot of possible options and will select the best possible option based on your preference!
Aligning in columns
A popular options under PLC programmers. STweep supports aligning in columns for several constructs:
- Invocation call parameters
- Invocation call parameter assignments
- Chained binary expressions
- Assignments
- End of line comments
- Variable declarations
- Variable initializers
The following example code:
VAR ConveyerSpeed:INT:=20;//mm/s ConveyerForceLimit:REAL:=1000; //Newtons ConveryerTimeOut:TIME:=T#60S; END_VAR
ConveyerSpeed := 20; //mm/s ConveyerForceLimit := 1000; //Newtons ConveryerTimeOut := FunctionCall();
Can be automatically formatted in this:
VAR ConveyerSpeed : INT :=20; //mm/s ConveyerForceLimit : REAL :=1000; //Newtons ConveryerTimeOut : TIME :=T#60S; END_VAR
ConveyerSpeed := 20; //mm/s ConveyerForceLimit := 1000; //Newtons ConveryerTimeOut := FunctionCall();
Managing blank lines
Blank lines are important to keep your code readable. Not to much not to less. The STweep formatter for Structured Text can preserve your manually set blank lines. But also insert a custom number of blank lines.
You can for example specify a number of blank lines
- Before and after single line statements
- Before and after multi line statements
- Around statements with a body (such as an IF statement).
- Before and after multi line comments
- And more
Consider the example from the paragraph about line breaking which now has an additional function call:
IF Variable1 THEN IF Variable2 THEN Library1.Library2.DoSomething(FunctionInput1 := FunctionArgument1,FunctionInput2 := FunctionArgument2,FunctionInput3 := FunctionArgument3,FunctionInput4 := FunctionArgument4); FunctionCall(); END_IF END_IF
A popular way of formatting is adding a blank line after a multi line statement, but not after a single line. In the example there is no multi line statement. However after formatting there is a multi line statement because the long function call will be split into multiple lines. STweep will notice this and can automatically insert a configured number of blank lines:
IF Variable1 THEN IF Variable2 THEN Library1.Library2.DoSomething(FunctionInput1 := FunctionArgument1, FunctionInput2 := FunctionArgument2, FunctionInput3 := FunctionArgument3, FunctionInput4 := FunctionArgument4); FunctionCall(); END_IF END_IF
Automatic indentation
Do you prefer spaces or tabs? STweep can handle both.
With automatic formatting you don’t have to worry any more about having the correct indentation. STweep will analyze your code and set the configured number of indentations.
There has been special thought for indents around ‘CASE’ statements. Do you want to indent your cases from the ‘CASE’ statement? And do you want to indent your statements from the surrounding case or not? STweep has several options specially for CASE statements.
This example (worst case) code:
IF Variable1 THEN CASE Variable2 OF 1: IF VARIABLE3 THEN ; END_IF END_CASE END_IF
Can be automatically formatted into:
IF Variable1 THEN CASE Variable2 OF 1: IF VARIABLE3 THEN ; END_IF END_CASE END_IF
STweep for TwinCAT
STweep is also available for TwinCAT visit the STweep store on www.stweep.com
STweep for Codesys
STweep is available for Codesys. Using the Codesys store you can download the package to integrate STweep in Codesys.
IDE derived from Codesys like ABB Automation Builder and Wago e!Cockpit can also use the Codesys version of STweep.
Conclusion
With the STweep formatter you can enforce a consisting code style, and focus on the functionalty of your Structured Text source code! STweep is available for Codesys and for TwinCAT.
More info can be found on www.stweep.com.
Don’t hesitate to contact me for any questions/ bugs or feature requests!