Arduino Code Snippets

by Visual Micro 2. June 2013 18:14

Visual Studio has a powerful snippet and macro system but the VisualAssistX snippet manager in Atmel Studio is just awsome!

Extracted from http://www.wholetomato.com/products/features/vasnippets.asp

Introduction to VA Snippets

VA Snippets are reusable code templates that allow you to quickly insert commonly used code fragments or to surround selected portion of code in the text editor. Used code templates may contain placeholders replaced dynamically, when the snippet is applied, by the meaningful blocks of code. VA Snippets are easy to create, simple to use and allow you to apply extra parameters taken from the context of the surrounding code, from environment variable or simply from your input.

The main advantage of VA Snippets is that they can be modified easily (no XML) to satisfy current needs and used immediately after modifications. Also by modification of specialised VA Snippets you can change behavior of each refactoring and code autocompletion related feature of Visual Assist.

Snippet definition in VA Snippet Editor:
Snippet applied in text editor:

 

Groups of snippets

VA Snippets can be divided into following groups:
Note: one snippet may appear in multiple groups if it meets the requirements of the each individual group.

 

  • Snippets using clipboard- Snippets with $clipboard$
    Includes snippets, where $clipboard$ placeholder is used within the body of the snippet definition.
    During applying of these snippets, a text contained in the clipboard is used as a source for the replacement of the$clipboard$ placeholders.
    User can define new such snippets.

      

 

  • Snippets for Surround With - Snippets with $selected$
    Includes snippets, where $selected$ placeholder is used within the body of the snippet definition.
    During applying of these snippets, the current selection in the text editor is used as a source for the replacement of the$selected$ placeholders.
    User can define new such snippets.

      

 

  • Snippets for Suggestion Lists - Snippets with shortcut
    Includes all snippets with the assigned shortcut.
    Their entries appear in suggestions list when you type their shortcuts in text editor.
    The result is inserted (replaces a written shortcut) after user’s confirmation of the selected snippet entry by pressing Tab orEnter key on keyboard.
    User can define new such snippets.

      

 

  • Refactoring snippets
    Specialized snippets used by Visual Assist X for refactoring purposes.
    Code templates contain context and refactoring operation specific placeholders.
    Titles of these snippets always begin with word Refactor.
    User can customize, but cannot create new such snippets.

      

 

  • Smart Suggestions
    Highly context-aware code hints suggesting text you're likely to type in the current context.
    With Smart Suggestions, C++ users enjoy C#-like autocompletion for several common scenarios.
    Code of snippets contains lines, where each line of code means one entry in suggestions lists.
    Each of these lines represents individual code template, so you can use expandable placeholders in their definitions.
    User can edit all such snippets, but can create only those for datatypes, so can not create context related.
    Title of these snippets must begin with keyword SuggestionsForType.

 

Supported placeholders

Placeholders in VA Snippets are keywords to be replaced by meaningful code enclosed by markers. There are two main types of placeholders - those enclosed in dollar '$' signs and Environment Variables enclosed in percent '%' signs.  Remember, that those enclosed in dollar signs are case-sensitive and Environment variables are case-insensiteve, so take care of it during editing or creating snippets.

Supported placeholders are:

  • Reserved Strings
    Predefined placeholders maitained by Visual Assist, their name is enclosed in $ signs.
    User can affect their content only by current IDE settings, project properties, system settings or by positioning caret to specific code context.
    For example, $clipboard$ will always expand to the content of clipboard, $DATE$ will always expand to current system date and so on.
    This group also inlcudes special placeholders usable only within Refactoring snippets.
    Click here to see the complete list of reserved strings with their meaning
  • User Prompts
    User-defined placeholders placed by enclosing meaningful keyword in $ signs.
    To work properly, used keyword must be equal to all its references within snippet definition, and must not be included inReserved strings.  
    During applying process of snippets containing such placeholders, user is prompted for actual values to be used as a result of their expansion.
    The maximum number of user prompts within one snippet is limited to eight.
    Click here for more info about User prompts in VA snippets.
  • Environment Variables 
    In VA Snippets, environment variables are placed as their names enclosed in % signs.
    Unlike other placeholders are case-insensitive, and are expanded before applying of another types of placeholders.
    That enables also a usage of environment variables as a default values in the user defined placeholders.
    Click here for more info about Environment Variables in VA snippets.

 

First touch - "for" loop

To try how to use VA Snippets to place a “for" loop do following:

  • Ensure that you have opened a C/C++ or a C# project.
  • Place the caret in the text editor to position, where you want to insert a new “for" loop.

    

  • From IDE’s main menu choose VAssistX -> Insert VA Snippet...

      

  • In appeared menu choose VA Snippets with shortcuts and then select entry “for loop forward”.

  • Dialog with two input boxes opens:
    The input box labeled “Index” asks for a name of the index variable for the scope, usually programmers use “i”.
    The input box labeled “Length” asks for a number of iterations, so insert for example 100.

    

  • Click on OK button or press Enter key on keyboard to confirm.
    This is the result:

    

Now you are ready to insert a “for" loop in simpler way - using shortcut.

  • Move the caret to the position, where you want to insert another “for" loop.
  • In the text editor write “forr” (double “r”) - the shortcut of the predefined For loop snippets.
  • In the suggestion list (which is a Visual Assists’s replacement of intellisense in Visual Studio)
    you should see entries: For loop forwardFor loop reverse and perhaps some other suggestions.
  • Select entry For loop reverse and confirm by pressing Tab or Enter key.

    

  • Similar dialog as in previous case opens, so insert “i” for index and 100 for length.

  • Click on OK button or press Enter key on keyboard to confirm.
    Follows both results together (current on bottom):