Adding code snippets to chapters
Inserting code snippets
Inserting code into the tutorial is done easily via drag&drop from an outline of the source code file.
- Open the CodeFile from which code should be added. It is displayed as text in the editor window and as tree in the Code Outline view.
- Activate the chapter into which to add the code snippet.
- In the Code Outline navigate to the position of the code to be inserted. If the leaf nodes presented are not fine-grained enough, don't worry, there are possibilities to postprocess the embedded code snippets. They are explained below.
- Select all the nodes in the Code Outline which should be part of the embedded code snippet. To select multiple nodes, press and hold the Ctrl key while left-clicking the desired nodes.
- Drag&drop the selected nodes into the chapter. The position indicator at the lower left of DEFT and the cursor show where the code will be inserted when the mouse button is released.
- Once the dragged nodes are released, the Format dialog pops up. Formats are explained below. After a format has been chosen, click OK. The code is now embedded. Additionally an entry has been created in the Code snippets section of the Project explorer. This entry, the CodeSnippet, can also be drag&dropped into the chapter if it is ever needed again.
Applying formats
There are several reasons why the standard way of inserting code is insufficient.
- The smallest "resolution" of code to be inserted might be too coarse-grained. For example, in C# nothing smaller than a statement can be inserted. But what if you only need a fraction of it?
- The code snippet is rather long, e.g. a method. What if you only want to include the method header and some selected statements, while the remaining statements are left out?
- In these cases formats are the solution. Formats can be applied to embedded code snippets and alter their displayed content.
Predefined formats
There are predefined formats which can be applied to each
compatible code snippet, e.g. header_only for
methods to have their body replaced by {...}. To
change the format of a code snippet do the following:
- Left-click on the code snippet to be formatted (in the editor). In the Properties window the available formats are displayed
- Left-click on the format to be applied. The changes are immediately visible.
- Select the display type of the code snippet: Inline or Block. Inline means that the code snippet appears in flowing text, block means that the code snippet is, well, a block.
Custom formats
If more fine-grained control is necessary, a custom format can be created. Such a custom format is only available for the code snippet for which it has been created and cannot be applied to other code snippets.
- Left-click on the code snippet to be formatted (in the editor). In the Properties window the available formats are displayed
- Click the New button. A dialog opens with a tree representation of the code file. This tree representation is much more extensive than the one from the Code Outline and allows full control of how the final code snippet will look like.
- Enter a name for the format.
- Optionally choose the display types for which this format is applicable. For example, for a format that displays only the name of a variable one would typically choose the Inline format, so Block could be disabled.
- Define visibility of the code snippet contents. Only the tokens of checked fields are displayed.
- Define alternative text. For tokens to be hidden (i.e. the nodes are unchecked) it is possible to define a replacement text, indicating that there is content missing. For example, one could replace the body of a method with { ... } or a couple of statements which are currently not important with /*some code left out*/. Double-click on the content (text) of an unselected node in the left table cell. A cursor will appear in the right table cell. Type the alternative text.
- Confirm with OK.
It is also possible to edit and delete custom formats via the Edit and Delete buttons. Note that predefined formats can neither be edited nor deleted.
Development Environment For Tutorials