zoukankan      html  css  js  c++  java
  • vs中的code snippet技术

        注:本文是翻译msdn上的文章,原文http://msdn.microsoft.com/zh-cn/library/ms379562(VS.80).aspx本来是想尽量保持与原文的一致,但是时间有限省略了一部分,翻译的不是很恰当,不妥之处请指正。
        
           Summary:这篇文章探究vs2005和vs2005 express 版本(包括vs2008,其实没多大区别,有区别的地方我会用括号做提示)的code snippet技术,包括一个完整的从建立到注册自定义代码扩展的过程.

       一、理解code snippet的角色

          在vs2005中及visual c# express版中都支持一种技术,被称为code snippet,这项技术是下面两种相关代码生成技术的基础:

         1、扩展模板(expansion template)(中文vs里翻译为代码段)

         2、围绕

        总之,code snippet 技术存在的唯一原因--开发人员的生产力。扩展模板和围绕使开发者可以快速的使用下面的任何途径产生代码块。

        ·IDE的 编辑-〉智能感应菜单

         ·快捷键

        ·鼠标右键

        ·输入一个已注册的snappet的shortcut

      二、简单的示例

         1)代码段示例

       为了说明expansion template的作用,假设你已经传见了一个c# 类(叫做SportsCar),而且希望快速的给它添加一个属性,除了可以手动输入外,你可以使用上面提到的方式激活property扩展(Expansion),如IDE的 编辑-〉智能感应-〉插入代码段可,在选择列表中选择property(vs2008里是prop)如下图:

    可以双击,或者按两下tab键,一旦激活就会产生如下代码(vs2005会产生一个私有字段)


          
    public int MyProperty { getset; }

     如下图:

     黄色的部分(vs2008中颜色不一样)可以编辑属性名称(可以通过tab键来在高亮的字段间跳转)

                 2)围绕示例

          为了说明,假设你有段代码想把它放到# #region / #endregion 指令当中,首先选中代码段,右击选择插入代码段

     在弹出的列表中选择#region,编辑region,name

        三、 Code Snippet 文件的位置

             在下面的位置中你可以找到,大量的内置的 Code Snippet 文件,vs2005中是以.xml结尾,vs2008中是 .snippet结尾

        <drive>:\Program Files\Microsoft Visual Studio 8\VC#\ Expansions\1033\Expansions

       <drive>::\Program Files\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#
    //<drive>驱动器名

        四、code snippet文件的结构
    代码
    <?xml version="1.0" encoding="utf-8" ?>
    <CodeSnippet Format="1.0.0">
      
    <Header>
        
    <Title>class</Title>
        
    <Shortcut>class</Shortcut>
        
    <Description>Expansion snippet for class</Description>
        
    <SnippetTypes>
          
    <SnippetType>Expansion</SnippetType>
          
    <SnippetType>SurroundsWith</SnippetType>
        
    </SnippetTypes>
      
    </Header>
      
    <Snippet>
        
    <Declarations>
          
    <Literal default="true">
            
    <ID>name</ID>
            
    <ToolTip>Class name</ToolTip>
            
    <Default>MyClass</Default>
          
    </Literal>
        
    </Declarations>
       
    <Code Language="csharp" Format="CData">
        
    <![CDATA[class $name$
        {
          $selected$$end$
        }]]
    >
       
    </Code>
      
    </Snippet>
    </CodeSnippet>

    包含一个名为CodeSnippet的根节点,和两个字节点:Header,Snippet,(vs2008中在CodeSnippet外有CodeSnippets节点)

                           <Header>节点及子字节点

                header节点用来描述code snappet的基本属性,包括如下字节点

                

    <Header> Sub-Element Definition
    <Title> The display title for the code snippet.
    <Shortcut> Defines the shortcut for the code snippet.

    In the IDE, type in the shortcut name to select the snippet followed by the Tab. If you type a partial shortcut name ( 'cla' rather than 'class' for example), you will need to Tab twice; once to complete the named expansion and again to insert the snippet.

    <Description> A human-readable description of the snippet, which is displayed when selecting a snippet from the IDE.
    <SnippetType> Specifies which category the code snippet belongs to (Expansion, SurroundsWith or Refactoring). Do note that a single code snippet may belong to more than one group.

    Microsoft IDEs use this value to determine which context menu will be used to display the code snippet.

                 <Snippet>节点及子节点

                  snippet节点主要用来定义两部分内容:

                      a,变量,就是插入时高量的部分

                                                      b,代码骨架

                  包含的子节点有两个如下literal,code:

    <Declarations>
      
    <Literal default="true">
        
    <ID>name</ID>
        
    <ToolTip>Class name</ToolTip>
        
    <Default>MyClass</Default>
      
    </Literal>
    </Declarations>
    <Code Language="csharp" Format="CData">
      <![CDATA[class $name$
      {
        $selected$$end$
      }]]>
    </Code>

                                             

    <Literal> 的子节点 Definition
    <ID> 变量的表示
    <ToolTip> 鼠标移动到上面的提示信息.
    <Default> 变量的默认值.

                  <Code>节点

              code用来定义要插入的代码,在上面的<Snippet>节点中可以看到有一部分用$$包围着,这种语法是用来引用上面 <Literal>的<ID>结点定义的变量(注意名称的一致)

                                 关于 $selected$  $end$

                                          $selected$是在插入代码段(围绕)时vs用来替换你选中的内容,$end$是插入完成后鼠标停留的位置

           五、注册自定义的code snappet

                            两种方式:

              最简单的直接把文件复制到 <drive>:\Program Files\Microsoft Visual Studio 8(9)\VC#\ Expansions\1033\Expansions
    下,(vs2008是:X:\Program Files\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#),第二种通过菜单Tools | Code Snippet Manager 再弹出菜单中添加即可。

                 附件:下面是我自己写的asp.net mvc 的action方法的snappet,注册完后在vs中输入action即可激活

                        action.xml  因博客上传文件的限制我把它改名为xml,下载后直接改为.snappet即可

  • 相关阅读:
    BZOJ 1191 HNOI2006 超级英雄hero
    BZOJ 2442 Usaco2011 Open 修建草坪
    BZOJ 1812 IOI 2005 riv
    OJ 1159 holiday
    BZOJ 1491 NOI 2007 社交网络
    NOIP2014 D1 T3
    BZOJ 2423 HAOI 2010 最长公共子序列
    LCA模板
    NOIP 2015 D1T2信息传递
    数据结构
  • 原文地址:https://www.cnblogs.com/actberw/p/1708395.html
Copyright © 2011-2022 走看看