Qt Creator使用Clang Format
習(xí)慣性的想格式化代碼,發(fā)現(xiàn)Qt Creator默認(rèn)居然是沒(méi)有代碼格式化的,只有一個(gè)縮進(jìn)。
Qt Creater中有個(gè)插件:beautifier,在"幫助->關(guān)于"插件中,開(kāi)啟“Beautifier”即可(會(huì)提醒需要重啟Qt creator)。
注:Qt Creator安裝包中不包含這個(gè)格式化工具,它的作用是調(diào)用格式化工具進(jìn)行格式化。
windows系統(tǒng):
安裝:
官方的說(shuō)支持三種外部格式化工具:Artistic Style、ClangFormat、Uncrustify 。
這里僅以 ClangFormat 來(lái)說(shuō)明:
ClangFormat 是LLVM的一個(gè)子功能,LLVM是類似GCC的東西。
https://github.com/llvm/llvm-project/releases/
所以,想用 ClangFormat ,就需要下載LLVM,根據(jù)上面的地址,搜索適合自己的版本即可。
安裝LLVM的時(shí)候PATH選項(xiàng)無(wú)所謂,因?yàn)镼t Creater的beautifier的Clang Format選項(xiàng)中,既可以根據(jù)PATH搜索,也可以自行指定路徑。
配置:
安裝好LLVM之后,就可以設(shè)置 Clang Format 選項(xiàng)。
“編輯->preferences”; “Beautifier->Clang Format”: Configure: Clang Format command:
先配置路徑(如:C:\Program Files\LLVM\bin\clang-format.exe );
選擇代碼樣式
默認(rèn)樣式和自定義樣式。
默認(rèn)樣式:LLVM、Google、Chromium、Mozilla、WebKit、File
需要重點(diǎn)說(shuō)一下File樣式:
因?yàn)槠渌臉邮蕉际墙o定的樣式,而 File 則不是。
File 是 clang-format.exe -style=file 的意思,意味著 clang-format.exe 會(huì)去搜索樣式文件( *.clang-format 文件)。
需要注意的是,它的搜索路徑是當(dāng)前文件所在的文件夾或者當(dāng)前項(xiàng)目。
自定義樣式:Options -> Use customized style -> Add
AccessModifierOffset: 0
AlignEscapedNewlinesLeft: false
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackParameters: false
BreakBeforeBinaryOperators: false
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 128
BreakBeforeBraces: Attach
CommentPragmas: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 0
ContinuationIndentWidth: 0
Cpp11BracedListStyle: false
DerivePointerBinding: false
IndentCaseLabels: true
IndentFunctionDeclarationAfterType: false
IndentWidth: 4
Language: Cpp
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
ObjCBlockIndentWidth: 4
PenaltyBreakBeforeFirstCallParameter: 100
PenaltyBreakComment: 100
PenaltyBreakFirstLessLess: 0
PenaltyBreakString: 100
PenaltyExcessCharacter: 1
PenaltyReturnTypeOnItsOwnLine: 20
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
TabWidth: 4
UseTab: Never
使用:
工具->Beautifier->ClangFormat 里面有兩個(gè)選項(xiàng),分別是格式化當(dāng)前文件、格式化選定內(nèi)容。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-688839.html
注:也可使用快捷方式進(jìn)行操作,“編輯->preferences->環(huán)境->鍵盤->ClangFormat": ***文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-688839.html
linux系統(tǒng):
macOS系統(tǒng):
到了這里,關(guān)于Qt Creator使用Clang Format方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!