6

For some time I have had a custom Visual Studio code snippet to assist in injecting a copyright header in my C# source files. It looks something like this:

<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>File Header</Title>
    <Author>Me</Author>
    <Shortcut>header</Shortcut>
    <Description>Inserts a standard copyright header.</Description>
    <SnippetTypes>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>FileName</ID>
        <ToolTip>The name of the C# code file.</ToolTip>
        <Default>FileName</Default>
      </Literal>
    </Declarations>
    <Code Language="CSharp"><![CDATA[// -----------------------------------------------------------------------
// <copyright file="$FileName$.cs" company="Company Name">
// Copyright © 2011-2016 by Company Name. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

]]></Code>
  </Snippet>
</CodeSnippet>

The important thing to note for this question is the two trailing endlines at the end of the CDATA block. In editions of Visual Studio prior to 2015, I could place my cursor at the beginning of a file, right before the first using declaration, type header+TAB, and my header would appear with an extra empty line in between the last comment and the first using declaration.

Visual Studio 2015 appears to not honor the trailing whitespace. When I type header+TAB, the first using declaration appears on the same line as the last comment.

Am I looking at a bug, or is there a way to configure my code snippet so that Visual Studio 2015 will honor the trailing whitespace?

4
  • Have you tried putting in a \n on the last line? Commented Jun 14, 2016 at 19:33
  • Thanks @PaulSwetz. Alas, that causes the snippet to inject the literal text, \n onto my source file. Similarly, adding &#xD;&#xA; outside of the CDATA block (XML equivalent of \r\n) does not rectify the issue. Commented Jun 14, 2016 at 19:45
  • The common thing Im seeing looking the snippets that come with VS is most the code end with $end$ Example from switch <Code Language="csharp"><![CDATA[switch ($expression$) { $cases$ }$end$]]> </Code> Commented Jun 14, 2016 at 19:53
  • Thanks, @PaulSwetz. I tried this and it did, in fact, solve the issue. If you post your comment as an answer, I'll gladly give it a vote and an accept. Commented Jun 14, 2016 at 23:44

1 Answer 1

5

The common thing I'm seeing looking at the snippets that come with VS is most the code ends with $end$

Example from switch:

<Code Language="csharp"><![CDATA[switch ($expression$) { $cases$ }*$end$*]]> </Code> 

Place $end$ at the end of the trailing whitespace, like so:

<![CDATA[// -----------------------------------------------------------------------
// <copyright file="$FileName$.cs" company="Company Name">
// Copyright © 2011-2016 by Company Name. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

$end$]]>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.