繁体   English   中英

使用Uncrustify不仅在逗号处完全拆分长函数定义

[英]Fully split long function definitions not only at commas using Uncrustify

我正在使用Uncrustify v0.60格式化我的C ++源代码。 为了配置Uncrustify,我正在使用UniversalIndentGUI v1.2.0 rev.1070

在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在逗号处拆分了参数列表,现在方法声明以<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 );

不幸的是,对于当前的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"))) {
  // ...
}

现在,前两个案例中的任何一种都不适合,因此它们中的一个都合并到下一个也是唯一可能的配置中:

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"))) {
  // ...
}

最后,前两个案例中的任何一种都不适合,因此它们中的任何一个都合并为最终且唯一可能的配置:

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提出了此功能。 但是,我怀疑我们是否会很快看到它的实现,因为该项目的作者似乎太忙于其他工作,或者对再积极开发该项目并不感兴趣。

对我来说(使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM