开发者

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

开发者_JS百科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.


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\.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜