Using code templates

In this section you will learn to use Content Assist to fill in a template for a common loop structure. Open as_environment_manager.magik file in Magik editor, if it is not already opened.

  1. Start adding a new method by typing the following code:

    _method as_environment_manager.example_code_templates(values)
    vars << rope.new()
    for
    _endmethod

  2. With the cursor at the end of "for", press Ctrl+Space to enable Content Assist.  Repeat pressing Ctrl+Space until Template Proposals appear. You will see a list of common templates for "for" loop.  When you single-click a template, or select it with the Up or Down arrow keys, you will see the code of the selected template in its help message.  Note that the local array name is determined automatically.

    Content assist for "for"

  3. Choose for - full controlled loop entry and press Enter to confirm the template.  The template will be inserted into your source code.

    Inserted "for" template

  4. Change the name of the index variable from iter_value to value. To do so simply type value, and the index variable will be automatically selected. Note, that the name of the index variable changes in all places. While inserting a template all references to the same variable are being connected to each other. When you change one, all the other values will be changed as well.

    Altered "for" template

  5. Press the Tab key to move the cursor to the next variable of the code template. It is the array iter_invocation.

    Altered "for" template

  6. Change the name of the variable from iter_invocation to values.elements().

    Altered "for" template

  7. Add the following method:

    _method as_environment_manager.example_code_templates(values)
    vars << rope.new()
    _for value _over values.elements()
    _loop @values_loop
        vars.add(value)
    _finally _with value
    _endloop
    _endmethod

  8. Save the file.

Related concepts

Magik editor
Templates

Related reference

Templates Preferences
Magik Editor Preferences