天天看點

Qt Creator使用clang-format實作源代碼格式化排版(Windows/macOS2)

重點來了,在Value裡,輸入以下部分;

# 程式設計語言: None, Cpp, Java, JavaScript, ObjC, Proto, TableGen, TextProto
Language: Cpp
# 基礎樣式
BasedOnStyle: Google
#指針的*的挨着哪邊,例如int* a
DerivePointerAlignment: false
PointerAlignment: Left
# 通路修飾符前的空格,例如public,private等
AccessModifierOffset: -4
# 縮進寬度
IndentWidth: 4
# 要保留的最大連續空行數
MaxEmptyLinesToKeep: 1
# 大括号{}的換行方式,數值是Allman或Attach
BreakBeforeBraces: Allman
# 是否允許短方法單行,例如int f() { return 0; }
AllowShortFunctionsOnASingleLine: true
# 支援一行的if,例如值為true,則可以放在一行上。if (a) return;
AllowShortIfStatementsOnASingleLine: false
# 在未封閉(括号的開始和結束不在同一行)的括号中的代碼是否對齊,為true,則将參數在左方括号後水準對齊
AlignAfterOpenBracket: true
# switch的case縮進
IndentCaseLabels: false
# 針對OC的block的縮進寬度
ObjCBlockIndentWidth: 4
# 針對OC,屬性名後加空格
ObjCSpaceAfterProperty: true
# 每行字元的長度
ColumnLimit: 0
# 注釋對齊
AlignTrailingComments: true
# 括号後加空格,例如(int) i;
SpaceAfterCStyleCast: false
# 換行的時候對齊操作符
AlignOperands: true
# 中括号兩邊空格 []
SpacesInSquareBrackets: false
# 多行聲明語句按照=對齊
AlignConsecutiveDeclarations: false
# 容器類的空格 例如 OC的字典
SpacesInContainerLiterals: false
# 在構造函數初始化時按逗号斷行,并以冒号對齊
BreakConstructorInitializersBeforeComma: true
# 函數參數換行
AllowAllParametersOfDeclarationOnNextLine: true
#在續行(#下一行)時的縮進長度
ContinuationIndentWidth: 4
# tab鍵盤的寬度
TabWidth: 4
# 指派運算符前加空格
SpaceBeforeAssignmentOperators: true
# 行尾的注釋前加1個空格
SpacesBeforeTrailingComments: 1      

點選确認按鈕之後,最終會在路徑C:\Users\<使用者名>\AppData\Roaming\QtProject\qtcreator\beautifier\clangformat\myclangconfig生成.clang-format檔案。如果該檔案使用了中文注釋,那麼就需要手動另存為UTF-8的編碼,否則格式化時會報錯:error: Got empty plain scalar。

還有一個更好的辦法,先進入路徑C:\Users\<使用者名>\AppData\Roaming\QtProject\qtcreator\beautifier\clangformat\myclangconfig,然後在指令行終端輸入以下指令,可以生成參數模闆.clang-format檔案

clang-format-2663a25f.exe -style=Microsoft -dump-config > .clang-format

然後再該檔案的基礎上,修改自己想要的參數。比如:

BasedOnStyle: Microsoft
AccessModifierOffset: -4
AlignConsecutiveMacros: true
AlignTrailingComments: true
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: false
BreakBeforeBraces: Allman
ColumnLimit: 0      

詳細的變量說明請參見clang官網

https://clang.llvm.org/docs/ClangFormatStyleOptions.html

也可以看看效果

https://clangformat.com/

Google C++ Style Guide

https://google.github.io/styleguide/cppguide.html

選擇Tools->Options,點選左欄的Beautifier,選擇General;勾選Enable auto format on save file;Tool選擇ClangFormat。

Qt Creator使用clang-format實作源代碼格式化排版(Windows/macOS2)

通過菜單操作可以實作格式化

Qt Creator使用clang-format實作源代碼格式化排版(Windows/macOS2)

4、設定clang-format快捷鍵(可有可無)

點選Tools->Options->Environment->Keyboard;

在Filer裡輸入clang,濾掉一部分友善查找,然後想要的出現了;

選上FormatFile,點下那一欄就行了;

在Key sequence裡手動打入Ctrl+shift+k,當然點選右邊的Record按鈕,按鈕會變成stop recording,再在鍵盤上按照Ctrl->shift->k的順序按下去,前面兩個鍵按住了不要松,等到按到k再松開,然後快捷鍵欄裡面會出現Ctrl+shift+k,點選stop recording,點選儲存。

再補充macOS的用法

官網下載下傳Pre-Built Binaries中macOS版本的tar-xz檔案,大小300+M,注:點選macOS而不是後面的(.sig)

       clang+llvm-9.0.1-x86_64-apple-darwin.tar.xz

解壓

把/clang+llvm-9.0.1-x86_64-apple-darwin/bin目錄下的clang-format檔案和clang-tidy複制到某個檔案夾裡,比如

       /Users/<username>/Qt5.12.7/Qt Creator.app/Contents/Resources/libexec/clang/bin9/clang-tidy

       /Users/<username>/Qt5.12.7/Qt Creator.app/Contents/Resources/libexec/clang/bin9/clang-format

這裡的bin9是建立的檔案夾,差別于原來的bin

設定QtCreator,clang-format和clang-tidy分别指向該路徑

配置clang-format的格式化風格,.clang-format檔案最終會儲存在:

      /Users/<username>/.config/QtProject/qtcreator/beautifier/clangformat

姊妹篇

《VS2019使用clang-format實作源代碼格式化排版》

https://libaineu2004.blog.csdn.net/article/details/112760035

---

參考文獻

https://www.jianshu.com/p/e8e0158258f0

clang-format常用配置

https://www.jianshu.com/p/ba7aa6a2e4d1

Clang-Format在 Mac上的安裝與使用

https://blog.csdn.net/core571/article/details/82867932

 C++ 代碼格式化方法(clang-format)

https://github.com/FISCO-BCOS/FISCO-BCOS/blob/master/.clang-format https://gitee.com/mirrors/FISCO-BCOS/blob/master/.clang-format

另外也有提供給VS使用的clang-format插件,VS2017主菜單-工具-選項-文本編輯器-C/C++-格式設定。

https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.ClangFormat https://marketplace.visualstudio.com/items?itemName=MariuszBrzeski.CodeBeautifier

不過VS我個人推薦使用CodeMaid插件

https://marketplace.visualstudio.com/items?itemName=SteveCadwallader.CodeMaid