QtCreator提供了一个Beautifier插件,用于加载外部工具(Artistic Style、ClangFormat、Uncrustify)对代码进行格式化。
Beautifier会将源代码解析为结构模块,如赋值语句、IF语句块、循环体等等,并用Beautifier指定的选项对模块进行格式化。
Beautifier能够使用预约义和自定义风格对代码进行格式化。html
Clang-format是一个基于LLVM的C++代码格式化工具。
LLVM下载地址:http://ftp.math.utah.edu/pub/llvm/java
根据GCC版本下载相应的LLVM,本人操做系统为RHEL7.3,GCC版本为4.8.5,选择Fedora 20 x86-64的LLVM版本。若是选择Red Hat 6 x86-6四、Red Hat 7 x86-64版本则GLIBC版本要求GLIBCXX_3.4.20,RHEL7.3不能知足。
解压llvm-20140716.tar.gz文件
tar -zxvf llvm-20140716.tar.gz
进入llvm-20140716/bin目录
将clang-format拷贝到/usr/local/bin目录
sudo cp -rf clang-format /usr/local/binlinux
打开QtCreator4.6,点击菜单栏“Help > About Plugins > C++ > Beautifier ”菜单项。
选中Beautifier插件
重启QtCreator4.6,Beautifier插件便可用。
打开“Tools > Options > Beautifier”配置clang-format工具。
选中“Enable auto format on file save”,选择Tool为“ClangFormat”。
在ClangFormat选项页,选择使用预约义风格为Google。
为了防止风格配置文件不可用,能够指定一个回退风格,选择default将使用default风格,选择none将忽略格式化。
在打开“Tools > Options > Environment->Keyboard->ClangFormat->FormatFile”,设置“Key sequence”为Ctrl+Alt+K。
肯定“OK”按钮。git
clang-format预约义了LLVM、Google、Chromium、Mozilla、WebKit五种风格。github
使用QtCreator编辑文件时,保存文件时或是使用快捷键Ctrl+Alt+K,clang-format都会自动格式化当前编辑的文件。web
使用命令行能够导出预约义风格,语法以下:
clang-format -style=格式名 -dump-config > 文件名
格式名的取值能够为llvm, google, chromium, mozilla, webkit中的任一种;文件名能够取任何名字,通常取.clang-format或_clang-format,由于自定义的排版格式文件只有取这两种名字之一,才能被Clang-Format识别。clang-format -style=google -dump-config > .clang-format
ide
Clang-format不会对// clang-format off和// clang-format on代码区间与/ clang-format off /和 / clang-format on /代码区间的代码进行格式化,但注释会被正常格式化。函数
// clang-format off void unformatted_code: // clang-format on /* clang-format off */ void unformatted_code: /* clang-format on */
clang-format支持自定义风格(经过编写 .clang-format 文件),自定义风格一般在预约义风格基础上进行修改。自定义风格文件内容以下:工具
#基础样式 BasedOnStyle: LLVM #指针的*的挨着哪边 PointerAlignment: Right # 缩进宽度 IndentWidth: 4 # 连续的空行保留几行 MaxEmptyLinesToKeep: 1 # 圆括号的换行方式 BreakBeforeBraces: Attach # 是否容许短方法单行 AllowShortFunctionsOnASingleLine: true # 支持一行的if AllowShortIfStatementsOnASingleLine: true # 在未封闭(括号的开始和结束不在同一行)的括号中的代码是否对齐 AlignAfterOpenBracket: true # 圆括号的换行方式 BreakBeforeBraces: Attach # switch的case缩进 IndentCaseLabels: true # 针对OC的block的缩进宽度 ObjCBlockIndentWidth: 4 # 针对OC,属性名后加空格 ObjCSpaceAfterProperty: true # 每行字符的长度 ColumnLimit: 0 # 注释对齐 AlignTrailingComments: true # 括号后加空格 SpaceAfterCStyleCast: false # 换行的时候对齐操做符 AlignOperands: true # 中括号两边空格 [] SpacesInSquareBrackets: false # 多行声明语句按照=对齐 AlignConsecutiveDeclarations: false # 容器类的空格 例如 OC的字典 SpacesInContainerLiterals: false # 在构造函数初始化时按逗号断行,并以冒号对齐 BreakConstructorInitializersBeforeComma: true # 函数参数换行 AllowAllParametersOfDeclarationOnNextLine: true # 每行字符的长度 ColumnLimit: 120 #在续行(\ # 下一行)时的缩进长度 ContinuationIndentWidth: 4 # tab键盘的宽度 TabWidth: 4 # 赋值运算符前加空格 SpaceBeforeAssignmentOperators: true # 行尾的注释前加1个空格 SpacesBeforeTrailingComments: 1
注意:实际使用中,必须注意不一样版本对于某些选项的支持。低版本clang-format可能对于某些选项并不支持。所以,用户须要阅读当前使用clang-format版本的对应文档。测试
Artistic Style是一个支持C、C++, C++/CLI, Objective-C, C#, Java语言的代码格式化工具。
官方网站:
http://astyle.sourceforge.net/
下载地址:
https://sourceforge.net/projects/astyle/files/
解压astyle_3.1_linux.tar.gz
tar -zxvf astyle_3.1_linux.tar.gz
进入astyle/build目录
cd astyle/build
使用CMake生成MakeFile
cmake ../
编译
make
将编译生成的astyle拷贝到/usr/local/bin目录
sudo cp -rf astyle /usr/local/bin
在命令行测试:
astyle
打开QtCreator4.6,点击菜单栏“Help > About Plugins > C++ > Beautifier ”菜单项。
确认Beautifier插件已经被选中。
打开“Tools > Options > Beautifier”配置Artistic-Style工具。
选中“Enable auto format on file save”,选择Tool为“ClangFormat”。
在Artistic Style选项页,选择Artistic Style命令程序所在的路径:/usr/local/bin/astyle。
选中“Use customized style”,点击Add按钮,增长一种自定义风格:
使用ansi风格,在运算符两侧增长空格做为风格。
在打开“Tools > Options > Environment->Keyboard->ArtisticStyle->FormatFile”,设置“Key sequence”为Ctrl+Alt+A。
Artistic Style预约义了多种代码缩进风格,如allman、kr、java、stroustrup、whitesmith、vtk、gnu、linux、horstmann、google、mozilla、lisp、pico、ansi等风格。能够使用--style参数指定。
使用QtCreator编辑文件时,保存文件时或是使用快捷键Ctrl+Alt+A,Artistic Style都会自动格式化当前编辑的文件。
Artistic Style自定义风格一般在预约义风格基础上进行修改。
使用astyle -h能够查看Artistic Style的全部选项参数。
最简单的自定义风格以下:--style=ansi -p
具体选项参数的含义查看http://astyle.sourceforge.net/astyle.html。
Uncrustify是一个支持C, C++, C#, Objective-C, D, Java, Pawn、VALA的代码格式化工具。
官网地址:http://uncrustify.sourceforge.net/
下载地址:https://sourceforge.net/projects/uncrustify/files/
Linux环境下载源码uncrustify-master.zip,源码编译安装。
解压源码压缩包后进入uncrustify-master目录
建立编译目录:mkdir build
进入编译目录:cd build
生成MakeFile:cmake ..
编译:make
进入bin目录:cd bin
拷贝程序到/usr/lcoal/bin:sudo cp -rf uncrustify /usr/lcoal/bin
在命令行测试:uncrustify
打开QtCreator4.6,点击菜单栏“Help > About Plugins > C++ > Beautifier ”菜单项。
确认Beautifier插件已经被选中。
打开“Tools > Options > Beautifier”配置Uncrustify工具。
选中“Enable auto format on file save”,选择Tool为“Uncrustify”。
在Uncrustify选项页,选择Uncrustify命令程序所在的路径:/usr/local/bin/uncrustify。
选中“Use customized style”,点击Add按钮,增长一种自定义风格:
自定义风格的内容参考uncrustify-master/etc/gnu-indent.cfg文件,uncrustify-master/etc有多种风格的配置文件可供参考。
在打开“Tools > Options > Environment->Keyboard->Uncrustify->FormatFile”,设置“Key sequence”为Ctrl+Alt+U。
使用QtCreator编辑文件时,保存文件时或是使用快捷键Ctrl+Alt+U,Uncrustify都会自动格式化当前编辑的文件。
Uncrustify一般经过-c参数读取配置文件。如没有指定-c参数,Uncrustify会尝试从UNCRUSTIFY_CONFIG环境变量读取配置文件或是读取用户主目录下的uncrustify或.uncrustify文件。
uncrustify --show-config
列出全部可用的选项
uncrustify --update-config
uncrustify --update-config-with-doc
列出配置文件中全部的可用选项,二者相同
uncrustify --update-config-with-doc -c path/to/your.cfg
导出当前风格到指定文件
Uncrustify自定义风格能够使用基于web的uncrustify_config配置工具进行生成。
https://cdanu.github.io/uncrustify_config_preview/index.html能够使用界面交互的方式配置自定义风格文件。