Code formatting

To bring your code to consistent style and increase its readability, use the Format Code command in AnyLogic. You can apply the formatting to individual elements of the model or to the model overall.

Function code prior to formatting

Function code after formatting

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

 To format code

  1. Right-click (Mac OS: Ctrl+click) the model element that contains 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. Operation results preview dialog will appear.


  3. In Changes to be performed list you can select the elements where you want to format 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 to run the code formatting in the background next time, remove the selection from 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:

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

Navigation controls

 Swap Left and Right View - Click this button to swap the placement of Original Value and Refactored Value preview sections with each other.

 Next Difference - Click this button to move to the next section of code that is separated from the one you are currently viewing by an empty line.

 Previous Difference - Click this button to move to the previous section of code that is separated from the one you are currently viewing by an empty line.

 Next Change - Click this button to highlight the next change suggested by the AnyLogic code formatting.

 Previous Change - Click this button to highlight the previous change suggested by the AnyLogic code formatting.


Related topics

Operation results preview dialog