Search code examples
sql-serverssmscode-snippetsvscode-snippets

Snippet to create Snippet in SSMS


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?


Solution

  • 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