AnyLogic
Expand
Font size

Code formatting

To give your code a consistent style and improve readability, use the Format Code command in AnyLogic. You can apply the formatting to individual elements of the model or to the entire model.

AnyLogic: Code before formattingFunction code before formatting

AnyLogic: Code after formattingFunction code after formatting

AnyLogic allows you to preview the formatted code and specify the maximum line width of your code. These settings are adjusted in the Development section of the Preferences… menu. The option to preview the results of code formatting is selected by default. The default code line width is 60 symbols.

To format code

  1. Right-click (macOS: Ctrl + click) the model element containing the code you want to format either in the graphical editor or in the Projects view and choose the Format code... command from the context menu. To select the whole model for code formatting, right-click the model’s name.
  2. The Operation results preview dialog will appear.

    AnyLogic: The Operation results preview dialog

  3. In the Changes to be performed list, you can select the elements for which you want to format the code.
  4. To navigate through the differences between the original and the formatted code, use the controls above the preview section of this dialog.
  5. If you want the code formatting to run in the background the next time, clear the Always preview the results check box.
  6. To apply the changes and finish the code formatting operation, click the OK button.

If the code you want to format contains errors, the formatting operation will not be executed and the following message will appear:

AnyLogic: Format Code No Changes message

The same message will appear if you attempt to format the code that is already compliant with the AnyLogic suggested style.

AnyLogic: Swap Left and Right View Swap Left and Right View Swap the Original Value and Refactored Value preview sections.
AnyLogic: Next Difference Next Difference Jump to the next section of code separated by a blank line from the one you are currently viewing.
AnyLogic: Previous Difference Previous Difference Jump to the previous section of code separated by a blank line from the one you are currently viewing.
AnyLogic: Next Change Next Change Highlight the next change suggested by the AnyLogic code formatting.
AnyLogic: Previous Change Previous Change Highlight the previous change suggested by the AnyLogic code formatting.
How can we improve this article?