How to get clang-format Visual Studio 2015 Extension to not put braces on the same line as the if 语句
How to get clang-format Visual Studio 2015 Extension to not put braces on same line as the if statement
在我看来,对于大括号是可选的任何语言,将大括号与 if 语句放在同一行是不可取的。考虑以下因素。
if (VeryLongConditionThatIsWiderThanScreen) {
// Thousands of lines of badly indented code.
// You cannot rely on indentation to tell you were the block ends.
}
如果大括号位于 if 语句的末尾,我必须寻找并按下结束键才能确定该代码块的结束位置。我讨厌那样做。我是一个视力不佳的打字员,我需要付出相当大的努力才能找到那个结束键,这样我才能找到那个代码块结束的地方。
我正在尝试使用 clang-format,使用 ClangFormat Visual Studio 2015 Extension,但我坚持认为它不会将大括号与 if 放在同一行。所有内置样式都可以。我在 http://clang.llvm.org/docs/ClangFormatStyleOptions.html 阅读了文档并编写了以下 .clang-format 文件。
---
Language: Cpp
BasedOnStyle: WebKit
AlignAfterOpenBracket: AlwaysBreak
AllowShortBlocksOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakBeforeMultilineStrings: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterControlStatement: true
AfterEnum: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: true
ColumnLimit: 100
ConstructorInitializerAllOnOneLineOrOnePerLine: true
Cpp11BracedListStyle: true
IndentWidth: 2
SortIncludes: false
TabWidth: 2
...
如果我正确地解释了文档,那么将 AfterControlStatement 设置为 true 应该会导致 clang-format 将大括号放在 if 之后的行上,这正是我想要的。这没有发生。我已将 .clang-format 文件放在与我的项目文件相同的目录中。我还尝试将其命名为 _clang-format。什么都不管用。每次我使用 CLang 格式文档菜单项时,它都会将与 if 语句关联的所有大括号放在与 if.
相同的行上
事实证明,我的问题是由我将 AllowShortLoopsOnASingleLine 和 AllowShortBlocksOnASingleLine 设置为 true 引起的。
此问题已报告为 bug 25069 (clang-format BreakBeforeBraces
behavior changes with Allow...OnASingleLine
parameters)。
我最终使用以下 .clang 格式文件来实现我的目标。
---
Language: Cpp
BasedOnStyle: WebKit
AccessModifierOffset: -2
AlignAfterOpenBracket: AlwaysBreak
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: true
AlwaysBreakBeforeMultilineStrings: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
ColumnLimit: 100
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 2
Cpp11BracedListStyle: true
IndentWidth: 2
KeepEmptyLinesAtTheStartOfBlocks: false
PointerAlignment: Right
SortIncludes: false
SpacesInContainerLiterals: false
TabWidth: 2
UseTab: ForContinuationAndIndentation
...
在我看来,对于大括号是可选的任何语言,将大括号与 if 语句放在同一行是不可取的。考虑以下因素。
if (VeryLongConditionThatIsWiderThanScreen) {
// Thousands of lines of badly indented code.
// You cannot rely on indentation to tell you were the block ends.
}
如果大括号位于 if 语句的末尾,我必须寻找并按下结束键才能确定该代码块的结束位置。我讨厌那样做。我是一个视力不佳的打字员,我需要付出相当大的努力才能找到那个结束键,这样我才能找到那个代码块结束的地方。
我正在尝试使用 clang-format,使用 ClangFormat Visual Studio 2015 Extension,但我坚持认为它不会将大括号与 if 放在同一行。所有内置样式都可以。我在 http://clang.llvm.org/docs/ClangFormatStyleOptions.html 阅读了文档并编写了以下 .clang-format 文件。
---
Language: Cpp
BasedOnStyle: WebKit
AlignAfterOpenBracket: AlwaysBreak
AllowShortBlocksOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakBeforeMultilineStrings: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterControlStatement: true
AfterEnum: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: true
ColumnLimit: 100
ConstructorInitializerAllOnOneLineOrOnePerLine: true
Cpp11BracedListStyle: true
IndentWidth: 2
SortIncludes: false
TabWidth: 2
...
如果我正确地解释了文档,那么将 AfterControlStatement 设置为 true 应该会导致 clang-format 将大括号放在 if 之后的行上,这正是我想要的。这没有发生。我已将 .clang-format 文件放在与我的项目文件相同的目录中。我还尝试将其命名为 _clang-format。什么都不管用。每次我使用 CLang 格式文档菜单项时,它都会将与 if 语句关联的所有大括号放在与 if.
相同的行上事实证明,我的问题是由我将 AllowShortLoopsOnASingleLine 和 AllowShortBlocksOnASingleLine 设置为 true 引起的。
此问题已报告为 bug 25069 (clang-format BreakBeforeBraces
behavior changes with Allow...OnASingleLine
parameters)。
我最终使用以下 .clang 格式文件来实现我的目标。
---
Language: Cpp
BasedOnStyle: WebKit
AccessModifierOffset: -2
AlignAfterOpenBracket: AlwaysBreak
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: true
AlwaysBreakBeforeMultilineStrings: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
ColumnLimit: 100
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 2
Cpp11BracedListStyle: true
IndentWidth: 2
KeepEmptyLinesAtTheStartOfBlocks: false
PointerAlignment: Right
SortIncludes: false
SpacesInContainerLiterals: false
TabWidth: 2
UseTab: ForContinuationAndIndentation
...