Separate definitions of long functions completely not only into commas using Uncrustify

I am using Uncrustify v0.60 to format my C ++ source code. To configure Uncrustify, I use UniversalIndentGUI v1.2.0 rev.1070 .

In the Line Splitting options section of UniversalIndentGUI, I set the Code Width to 120.

Suppose I have the following example code snippet:

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

The declaration of this method ends in a column> 120, so Uncrustify returns the following result:

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

As you can see, Uncrustify breaks the parameter list with a comma, and now the method declaration ends in column <120. However, in this case, I want Uncrustify to put the first parameter in its own row, for example:

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

Can this be done with Uncrustify v0.60?

I am aware of the options in the Newline adding and removing , such as Nl Func Decl Start or Nl Func Def Start , which add a new line after the opening bracket character ( but this also affects the code, 120 characters. I do not want the following code spread over several lines:

 int Sum( int a, int b, int c, int d ); 
+4
source share
2 answers

Unfortunately, this is not possible in the current state of Uncrustify. Therefore, the best thing you can do is configure the options you mentioned as follows:

 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 

and manually pack the first parameter as appropriate. However, I personally do not think this is a good idea. Just let it automatically do lazy / on demand. For example, I like the same settings that I listed above and still have very neat code in every possible case. The following are examples.

There are no wrapping parameters - constructor and list of constructor initializers that fit into the maximum string length:

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

Now they do not fit, and by agreement, we first decided to move the list of initializers:

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

another agreement is possible:

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

Now, any case from the previous two does not fit, and therefore any of them merges into the following and only possible configuration:

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

Now we are no longer suitable, and by agreement, we decided to move the column of the list of constructor initializers to the column of the list of constructor parameters:

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

By the way, we again have a branching case, i.e. it is also possible:

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

Finally, any of the two previous cases does not fit, and therefore any of them merges into the final and only possible configuration:

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

It would be great if Uncrustify suggested the option to "avoid confusing indentation", for example, Jindent. In this case, the last fragment, for example, looks like this:

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

which is more readable and enjoyable. I suggested this feature for Uncrustify. Nevertheless, I doubt that we will see that this is realized in the near future, because the author of this project seems to be too busy with some other material or not interested in actively developing this project.

+3
source

For me (using Uncrustify 0.63) to achieve what you wanted, it works with this combination:

Add or remove new line after '(' in function declaration

 nl_func_decl_start = add 

Add or remove new line after '(' in function definition

 nl_func_def_start = add 

Overrides nl_func_decl_start when there is only one parameter.

 nl_func_decl_start_single = remove 

Overrides nl_func_def_start when there is only one parameter.

 nl_func_def_start_single = remove 

Add or remove a new line after each ',' in a function declaration

 nl_func_decl_args = add 

Add or remove a new line after each ',' in the function definition

 nl_func_def_args = add 

Add or remove a new line before the character ')' in the function declaration

 nl_func_decl_end = add 

Add or remove a new line before the `` '' in the function definition

 nl_func_def_end = add 

Overrides nl_func_decl_end when there is only one parameter.

 nl_func_decl_end_single = remove 

Overrides nl_func_def_end when there is only one parameter.

 nl_func_def_end_single = remove 

Compact version (no explanation):

 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 
+3
source

All Articles