Visual Studio 2019 设置手动触发 clang-format 格式化
I - 编码风格
现今大多数 C++ 编码方式参考 Google 风格,使用 .clang-format 文件可以比较方便的修改整个文件的代码风格。
Google coding style 链接
https: //zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styleguide/contents/
如何获取
关于如何获取一个 .clang-format 文件,链接
https: //github.com/kehanXue/google-style-clang-format/blob/master/.clang-format
示例
Language: Cpp
BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: None
AlignOperands: Align
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: Never # To avoid conflict, set this "Never" and each "if statement" should include brace when coding
AllowShortLambdasOnASingleLine: Inline
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterStruct: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 80
CompactNamespaces: false
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false # Make sure the * or & align on the left
EmptyLineBeforeAccessModifier: LogicalBlock
FixNamespaceComments: true
IncludeBlocks: Preserve
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 2
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PointerAlignment: Left
ReflowComments: false
# SeparateDefinitionBlocks: Always # Only support since clang-format 14
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never
如何使用 clang-format
将 .clang-format 文件放置在工程根目录即可,Visual Studio 会自动识别。
II - 设置 VS 手动触发
2.1 - 为何要设置为手动触发
团队开发时,.clang-format 文件会在编写代码的过程中自动执行格式化,触发某些条件将自动格式化整个文件,有可能导致代码冲突的概率提升,也会造成编码时的不便。
2.2 - 如何设置手动触发
需要修改三处,首先打开 Visual Studio 关于 clang-format 的相关设置
" 工具 (Tools) " → " 选项 (Options...) " → " 文本编辑器 (Text Editor) " → " C/C++ " → " 格式设置 (Formatting) " → " 常规 (General) "
- 1 - 设置粘贴时,不做任何操作
( When I paste → Do nothing) - 2 - 设置 仅针对手动调用的格式设置命令运行 ClangFormat
( Run ClangFormat only for manually invoked formatting commands ) - 3 - 调整 默认格式设置样式为 Google 风格
( Default formatting style → Google )
2.3 - 如何手动触发
对于修改完的文件,执行快捷键 Ctrl + K, Ctrl + D 可以对整个文件进行格式化, 速记 D = Document 文档文件,
也可以对选中内容执行,使用 Ctrl + K, Ctrl + F,速记 F = Format 格式化。
对于不希望被修改格式的代码,使用注释 clang-format off 和 clang-format on ,包裹起来。
// clang-format off
some code... // this part will not be be formatted by .clang-format.
// clang-format on
被两种注释包裹起来的代码,不会被 .clang-format 文件格式化。
关于 clang-format 中各种参数的含义可以参考链接