簡體   English   中英

clang-format: 打破 function 參數

[英]clang-format: break function parameters

我想使用 clang-format 來生成 C 功能,例如:

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

閱讀手冊后,我認為 clang-format 不能做到這一點。 可能嗎?

這是我目前打破 function 參數以使用代碼圍欄而不是長線的最佳解決方案。 我希望你能在這里找到有用的東西。

為了被圍起來,有問題的代碼必須比限制長,所以這里是它如何格式化更長的例子:

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

尚未實現 clang 格式的指針 Alignment。 請參考這個方法

void WhitespaceManager::alignConsecutiveDeclarations()

在以下鏈接中:Github 中的參考鏈接

代碼:

void WhitespaceManager::alignConsecutiveDeclarations() {
  if (!Style.AlignConsecutiveDeclarations)
    return;

  // FIXME: Currently we don't handle properly the PointerAlignment: Right
  // The * and & are not aligned and are left dangling. Something has to be done
  // about it, but it raises the question of alignment of code like:
  //   const char* const* v1;
  //   float const* v2;
  //   SomeVeryLongType const& v3;

  AlignTokens(Style, [](Change const &C) { return C.IsStartOfDeclName; },Changes);
}

希望這會有所幫助。

根據您的問題,我研究了很多文件。 不幸的是,沒有辦法像這樣破壞你的代碼

void cfg_InitConfig
(
    cfg_Config_t* cfg,
    char*         name
)

但是有人可以創建補丁來破解那種 alignment 是可能的。 創建那種補丁並不容易。

但如果你使用它,你可以獲得接近的答案(80% 正確),但實際上並不是你想要的。 這是代碼

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

使用這個結果將是

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

您必須擴展 function 的變量名稱,除非它們可以放在一行中

參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM