如何以 clang 格式对齐函数参数名称?

lan*_*oxx 5 c clang clang-format

是否可以将clang-format结构成员和函数参数名称格式化为列?

例如:

struct
{
   int           alpha; //aligned to 'b' of "beta"
   unsigned int  beta;
   MyObject     *gamma; //aligned with 'g' not '*'
};

void foobar (int           alpha, //aligned to 'b' of "beta"
             unsigned int  beta
             MyObject     *gamma) //aligned with 'g' not '*'
{
}
Run Code Online (Sandbox Code Playgroud)

如果不可能,我可以以某种方式扩展 clang-format 来实现这一点吗?

lan*_*oxx 5

clang-format最近(3.7 或 3.8)获得了更多调整对齐的选项。我在问题中提出的问题仍然没有得到完全支持,但我们现在可以更接近一些:

AlignConsecutiveDeclarations: true
Run Code Online (Sandbox Code Playgroud)

其对齐声明如上所示。

不幸的是,对指针星号格式的控制似乎仍然有限,请参阅clang-format: 将指针声明的星号 (*) 与变量名称对齐


Chr*_*eck 2

嗯,你可以靠近一点。

对于函数声明:

您可以设置BinPackParameters=false,这将强制函数声明的所有参数都在一行上或每个参数在不同的行上,并且它们将如您所示对齐。

(但类型和标识符之间不能使用制表位。据我所知,这在 clang 格式中是不可能的。)

另请参阅选项AllowAllParametersOfDeclarationOnNextLine

即使 BinPackParameters 为 false,也允许将函数声明的所有参数放在下一行。

对于结构,我认为你无法实现这一点。

过去我自己为 clang-format 编写过补丁,我认为要像您建议的那样进行对齐需要做很多工作。您必须自己将相当多的 C++ 代码编写成 clang lib 格式才能支持这一点。