A DevOps Model RealTime model contains snippets of code in many places where actions and expressions can be specified. There are four ways in which such code snippets can be edited:
Using the Code view
The Code view is mainly intended to be used for viewing code snippets and to make small modifications to them. The Code view can only show one code snippet at a time and you can only have one Code view open.
Using the Code editor
The Code editor is recommended for general editing of code snippets. You can have multiple code editors open at the same time and all common benefits with Eclipse editors also apply for code editors. The Code editor can either show one specific code snippet, or all code snippets for the element. See Working with multiple code snippets in the Code Editor.
Using the CDT source editor
You can use this editor after you have transformed your model into C++ code. The CDT editor shows a generated C++ file in its entirety and this file might contain multiple code snippets. The code snippets are enclosed within USR comment blocks. Here is an example of what it might look like:
//{{{USR platform:/resource/Sample/HelloWorld.emx#_ZT0WMI7mEeG--Krzx7QXSQ
log.show("Hi!");
//}}}USR
A benefit of editing code snippets in the CDT editor is that you can see the full context where the code snippet is located. This might make it easier to understand the code, than when just looking at the code snippet in isolation (as you do with the Code view and Code editor).
When editing a code snippet using the Code view or Code editor, keep the following in mind:
In the former case, the code snippet is located in the model. In the latter case, the code snippet is located in a generated file.
When editing a code snippet located in the model, certain features of the Code view or Code editor are not available or work differently. For example, all editor features that require a complete C++ file, such as Open Declaration, Open Type Hierarchy, and so forth, are not available. Features such as code completion are limited to finding only those elements that are defined in the model (for example, you only get basic content assist; see Content assist).
When editing a code snippet located in a generated file, the Code view or editor actually edits the generated file, although it only shows one particular code snippet of that file. All CDT editor features are available when the Code view or editor operates in this mode. Because of this you usually want to use the Code view or editor to edit a code snippet located in a generated file. However, there are cases when this is not possible:
By default, the Code view or editor tries to locate a generated file for a code snippet as soon as it gets focus. This behavior is controlled by a preference (RealTime Development - Code Editing - Auto load generated source when focus gained). It is recommended that you do not turn off this preference.
However, even if this preference is enabled, there are cases when the generated file cannot be automatically loaded. So, if you find that a code snippet that you are about to edit is located in the model, it is recommended to try and manually load the generated file so that the Code view or editor edits the code snippet stored in the generated file. To manually load the generated file, invoke the command Load Generated Source, which is available in the context menu of the Code view or editor. If the loading of the generated file fails (for reasons mentioned above), you have no other choice than to edit the code snippet located in the model.
When you edit a code snippet (or multiple code snippets) using an external or internal source editor (such as the CDT editor), keep the following in mind:
Do not make any modifications that are not within a code snippet (for example, do not change something outside of a USR comment block). Such modifications are lost the next time the file is regenerated. Also, such modifications might cause failure of the code-to-model synchronization, which means that you might also lose changes made within code snippets.
Do not make changes to the lines that start or end a USR comment block. Such changes might cause code-to-model synchronization to fail.
When you have modified one or multiple code snippets in a generated file, save the file before leaving the CDT editor. If you do not save, and instead perform an operation that might indirectly alter the file you just modified (such as a refactoring, or C++ code generation), Model RealTime usually detects that the file has not been saved and opens a warning dialog where you can save the editor. Always accept to save the file to avoid losing changes. There are some operations where Model RealTime cannot open the warning dialog. It is good to be proactive and not rely on this mechanism. The recommendation is to save a modified CDT editor before you start doing something else.
When you save a generated file in which you have modified at least one code snippet, expect to see the "Code To Model Synchronization" dialog. If you do not see the dialog, something has gone wrong and Model RealTime failed to detect the changes you made. In this case, copy the changes you made to a different text editor, close all editors in Model RealTime, and regenerate the file by transforming the model to C++ again. After transforming the file, you can open the CDT editor and reintroduce the changes to the code snippets. Save the file to trigger the code-to-model synchronization.
Review the changed code snippets in the "Code To Model Synchronization" dialog before pressing the Commit button. By reviewing the code snippets, you have a chance to detect if the changes you have made to the code snippets in the generated file will overwrite other changes you have made to those code snippets that are stored in the model (this should not happen unless you have enabled the preference RealTime Development - Code To Model Synchronization - Allow modified code snippets in the model to be overwritten by unmodified code snippets from the file). To review a code snippet, double-click it in the Structure Compare area. Here is an example of how the dialog might look:
The code in the Old Source view shows the code snippet as stored in the model. The New Source view shows the modified code snippet that will be committed to the model when you click Commit. If you do not want to commit some of the updated code snippets, you can deselect the checkbox for that code snippet before clicking Commit. If you press Cancel, no code snippets are updated, and all changes are lost the next time the file is regenerated.
To summarize the most important recommendations mentioned above:
For an in-depth visual guide on code-editing best practices, check out this video tutorial: