2013-03-16 61 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