Refactoring

In general, refactoring is a technique for improving the design of existing software code without changing the way it functions.

In CODESYS, refactoring provides functions for renaming objects and variables and updating referenced pins. You can display all occurrences of renamed objects and variables and then rename them all at once or individually. In Tools ‣ Options, you can also configure where CODESYS will prompt you for refactoring.

Renaming global variables

Requirement: A project is open with at least one function block (FB) and one global variable list (GVL). The global variable list is open in its editor and contains a variable declaration (for example, iglob1). FB uses iglob1.

  1. Select the global variable name iglob1.

  2. Right-click the variable and click Refactoring ‣ Rename iglob1.

  3. In the Rename dialog box, type a name in the New name input field, for example iglobNeu, and click OK.

    The Refactoring dialog box opens. In the device tree view on the left, the GVL and FB objects are highlighted in red and yellow. In the view on the right, FB in is open in its editor and iblob1 has already been renamed as iglobNeu.

  4. Click OK.

  5. The iglob1 global variable is now named iglobNeu across your project.

  1. Select the global variable name iglob1.

  2. Right-click the variable and click Refactoring ‣ Rename iglob1.

  3. In the Rename dialog box, type a name in the New name input field, for example iglobNeu, and click OK.

    The Refactoring dialog box opens. In the device tree view on the left, the GVL and FB objects are highlighted in red and yellow. In the view on the right, FB in is open in its editor and iblob1 has already been renamed as iglobNeu.

  4. Right-click in the view on the right.

  5. Click Reject this object and click OK.

    The global variable (iglob1) is not renamed as iglobNeu in FB. In all other objects in the project, the variable is renamed as iglobNeu.

    An error message in the messages view reports that the iglob1 identifier is not defined.

See also

Adding and removing input variables

In the declaration section of blocks, you can add and delete input and output variables by using the refactoring commands. CODESYS performs updates at the occurrence locations and calling locations of the blocks. You can accept or reject these updates individually. The Refactoring dialog box also opens for this purpose.

Requirement: The FCT (function type) POU is open in the editor. The function already contains the input variables input1, input2, and inputx. They are called in the PLC_PRG and POU programs.

  1. Set the focus in the declaration section of the FCT function.

  2. Click Refactoring ‣ Add Variable.

    The default dialog box opens for declaring variables.

  3. Declare the variable input_3 with the scope of VAR_INPUT and data type INT. Click OK to close the dialog box.

    The Refactoring dialog box opens (see figure below). The affected locations are marked in yellow. (1)+(2)

  4. In the upper right corner, select Add inputs with placeholder text from the drop-down list. (3).

  5. In the left side of the window, click one of the highlighted objects (for example, PLC_PRG). Right-click and choose the Accept whole project command to add the new variable at the new location of use in FCT for the entire project.

    In the right side of the window, the change is accepted in the implementation section of PLC_PRG: The _REFACTOR_ placeholder appears at the location where the new variable was inserted.

  6. Click OK to close the Refactoring dialog box.

  7. Click Edit ‣ Search. Search the project for “_REFACTOR_” to check and edit the affected locations.

  8. Please note: As an alternative, you can insert the new variable with another initialization value without working with a placeholder first. In this case, in Step 4 you select “Add inputs with the following value” and type the value in the field on the right side of the drop-down list.

../_images/8ee412eae60cabccc0a8640e0032b111

Example of a new variable with placeholder text in a CFC block:

857adee5e3a92356c0a8640e0027b4e2_5c6c72d303fbcb27c0a8640e009140dd

Please note that you can also remove variables with refactoring.

See also

Reordering variables in the declaration

In the declaration part of function blocks, you can change the order of declarations by refactoring This is possible for declarations with scope VAR_INPUT, VAR_OUTPUT, or VAR_IN_OUT.

Requirement: The declaration part of a POU is open and includes declarations, for example:

VAR_INPUT
  invar2 : INT;
  invar1 : INT;
  in : DUT;
  bvar : BOOL;
  invar3 : INT;
  1. Right-click in this declaration block to access the context menu.

  2. Click Refactoring ‣ Reorder Variables.

    The Reorder dialog box opens with a list of VAR_INPUT variables.

  3. Drag the invar1 : INT; entry to the position before the invar2. entry.

    The invar1 declaration is now at the top position.

  4. Click OK to close the dialog box.

    The Refactoring dialog box opens. The affected locations are marked in yellow (see figure above).

  5. Click OK to accept the new order for the function block.