c++ - 在多行中用相应的元素格式化 C++ 代码

标签 c++ vim ide indentation code-formatting

我正在寻找一种工具来制作丑陋的方 block

if   ( str == "str" ) decorator["str"] = &Props::goodstr;
  else if ( str == "strM" )
      decorator["strM"] =     &Props::goodstrM;
  else if ( str == "strXL" )     decorator["strXL"] =     &Props::goodstrXL;
  else if ( str == "strXXXL" ) decorator["strXXXL"] = &Props::goodstrXXXL;

在 vim 的可视 block 模式下可以轻松地在多行上编辑的漂亮 block :

if      ( str == "str"     ) decorator["str"    ] = &Props::goodstr    ;
else if ( str == "strM"    ) decorator["strM"   ] = &Props::goodstrM   ;
else if ( str == "strXL"   ) decorator["strXL"  ] = &Props::goodstrXL  ;
else if ( str == "strXXXL" ) decorator["strXXXL"] = &Props::goodstrXXXL;

或者类似的东西。
它不必在 vim 中!我只是将它标记为 vim,因为那是我通常使用的。请推荐其他可以完成这项工作的工具。

最佳答案

我实际上知道有一种工具可以帮助对齐所有“丑陋”的 block 。它叫做Align .你所要做的就是给他你想要对齐的模式。

所以对于上面的代码你可以这样做:

:%normal! ggJ
:%s/;/;\r/g
:%left
:AlignCtrl =Clp1P1IW 
:% Align (\|)\|]\|[\|&.\+ 
:% Align ;

从第 5 个命令可以看出,您需要通过提供模式手动重新组织代码

结果:

if      ( str == "str"     ) decorator [ "str"     ] = &Props::goodstr     ;   
else if ( str == "strM"    ) decorator [ "strM"    ] = &Props::goodstrM    ;   
else if ( str == "strXL"   ) decorator [ "strXL"   ] = &Props::goodstrXL   ;   
else if ( str == "strXXXL" ) decorator [ "strXXXL" ] = &Props::goodstrXXXL ; 

关于c++ - 在多行中用相应的元素格式化 C++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38465034/

相关文章:

c++ - 通过具有 vector 基地址的指针读取 vector 元素时出错

c++ - 复数矩阵乘法 Eigen 与 Matlab

c++ - Visual Studio 2012 Ultimate 中的反汇编

vim - 如何在vim中使用foldmethod indent创建折叠?

vim - 在 Vim 命令中使用当前目录

c++ - BOOST_PP_ITERATION 可变长度参数

linux - 如何在大型代码库中运行 ctags?

frameworks - IDE和Framework之间的区别

visual-studio-2010 - 如何在方法 block 内折叠注释 block 的代码?

delphi - Delphi IDE-如何使禁用的工具栏按钮呈灰度?