需要帮助调整连续分配

Need help in aligning consecutive assignment little differently

我对 C++ 使用 clang-format 选项 AlignConsecutiveAssignments: true。但是,对于像下面这样的情况,我想避免额外的间距。

当前输出:

    int i                                 = 0;
    int vol                               = 0;
    int mass                              = 0;
    string center_of_mass                 = "";
    unsigned center_of_gravity            = 0;
    int j                                 = 0;
    int wt                                = 0;
    int longest_variable_name_of_them_all = 0;
    // int a           = 0;
    cout << "item1       = " << wt << endl;
    cout << "longer_item = " << vol << endl;

期望的分段比对结果:

    int i    = 0;
    int vol  = 0;
    int mass = 0;
    string center_of_mass = "";
    unsigned center_of_gravity = 0;
    int j  = 0;
    int wt = 0;
    int longest_variable_name_of_them_all = 0;
    // int a           = 0;
    cout << "item1       = " << wt << endl;
    cout << "longer_item = " << vol << endl;

根据经验,我想避免在 = 之前有超过 4 个空格(这就是 center_of_mass 与下一行不对齐的原因)。

有没有人有我可以在格式化输出文件上 运行 的脚本?注释将被忽略以保留其中的 ASCII 艺术(如果有的话):-)

谢谢!

clang-format 中不可能有想要的东西。您可以通过在如下代码区域中禁用 clang-format 来部分实现它

int i    = 0;
int vol  = 0;
int mass = 0;
// clang-format off
string center_of_mass = "";
unsigned center_of_gravity = 0;
// clang-format on
int j  = 0;
int wt = 0;
// clang-format off
int longest_variable_name_of_them_all = 0;
// clang-format on
// int a           = 0;
cout << "item1       = " << wt << endl;
cout << "longer_item = " << vol << endl;

或者您可以用空行打断声明,并在被空行反弹的代码块中连接长名称。

int i    = 0;
int vol  = 0;
int mass = 0;
int j    = 0;
int wt   = 0;

string center_of_mass      = "";
unsigned center_of_gravity = 0;

int longest_variable_name_of_them_all = 0;

// int a           = 0;
cout << "item1       = " << wt << endl;
cout << "longer_item = " << vol << endl;