【问题标题】:Is there any options to make clang-format produce the following effect?是否有任何选项可以使 clang-format 产生以下效果?
【发布时间】:2022-01-21 00:25:31
【问题描述】:

我用clang-format格式化下面的代码,但是效果不是我想要的:

void f(int, int, std::vector<int>, std::map<int, int>)
{}

int main()
{
    f(
        {
    },
        {}, {1, 2, 3},
        {
            {1, 2},
            {3, 4},
        });
}

有没有办法让clang-format产生如下效果?

void f(int, int, std::vector<int>, std::map<int, int>)
{}

int main()
{
    f({}, 
      {}, 
      {1, 2, 3},
      {
          {1, 2},
          {3, 4},
      });
}

【问题讨论】:

    标签: c++ c++11 ide clang clang-format


    【解决方案1】:

    是的,使用你想要的style。 (真的就这么简单!)

    clang-format -h 告诉您 clang-format 具有的可用选项。

      --style=<string>           - Coding style, currently supports:
                                     LLVM, GNU, Google, Chromium, Microsoft, Mozilla, WebKit.
                                   Use -style=file to load style configuration from
                                   .clang-format file located in one of the parent
                                   directories of the source file (or current
                                   directory for stdin).
    

    在你的情况下,你可能想要clang-format --style=LLVM这样的东西。

    但它不会是完全你想要的。因此,您需要编写自己的样式文件。这并不难。使用clang-format --style=file 并将样式定义放在一个名为.clang-format 的文件中,该文件位于同一目录或其父目录之一中。

    documentation 准确地解释了您需要做什么。如果您更喜欢视觉类型,this 生成器可能会有所帮助。

    但请注意,您最好只对“通用”基本样式进行细微调整;当你做的代码与他们习惯的其他代码完全不同时,其他程序员可能不喜欢它。有关根据需要修改为需要处理特定项目需要样式文件的示例,请参阅 GNU Radio 的 .clang-format 文件。

    【讨论】:

      猜你喜欢
      • 2021-12-05
      • 2012-06-06
      • 2014-05-16
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-08
      • 2018-04-16
      相关资源
      最近更新 更多