Aiding your work with Visual Studio Code Snippets

I’ve bin writing //TODO comments in my code every time wondering what pattern have we agreed upon in our team. Was it a

// TODO Recipient Priority Sender, Message


or

// TODO Priority Sender Recipient, Message


or something else.
Today I thought: who am I to remember all those dentils? Do I have something to help me with this burning problem?
Well I have. The name is Visual Studio Code Snippet. It in a XML file that describes a quickly insertable text. With such XML description I will by able to write only a word “todo” in my VS and everything else will happen automagically. There will by a nicely highlighted input places allowing me to navigate with a Tab key to complete my //TODO comment.
Here is how to create such file:
1. create a XML file with snippet extension
2. the root element name is CodeSnippets

<CodeSnippets
        xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <!--Insert code snippets here-->
</CodeSnippets>

3. insert one or more code snippets in a tag named CodeSnippet

<CodeSnippet Format="1.0.0">
        <Header>
        <!--Header tags-->
        </Header>
        <Snippet>
        <!--Snippet tags-->
        </Snippet>
</CodeSnippet>

4. define your header

<Header>
        <Title>ToDo Comment</Title>
        <Shortcut>todo</Shortcut>
        <Description>Inserts todo comment</Description>
        <Author>Marcin Kawalerowicz</Author>
</Header>

The Shortcut tag is interesting. With this tag you will by able to define a short cut that will trigger your snippet. After this definition you will by able to write simply “todo” in your VS, then press Tab key an the magic will happen.
5. Define your Snippet

<Code Language="CSharp">
        <![CDATA[// TODO $Recipient$ $Prio$ $Sender$, $Message$]]>
</Code>

6. Do you see the variables between $ you have to declare them too

<Declarations>
        <Literal>
              <ID>Sender</ID>
              <ToolTip>ToDo sender</ToolTip>
              <Default>??</Default>
        </Literal>
        <Literal>
              <ID>Recipient</ID>
              <ToolTip>ToDo recipient</ToolTip>
              <Default>??</Default>
        </Literal>
        <Literal>
              <ID>Prio</ID>
              <ToolTip>Priority</ToolTip>
              <Default>0</Default>
         </Literal>
         <Literal>
              <ID>Message</ID>
              <ToolTip>Message</ToolTip>
              <Default>!</Default>
              </Literal>
         </Declarations>

7. Voila you are ready. You have to import this file to Visual Studio using Tools -> Code Snippet Manager… and the Import… button.


Now press Ctrl+K+X and you can browse to your new snippet or write simply “todo” and press Tab to see something like this:

image

Here is a complete code of our snippet (with NOTE comment snippet a a bonus 😉

<CodeSnippets
        xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
                <Header>
                <Title>
            - NOTE Comment
            </Title>
                        <Shortcut>note</Shortcut>
                        <Description>Inserts note comment</Description>
                        <Author>Marcin Kawalerowicz</Author>
                </Header>
                <Snippet>
            <Declarations>
                <Literal>
                    <ID>Message</ID>
                    <ToolTip>Message</ToolTip>
                    <Default>!</Default>
                </Literal>
                <Literal>
                    <ID>Sender</ID>
                    <ToolTip>Sender</ToolTip>
                    <Default>??</Default>
                </Literal>
            </Declarations>
                        <Code Language="CSharp">
            <![CDATA[// NOTE $Sender$, $Message$]]>
                        </Code>
                </Snippet>
        </CodeSnippet>
    <CodeSnippet Format="1.0.0">
                <Header>
                <Title>
            - ToDo Comment
            </Title>
                        <Shortcut>todo</Shortcut>
                        <Description>Inserts todo comment</Description>
                        <Author>Marcin Kawalerowicz</Author>
                </Header>
                <Snippet>
            <Declarations>
                <Literal>
                    <ID>Sender</ID>
                    <ToolTip>ToDo sender</ToolTip>
                    <Default>??</Default>
                </Literal>
                <Literal>
                    <ID>Recipient</ID>
                    <ToolTip>ToDo recipient</ToolTip>
                    <Default>??</Default>
                </Literal>
                <Literal>
                    <ID>Prio</ID>
                    <ToolTip>Priority</ToolTip>
                    <Default>0</Default>
                </Literal>
                <Literal>
                    <ID>Message</ID>
                    <ToolTip>Message</ToolTip>
                    <Default>!</Default>
                </Literal>
            </Declarations>
                        <Code Language="CSharp">
            <![CDATA[// TODO $Recipient$ $Prio$ $Sender$, $Message$]]>
                        </Code>
                </Snippet>
        </CodeSnippet>
</CodeSnippets>

2 Comments

  • Mateusz Loskot

    Marcin, you can also navigate through comments marked with TODO, XXX, FIXME, and custom markers (these can be configured in Tool -> Options) using the Task List window. But, you’ve been aware of that, I’m sure 🙂

  • Marcin Kawalerowicz

    Hi Mateusz, great point. In a matter of fact it is the sole purpose of using markers like TODO, NOTE and so on. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *