Is it possible to insert a multi-line code snippet relative to the cursor position in Visual Studio?
Asked Answered
G

2

7

When using code snippets in Visual Studio that contain multiple lines the following lines will preserve the whitespace that was set in the .snippet file instead of positioning the code relative to the original cursor placement.

When using the foreach snippet you'll get code like this:

    foreach (var item in collection)
{

}

Instead of:

    foreach (var item in collection)
    {

    }

Is there a way to change this behavior? Is there a keyword that needs to be used in the .snippet file?

Gorrono answered 28/1, 2009 at 16:45 Comment(0)
T
3

The code portion of a snippet file is contained in a CDATA which preserves whitespace. The best thing I can tell you is to go into the file and edit it to suit your needs. Your only other option is to do a quick Ctrl+K and Ctrl+D after you use the snippet to auto-format the code which will fix the indenting.

Tarahtaran answered 28/1, 2009 at 16:54 Comment(0)
M
2

Edit the snippit files:

  1. Open some text editor, like notepad, as administrator
  2. Open the snippet file you want to fix, e.g., foreach.snippet. (Located in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C# on my computer)
  3. Remove the leading tabs of the Code Element. So change the Code... xml element from:

            <Code Language="csharp"><![CDATA[foreach ($type$ $identifier$ in $collection$)
            {
                $selected$ $end$
            }]]>
            </Code>
    

to:

            <Code Language="csharp"><![CDATA[foreach ($type$ $identifier$ in $collection$)
{
    $selected$ $end$
}]]>
            </Code>
Mcnulty answered 28/3, 2011 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.