Write C # snippets that end by pressing TAB

The C # code scripts that I create can only be completed by pressing Enter, while inline snippets such as "struct" can be populated by tabbing through custom fields.

This is not a big problem, but it is annoying. I tried to copy all the XML for the structure fragment into my own, replacing only the label name. However, I had to press Enter to complete the input.

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

Here is the snippet I'm 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 indicated by Carl G, Visual Studio behavior ends only by pressing ENTER or ESC. Termination with TAB is the behavior of the ReSharper Live Template.

+4
source share
2 answers

I get it now.

I am using the ReSharper add-on and it seems to have a Live Template function that overrides Visual Studio fragments. ReSharper added functionality that TAB could use to complete the fragment in its own VS fragments, as well as in its live templates.

When I converted my custom snippets to live templates, I got the behavior that I wanted. Now I can TAB through each field and change them as I go, and end the fragment with TABing past the last item.

For those interested, living templates provide much more functionality. For example, you can run simple field macros so that, for example, feedback fields can have a lowercase letter (if it's a naming convention) when reusing a property name. Highly recommend adding if you can afford it!

+1
source

I can get a fragment of the structure to commit when I press enter / escape, but not on the tab. I do not think that what you want to achieve is possible (or reasonable, for that matter). Agreement with the fragments to complete - either press enter or exit this documented behavior .

Do anything else, breaking convention? You can understand why the convention is useful when you use parameterized fragments, for example. Tabbing allows you to change values ​​several times before you are happy with your decision and press enter.

+3
source

All Articles