我正在尝试将
clang-format
配置为在每个函数参数之后中断,并将 ) 和 } 放在单独的行上,如下所示:
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;
或者这个
lua.new_usertype<audio::soundmanager>(
"SoundManager",
"play", &audio::soundmanager::play,
"stop", &audio::soundmanager::stop
);
不是这个
lua.new_usertype<audio::soundmanager>(
"SoundManager",
"play", &audio::soundmanager::play,
"stop", &audio::soundmanager::stop);
我的
.clang-format
文件
BasedOnStyle: LLVM
UseTab: Never
IndentWidth: 2
TabWidth: 2
BreakBeforeBraces: Attach
AllowShortIfStatementsOnASingleLine: true
IndentCaseLabels: false
ColumnLimit: 0
AccessModifierOffset: -2
FixNamespaceComments: false
右括号上的
const noexcept
让我得出结论,您实际上在这里向我们展示了一个脱离上下文的方法声明。我尝试构建一个测试文件来涵盖有趣的用例。
输入
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;
}
我基于
.custom-style
文件 Chromium
并调整了几个配置值:
.custom-style
:
---
BasedOnStyle: Chromium
Language: Cpp
IndentWidth: 4
AlignAfterOpenBracket: BlockIndent
DerivePointerAlignment: false
PointerAlignment: Right
# not shown in original question:
AccessModifierOffset: -2
QualifierAlignment: Left
...
我运行命令
$> clang-format --style=file:.custom-style` test.cpp > test-s.cpp
输出(
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;
}
最后,应该注意的是,参数通常仅在超出一行时才进行换行。通过调整一个或多个
Penalty*
值仍然可以实现这一目标。