2

Problem: Usually, if you want to save your code as a snippet, you have to open some xml template, investigate tags etc. Reference

Is it possible to simplify this process as much as possible: i.e. create a SurroundsWith snippet that will wrap selected code by snippet code?

1 Answer 1

4

Please try following:

  1. Save this code as a create_new_snippet.snippet and add it to your SSMS through code snippet manager (Ctrl+K, Ctrk+B)
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Create_new_snippet</Title>
            <Shortcut></Shortcut>
            <Description>Snippet to create a snippet</Description>
            <Author>Denis Sipchenko</Author>
            <SnippetTypes>
                <SnippetType>SurroundsWith</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal> <ID>Title</ID>        <Default>NewSnippetTitle</Default>         <ToolTip>NewSnippetTitle</ToolTip>                   </Literal>
                <Literal> <ID>Description</ID>  <Default>NewSnippetDescription</Default>   <ToolTip>NewSnippetDescription</ToolTip>             </Literal>
                <Literal> <ID>Author</ID>       <Default>Unsung Hero</Default>             <ToolTip>NewSnippetAuthor</ToolTip>                  </Literal>
                <Literal> <ID>SnippetType</ID>  <Default>SurroundsWith</Default>           <ToolTip>SurroundsWith OR Expansion</ToolTip>                  </Literal>
                <Literal> <ID>CodeComment</ID>  <Default>-- Sorry. I was too lazy to write some usefull comment here</Default>         <ToolTip>Comment for you code</ToolTip>                  </Literal>                
            </Declarations>
            <Code Language="sql"
                Delimiter="`">
                <![CDATA[<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>`Title`</Title>    <!--`Title`.snippet -copy/paste for filename-->
            <Shortcut></Shortcut>
            <Description>`Description`</Description>
            <Author>`Author`</Author>
            <SnippetTypes>
                <SnippetType>`SnippetType`</SnippetType>    <!--SurroundsWith/Expansion-->
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal> <ID></ID>     <Default></Default>         <ToolTip></ToolTip>                   </Literal>
            </Declarations>
            <Code Language="SQL">
                <![CDATA[`CodeComment`
$selected$`selected`$end$`end`
]]`fake`>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
  1. Select your code (that you want to save as a snippet) and call Surround With... snippet (Edit\IntelliSense\Surround With... Ctrl+K, Ctrl+S)

  2. Edit parameters if necessary (as Title, Description etc...) and save as [YourSnippetName].snippet into your snippet folder.

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Very creative solution @Denis. Kudos!

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.