Generally refactoring is the process of restructuring and improving existing software code without changing its behavior.
Refactoring in EcoStruxure Machine Expert provides functionalities for renaming object names and variable names. It allows you to show all usage locations of changed objects and variables and to get renamed all those at once, or only individual ones. You can configure whether and at which locations you should get asked for performing refactoring. Explicitly you can use the command .
For further information, also refer to Options, Refactoring.
The command
is used for a global, project wide renaming of object names, and variable names.It is available in the contextual menu if you set the cursor on a variable name in the declaration or if you right-click one of the following objects in a navigator:
Functions
POUs
GVLs
Methods
Properties
Devices
The command is also available if you select variables and unit conversions within the editor for unit conversions.
The command is not available in online mode.
After you have executed the
command, a dialog is displayed. It shows the locations where the concerned name is used: In the right part of the window the particular locations, in the left part the navigation tree where the concerned object is marked.You can select the following commands for each location individually:
: Reject/accept single changes in the right window.
: Reject/accept all changes in the concerned object.
: Reject/accept all changes in the project.
Accepted changes are displayed with a yellow background, rejected changes with gray background.
The command
allows you to declare variables in a POU and performs an automatic update at its different points of use.It is available in the contextual menu or in the dialog box opens.
menu if the cursor is placed in a declaration editor. TheAfter you have confirmed the
dialog box by clicking , the dialog box opens. It consists of the tree structure of the project on the left-hand side. The POUs where the new variable is used are highlighted in the tree. You can double-click a POU to open the detailed view on the right-hand side of the dialog box. In this declaration section and implementation of the POU on the right-hand side, new declarations are highlighted in yellow.Before you decide which changes you want to accept at which location, select the suitable option from the list above the declaration section and implementation of the POU on the right-hand side:
Option |
Description |
---|---|
|
Select this option to insert the standard placeholder text |
|
Select this option to insert an initialization value whenever the new variable occurs in the implementation code. Enter the initialization value in the text box on the right-hand side of the option . |
In order to accept or reject changes, right-click the changed locations or execute the commands in the left or right part of the dialog box, as described in the Process of Renaming paragraph in this chapter.
Example 1:
By refactoring, the fun
block receives a new input variable input3
with the initialization value 1. The change has the following effect:
Before refactoring:
fun(a + b, 3, TRUE);
fun(input1:= a + b , input2 :=3 , inputx := TRUE);
After refactoring:
fun(a + b, 3, input3:=1, TRUE);
fun(input1:= a + b , input2 :=3 , input3:=_REFACTOR_,inputx := TRUE);
Example 2:
By refactoring, the fun
block receives a new input variable input3
with the placeholder text _REFACTOR_
:
Before refactoring:
inst(input1 := a + b, input2 := 3, inputx := TRUE);
fun(a + b, 3, TRUE);
After refactoring:
inst(input1 := a + b, input2 := 3, input3 := _REFACTOR_, inputx := TRUE);
fun(a + b, 3, input3:=_REFACTOR_, TRUE);
The command
allows you to remove an input or output variable from a POU. It is removed automatically from the POU at its different points of use.The command is available in the contextual menu or in the
menu if the cursor is placed in the declaration editor on the identifier of the variable to be removed. A dialog box opens that provides information on the removal. If you confirm the message, the dialog box opens.When you accept the changes proposed in the
dialog box, the respective input or output variables are removed automatically from the respective POU at its different points of use.
In a POU, refactoring removes the input4
input variable. It is automatically removed from the respective POU at its different points of use:
Before removal:
inst(input1 := a + b, input2 := 3, input4 := 1, input5 := TRUE);
fun(a + b, 3, 1, TRUE);
After removal:
inst(input1 := a + b, input2 := 3, input5 := TRUE);
fun(a + b, 3, TRUE);
The command
only takes effect on the CFC editor and the FBD/LD/IL editor. It corresponds to the commands and .The command
updates the input and output pins of the POU, at its different points of use, according to the current POU declaration.It is available in the contextual menu or in the
menu if the cursor is placed in the POU name, in the first line of the POU declaration or in the device editor.The command VAR_INPUT, VAR_OUTPUT, or VAR_IN_OUT.
allows you to change the order of variable declarations within the scopesExample:
VAR_INPUT
invar2 : INT;
invar1 : INT;
in : DUT;
bvar : BOOL;
invar3 : INT;
Proceed as follows to change the order of declarations:
Step |
Action |
---|---|
1 |
Right-click the declaration part and execute the command .Result: The VAR_INPUT variables. dialog box opens with a list of |
2 |
Drag the invar1 : INT; entry to the position before the invar2. entry. Result: The invar1 declaration is at the top position. |
3 |
Click to close the dialog box.Result: The dialog box opens. The affected elements are marked in yellow. |
4 |
Click to accept the new order of parameters of the function block. Have in mind that the modified order of the parameters will be reflected also at the call usages of the POUs. |
Refactoring can help you in renaming variables (using
) as indicated in the following example.As a prerequisite for this example, a function block fb_A with an input iA must be available.
Step |
Action |
Comment |
---|---|---|
1 |
Execute the command . |
Result: The dialog box opens. |
2 |
Select the category on the left-hand side. |
– |
3 |
In the section, select the options and . |
– |
4 |
Double-click the function block fb_A. |
– |
5 |
Select a variable in the declaration of fb_A, for example, iA. |
As an alternative, you can place the cursor before or in the variable. |
6 |
Execute the command . |
As an alternative, use the shortcut + .Result: The iA are displayed. dialog box opens. The settings of the variable |
7 |
Edit the iA into iCounter_A. and change |
Result: The dialog box opens. The POUs where the variable is used are highlighted in the tree structure. |
8 |
Click to apply the renaming and to close the dialog box. |
– |