如果嫌打cout和打主函数太麻烦又不想装插件的情况下,那么可以自定义代码片段提高开发效率,下面就就简单介绍一下自定义代码片段的语法规则,然后列举一些常用的自定义代码片段。
一、代码片段文件基础结构
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0"> <!-- 必须指定版本 -->
<Header> <!-- 元数据 -->
<Title>代码片段标题</Title>
<Shortcut>触发词</Shortcut> <!-- 如输入"for"触发 -->
<Description>功能描述</Description>
</Header>
<Snippet> <!-- 代码逻辑 -->
<Declarations> <!-- 占位符定义 -->
<Literal Editable="true">
<ID>变量名</ID>
<Default>默认值</Default>
</Literal>
</Declarations>
<Code Language="语言类型"> <!-- 如C++、VB -->
<![CDATA[代码内容]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
二、关键元素讲解
1.Header元素
必需字段:
- <Title>:代码片段标题。如Class Template
- <Shortcut>:触发词。如 for 触发
可选字段:
- <Description>:功能描述
- <Author>:作者
2.Snippet元素
- <Declarations>:定义可编辑的占位符
<Declarations>
<Literal Editable="true"> //是否允许修改
<ID>type</ID> //变量名字
<Default>int</Default> //默认值
</Literal>
</Declarations>
- <code >元素
必须用<![CDATA[ ]]>
包裹代码,避免 XML 转义问题
支持多行代码,每行需独立定义
3.特殊语法标记
标记 | 功能 |
---|---|
$变量名$ | 引用Declarations 定义的占位符(如$className$ ) |
$end$ | 光标最终停留位置 |
三、具体实例(常用代码片段)
cpp_main.snippet
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>cpp_main_clean</Title>
<Shortcut>main</Shortcut>
<Description>纯净版C++主函数模板</Description>
</Header>
<Snippet>
<Code Language="CPP">
<![CDATA[#include <iostream>
using namespace std;
int main() {
$end$
return 0;
}]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
cpp_cout_endl.snippet
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>cpp_cout_endl</Title>
<Shortcut>coutl</Shortcut>
<Description>带换行的标准输出语句</Description>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>caption</ID>
<ToolTip>输入要显示的内容</ToolTip>
<Default>output</Default>
</Literal>
</Declarations>
<Code Language="CPP">
<![CDATA[cout << "$caption$" << endl;$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
getset.snippet
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>getset</Title>
<Shortcut>getset</Shortcut> <!-- 添加触发关键词 -->
<Description>Generate getter and setter for C++</Description>
</Header>
<Snippet>
<!-- 声明占位符变量 -->
<Declarations>
<Literal>
<ID>type</ID>
<Default>int</Default>
</Literal>
<Literal>
<ID>Name</ID>
<Default>Name</Default>
</Literal>
<Literal>
<ID>name</ID>
<Default>VariableName</Default>
</Literal>
</Declarations>
<Code Language="cpp">
<![CDATA[$type$ get$Name$() const { return $name$; }
void set$Name$($type$ $name$) { this->$name$ = $name$; }
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
四、导入到自定义代码片段
如下图所示,或者打开上面位置的目录,直接将.snippet文件拖入即可。