【发布时间】:2017-09-15 00:04:08
【问题描述】:
在我看来,对于任何大括号是可选的语言,将大括号与 if 语句放在同一行是不可取的。请考虑以下内容。
if (VeryLongConditionThatIsWiderThanScreen) {
// Thousands of lines of badly indented code.
// You cannot rely on indentation to tell you were the block ends.
}
如果大括号位于 if 语句的末尾,我必须寻找并按下 end 键以确定该代码块的结束位置。我讨厌那样做。我是一名视力不佳的打字员,我需要付出相当大的努力才能找到结束键,以便找出代码块的结束位置。
我正在尝试使用 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 放在同一行。
【问题讨论】:
-
我认为以下错误可能与我的问题有关:bugs.llvm.org//show_bug.cgi?id=25069.
标签: visual-studio-2015 clang-format