我正在寻找一个 clang-format
设置来防止该工具删除换行符。
例如,我将 ColumnLimit
设置为 120,当我重新格式化一些示例代码时会发生以下情况。
之前:
#include <vector>
#include <string>
std::vector<std::string> get_vec()
{
return std::vector<std::string> {
"this is a test",
"some of the lines are longer",
"than other, but I would like",
"to keep them on separate lines"
};
}
int main()
{
auto vec = get_vec();
}
之后:
#include <vector>
#include <string>
std::vector<std::string> get_vec()
{
return std::vector<std::string>{"this is a test", "some of the lines are longer", "than other, but I would like",
"to keep them on separate lines"};
}
int main()
{
auto vec = get_vec();
}
我想要的是该工具会中断超过 120 个字符的行,但不会仅仅因为它们少于 120 个字符而决定合并行。
有这样的选择吗?文档中的任何内容都没有让我印象深刻。
最佳答案
所以,弄乱了 clang 格式的代码并做了一些补丁,这是我的两分钱:
Clang 格式基于,
- 使用
libclang
解析AST,基本上消除了所有的空格 - 将标记序列分解为“展开的行”,类似于“逻辑”代码行
- 应用规则/配置信息有时将“展开的行”拆分为更小的单元
- 用新的空格/缩进再次把它全部吐出来
要让它尊重原始的whitepsace并不容易,当你第一次解析代码时会被扔掉。
- 使用
您可以通过
轻松控制换行符的位置- 设置列限制
- 使用“bin 包参数”选项
- 为各种中断设置惩罚——函数返回类型后中断、第一次调用参数前中断、字符串文字中断、注释中断...
- 将注释放在行尾(clang 格式无法删除注释,因此必须拆分行)
- 使用 clang 格式的 off/on 指令
您可以尝试以下一件事:
std::vector<std::string> get_vec()
{
return std::vector<std::string> { //
"this is a test", //
"some of the lines are longer", //
"than other, but I would like", //
"to keep them on separate lines" //
};
}
与 //clang-format off
相比,它的优势在于,如果您稍后更改选项卡宽度或其他选项,这些代码行仍将获得这些格式更改,因此您不会需要手动进入 //clang-format off
区域来修复它。但是,它仍然有点 hack,YMMV。
最终,clang-format
非常注重在整个代码库上强加统一格式,确保所有字符串文字在程序中的任何地方都以相同的样式格式化。如果您想对换行决策进行微观层面的控制,这并不真正符合该工具的精神,您必须执行诸如禁用它之类的操作。
这有时会令人沮丧,尤其是。当你想对数组做一些事情并让列对齐或其他东西时——例如,这里有一些来自 lua C api 的自然代码:
static luaL_Reg const methods[] = {
{"matches", &dispatch::intf_match_unit},
{"to_recall", &dispatch::intf_put_recall_unit},
{"to_map", &dispatch::intf_put_unit},
{"erase", &dispatch::intf_erase_unit},
{"clone", intf_copy_unit},
{"extract", &dispatch::intf_extract_unit},
{"advance", intf_advance_unit},
};
当 clang-format 超过它时,它通常不会对齐右列,它会在逗号后面放置固定数量的空格,而且你无能为力。
或者,如果您有用于 OpenGL 的 4 x 4 矩阵:
constexpr float shadow_skew_hardcoded[16] =
{ 1.0f, 0.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f };
如果你让 clang-format 运行这样的东西,它只会破坏它们,而且 afaik 没有简单的方法让它很好地格式化它们,所以你只需要求助于“大量琐碎的评论”黑客, 或者当你有这样的东西时使用 clang-format off。这些只是该工具的内在限制。如果你对不得不做这样的事情感到不高兴,那么它可能不适合你。
关于c++ - Clang 格式的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33656800/