0

问题:通常,如果要将代码保存为片段,则必须打开一些 xml 模板、调查标签等。参考

是否可以尽可能简化这个过程:即创建一个SurroundsWith片段,将通过片段代码包装选定的代码?

4

1 回答 1

1

请尝试以下操作:

  1. 将此代码保存为create_new_snippet.snippet并通过代码片段管理器(Ctrl+K、Ctrk+B)将其添加到您的 SSMS
<?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. 选择您的代码(您要保存为代码段)并调用Surround With...代码段 ( Edit\IntelliSense\Surround With... Ctrl+K, Ctrl+S)

  2. 如有必要,编辑参数(如标题、描述等)并保存[YourSnippetName].snippet到您的代码片段文件夹中。

在此处输入图像描述

于 2019-10-14T09:01:59.173 回答