【杂项】C & C++美化工具Uncrustify配置详解

C & C++美化工具Uncrustify配置详解


目录:

1:概述
2:下载
3:安装


0:更新日记

    2022/2/5 更新:章节 3.3:使用,后续会更新别的使用方法和可能用各种IDE来使用Uncrustify。


1:概述

    由于在学校使用了IDEA和pycharm,现在用什么编辑器都膈应,很难受,水平没上去强迫症和各种要求倒是上来了,现在急需自行搞出一个自己觉得舒服的编辑器,之前尝试了atom确实还不错[(【杂项】win10搭建atom+cpp环境)](https://blog.csdn.net/qq_39949271/article/details/122143445?spm=1001.2014.3001.5502),但是问题是atom在windows下总感觉卡卡的,而且很多地方配置起来有局限性,比如UE4根本用不了atom ~~当然你会用VS~~,做项目的时候由于自己太菜配置起来难度还是大。所以决定尝试一下别的。     这次使用VScode,作为菜鸡一直没用过,进去就遇到了上述属于技术性~~人菜瘾大和人笨刀钝~~问题。本文通过使用Uncrustify工具格式化代码达到美化的效果。

2:下载

Uncrustify下载很简单。 **链接**:[http://uncrustify.sourceforge.net/](http://uncrustify.sourceforge.net/)

    在下方的Where to get里面可以找到SourceForge的链接点进去下载就好了。同时还有GitHub项目的地址也可以点进去看看。
【杂项】C & C++美化工具Uncrustify配置详解

3:安装

##### 3.1:解压你的Uncrustify     随便把你的Uncrustify解压到你喜欢的位置(如果解压到atom的package目录下会在插件管理里看到),里面会有一个bin和一个share文档。bin里是我们需要的程序,而share里是一些参考文档配置分享等等。 ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=e2f70c23ed0a48f39ccff8c15736c3c6.png) ##### 3.2:环境变量     配置环境变量path,添加一条Uncrustify目录的bin目录即可。 ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=f66d591e4c034e849229d5f11b85ce37.png?,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA54ix5YaS6Zmp55qEcc-AKHMsYSk=,size_20,color_FFFFFF,t_70,g_se,x_16) ##### 3.3:使用     接下来你就可以用Uncrustify了,配置文件就不分析了,花了一个下午才把自己的配置文件改好,模板在 `Uncrustify/share/doc/uncrustify/examples` 里,你可以用这些个模板然后自己改一改什么的,或者也可以下载我的: [链接:https://pan.baidu.com/s/1DlwaLpKqgXUHd4BxCq-aUA:](https://pan.baidu.com/s/1DlwaLpKqgXUHd4BxCq-aUA) 提取码:nm5v

    搞好配置文件了以后放在你喜欢的地方。接下来我们看看怎么使用。如果你使用的是qt或者atom他们都有加载Uncrustify的方法,这里不管他们,我们从命令行起步(将就在vscode里玩了,方便看目录树和文件)。

    在命令行中输入uncrustify,会看到他的手册页(你应该会看到如下图,如果出现uncrustify不是内部命令等多半是环境变量问题):
【杂项】C & C++美化工具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上下文等等一些内容,很明显采用的是大括号上下对齐,iffor等后面只有语句没有括号等风格,当然我最早学c的时候就喜欢这种风格,但因为后面洗心革面了*学习了一下讨厌的java,决定使用新的风格(待会看格式化后的风格)。
【杂项】C & C++美化工具Uncrustify配置详解
    接着我们调出terminal然后输入uncrustify -c 配置目录 -f 源文件 -o 输出文件命令得到的新文件如下图左侧的temp.cpp(我的命令是uncrustify -c -f .\src\main.cpp -o .\src\temp.cpp因为我有默认配置所以-c后面没有目录参数):
【杂项】C & C++美化工具Uncrustify配置详解
    可以观察到,右边新文件已经按照之前一下午调好的配置文件(***.cgf)文件格式化了,是不是很棒棒?
【杂项】C & C++美化工具Uncrustify配置详解

上一篇:阮一峰ES6学习-Symbol


下一篇:第三周任务