I’m trying to configure clang-format
to break after each function parameter and place ) and } on separate lines for (member) function declarations and calls, like this:
void draw(
const geometry::rect &source,
const geometry::rect &destination,
const double angle = 0.0,
flip flip = flip::none,
const uint8_t alpha = 255
) const noexcept;
Or this
lua.new_usertype<audio::soundmanager>(
"SoundManager",
"play", &audio::soundmanager::play,
"stop", &audio::soundmanager::stop
);
I don't want the code to be formatted like this:
lua.new_usertype<audio::soundmanager>(
"SoundManager",
"play", &audio::soundmanager::play,
"stop", &audio::soundmanager::stop);
Here is the .clang-format
file I have so far:
BasedOnStyle: LLVM
UseTab: Never
IndentWidth: 2
TabWidth: 2
BreakBeforeBraces: Attach
AllowShortIfStatementsOnASingleLine: true
IndentCaseLabels: false
ColumnLimit: 0
AccessModifierOffset: -2
FixNamespaceComments: false
The const noexcept
on the closing parenthesis let me conclude that you are in fact showing us a decontextualized method declaration here. I tried to build a test file to cover interesting use cases.
Input test.cpp
:
int fun(int const *a, const int &b,
const int c, const int d, const int e) noexcept;
class Foo {
public:
int bar(
int const *a,
int const &b,
const int some_c, int const some_d, const int some_e
) const noexcept {
const int f = fun(a, b, some_c, some_d, some_e);
return scale *f;
}
private:
const int scale = 10;
};
int fun(
const int *a, const int &b, const int c, const int d, const int e) noexcept {
return *a + b + (c-d)*e;
}
I based .custom-style
file on Chromium
and adjusted several config values:
.custom-style
:
---
BasedOnStyle: Chromium
Language: Cpp
IndentWidth: 4
AlignAfterOpenBracket: BlockIndent
DerivePointerAlignment: false
PointerAlignment: Right
# not shown in original question:
AccessModifierOffset: -2
QualifierAlignment: Left
...
I run the command
$> clang-format --style=file:.custom-style` test.cpp > test-s.cpp
Output (test-s.cpp
):
int fun(
const int *a,
const int &b,
const int c,
const int d,
const int e
) noexcept;
class Foo {
public:
int bar(
const int *a,
const int &b,
const int some_c,
const int some_d,
const int some_e
) const noexcept {
const int f = fun(a, b, some_c, some_d, some_e);
return scale * f;
}
private:
const int scale = 10;
};
int fun(
const int *a,
const int &b,
const int c,
const int d,
const int e
) noexcept {
return *a + b + (c - d) * e;
}
Finally, it should be noted that parameters are normally only wrapped if they would otherwise exceed one line. It may still be possible to achieve this by adjusting one or more of the Penalty*
values.