zmb*_*zmb 45 c++ code-formatting clang clang-format
我正在寻找一个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();
}
Run Code Online (Sandbox Code Playgroud)
后:
#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();
}
Run Code Online (Sandbox Code Playgroud)
我想要的是该工具会破坏超过120个字符的行,但不会因为它们少于120个字符而决定组合行.
有这样的选择吗?文档中没有任何内容对我有用.
Chr*_*eck 39
所以,在clang格式代码中弄乱并制作了一些补丁,这是我的两分钱:
Clang格式基于,
libclang,基本上消除了所有空格让它尊重原始的whitepsace并不容易,当你第一次解析代码时会被抛出.
你最容易控制它在哪里放置换行符
这是你可以尝试的一件事:
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" //
};
}
Run Code Online (Sandbox Code Playgroud)
这样做的好处// clang-format off是,如果您稍后更改选项卡宽度或其他选项,那些代码行仍将获得这些格式更改,因此您无需手动进入// clang-format off区域来修复它.然而,它仍然是一个黑客,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},
};
Run Code Online (Sandbox Code Playgroud)
当clang-format运行时,它通常不会对齐右列,它会在逗号后面放置固定数量的空格,并且你可以做的事情并不多.
或者,如果你有4 x 4矩阵用于OpenGL:
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 };
Run Code Online (Sandbox Code Playgroud)
如果你让clang-format运行这样的东西,它只会破坏它们,并且afaik没有简单的方法让它很好地格式化它们,所以你只需要求助于"许多琐碎的评论"hack,或者使用当你有这样的东西时,你会格式化.这些只是该工具的内在局限性.如果你不高兴做这样的事情那么它可能不适合你.
小智 14
我不确定你的格式是否能完全符合你的要求,但是可以告诉clang-format单独留下代码段.我将此用于您正在谈论的那种场景,代码块,其中非常特殊的格式使其更易于阅读.
std::vector<std::string> get_vec()
{
// clang-format off
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 on
}
Run Code Online (Sandbox Code Playgroud)
请参阅:http: //clang.llvm.org/docs/ClangFormatStyleOptions.html#disabling-formatting-on-a-piece-of-code
Mac*_*Mac 14
在最后一个字符串后添加一个逗号。这告诉 clang-format 垂直格式化它。例如:https : //godbolt.org/z/bZxr__右键单击 > 格式化文本
#include <string>
#include <vector>
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", // comma here after last element
};
}
int main() { auto vec = get_vec(); }
Run Code Online (Sandbox Code Playgroud)
我没有在文档 中看到任何允许您这样做的内容.
将ColumnLimit设置为0仍将保留文本换行.
clang-format-mp-3.4 test.c -style="{ ColumnLimit: 0 }"
#include <vector>
#include <memory>
#include <string>
int main() {
std::vector<std::string> vec = {
"this is a test",
"with some strings",
"that I want on separate lines"
};
}
Run Code Online (Sandbox Code Playgroud)