Reputation: 8668
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">
<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>
Edit: As pointed out by Carl G, the Visual Studio behavior is to end by ENTER or ESC only. Ending by TAB is a ReSharper Live Template behavior.
Upvotes: 4
Views: 1766
Reputation: 8668
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!
Upvotes: 1
Reputation: 29157
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.
Upvotes: 3