【问题标题】:How to get clang-format Visual Studio 2015 Extension to not put braces on same line as the if statement如何获得 clang 格式的 Visual Studio 2015 扩展以不将大括号与 if 语句放在同一行
【发布时间】: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 放在同一行。

【问题讨论】:

标签: visual-studio-2015 clang-format


【解决方案1】:

事实证明,我的问题是因为我将 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
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-30
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多