如何使用clang-format自动缩进4个空格的C ++类? [英] How to auto indent a C++ class with 4 spaces using clang-format?

查看:1172
本文介绍了如何使用clang-format自动缩进4个空格的C ++类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目的根目录中得到了下一个.clang格式的文件:

I got the next .clang-format file in my project's root directory:

---
AlignTrailingComments: true
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
BreakBeforeBinaryOperators: false
IndentWidth: 4
SortIncludes: false
NamespaceIndentation: All
...

当我在c ++头文件上运行clang-format时,问题就来了,这些类会像这样自动缩进:

Problem comes when I run clang-format on my c++ headers, the classes become autoindented like this:

如您所见,标签public&私有仅缩进2个空格.但是我想要实现的是下面的输出(缩进是手动调整的):

As you can see, labels public & private are indented only with 2 spaces. But what I'm trying to achieve is the below output (indentation was manually tweaked):

这样,代码折叠变得非常令人愉快.

That way code-collapsing becomes something really pleasant to do.

如何调整.clang格式以实现此效果?如果不可能的话,您将如何修补clang格式的源代码以实现所需的行为?

How could I tweak my .clang-format to achieve this effect? If not possible, how would you patch clang-format source code to achieve this desired behaviour?

我尝试使用失败的AccessModifierOffset,我在下面使用了值{-2,0,2,4}的示例:

I've tried using unsuccessfully AccessModifierOffset, I've used values {-2,0,2,4} example below:

如您所见,公共块内的语句不会正确缩进.

As you can see the statement inside the public block won't be indented properly.

我尝试了@Henrique Jung解决方案,这绝对不是我要的,如果使用该组合,结果将是这样的:

I've tried the @Henrique Jung solution and that's definitely not what I'm asking for, if using that combination the result would be something like this one:

并且您可以看到,函数内部的内容缩进了8个空格,而不是4个空格.

And as you can see, the content inside the functions are indented 8 spaces instead 4, which is not good.

几个月前,我提供了一笔赏金,所以我将再次尝试,因为这肯定很有趣.如果我对clang格式的源代码有足够的了解,可以尝试一下,但是不幸的是我没有.

I gave a bounty few months ago so I'm going to try again as this one is definitely interesting. If I got enough knowledge about clang-format source code I'd give it a shot, unfortunately I don't.

推荐答案

据我所知,clang-format没有提供不同于非访问修饰符类内容的缩进功能选项.也就是说,请考虑以下代码:

As near as I can tell, clang-format offers no option for indenting function contents differently from non-access-modifier class contents. That is, consider the following code:

class A {
  public:
    void foo() {}
}

void bar() {
    int a;
}

在此代码中,"void foo(){}"行的缩进量始终与"int a;"相同.通过clang格式.

In this code, the line "void foo() {}" will always be indented the same amount as "int a;" by clang-format.

与您想要的样式最接近的东西来自于不缩进访问修饰符,例如:

The closest thing to the style you seem to want that is available would come from not indenting the access modifiers, e.g.:

class A {
public:
    void foo() {}
}

void bar() {
    int a;
}

例如,这是通过WebKit,Mozilla和LLVM样式完成的.通过设置:

This is done, for example, by the WebKit, Mozilla, and LLVM styles. It's achieved by setting:

IndentWidth: 4
AccessModifierOffset: -4

这篇关于如何使用clang-format自动缩进4个空格的C ++类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆