VScode中C & C++代码美化工具Uncrustify配置详解
目录:
1:概述
2:下载
3:安装
4:在vscode里玩Uncrustify
0:更新日记
2022/2/5 更新:章节 3.3:使用,后续会更新别的使用方法和可能用各种IDE来使用Uncrustify。
1:概述
由于在学校使用了IDEA和pycharm,现在用什么编辑器都膈应,很难受,水平没上去强迫症和各种要求倒是上来了,现在急需自行搞出一个自己觉得舒服的编辑器,之前尝试了atom确实还不错,但是问题是atom在windows下总感觉卡卡的,而且很多地方配置起来有局限性,比如UE4根本用不了atom ~~当然你会用VS~~,做项目的时候由于自己太菜配置起来难度还是大。所以决定尝试一下别的。 这次使用VScode,作为菜鸡一直没用过,进去就遇到了上述属于技术性人菜瘾大和人笨刀钝问题。本文通过使用Uncrustify工具格式化代码达到美化的效果。
2:下载
Uncrustify下载很简单。链接:http://uncrustify.sourceforge.net/
在下方的Where to get里面可以找到SourceForge的链接点进去下载就好了。同时还有GitHub项目的地址也可以点进去看看。
3:安装
##### 3.1:解压你的Uncrustify 随便把你的Uncrustify解压到你喜欢的位置(如果解压到atom的package目录下会在插件管理里看到),里面会有一个bin和一个share文档。bin里是我们需要的程序,而share里是一些参考文档配置分享等等。3.2:环境变量
配置环境变量path,添加一条Uncrustify目录的bin目录即可。
3.3:使用
接下来你就可以用Uncrustify了,配置文件就不分析了,花了一个下午才把自己的配置文件改好,模板在 Uncrustify/share/doc/uncrustify/examples
里,你可以用这些个模板然后自己改一改什么的,或者也可以下载我的:
链接:https://pan.baidu.com/s/1DlwaLpKqgXUHd4BxCq-aUA:
提取码:nm5v
搞好配置文件了以后放在你喜欢的地方。接下来我们看看怎么使用。如果你使用的是qt或者atom他们都有加载Uncrustify的方法,这里不管他们,我们从命令行起步(将就在vscode里玩了,方便看目录树和文件)。
在命令行中输入uncrustify
,会看到他的手册页(你应该会看到如下图,如果出现uncrustify
不是内部命令等多半是环境变量问题):
下面直接贴出手册页内容:
PS G:\cProject\LearnOpenGL> uncrustify
Usage:
uncrustify.exe [options] [files ...]
If no input files are specified, the input is read from stdin
If reading from stdin, you should specify the language using -l
or specify a filename using --assume for automatic language detection.
If -F is used or files are specified on the command line,
the output filename is 'prefix/filename' + suffix
When reading from stdin or doing a single file via the '-f' option,
the output is dumped to stdout, unless redirected with -o FILE.
Errors are always dumped to stderr
The '-f' and '-o' options may not be used with '-F' or '--replace'.
The '--prefix' and '--suffix' options may not be used with '--replace'.
Basic Options:
-c CFG : Use the config file CFG, or defaults if CFG is set to '-'.
-f FILE : Process the single file FILE (output to stdout, use with -o).
-o FILE : Redirect stdout to FILE.
-F FILE : Read files to process from FILE, one filename per line (- is stdin).
--check : Do not output the new text, instead verify that nothing changes when
the file(s) are processed.
The status of every file is printed to stderr.
The exit code is EXIT_SUCCESS if there were no changes, EXIT_FAILURE otherwise.
files : Files to process (can be combined with -F).
--suffix SFX : Append SFX to the output filename. The default is '.uncrustify'
--prefix PFX : Prepend PFX to the output filename path.
--replace : Replace source files (creates a backup).
--no-backup : Do not create backup and md5 files. Useful if files are under source control.
--if-changed : Write to stdout (or create output FILE) only if a change was detected.
-l : Language override: C, CPP, D, CS, JAVA, PAWN, OC, OC+, VALA.
-t : Load a file with types (usually not needed).
-q : Quiet mode - no output on stderr (-L will override).
--frag : Code fragment, assume the first line is indented correctly.
--assume FN : Uses the filename FN for automatic language detection if reading
from stdin unless -l is specified.
Config/Help Options:
-h -? --help --usage : Print this message and exit.
--version : Print the version and exit.
--count-options : Print the number of available options and exit.
--show-config : Print out option documentation and exit.
--update-config : Output a new config file. Use with -o FILE.
--update-config-with-doc : Output a new config file. Use with -o FILE.
--universalindent : Output a config file for Universal Indent GUI.
--detect : Detects the config from a source file. Use with '-f FILE'.
Detection is fairly limited.
--set <option>=<value> : Sets a new value to a config option.
Debug Options:
-p FILE : Dump debug info into FILE, or to stdout if FILE is set to '-'.
Must be used in combination with '-f FILE'
-ds FILE : Dump parsing info at various moments of the formatting process.
--dump-steps FILE This creates a series of files named 'FILE_nnn.log', each
corresponding to a formatting step in uncrustify.
The file 'FILE_000.log' lists the formatting options in use.
Must be used in combination with '-f FILE'
-L SEV : Set the log severity (see log_levels.h; note 'A' = 'all')
-s : Show the log severity in the logs.
--decode : Decode remaining args (chunk flags) and exit.
--tracking_space FILE : Prepare tracking informations for debugging.
Cannot be used with the -o option'
Usage Examples
cat foo.d | uncrustify -q -c my.cfg -l d
uncrustify -c my.cfg -f foo.d
uncrustify -c my.cfg -f foo.d -L0-2,20-23,51
uncrustify -c my.cfg -f foo.d -o foo.d
uncrustify -c my.cfg -f foo.d -o foo.d -ds dump
uncrustify -c my.cfg foo.d
uncrustify -c my.cfg --replace foo.d
uncrustify -c my.cfg --no-backup foo.d
uncrustify -c my.cfg --prefix=out -F files.txt
Note: Use comments containing ' *INDENT-OFF*' and ' *INDENT-ON*' to disable
processing of parts of the source file (these can be overridden with
enable_processing_cmt and disable_processing_cmt).
There are currently 787 options and minimal documentation.
Try UniversalIndentGUI and good luck.
我们需要用上面搞好的配置文件去对一个源文件进行处理并且得到一个格式化后的输出。举个例子如下图是我的新玩具glad的一段程序,包含opengl上下文等等一些内容,很明显采用的是大括号上下对齐,if
和for
等后面只有语句没有括号等风格,当然我最早学c的时候就喜欢这种风格,但因为后面洗心革面了被迫学习了一下讨厌的java,决定使用新的风格(待会看格式化后的风格)。
接着我们调出terminal然后输入uncrustify -c 配置目录 -f 源文件 -o 输出文件
命令得到的新文件如下图左侧的temp.cpp(我的命令是uncrustify -c -f .\src\main.cpp -o .\src\temp.cpp
因为我有默认配置所以-c后面没有目录参数):
可以观察到,右边新文件已经按照之前一下午调好的配置文件(***.cgf)文件格式化了,是不是很棒棒?
4:在vscode里配置Uncrustify
首先你需要下载Uncrustify插件:
接着奇怪的事情就发生了,要如何在vsc里调用它呢?不可能在命令行里像上文那样敲命令吧,有点恐怖。VSCode对于才接触的朋友来说有些配置不太友好,因为全是Jason字符串做配置门槛不免有点高,不能靠点鼠标配置了。
直接在文本中按下shift+alt+f
这是vscode格式化代码的指令,按下后会执行format.Document命令,如果你是第一次按会提示你没有指定格式化代码的程序会有一个对话框选择uncrustify即可,但是,这个对话框只会出现一次,很崩溃。
那么如果现在要更改uncrustify怎么办呢,比如换成clang-format或者ms-vscode.cpptools工具怎么办呢。进入设置,点击右上角的小按钮。(进入jason文件)
进入以后会发现我下面图的还多了个configPath是我添加的,如果你才下载应该是没有这一行的,但如果你设置了uncrustify那就会有(插件怎么用就不放了,直接set uncrustify的extension setting就好)。
如果我们之前用对话框的方式设定了默认格式化工具就会多一个这个。
所以说如果你想换,直接把这一行去掉或者把字符串改掉就好了,但是这个字符串为什么那么长多了这么多,是因为插件会有一个Extension id,通过这个找到目标插件。
点击上方的copy和copy extension id你会得到下图的内容
当你完成以上所有配置的时候就可以按shift+alt+f
默认按键来格式化你的c++代码了,如果你需要别的工具格式化也可以用同样的方式来设置,但这意味着你需要打更多的插件和扩展。