Add code snippets for CLANG in VS Code
前记:今天试着用了下Atom,发现Atom居然预装了CLANG的snippets,而且远比VSCode的已有拓展C/C++ Snippets中的丰富!身为VSCode的死忠粉,我决定立马把Atom的 C snippets搬到VSCode上来。
既然你点开了这个页面,就说明要么你不知道VSCode上已有拓展C/C++ Snippets,要么你对这个拓展失望透顶。对于后者,本文将为你介绍如何在VSCode上设置snippets,并为你提供一套可以直接用的C语言的snippets。
什么是 snippet
snippet[ˈsnɪpɪt],或者说code snippet,指的是能够帮助输入重复代码模式串,比如循环或条件语句的模板。通过snippet,仅仅输入一小段代码就可以生成预定义的模板代码,甚至可以通过内部跳转快速补全模板。
当然,看图更易懂:
图中将atc替换成了定义ActiveSupport::TestCase(简称ATC)子类的模板,并跳转到case_name处进行了修改 .etc.
如何配置snippet
操作流程
进入snippet设置文件
这里提供了两种方法:
按alt键切换菜单栏,通过文件>首选项>用户代码片段,选择进入目的语言的代码段设置文件;
通过快捷键ctrl+shift+P打开命令窗口(all command window),输入snippet,通过候选栏中的选项进入目的语言的代码段设置文件。
填写snippets
VSCode中snippet的文法1
设置文件头部通过一个块注释讲解了snippet的文法,了解json就不会对此感到奇怪了。
// Place your snippets for C here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
"Print to console": {
"prefix": "log",,
"body": [
"console.log('$1');",
"$2"
],
"description": "Log output to console"
}
上例snippet在输入log后效果如下,在确定后将替换为console.log('');。
snippet由三部分组成:
prefix:前缀,定义了snippets 从IntelliSense中