一杯清酒邀明月
天下本无事,庸人扰之而烦耳。

  clang-format是一种格式化代码的插件,可用于格式化C / C ++ / Java / JavaScript / Objective-C / Protobuf / C#代码。而Qt Creator的格式化代码的功能只支持非常简单的缩进纠正,本博客主要讲述如何在Qt Creator上配置clang-format插件。

打开Beautifier插件

打开帮助->关于插件->在Beautifier处打勾,然后重启Qt Creator。

注意一定要重启Qt Creator,Beautifier插件才能生效。

装载Clang-format插件

首先要到LLVM下载网站处下载clang-format可执行文件。这时需要根据自己的操作系统和电脑配置挑选适合的版本。我下载的是windows64位版本。

 安装包下载完成之后,双击安装即可。可自定义安装路径,我是安装到了D盘,这里最好记住你安装的路径。

 打开Qt Creator,点击工具->选项->Beautifier->Clang-format

 点击浏览,会跳出一个windows文件窗口,找到clang-format.exe文件并双击,Qt Creator就会自动填入clang-format.exe文件的绝对路径。当然,你也可以选择手动输入clang-format.exe文件的绝对路径。

 然后点击Use customized style->Add

 在新跳出来的窗口中写入你想要的代码格式化样式,关于可设置的样式可以参考Clang-format官方文档。我的format样式如下:

  1 {
  2 
  3 BasedOnStyle: Google,
  4 
  5 AccessModifierOffset: -2,
  6 
  7 AlignAfterOpenBracket: Align,
  8 
  9 AlignConsecutiveAssignments: false,
 10 
 11 AlignConsecutiveDeclarations: false,
 12 
 13 AlignEscapedNewlines: DontAlign,
 14 
 15 AlignOperands: true,
 16 
 17 AllowAllParametersOfDeclarationOnNextLine: true,
 18 
 19 AllowShortBlocksOnASingleLine: true,
 20 
 21 AllowShortCaseLabelsOnASingleLine: true,
 22 
 23 AllowShortFunctionsOnASingleLine: All,
 24 
 25 AllowShortIfStatementsOnASingleLine: false,
 26 
 27 AllowShortLoopsOnASingleLine: false,
 28 
 29 AlwaysBreakAfterDefinitionReturnType: None,
 30 
 31 AlwaysBreakAfterReturnType: None,
 32 
 33 AlwaysBreakBeforeMultilineStrings: false,
 34 
 35 AlwaysBreakTemplateDeclarations: true,
 36 
 37 AlwaysBreakAfterDefinitionReturnType: None,
 38 
 39 AlwaysBreakAfterReturnType: None,
 40 
 41 AlwaysBreakBeforeMultilineStrings: false,
 42 
 43 AlwaysBreakTemplateDeclarations: true,
 44 
 45 BinPackArguments: true,
 46 
 47 BinPackParameters: true,
 48 
 49 BreakBeforeBinaryOperators: None,
 50 
 51 BreakBeforeBraces: Attach,
 52 
 53 BreakBeforeTernaryOperators: false,
 54 
 55 BreakConstructorInitializers: AfterColon,
 56 
 57 BreakStringLiterals: true,
 58 
 59 ColumnLimit: 120,
 60 
 61 CommentPragmas: '^ IWYU pragma:',
 62 
 63 CompactNamespaces: false,
 64 
 65 ConstructorInitializerAllOnOneLineOrOnePerLine: false,
 66 
 67 ConstructorInitializerIndentWidth: 4,
 68 
 69 ContinuationIndentWidth: 4,
 70 
 71 Cpp11BracedListStyle: true,
 72 
 73 DerivePointerAlignment: false,
 74 
 75 DisableFormat: false,
 76 
 77 ExperimentalAutoDetectBinPacking: false,
 78 
 79 FixNamespaceComments: true,
 80 
 81 ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ],
 82 
 83 IncludeBlocks: Regroup,
 84 
 85 IndentCaseLabels: true,
 86 
 87 IndentPPDirectives:  AfterHash,
 88 
 89 IndentWidth: 4,
 90 
 91 IndentWrappedFunctionNames: true,
 92 
 93 KeepEmptyLinesAtTheStartOfBlocks: false,
 94 
 95 MacroBlockBegin: '',
 96 
 97 MacroBlockEnd: '',
 98 
 99 MaxEmptyLinesToKeep: 1,
100 
101 NamespaceIndentation: All,
102 
103 ObjCBlockIndentWidth: 4,
104 
105 ObjCSpaceAfterProperty: false,
106 
107 ObjCSpaceBeforeProtocolList: true,
108 
109 PenaltyBreakBeforeFirstCallParameter: 19,
110 
111 PenaltyBreakComment: 300,
112 
113 PenaltyBreakFirstLessLess: 120,
114 
115 PenaltyBreakString: 1000,
116 
117 PenaltyExcessCharacter: 1000000,
118 
119 PenaltyReturnTypeOnItsOwnLine: 60,
120 
121 PointerAlignment: Left,
122 
123 ReflowComments: true,
124 
125 SortIncludes: true,
126 
127 SpaceAfterCStyleCast: false,
128 
129 SpaceAfterTemplateKeyword: true,
130 
131 SpaceBeforeAssignmentOperators: true,
132 
133 SpaceBeforeParens: ControlStatements,
134 
135 SpaceInEmptyParentheses: false,
136 
137 SpacesBeforeTrailingComments: 2,
138 
139 SpacesInAngles: false,
140 
141 SpacesInContainerLiterals: false,
142 
143 SpacesInCStyleCastParentheses: false,
144 
145 SpacesInParentheses: false,
146 
147 SpacesInSquareBrackets: false,
148 
149 TabWidth: 4,
150 
151 UseTab: Never,
152 
153 }

 Clang-format插件就安装好了。赶快新建一个hello world工程试一下。

原始代码

 点击工具->Beautifier->ClangFormat->Format Current File,就可以格式化代码。

 格式化后代码格式如下:

设置快捷键

每次格式化都需要鼠标点击很多次,有些麻烦。我们可以为格式化代码功能设置一个快捷键。点击工具->选项->环境->键盘

搜索Clang,找到FormatFile,在下方的Key sequence处填写你想要的快捷键,我设置的是Alt+Shift+F,点击OK按钮就可以了。

posted on 2024-02-02 16:34  一杯清酒邀明月  阅读(139)  评论(0编辑  收藏  举报