【问题标题】:clang-format: break function parametersclang-format:中断函数参数
【发布时间】:2020-03-18 14:17:25
【问题描述】:

我想使用 clang-format 来生成 C 函数,例如:

void cfg_InitConfig
(
    cfg_Config_t* cfg,
    char*         name
)
{
    // TODO function
}

阅读手册后,我认为 clang-format 不能做到这一点。 有可能吗?

【问题讨论】:

标签: c clang-format


【解决方案1】:

这是我目前打破函数参数以使用代码围栏而不是长行的最佳解决方案。我希望你能在这里找到有用的东西。

为了被围起来,有问题的代码必须比限制长,所以这里是它如何格式化更长的例子:

void cfg_InitConfig(cfg_Config_t *cfgxxxxxxxxxxxxxx,
                    char *namexxxxxxxxxxxxxxxxxxx) {
  // TODO function
}

! 符号表示这些行与代码围栏有关。

---
Language:      Cpp
BasedOnStyle:  Google

AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignOperands: false
AlignTrailingComments: false

# !
AllowAllParametersOfDeclarationOnNextLine: false

AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None

AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes

# !
BinPackArguments: false
BinPackParameters: false

ColumnLimit: 80
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 2
CompactNamespaces: false

IncludeBlocks: Preserve
IndentWidth: 2

DerivePointerAlignment: false
PointerAlignment: Right

SortIncludes: true
SortUsingDeclarations: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: true

【讨论】:

  • 您可以使用此配置添加示例输出吗?
【解决方案2】:

根据您的问题,我研究了很多文件。不幸的是,没有办法像这样破坏你的代码

void cfg_InitConfig
(
    cfg_Config_t* cfg,
    char*         name
)

但是有人可以创建补丁来打破这种对齐是可能的。创建这种补丁并不容易。

但如果你使用它,你可以获得接近的答案(80% 正确)但实际上并不是你想要的。这是代码

BinPackArguments : false
BinPackParameters: false
AlignConsecutiveAssignments : true
AlignConsecutiveDeclarations: true

使用这个结果将是

void cfg_InitConfig(cfg_Config_t* cfgxxxxxxxxxx,
                    char*         namexxxxxxxx)
{
    // TODO function
}

您必须扩展函数的变量名称,除非它们可以放在一行中

Reference

【讨论】:

    猜你喜欢
    • 2020-06-26
    • 2014-03-24
    • 2022-12-23
    • 2020-09-15
    • 1970-01-01
    • 2015-11-30
    • 2020-04-04
    • 1970-01-01
    • 2016-12-02
    相关资源
    最近更新 更多