How to create a 'surround with' type of snippet in Visual Studio 2010?
Asked Answered
O

4

46

Is there a way to create a 'surround with' snippet in visual studio 2010? I know how to create a replacement type of snippet. A simple surround with snippet could surround a block of text with an asp:hyperlink. Similar to the way the default 'surround with' snippets can surround a block of code with an asp:panel.

Orlon answered 5/5, 2011 at 16:49 Comment(1)
slightly different question, but same answer: https://mcmap.net/q/180625/-how-do-i-wrap-a-selection-with-an-html-tag-in-visual-studioGains
R
28

Have a look at the MSDN article on creating snippets. It should give you a good start. When you create your snippet, be sure to give it a SnippetType of SurroundsWith to make the snippet surround selected code.

For the latest Visual Studio (currently 2017), there's Code Snippets on Microsoft Docs, as well as Walkthrough: Creating a Code Snippet.

Rivulet answered 5/5, 2011 at 17:24 Comment(3)
I don't know how you guys do it but snippets never work for me! Not even when I replace code in the built-in ones and place them in the appropriate folder! (VsDir\Code Snippets\Visual Basic) or (VsDir\Code Snippets\Visual C#)Billfish
@Alex: I always use Resharper, which has vastly superior snippet support, among support for just about everything else.Rivulet
Problem with resharper is, once you are used to it, you wont survive without it and miss out on basic how to do.Staal
D
48

It turns out that there are some pre-defined ID's that are not well documented. Specifically For SurroundWith type snippets, there is an ID $selected$. So, for example, the code for the #if snippet is:

...
<Code Language="csharp">
    <![CDATA[#if $expression$ $selected$ $end$ #endif]]>
</Code>
...

The $end$ ID indicates where to place the cursor when the Surround function is complete. That's really all there is to it. Of course, remember to include SurroundsWith as the SnippetType

For more examples, try taking a look at the predefined snippets in C:\Program Files\Microsoft Visual Studio 10.0\\Snippets\1033\.

Donatus answered 18/10, 2011 at 22:39 Comment(4)
VS2012 doesn't store snippets in the same place as VS2010. If you open the Code Snippets Manager and click on a set of snippets it'll show you the path where they are stored. It's not miles away but it still stumped me until I found the pathSerpasil
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#Inglis
This answer contains the documentation that Microsoft should have written. Thanks for sharing @DiamondBack.Semiprofessional
For reference, the $selected$ keyword actually IS documented, along with the $end$ keyword here: learn.microsoft.com/en-us/visualstudio/ide/… They should have included this in their main article for creating code snippets.Interferon
R
28

Have a look at the MSDN article on creating snippets. It should give you a good start. When you create your snippet, be sure to give it a SnippetType of SurroundsWith to make the snippet surround selected code.

For the latest Visual Studio (currently 2017), there's Code Snippets on Microsoft Docs, as well as Walkthrough: Creating a Code Snippet.

Rivulet answered 5/5, 2011 at 17:24 Comment(3)
I don't know how you guys do it but snippets never work for me! Not even when I replace code in the built-in ones and place them in the appropriate folder! (VsDir\Code Snippets\Visual Basic) or (VsDir\Code Snippets\Visual C#)Billfish
@Alex: I always use Resharper, which has vastly superior snippet support, among support for just about everything else.Rivulet
Problem with resharper is, once you are used to it, you wont survive without it and miss out on basic how to do.Staal
B
5

An Example and some advice (based on the previous answers) for VS2017:

The example is for an XML snippet but I'm guessing that this will apply more or less to any language.

  1. Paste the code below into a file and save anywhere as example.snippet
  2. from VS do Tools->Code Snippet Manager, select Language of XML and then My XML Snippets
  3. Click Import and select the example.snippet.
  4. Click Finish and OK
  5. At this point I restarted VS but such draconian measures might not be requred.
  6. In an XML file select the text you want to surround with para tags.
  7. Ctrl+K, Ctrl+S, select My XML Snippets and then "Example of a SurroundWith Snippet".

A <para> tag will be placed before your text and </para> after.

If you include a trailing new line then you get an extra blank line after the text and the text following the end tag appears on the same line as the end tag. It makes sense if you think about it.

If you select only part of the text on a line then you may or may not get what you are expecting. The snippet might need tweaking.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Example of a SurroundsWith snippet</Title>
      <Shortcut>shortcutsdontwork</Shortcut>
      <Description>
        complete example of SurroundsWith
        will put para tag on the line above selected text
        and /para end tag on the line below selected text
        will positon the cursor immediately after the
        closing angle bracket of the end tag
      </Description>
      <Author>mikedamay - TheDisappointedProgrammer</Author>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="XML">
        <![CDATA[<para>
        $selected$
        </para>$end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

I will restrict my editorial comments about the behaviour of code snippets in VS2017 to saying that the implementation is unusual for the modern Microsoft tools team. In particular beware of claims about what shortcuts do. As far as I can see they do nothing useful.

Bassarisk answered 28/9, 2017 at 9:19 Comment(0)
S
1

Appart from useful answers above, here is Code Snippets Schema Reference, which is useful for figuring out valid values for snippets elements.

Sileas answered 7/4, 2017 at 12:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.