clang-format代码格式插件设置(最简教程)

这里我们介绍一下如果通过一个插件来指定代码的格式。先说作用,可以让代码按照自己的需要修改成对应的风格:
在这里插入图片描述
使用后:
在这里插入图片描述
自动变得规整。这里没有注意空行的处理,不过可以配置一下。

安装vscode

直接官网下载安装即可https://code.visualstudio.com/在这里插入图片描述

下载C/C++插件

下载我们需要的clang-format插件
在这里插入图片描述
此时的插件安装到了这个路径下面:
C:\Users\24541.vscode\extensions\ms-vscode.cpptools-1.25.3-win32-x64\LLVM\bin
在这里插入图片描述

为了方便管理,我们把后面的配置文件也放在这。

配置clang-format

在这里插入图片描述
配置可以直接在GUI界面改,也可以使用专门的配置文件。我一般直接使用要求的配置文件即可。
在这里插入图片描述

将我们的clang-format文件的路径写入即可,我们的文件放在了:
在这里插入图片描述

这里需要设置的是两个路径,一个可执行文件的路径,一个代码风格的配置路径
在这里插入图片描述
在这里插入图片描述

分别对应:C:\Users\24541.vscode\extensions\ms-vscode.cpptools-1.25.3-win32-x64\LLVM\bin\clang-format

file:C:\Users\24541.vscode\extensions\ms-vscode.cpptools-1.25.3-win32-x64\LLVM\bin.clang-format
注意,设置风格的时候默认是file,也就是在exe文件下去自动寻找.clang-format文件,当然,也可以直接指定全局:file:C:\Users\24541.vscode\extensions\ms-vscode.cpptools-1.25.3-win32-x64\LLVM\bin.clang-format形式。

这是我的clang-format文件:

---
Language: Cpp
# BasedOnStyle:  Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
  AfterClass: false
  AfterControlStatement: false
  AfterEnum: false
  AfterFunction: false
  AfterNamespace: false
  AfterObjCDeclaration: false
  AfterStruct: false
  AfterUnion: false
  AfterExternBlock: false
  BeforeCatch: false
  BeforeElse: false
  IndentBraces: false
  SplitEmptyFunction: true
  SplitEmptyRecord: true
  SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 120
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
  - foreach
  - Q_FOREACH
  - BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
  - Regex: '^<ext/.*\.h>'
    Priority: 2
  - Regex: '^<.*\.h>'
    Priority: 1
  - Regex: '^<.*'
    Priority: 2
  - Regex: '.*'
    Priority: 3
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
TabWidth: 8
UseTab: Never
...

处理代码

在这里插入图片描述
转变前:
在这里插入图片描述
转变后
在这里插入图片描述
总之就可以在写代码的时候不用特别注意格式了!!

### clang-format 文件常见配置项 Clang-format 是一种强大的工具,用于自动格式化 C/C++/Java/Objective-C 等编程语言的源代码。通过 `.clang-format` 文件可以自定义代码风格设置。常见的配置项及其功能描述如下: #### 1. 基本样式参数 - **基于现有风格** - `BasedOnStyle`: 定义基础风格,可选值有 LLVM、Google、Chromium、Mozilla 和 Microsoft。 - 使用命令如 `clang-format -style=llvm -dump-config > .clang-format` 可以创建基于特定风格的基础配置文件[^1]。 #### 2. 缩进控制 - **缩进宽度** - `IndentWidth`: 设置每一级缩进所使用的空格数,默认为 2 或者遵循选定的基本风格设定。 - **连续声明对齐** - `AlignConsecutiveDeclarations`: 控制是否使连续变量声明左对齐。 - **宏定义对齐** - 对于某些版本可能不支持像 `AlignConsecutiveMacros` 的选项,在遇到此类情况时应考虑更新 Clang 版本来获得新特性支持[^3]。 #### 3. 行宽与换行策略 - **大行长度** - `ColumnLimit`: 设定每行的大字符数量限制;当超过此限值时会尝试折行处理。 - **短语拆分方式** - `BinPackParameters`: 影响函数调用中的参数列表如何被包裹成多行。 - 当设为 `true` 时倾向于保持紧凑布局; - 若为 `false` 则更偏好让每个实参独占一行。 #### 4. 大括号放置规则 - **类体大括号位置** - `AccessModifierOffset`: 调整访问修饰符(public, private, protected)相对于类名的偏移量。 - **其他结构的大括号** - `BreakBeforeBraces`: 指明在哪些情况下应该把开括号放在新行上而不是紧跟其前的内容之后。 - 支持多种模式如 Allman、GNU、Stroustrup 等不同编码习惯下的大括号摆放方案。 以下是部分常用配置项的一个单例子: ```yaml --- Language: Cpp # Based on the Google style guide. BasedOnStyle: Google IndentWidth: 4 ContinuationIndentWidth: 4 UseTab: Never ColumnLimit: 80 AccessModifierOffset: -4 AllowShortIfStatementsOnASingleLine: false AlwaysBreakTemplateDeclarations: true ... ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白云千载尽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值