【问题标题】:clang-format: keep structure initialization in one lineclang-format:将结构初始化保持在一行
【发布时间】:2020-05-16 05:43:27
【问题描述】:

我有一个结构。我想以每个元素一行的方式初始化一个这种结构的对象数组:

static const struct
{
    const ErrorCode & code;
    const std::string &expected;
}
error_codes[]
{
    { ErrorCode::kNone,             "No error",                             },
    { ErrorCode::kInvalidParameter, "Invalid parameter",                    },
};

clang-format 将其更改为:

error_codes[]{
    {
        ErrorCode::kNone,
        "No error",
    },
    {
        ErrorCode::kInvalidParameter,
        "Invalid parameter",
    },
};

保持原始格式的设置是什么? clang 格式版本 10.0。

配置文件:

---
Language:        Cpp
BasedOnStyle:  Google
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignEscapedNewlines: Left
AlignOperands:   true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true # false
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterCaseLabel:  true
AfterClass:      true # false
AfterControlStatement: MultiLine # false
AfterEnum:       false
AfterFunction:   true # false
AfterNamespace:  true # false
AfterObjCDeclaration: true # false
AfterStruct:     true # false
AfterUnion:      true # false
AfterExternBlock: true # false
BeforeCatch:     true # false
BeforeElse:      true # false
IndentBraces:    true # false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Allman # Attach
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit:     120
CommentPragmas:  '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat:   false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks:   Regroup
IncludeCategories:
# Headers in <> without extension.
- Regex:           '^<([A-Za-z0-9\Q/-_\E])+>'
    Priority:        5
# Headers in <> from specific libraries.
- Regex:           '^<(gtest|gmock|mpark|pugixml|idl)'
    Priority:        4
# One word headers in <> with .h extension (C headers).
- Regex:           '<([A-Za-z0-9])+.h>'
    Priority:        3
# Headers in "" with extension from avcommon library.
- Regex:           '^"include/([A-Za-z0-9.\Q/-_\E])+"'
    Priority:        2
# Headers in "" with extension.
- Regex:           '^"([A-Za-z0-9.\Q/-_\E])+"'
    Priority:        1

IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth:   4  #2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd:   ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Middle # Right
ReflowComments:  true
SortIncludes:    true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles:  false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never
...

【问题讨论】:

  • 我有相反的问题,我得到了你想要的行为,但我想要你的 clang 格式的结果。你能添加你的clang-format 选项吗?
  • 有趣。我已经添加了格式文件。你能分享你的@SolArnu 吗?
  • 嗯,使用你的文件,我的 .clang 格式和我在这里的文件一样(它很短)。我主要在 c 代码的上下文中查看这个,而不是 c++ 代码,那里有初始化程序。 BasedOnStyle: LLVM IndentWidth: 2 BreakBeforeBraces: Stroustrup AllowShortFunctionsOnASingleLine: false SpacesInConditionalStatement: true ColumnLimit: 90 PointerAlignment: Right AllowAllArgumentsOnNextLine: false IndentPPDirectives: None BinPackArguments: true IndentCaseLabels: true Cpp11BracedListStyle: false AlignConsecutiveMacros: true
  • 抱歉,您得到的结果和以前一样吗?
  • 我将发布一个解释我的意思的答案,主要是因为格式可能会被保留。

标签: clang-format


【解决方案1】:

如果我理解正确,我可以使用以下 clang 格式文件获得您想要的行为,但仅适用于以下示例的 c 代码:

~/src/apollo_cm_mcu$ cat test2.c 
static
struct command_t commands[] = {
    {
        "adc",
        adc_ctl,
        "adc\r\n Displays a table showing the state of ADC inputs.\r\n",
        0
    },
    {
        "alm",
        alarm_ctl,
        "alm (clear|status|settemp #)\r\n Get or clear status of alarm task.\r\n",
        -1
    },
    {
        "bootloader",
        bl_ctl,
        "bootloader\r\n Call the boot loader\r\n",
        0
    },
};
~/src/apollo_cm_mcu$ clang-format --style=file test2.c
static struct command_t commands[] = {
  { "adc", adc_ctl, "adc\r\n Displays a table showing the state of ADC inputs.\r\n", 0 },
  { "alm", alarm_ctl,
    "alm (clear|status|settemp #)\r\n Get or clear status of alarm task.\r\n", -1 },
  { "bootloader", bl_ctl, "bootloader\r\n Call the boot loader\r\n", 0 },
};

这是 clang 格式的文件:

BasedOnStyle: LLVM
IndentWidth: 2
BreakBeforeBraces: Stroustrup
AllowShortFunctionsOnASingleLine: false
SpacesInConditionalStatement: true
ColumnLimit:     90
PointerAlignment: Right 
AllowAllArgumentsOnNextLine: false
IndentPPDirectives: None
BinPackArguments: true
IndentCaseLabels: true
Cpp11BracedListStyle: false
AlignConsecutiveMacros: true

但是,对于您的 sn-p,它不会做同样的事情。

~/src/apollo_cm_mcu$ clang-format --style=file test.cpp 
static const struct {
  const ErrorCode &code;
  const std::string &expected;
} error_codes[]{
  {
      ErrorCode::kNone,
      "No error",
  },
  {
      ErrorCode::kInvalidParameter,
      "Invalid parameter",
  },
};

也许这是一个线索。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多