2013-03-16 60 views
4

完全拆分长功能定义我使用Uncrustify v0.60来格式化我的C++源代码。为了配置Uncrustify,我使用了UniversalIndentGUI v1.2.0 rev.1070不仅在逗号分隔使用Uncrustify

在UniversalIndentGUI的Line Splitting options部我已设置Code Width〜120

假设我有以下的片的示例代码:

namespace MyNameSpace 
{ 
    class MyClass 
    { 
    public: 
     std::map< std::string, MyOtherClass* >* ConstructMyOtherClassMap(std::vector<std::string>* allNames, int arg0, double arg1, char arg2); 

    } 
} 

也就是说方法声明在列> 120结束,所以Uncrustify返回以下结果:

namespace MyNameSpace 
{ 
    class MyClass 
    { 
    public: 
     std::map< std::string, MyOtherClass* >* ConstructMyOtherClassMap(std::vector<std::string>* allNames, 
      int arg0, 
      double arg1, 
      char arg2); 

    } 
} 

正如您可以看到Uncrustify将参数列表拆分为c ommas现在的方法声明中列结束< 120.然而,在这种情况下,我想Uncrustify穿上它自己的行中的第一个参数,以及像这样:

namespace MyNameSpace 
{ 
    class MyClass 
    { 
    public: 
     std::map< std::string, MyOtherClass* >* ConstructMyOtherClassMap( 
      std::vector<std::string>* allNames, 
      int arg0, 
      double arg1, 
      char arg2); 

    } 
} 

是否有可能用做Uncrustify v0.60?

我知道在Newline adding and removing部分选项,例如Nl Func Decl StartNl Func Def Start的左括号(字符后添加一个换行,但这也影响了代码,长< 120个字符。我不希望以下代码分布在多行:

int Sum(int a, int b, int c, int d); 
+0

与Uncrustify 0.63任何更新?你是否设法达到你想要的? – mtb 2016-07-18 14:28:18

+0

@mtb:我不再使用Uncrustify,所以我没有测试过这个。你似乎已经知道自己了,但是,从你的回答来看。 – Manuzor 2016-07-25 14:22:25

回答

3

不幸的是,对于当前的Uncrustify状态,这是不可能的。所以你能做的最好的事情是配置您在以下方式中提到的选项:

nl_func_decl_start = ignore 
nl_func_def_start = ignore 

nl_func_decl_start_single = ignore 
nl_func_def_start_single = ignore 

ls_func_split_full = true 

和手工包装在适当的情况下,第一个参数。但是,我个人认为这不是一个好主意。只需让它自动执行懒/按需包装。例如,我喜欢上面列出的相同设置,并且在任何可能的情况下仍然有非常整齐的代码。例子如下。

没有包装 - 构造函数的参数和构造函数初始化列表中都嵌入最大线路长度:

PluginDialog:: 
PluginDialog(QString const& path, QStringList const& fileNames, QWidget* parent): QDialog(parent), label(new QLabel), treeWidget(new QTreeWidget), okButton(new QPushButton(tr("OK"))) { 
    // ... 
} 

现在他们不适合,而按照惯例,我们决定换初始化列表第一:

PluginDialog:: 
PluginDialog(QString const& path, QStringList const& fileNames, QWidget* parent): QDialog(parent), 
                        label(new QLabel), 
                        treeWidget(new QTreeWidget), 
                        okButton(new QPushButton(tr("OK"))) { 
    // ... 
} 

相反的约定也是可能的:

PluginDialog:: 
PluginDialog(QString const&  path, 
      QStringList const& fileNames, 
      QWidget*   parent): QDialog(parent), label(new QLabel), treeWidget(new QTreeWidget), okButton(new QPushButton(tr("OK"))) { 
    // ... 
} 

现在从2层以前的两种情况下不适合,因此,它们中的合并到下一个,而唯一可能的配置:

PluginDialog:: 
PluginDialog(QString const&  path, 
      QStringList const& fileNames, 
      QWidget*   parent): QDialog(parent), 
             label(new QLabel), 
             treeWidget(new QTreeWidget), 
             okButton(new QPushButton(tr("OK"))) { 
    // ... 
} 

现在我们不要再适应,并按照惯例,我们决定移动构造函数初始化构造函数参数列表列下清单列:

PluginDialog:: 
PluginDialog(QString const&  path, 
      QStringList const& fileNames, 
      QWidget*   parent): 
    QDialog(parent), 
    label(new QLabel), 
    treeWidget(new QTreeWidget), 
    okButton(new QPushButton(tr("OK"))) { 
    // ... 
} 

通过我们再次分支的情况下,即这可能是可能的方式:

PluginDialog:: 
PluginDialog(
    QString const&  path, 
    QStringList const& fileNames, 
    QWidget*   parent): QDialog(parent), 
           label(new QLabel), 
           treeWidget(new QTreeWidget), 
           okButton(new QPushButton(tr("OK"))) { 
    // ... 
} 

最后,从2层以前的两种情况下不适合,因此,它们中的合并到最后的,唯一可能的配置:

PluginDialog:: 
PluginDialog(
    QString const&  path, 
    QStringList const& fileNames, 
    QWidget*   parent): 
    QDialog(parent), 
    label(new QLabel), 
    treeWidget(new QTreeWidget), 
    okButton(new QPushButton(tr("OK"))) { 
    // ... 
} 

这将是巨大的,如果Uncrustify提供一种“避免混淆凹槽”选项像Jindent一样。在这种情况下,最后一个片段例如如下所示:

PluginDialog:: 
PluginDialog(
    QString const&  path, 
    QStringList const& fileNames, 
    QWidget*   parent): 
    QDialog(parent), 
    label(new QLabel), 
    treeWidget(new QTreeWidget), 
    okButton(new QPushButton(tr("OK"))) { 
    // ... 
} 

这显然更具可读性和令人愉快。我为Uncrustify提出了这个特性。然而,我怀疑我们很快就会看到它的实施,因为这个项目的作者似乎要么忙于其他一些东西,要么对这个项目不再积极地开发活动感兴趣。

3

对我来说,(使用Uncrustify 0.63),以达到你想要的东西,它的工作原理是组合:

添加或以后删除换行“(”在函数声明

nl_func_decl_start      = add 

添加或删除换行符后“(”在函数定义

nl_func_def_start      = add 

重写nl_func_decl_start当仅存在一个参数。

nl_func_decl_start_single    = remove 

当只有一个参数时,覆盖nl_func_def_start。

nl_func_def_start_single     = remove 

添加或在函数声明在函数定义后的各除去换行符“”

nl_func_decl_args      = add 

添加或之后的每个移除换行“”

nl_func_def_args       = add 

添加或删除')'之前的换行符

nl_func_decl_end       = add 

在函数定义

nl_func_def_end       = add 

重写添加或之前移除换行“)” nl_func_decl_end当仅存在一个参数。

nl_func_decl_end_single     = remove 

当只有一个参数时,覆盖nl_func_def_end。

nl_func_def_end_single     = remove 

紧凑的版本(不解释):

nl_func_decl_start      = add 
nl_func_def_start      = add 
nl_func_decl_start_single    = remove 
nl_func_def_start_single     = remove 
nl_func_decl_args      = add 
nl_func_def_args       = add 
nl_func_decl_end       = add 
nl_func_def_end       = add 
nl_func_decl_end_single     = remove 
nl_func_def_end_single     = remove