Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell clang-format to follow this convention?

I would like to have this:

if (!enabled)
{
    return;
}

turned to this:

if (!enabled) { return; }

(In other words, I want short if-statements on a single line but keep the {} around them)

Currently I'm using the following configuration:

AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortBlocksOnASingleLine: true
BreakBeforeBraces: Allman

However, the output I'm getting is:

if (!enabled) 
{
    return;
}

Is the above formatting possible to accomplish with clang-format?

like image 732
Tamás Szelei Avatar asked Jun 22 '15 12:06

Tamás Szelei


Video Answer


1 Answers

Removing

BreakBeforeBraces: Allman

Seems to do what you want (for me). I'm using SVN clang though. Although you probably wanted it there for a reason.

According to the clang-format docs, the AllowShortBlocksOnASingleLine should do exactly what you want (regardless of brace style). This might be a bug in clang-format.

like image 136
silverclaw Avatar answered Oct 12 '22 23:10

silverclaw