Exercise 2: Authoring Code Snippets for Visual C++

Code snippets provide a way for you to insert ready-made snippets of code into your projects with minimal keystrokes. As you start typing code in Visual Studio, the IntelliSense member list shows you the relevant code snippet that you can use.

Code snippets are extensible so you can build your own custom code snippets by creating XML files with a .snippet file extension that adhere to the Code Snippet XML schema. One of the advantages of authoring your own snippets is that you can insert the code structures you frequently use, such as class scaffolds or repeated functions, without doing copy-paste and removing the previous code.

In this exercise, you will learn how to create your own custom code snippet, install it in your environment and use it from the Code Snippets Manager or the IntelliSense members list.

For more information on Code snippets check out the following link at MSDN: https://msdn.microsoft.com/en-us/library/ms165392(VS.110).aspx

Task 1 – Authoring Code Snippets

In this task, you will create your own code snippet starting from a simple snippet template.

  1. Open Visual Studio 11 if it is not already opened.
  2. Select File | New | File. In the New File dialog, select the XML File template and click Open.
  3. Right-click on the text editor surface, select Insert Snippet and then Snippet. This is the basic XML structure for creating a code snippet, which you will now use as the skeleton.

    Figure 20

    Creating a custom code snippet

  4. Replace the placeholders of the Header fields with the following values. You can press the TAB key to switch between each placeholder.

    1. Title: pause
    2. Author: your name
    3. Shortcut: pause
    4. Description: Show a message and wait for the user to press a key

    Additionally, remove the SurroundsWith SnippetType element. The Header element should look similar to the following code.

    XML

    <Header> <Title>Pause</Title> <Author>[your name]</Author> <Description>Show a message and wait for the user to press a key</Description> <Shortcut>pause</Shortcut> <SnippetTypes> <SnippetType>Expansion</SnippetType> </SnippetTypes> </Header>
    Note:
    The text value of a Shortcut element can only contain alphanumeric characters, hyphens ( - ), and underscores ( _ ).

    The SnippetType element supports the following values:

    - SurroundsWith: enables the code snippet to be placed around a selected piece of code.

    - Expansion: enables the code snippet to be inserted at the cursor position.

    - Refactoring: specifies that the code snippet is used during Visual C# refactoring. Refactoring cannot be used in custom code snippets.

  5. Complete the Declarations element to define a literal replacement for the message placeholder with the following values:

    1. ID: message
    2. Default: Press any key to exit
    3. ToolTip (add this element): Replace with a message

    The Declarations element should look similar to the following code.

    XML

      </Code>
    <Declarations> <Literal> <ID>message</ID> <Default>Press any key to exit</Default> <ToolTip>Replace with a message</ToolTip> </Literal> </Declarations>FakePre-57f0880931ab4bd583b2b6911cb484b6-d8522685de854c9e9edb268e2dbcd916

  6. Now you will fill the code snippet body. Notice that the code will be written inside the <![CDATA{}]> field, while header fields are aimed at providing additional information for the IDE. Locate the Snippet tag and complete the code to use a placeholder for the message. Additionally, change the Language to cpp.

    XML

    <Code Language="cpp"> <![CDATA[std::cout << "$message$" << std::endl; Console::ReadKey(true);]]> </Code>

  7. Save the snippet file in the My Code Snippets folder.

    To do this, select File | Save XMLFile1.xml As. In the Save File As dialog, browse to Documents\Visual Studio 11\Code Snippets\Visual C++\My Code Snippets, set the file name to pause.snippet and click Save.

Task 2 – Using Your Code Snippet

In this task, you will use the code snippet you have just created to complete the solution from the previous exercise.

  1. In Visual Studio, select Tools |Code Snippets Manager. In the Code Snippets Manager dialog, select the Visual C++ language and expand My Code Snippets item. Notice Visual Studio now recognizes your code snippet and shows its header information automatically.

    Figure 21

    The new code snippet in the Code Snippets Manager

  2. Close the Code Snippets Manager.
  3. Open the WhatsNewCpp-Ex2-Begin.sln solution located in the Source\Ex2-CodeSnippets\Begin of this lab. Alternatively, you can continue using the solution you created in Exercise 1.
  4. Open WhastNewCpp.cpp class and locate the main method. Insert a new line after greetings.Show() instruction and start typing pause. Notice your code snippet appears in the list and you can see its title and description in the tooltip message.

    Figure 22

    Inserting your code snippet

  5. Press TAB to insert the code snippet. Notice that the cursor is positioned within the message to enable you to replace the text. In addition, if you hover over the text you can see its tooltip message.

    Figure 23

    Replacing the literal replacement

  6. Press ENTER once you have finished writing the message, or otherwise use the default.
  7. Press CTRL+SHIFT+B to build the solution. Then, press F5 to run the console application and see the results.
  8. In the console window, press any key to exit.