我正在使用Uncrustify v0.60格式化我的C++源代码。为了配置Uncrustify,我使用了UniversalIndentGUI v1.2.0rev.1070。
在Line Splitting options部分的UniversalIndentGUI中,我将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 Start或Nl Func Def Start,它们在开始括号(字符之后添加换行符,但这也影响到长度小于120个字符的代码。我不想让以下代码分散在几行代码中:
int Sum( int a, int b, int c, int d );发布于 2013-03-18 02:07:21
不幸的是,在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提供某种“避免令人困惑的缩进”选项,这将是很棒的。例如,在本例中,最后一个片段如下所示:
PluginDialog::
PluginDialog(
QString const& path,
QStringList const& fileNames,
QWidget* parent):
QDialog(parent),
label(new QLabel),
treeWidget(new QTreeWidget),
okButton(new QPushButton(tr("OK"))) {
// ...
}这显然是更易读和更令人愉快。我为Uncrustify提出了这个特性。然而,我怀疑我们很快就能看到它的实现,因为这个项目的作者似乎要么忙着做其他的事情,要么对开发这个项目不再感兴趣。
发布于 2016-07-19 07:31:03
对于我(使用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 = removehttps://stackoverflow.com/questions/15449079
复制相似问题