Snippet to create Snippet in SSMS

☆樱花仙子☆ 提交于 2020-04-17 22:52:16

问题


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:


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.



来源:https://stackoverflow.com/questions/58373350/snippet-to-create-snippet-in-ssms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!