Reputation: 29
When using custom snippets, VS always seems to expand the snippet on the next line, creating an empty line which is unwanted.
How can I prevent this?
A custom snippet:
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>Argument not null check</Title>
<Shortcut>an</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>argument</ID>
<ToolTip>Argument name</ToolTip>
<Default>arg</Default>
</Literal>
</Declarations>
<Code Language="csharp" Kind="method body" Delimiter="$"><![CDATA[if ($argument$ == null)
throw new ArgumentNullException("$argument$");]]></Code>
</Snippet>
</CodeSnippet>
How I would expect (and like) the snippets to work:
public void Concat(string a, string b)
{
an
an
return a + b;
}
...
public void Concat(string a, string b)
{
if (a == null)
throw new ArgumentNullException("a");
if (b == null)
throw new ArgumentNullException("b");
return a + b;
}
How it actually works:
public void Concat(string a, string b)
{
if (a == null)
throw new ArgumentNullException("a");
if (b == null)
throw new ArgumentNullException("b");
return a + b;
}
The weird thing is that the CodeContract snippets work as expected, but comparing the xml I can't see how they are different, even when I am using a single line custom snippet.
Upvotes: 1
Views: 435
Reputation: 1025
I ran into the same problem and did some testing. It seems that Visual Studio 2015 inserts a blank line when the code snippet doesn't contain an $end$
marker. If you insert an $end$
marker at the end of your CDATA section inside your <Code>
tag, Visual Studio will stop prepending a blank line at the beginning of your snippet.
Seems like a bug to me. Visual Studio 2013 didn't have this problem.
Upvotes: 2