Using AFL Code snippets

Code snippet is a small piece of re-usable AFL code. It can be inserted by

In version 5.90 Code snippets are also available in auto complete list in the AFL Editor. Just type @ plus first letter of snippet key trigger and auto-complete list would show you the list of available snippets that have keyboard triggers defined starting with that letter.

Replacement of keyboard triggers works even without auto complete activated, so just typing @keytrigger is replaced by snippet text.

DEFINING YOUR OWN SNIPPETS

You can add your own snippets fairly easy using new Code Snippet window. Code Snippets window is available in new AFL editor. It can be shown/hidden using Window menu.

To create your own snippet, do the following:

  1. type the code you want
  2. select (mark) the code you want to place in a snippet
  3. press Save selection as snippet button in the Code Snippets window

If you do the steps above the following dialog will appear:

Now you need to enter the Name of the snippet, the Description and Category. Category can be selected from already existing items (using drop down box), or new category name can be typed in the category field. Key trigger field is optional and contains snippet auto-complete trigger (described above). The Formula field is the snippet code itself. Once you enter all fields and press OK, your new snippet will appear in the list.

From then on you can use your own snippet the same way as existing snippets. Perhaps most convenient method is using drag-drop from the list to AFL editor.

As you may have noticed user-defined snippets are marked with red color box in the Code Snippets list. Only user-defined snippets can be overwritten and/or deleted.

To edit existing user-defined snippet, you can either follow the steps above and give existing name. AmiBroker will ask then if you want to overwrite existing snippet, or you can simply click on Properties button and edit the snippet directly, without re-inserting it.

To delete a snippet, select the snippet you want to delete from the list and press Delete (X) button in the Code Snippet window.

 

TECHNICAL INFO (advanced users only)

There are two files located in AmiBroker directory that hold snippets:
CodeSnippets.xml - these are snippets shipped with AmiBroker installation (and can be replaced in subsequent installations, so don't modify it!)
UserSnippets.xml - these are user-definable snippets. This file is NOT present in the installation and user can create it by him/herself.

The XML schema for snippets file is simple (as below). Key trigger functionality is NOT yet implemented, however Keytrigger fields should be included in the definition for future use. It will be work like 'autocomplete' so that you type the shortcut it, it will unfold to the formula.

<?xml version="1.0" encoding="ISO-8859-1"?>
<AmiBroker-CodeSnippets CompactMode="0">

<Snippet>
<Name>First Snippet</Name>
<Description>Description of the snippet</Description>
<Category>User category</Category>
<KeyTrigger>?trigger1</KeyTrigger>
<Formula>
<![CDATA[

// the formula itself

]]>
</Formula>
</Snippet>

<Snippet>
<Name>Second Snippet</Name>
<Description>Description of the snippet</Description>
<Category>User category</Category>
<KeyTrigger>?trigger2</KeyTrigger>
<Formula>
<![CDATA[

// the formula itself

]]>
</Formula>
</Snippet>
</AmiBroker-CodeSnippets>