Uncrustify折叠多行函数调用

时间:2012-09-05 07:16:18

标签: c++ c formatting uncrustify

我的函数调用看起来像这样(没有明显的原因):

func
(
    a,
    b,
    c
)

有没有办法让unrustify将函数折叠成一行?我一直在尝试两天不上下......

我让它用于函数声明,但是我没有让它用于函数调用。

虽然我们在这里,但我也有类似的功能:

func
(
    a, // (IN) the A
    b, // (IN) something b
    c  // (OUT) the resulting value
)

有没有办法处理这种情况,而不会破坏代码?因为unrustify保留评论,我认为这是不可能的。使用函数声明,它会将其折叠为第一个注释。

2 个答案:

答案 0 :(得分:2)

阅读文档,我想出了这个:

# Add or remove newline between a function name and the opening '('
nl_func_paren                            = remove   # ignore/add/remove/force

# Add or remove newline between a function name and the opening '(' in the definition
nl_func_def_paren                        = remove   # ignore/add/remove/force

# Add or remove newline after '(' in a function declaration
nl_func_decl_start                       = remove   # ignore/add/remove/force

# Add or remove newline after '(' in a function definition
nl_func_def_start                        = remove   # ignore/add/remove/force

# Add or remove newline after each ',' in a function declaration
nl_func_decl_args                        = remove   # ignore/add/remove/force

# Add or remove newline after each ',' in a function definition
nl_func_def_args                         = remove   # ignore/add/remove/force

# Add or remove newline before the ')' in a function declaration
nl_func_decl_end                         = remove   # ignore/add/remove/force

# Add or remove newline before the ')' in a function definition
nl_func_def_end                          = remove   # ignore/add/remove/force

正如你所预料的那样,评论有点毁了它。 是将单行注释//)更改为块注释({的选项{1}})但是,这应该可以让你更容易手动连接线(例如在vim /* ... */中)

v%J

我用原型,声明调用来测试它:

通话不受影响。另请注意以下相关选项:

# Whether to change cpp-comments into c-comments
cmt_cpp_to_c                             = true    # false/true

答案 1 :(得分:0)

在一些LOOONG研究之后,我得出结论,那个解开不能做到这一点。对于我的观点,我一起破解了一个小的perl脚本:

$filename = $ARGV[0];

{
    open(FILE, "<", $filename) or die "Cant open $filename for reading\n";
    local $/ = undef;
    $lines = <FILE>;
    close(FILE);
}

# squash comments in function calls and declarations
$lines =~ s/,[ \t]*\/\/[^\n\r]*/,/gm;
# squash last comment in function calls and declarations
$lines =~ s/[ \t]*\/\/[^\n\r]*\r\n[ \t]*\)/\)/gm;
# squash newlines at the start of a function call or declaration
$lines =~ s/\([ \t]*\r\n[ \t]*/\(/gm;
# squash newlines in function calls and declarations
$lines =~ s/,[ \t]*\r\n[ \t]*/, /gm;
# squash the last newline in a function call or declaration
$lines =~ s/[ \t]*\r\n[ \t]*\)/\)/gm;

{
    open(FILE, ">", $filename) or die "Cant open $filename for writing\n";
    print FILE $lines;
    close(FILE);
}

我将研究是否可以构建一个将该功能集成到unoctify中的补丁。