简体   繁体   中英

clang-format: break function parameters

I would like to use clang-format to product C functions like:

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

After reading the manual I don't think clang-format can do it. Is it possible?

This is my current best solution to break function parameters to have code fences instead of long lines. I hope you can find something useful here.

In order to become fenced the code in question has to be longer than a limit, so here is how it formats the longer example:

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

The ! symbol means that the lines have to do with code fences.

---
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

Pointer Alignment in clang-format is not yet implemented. Please refer this method

void WhitespaceManager::alignConsecutiveDeclarations()

in the following link: Reference Link in Github

Code:

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);
}

Hope this will be helpful.

I have studied lot of lot of documents according to your problem. Unfortunately there is no way to break your code like this

void cfg_InitConfig
(
    cfg_Config_t* cfg,
    char*         name
)

but someone can be create patch for break that kind of alignment it could be possible. Creating that kind of patch not so much easy.

but if you use this, you can get close answer(80% correct) but not actually you want. here is the code

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

using this outcome will be

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

you have to extend your variable names of the function unless they could fit on one line

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM