Clang格式换行符

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格式基于,

    • 使用解析AST libclang,基本上消除了所有空格
    • 将令牌序列分解为"未包装的行",这类似于"逻辑"代码行
    • 应用规则/配置信息有时会将"未包装的行"拆分为更小的单元
    • 用新的空格/缩进再次吐出来

    让它尊重原始的whitepsace并不容易,当你第一次解析代码时会被抛出.

  • 你最容易控制它在哪里放置换行符

    • 设置列限制
    • 使用"bin pack参数"选项
    • 为各种中断设置惩罚 - 在返回函数类型后中断,在第一个调用参数之前中断,中断字符串文字,中断注释...
    • 将注释放在一行的末尾(clang格式无法删除注释,因此必须拆分该行)
    • 使用clang-format 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" //
   };
}
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,或者使用当你有这样的东西时,你会格式化.这些只是该工具的内在局限性.如果你不高兴做这样的事情那么它可能不适合你.

  • 当我使用多行“#define”宏时,我必须调整你的技术。诀窍是用“/**/\\”结束每一行 (2认同)

小智 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

  • 我并不是说这是一个糟糕的答案,而是说这个问题的解决方案绝对是可怕的。用注释使代码变得混乱,以使自动格式正常工作。 (2认同)

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)

  • 我喜欢它,但这对我来说非常不明显。为什么它有效?是否有一个样式选项可以控制它? (3认同)

Sam*_*m P 8

我没有在文档 中看到任何允许您这样做的内容.

将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)

  • 对。这样可以保留我的包裹,但是无法修复过长的行(这是IMO工具的主要优势)。 (2认同)