vs 自定义注释/代码段



  1. 代码段管理:
    创建一个snippet文件,导入到vs中。键入自定义字符后,tab键两次,自动生成注释/代码段。
    snippet是讲一个key 与一段字符关联(可以使注释,可以是代码)
    如~~vs中输入tryf,两次tab后直接输出

    1
    2
    3
    4
    5
    6
    try 
    {
    }
    finally
    {
    }
  2. 自定义文件注释模板
    vs中输入guccangFC ,tab键2次,自动生成如下注释。

    1
    2
    3
    4
    5
    6
    7
    8
    /****************************************************************************
    File: AutoTest.cs
    Desc: Automated testing
    Date: 2015-7-27
    Author: guccang
    URL: http://guccang.github.io
    Email: guccang@126.com
    ****************************************************************************/
  3. 对应的snippet文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?xml version="1.0" encoding="utf-8"?>

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

<CodeSnippet Format="1.0.0">
<Header>
<Title>FileComment</Title>
<Shortcut>guccangFC</Shortcut>
<Description>FileComment 语句的代码段</Description>
<Author>guccang</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>

<Snippet>
<Declarations>
<Literal>
<ID>Name</ID>
<ToolTip>文件名</ToolTip>
<Default>fileName</Default>
</Literal>

<Literal>
<ID>License</ID>
<ToolTip>授权</ToolTip>
<Default>MIT</Default>
</Literal>

<Literal>
<ID>Author</ID>
<ToolTip>作者</ToolTip>
<Default>guccang</Default>
</Literal>

<Literal>
<ID>Date</ID>
<ToolTip>日期</ToolTip>
<Default>2015-7-21 11:11:11</Default>
</Literal>

<Literal>
<ID>Desc</ID>
<ToolTip>描述</ToolTip>
<Default>添加文件描述</Default>
</Literal>

<Literal>
<ID>URL</ID>
<ToolTip>url</ToolTip>
<Default>http://guccang.github.io</Default>
</Literal>

<Literal>
<ID>Email</ID>
<ToolTip>Email</ToolTip>
<Default>guccang@126.com</Default>
</Literal>


</Declarations>

<Code Language="csharp">
<![CDATA[
/****************************************************************************
File: $Name$
Desc: $Desc$
Date: $Date$
Author: $Author$
URL: $URL$
Email: $Email$
****************************************************************************/
$selected$ $end$]]>
</Code>

</Snippet>
</CodeSnippet>
</CodeSnippets>
  1. 将上述文件保存为fileComment.snippet , snippet文件扩展名
    打开vs导入snippet文件。
    vs-工具-代码段管理
    图片

  2. 参考资料
    [官方]https://msdn.microsoft.com/zh-cn/library/ms165394(v=vs.120).aspx
    博客01
    博客02
    博客03