Visual Studio 2019 设置手动触发 clang-format 格式化

简介: 合作开发时,.clang-format 文件会在编写代码的过程中自动执行格式化,触发某些条件将自动格式化整个文件,有可能导致代码冲突的概率提升,也会造成编码时的不便。

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) "

clang-format.png

  • 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 offclang-format on ,包裹起来。

// clang-format off
some code... // this part will not be be formatted by .clang-format. 
// clang-format on

被两种注释包裹起来的代码,不会被 .clang-format 文件格式化。

关于 clang-format 中各种参数的含义可以参考链接

目录
相关文章
|
2月前
|
JSON C++ 数据格式
如何在 Visual Studio Code 中使用 Prettier 格式化代码
如何在 Visual Studio Code 中使用 Prettier 格式化代码
63 0
|
5月前
|
前端开发 开发工具 C++
vscode ( Visual Studio Code )设置中文、字体和字号
vscode ( Visual Studio Code )设置中文、字体和字号
335 0
|
12月前
|
JSON JavaScript 前端开发
在 Visual Studio Code 里如何设置让搜索忽略指定的文件夹
在 Visual Studio Code 里如何设置让搜索忽略指定的文件夹
|
C语言 C++ Python
【ROS 开发神器 Visual Studio Code 的安装和设置】
【ROS 开发神器 Visual Studio Code 的安装和设置】
809 0
【ROS 开发神器 Visual Studio Code 的安装和设置】
|
XML 数据格式 C++
Visual Studio 2019 设置 doxygen 注释颜色
VS 中如何修改 doxygen 格式注释的颜色
244 0
Visual Studio 2019 设置 doxygen 注释颜色
Visual Studio 2019 设置程序结束控制台不关闭
修改设置使控制台应用运行结束,控制台不自动退出。
415 0
Visual Studio 2019 设置程序结束控制台不关闭
|
C++
Visual Studio Code 设置 doxygen 格式注释
vs code 使用 cschlosser.doxdocgen 插件,设置 doxygen 注释格式
852 0
Visual Studio Code 设置 doxygen 格式注释
|
C++
Visual Studio 2019 实用功能设置(背景颜色,代码字体及行号设置)
Visual Studio 2019 安装包的下载教程、安装教程 教程第一篇:Visual Studio 2019 详细安装教程(图文版) 第二篇:Visual Studio 2019 实用功能设置(背景颜色,代码颜色及行号设置) 第三篇:Visual Studio 2019 代码调试技巧 第四篇:Visual Studio 2019 解决scanf 警告问题
835 0
|
编译器 C++
Visual Studio 2022 中如何设置背景图片,壁纸
Visual Studio 2022 中如何设置背景图片,壁纸
下一篇
无影云桌面