【码农日常】vscode编码clang-format格式化简易教程

发布时间:2026/7/6 20:27:12
【码农日常】vscode编码clang-format格式化简易教程 文章目录0 前言1 工具准备1.1 插件准备1.2 添加.clang-format1.3 添加配置1.4 格式化工具配置2 快速上手0 前言各路大神都说clangd好我也来试试。这篇主要讲格式化部分。1 工具准备1.1 插件准备照图安装。1.2 添加.clang-format右键添加文件跟添加个.h或者.c文件一样。1.3 添加配置点进.clang-format添加如下内容英语好的看字面意思不好的让AI给翻译翻译动手试试修改效果。不一一赘述。---Language:Cpp BasedOnStyle:Google #访问声明符缩进 AccessModifierOffset:-4# 开括号后的对齐(包括小括号/大括号/尖括号),建议使用Align #-Align:对于开括号,即在换行情况下,换行的参数跟开括号对齐,建议使用 #-DontAlign:不对于开括号,即换行时使用配置的空格数 #-AlwaysBreak:永远换行,即第一个参数都不允许粘连括号,会强制换行,换行后使用配置空格数对齐 #-BlockIndent:同AlwaysBreak,多了一个操作:如果参数不固定在同一行,闭括号将在下一行 AlignAfterOpenBracket:AlwaysBreak #-结构休数组统一初始化对齐,建议不配置,没过多必要,详见clang-format doc #-None:不做处理,即保留开发者的代码 #-Left:左对齐 #-Right:右对齐 AlignArrayOfStructures:None # 连续赋值语句的对齐即多个赋值语句连续出现时的对齐策略配置,包含多个子配置项 AlignConsecutiveAssignments:# 是否启用,建议不启用 Enabled:false # 是否跨过空行,即多个对齐语句中间有空行时,是否跨过,如果要开启连续赋值语句的配置,建议为false AcrossEmptyLines:false # 是否跨过注释,建议false AcrossComments:false # 是否跨过复合语句(包括空行及注释),建议False AlignCompound:false AlignFunctionPointers:false # 是否(右)对齐赋值操作的操作符,建议true PadOperators:true # 同AlignConsecutiveAssignments,表示连续位定义语句出现时,是否需要对齐:符号,位变量定义用得少,可以不开启 AlignConsecutiveBitFields:Enabled:false AcrossEmptyLines:false AcrossComments:false AlignCompound:false AlignFunctionPointers:false PadOperators:false AlignConsecutiveDeclarations:Enabled:false AcrossEmptyLines:false AcrossComments:false AlignCompound:false AlignFunctionPointers:false PadOperators:false # 宏定义自动对齐 AlignConsecutiveMacros:Enabled:true AcrossEmptyLines:false AcrossComments:false AlignCompound:false AlignFunctionPointers:false PadOperators:false AlignConsecutiveShortCaseStatements:Enabled:false AcrossEmptyLines:false AcrossComments:false AlignCaseColons:false # 续行符(\\)对齐:#-DontAlign:不做操作 #-Left:尽可能向左对齐,即最长一行代码为准 #-Right:跟开发都写的最远的\\对齐(即不会自动缩减你的空格),建议使用这个 AlignEscapedNewlines:Right AlignOperands:Align AlignTrailingComments:Kind:Always OverEmptyLines:0AllowAllArgumentsOnNextLine:true AllowAllParametersOfDeclarationOnNextLine:true AllowBreakBeforeNoexceptSpecifier:Never AllowShortBlocksOnASingleLine:Never AllowShortCaseLabelsOnASingleLine:false AllowShortCompoundRequirementOnASingleLine:true AllowShortEnumsOnASingleLine:true AllowShortFunctionsOnASingleLine:All AllowShortIfStatementsOnASingleLine:Never AllowShortLambdasOnASingleLine:All AllowShortLoopsOnASingleLine:false AlwaysBreakAfterDefinitionReturnType:None AlwaysBreakAfterReturnType:None AlwaysBreakBeforeMultilineStrings:false AlwaysBreakTemplateDeclarations:MultiLine AttributeMacros:-__capability BinPackArguments:true BinPackParameters:true # 位定义:前后是否增加空格,可选:#-Both:前后都添加 #-Before:只在前增加 #-After:只在后增加 #-None:不增加,建议,没有必要因为过多的space(s)影响代码紧凑 BitFieldColonSpacing:None # 大括号换行 BraceWrapping:# 在case后的大括号是否换行 AfterCaseLabel:true#class后AfterClass:true # 控制语句(if/for/while/switch/...)后是否换行 #-Never:永远不,即永远将语句体的大括号放置于控制语句同一行 #-MultiLine:多行控制语句才进行换行 #-Always:永远换行,建议 AfterControlStatement:Always AfterEnum:true AfterExternBlock:false AfterFunction:true AfterNamespace:false AfterObjCDeclaration:false AfterStruct:true AfterUnion:true BeforeCatch:false BeforeElse:true BeforeLambdaBody:false BeforeWhile:true IndentBraces:false SplitEmptyFunction:true SplitEmptyRecord:true SplitEmptyNamespace:true BreakAdjacentStringLiterals:true BreakAfterAttributes:Leave BreakAfterJavaFieldAnnotations:false BreakArrays:true BreakBeforeBinaryOperators:None BreakBeforeConceptDeclarations:Always # 大括号换行风格,Custom即可,具体值可参考上方文档 BreakBeforeBraces:Custom BreakBeforeInlineASMColon:OnlyMultiline BreakBeforeTernaryOperators:true BreakConstructorInitializers:BeforeColon BreakInheritanceList:BeforeColon BreakStringLiterals:true ColumnLimit:150CommentPragmas:^ IWYU pragma:CompactNamespaces:false ConstructorInitializerIndentWidth:4ContinuationIndentWidth:4#C11的统一初始化列表大括号风格,建议trueCpp11BracedListStyle:true DerivePointerAlignment:false DisableFormat:false EmptyLineAfterAccessModifier:Never EmptyLineBeforeAccessModifier:LogicalBlock ExperimentalAutoDetectBinPacking:false FixNamespaceComments:true ForEachMacros:-foreach-Q_FOREACH-BOOST_FOREACH IfMacros:-KJ_IF_MAYBE#include代码块操作,前提是SortIncludes开启:#-Preserve:只对每个代码块排序 #-Merge:对所有代码块合并,并在合并后排序 #-Regroup:对所有include块进行分析,并重新分块,不建议!IncludeBlocks:Preserve IncludeCategories:-Regex:^(llvm|llvm-c|clang|clang-c)/Priority:2SortPriority:0CaseSensitive:false-Regex:^(|(gtest|gmock|isl|json)/)Priority:3SortPriority:0CaseSensitive:false-Regex:.*Priority:1SortPriority:0CaseSensitive:false IncludeIsMainRegex:(Test)?$IncludeIsMainSourceRegex:IndentAccessModifiers:false IndentCaseBlocks:false IndentCaseLabels:false IndentExternBlock:AfterExternBlock IndentGotoLabels:true IndentPPDirectives:None IndentRequiresClause:true IndentWidth:4IndentWrappedFunctionNames:false InsertBraces:false InsertNewlineAtEOF:false InsertTrailingCommas:None IntegerLiteralSeparator:Binary:0BinaryMinDigits:0Decimal:0DecimalMinDigits:0Hex:0HexMinDigits:0JavaScriptQuotes:Leave JavaScriptWrapImports:true KeepEmptyLinesAtTheStartOfBlocks:true KeepEmptyLinesAtEOF:false LambdaBodyIndentation:Signature LineEnding:DeriveLF MacroBlockBegin:MacroBlockEnd:MaxEmptyLinesToKeep:1NamespaceIndentation:None ObjCBinPackProtocolList:Auto ObjCBlockIndentWidth:2ObjCBreakBeforeNestedBlockParam:true ObjCSpaceAfterProperty:false ObjCSpaceBeforeProtocolList:true # 是否打包构造函数初始化列表,建议Never,可选:#-Never:永远不做操作,即一个参数一行 #-BinPack:两个参数一行 #-CurrentLine:所有参数放置于一行,如果放不下,就一个参数一行 #-NextLine:同CurrentLine有点像,唯一不同就是如果放不行,将剩余参数放置于下一行(即不自动一参一行)PackConstructorInitializers:NextLine PenaltyBreakAssignment:2PenaltyBreakBeforeFirstCallParameter:19PenaltyBreakComment:300PenaltyBreakFirstLessLess:120PenaltyBreakOpenParenthesis:0PenaltyBreakScopeResolution:500PenaltyBreakString:1000PenaltyBreakTemplateDeclaration:10PenaltyExcessCharacter:1000000PenaltyIndentedWhitespace:0PenaltyReturnTypeOnItsOwnLine:60# 指针对齐,建议Right PointerAlignment:Right PPIndentWidth:-1QualifierAlignment:Leave ReferenceAlignment:Pointer # 是否允许clang-format尝试重新粘合注释(true/false),不建议使用 ReflowComments:false RemoveBracesLLVM:false RemoveParentheses:Leave RemoveSemicolon:false # 模板中的require语句位置,建议OwnLine RequiresClausePosition:OwnLine RequiresExpressionIndentation:OuterScope # 分隔不同定义块,建议Always,可选:#-Leave-不处理,建议,即由业务决定,也可以使用Always #-Always-永远进行分隔 #-Never:永远 不进行,不建议 SeparateDefinitionBlocks:Leave ShortNamespaceLines:1SkipMacroDefinitionBody:false SortIncludes:CaseSensitive SortJavaStaticImport:Before SortUsingDeclarations:LexicographicNumeric SpaceAfterCStyleCast:false SpaceAfterLogicalNot:false SpaceAfterTemplateKeyword:true SpaceAroundPointerQualifiers:Default SpaceBeforeAssignmentOperators:true SpaceBeforeCaseColon:false SpaceBeforeCpp11BracedList:false SpaceBeforeCtorInitializerColon:true SpaceBeforeInheritanceColon:true SpaceBeforeJsonColon:false SpaceBeforeParens:ControlStatements SpaceBeforeParensOptions:AfterControlStatements:true AfterForeachMacros:true AfterFunctionDefinitionName:false AfterFunctionDeclarationName:false AfterIfMacros:true AfterOverloadedOperator:false AfterPlacementOperator:true AfterRequiresInClause:false AfterRequiresInExpression:false BeforeNonEmptyParentheses:false SpaceBeforeRangeBasedForLoopColon:true SpaceBeforeSquareBrackets:false SpaceInEmptyBlock:false SpacesBeforeTrailingComments:1SpacesInAngles:Never SpacesInContainerLiterals:true SpacesInLineCommentPrefix:Minimum:1Maximum:-1SpacesInParens:Never SpacesInParensOptions:InCStyleCasts:false InConditionalStatements:false InEmptyParentheses:false Other:false SpacesInSquareBrackets:false Standard:Latest StatementAttributeLikeMacros:-Q_EMIT StatementMacros:-Q_UNUSED-QT_REQUIRE_VERSION TabWidth:8UseTab:Never VerilogBreakBetweenInstancePorts:true WhitespaceSensitiveMacros:-BOOST_PP_STRINGIZE-CF_SWIFT_NAME-NS_SWIFT_NAME-PP_STRINGIZE-STRINGIZE...1.4 格式化工具配置在settings.json中添加如下内容// 字符集编码选择“files.encoding”: “utf8”,// 自动保存任意文件“files.autoSave”: “afterDelay”,// 开启 material icons“workbench.iconTheme”: “vs-seti”,// theme主题设置“workbench.colorTheme”: “Dark Modern”,//粘贴时格式化代码“editor.formatOnPaste”: true,//保存时格式化代码“editor.formatOnSave”: true,//设置字体的大小最小值能设置为6“editor.fontSize”: 15,//设置字体的粗细// “editor.fontWeight”: “500”,“editor.fontWeight”: “normal”,//禁用字体可变属性“editor.fontVariations”: false,//设置字体的样式// “terminal.integrated.fontFamily”:“Courier New”,//使用Ctrl滚轮缩放编辑区的字体大小“editor.mouseWheelZoom”: true,//默认格式化工具clangd“editor.defaultFormatter”: “llvm-vs-code-extensions.vscode-clangd”,2 快速上手快捷键 alt shift F。Done