Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang-format force each argument / parameter to own line when column exceeded?

I am trying to figure out if it is possible with clang-format for get the arguments / parameters the way I am looking.

I have tried a number of things however so far I can only get this:

SDL_SetRenderDrawColor(renderer,153 + floor(toGreenRed * percentage),51 + floor(toGreenGreen * percentage),51 + floor(toGreenBlue * percentage),255);

to format into this:

SDL_SetRenderDrawColor(renderer,
  153 + floor(toGreenRed * percentage),
  51 + floor(toGreenGreen * percentage),
  51 + floor(toGreenBlue * percentage),
  255);

but I want this:

SDL_SetRenderDrawColor(
  renderer,
  153 + floor(toGreenRed * percentage),
  51 + floor(toGreenGreen * percentage),
  51 + floor(toGreenBlue * percentage),
  255
);

also currently this:

void renderHealthBox( SDL_Renderer *renderer, int sideUiWidth, int yOffset, float percentage, float laksjdlkajsdlkasjdlkasjd) {

formats into this:

void renderHealthBox(
  SDL_Renderer *renderer, int sideUiWidth, int yOffset, float percentage, float laksjdlkajsdlkasjdlkasjd) {

however I would want:

void renderHealthBox(
  SDL_Renderer *renderer, 
  int sideUiWidth, 
  int yOffset, 
  float percentage, 
  float laksjdlkajsdlkasjdlkasjd
) {

Is what I am looking for possible with clang-format?

My current config is:

Language: Cpp
IndentWidth: 2
ObjCBlockIndentWidth: 2
ContinuationIndentWidth: 2
ColumnLimit: 120
BinPackArguments: false
BinPackParameters: false
AlignAfterOpenBracket: DontAlign
IndentCaseLabels: true
like image 332
ryanzec Avatar asked May 13 '18 16:05

ryanzec


1 Answers

Is this configuration good enough? It's based on @hit's comment, but I don't think this question is a duplicate of the other one. The main part of it is AlignAfterOpenBracket: AlwaysBreak. From clang-format documentation:

BAS_AlwaysBreak (in configuration: AlwaysBreak) Always break after an open bracket, if the parameters don’t fit on a single line


Example:

.clang-format:

Language: Cpp
IndentWidth: 2
ObjCBlockIndentWidth: 2
ContinuationIndentWidth: 2
ColumnLimit: 120
BinPackArguments: false
BinPackParameters: false
AlignAfterOpenBracket: AlwaysBreak
IndentCaseLabels: true
AllowAllParametersOfDeclarationOnNextLine: false

test.cpp:

void renderHealthBox(SDL_Renderer *renderer, int sideUiWidth, int yOffset, float percentage, float laksjdlkajsdlkasjdlkasjd) {
}

int main()
{
    SDL_SetRenderDrawColor(renderer,153 + floor(toGreenRed * percentage),51 + floor(toGreenGreen * percentage),51 + floor(toGreenBlue * percentage),255);
}

$ clang-format test.cpp:

void renderHealthBox(
  SDL_Renderer *renderer,
  int sideUiWidth,
  int yOffset,
  float percentage,
  float laksjdlkajsdlkasjdlkasjd) {}

int main() {
  SDL_SetRenderDrawColor(
    renderer,
    153 + floor(toGreenRed * percentage),
    51 + floor(toGreenGreen * percentage),
    51 + floor(toGreenBlue * percentage),
    255);
}

The only difference from your desired format is that the closing parentheses are not on their own line. I think that's a bit of a strange requirement, so I don't think it's possible to achieve with clang-format.

like image 153
gflegar Avatar answered Nov 16 '22 17:11

gflegar