views:

462

answers:

2

The C# code snippets I create can only be ended by hitting Enter, while the built-in snippets such as 'struct' can be completed by tabbing through the custom fields.

It's not a big issue but it is annoying. I tried copying the entire XML for the struct snippet into my own, replacing only the shortcut name. Even so I had to hit Enter to complete the insertion.

Any ideas why this is so? Can anyone else reproduce this behavior?

Here is the snippet I am trying to write:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"&gt;
  <CodeSnippet Format="1.0.0">
    <Header>
  <Title>cmd</Title>
  <Shortcut>cmd</Shortcut>
  <Description>Code snippet for ICommand</Description>
  <Author>Andreas Larsen</Author>
  <SnippetTypes>
    <SnippetType>Expansion</SnippetType>
  </SnippetTypes>
</Header>
<Snippet>
  <Declarations>
    <Literal>
      <ID>name</ID>
      <ToolTip>Command name</ToolTip>
      <Default>My</Default>
    </Literal>
  </Declarations>
  <Code Language="csharp"><![CDATA[public ICommand $name$Command
  {
  get
  {
    if (_$name$Command == null)
        _$name$Command = new DelegateCommand($name$);
    return _$name$Command;
  }
}
$end$]]>
  </Code>
 </Snippet>
</CodeSnippet>
</CodeSnippets>
+2  A: 

I can only get the struct snippet to commit on pressing enter/escape but not a tab. I don't think what you want to achieve is possible (or sensible for that matter). The convention with snippets to complete is to either press enter or escape this is the documented behaviour.

Doing anything else is breaking convention surely? You can see why the convention is useful when you use parameterised snippets such as for. Tabbing allows you to modify the values multiple times before you are happy with your decision and press enter.

RichardOD
I suppose you are right, but if I have multiple fields I want to tab through them and it is very convenient to keep tabbing to complete the snippet. Especially since the built-in snippets support this too it has become a natural behavior to me.
Andreas Larsen
How does the snippet know to complete on a tab? All the ones I've used allow you to tab between fields and when you get to the last field and press tab again, it just goes back to the first field. I've only ever seen enter or escape terminate the snippet.
Chris Dunaway
A: 

I figured it out now.

I am using the ReSharper addin and it appears to have a Live Template function that overrides Visual Studio's snippets. ReSharper added the functionality that TAB could be used to complete a snippet on native VS snippets as well as on its live templates.

When I converted my custom snippets to live templates instead, I got the behavior I wanted. I can now TAB through each field and change them as I go, and finish the snippet by TABing past the final element.

For those interested, live templates provides a much more feature rich snippet functionality. For instance you can run simple macros on the fields, so that for instance backing fields can have lower case first letter (if that's your naming convention) when reusing the name of a property. Very much recommended addin if you can afford it!

Andreas Larsen