Uncrustify Collapse Multiline Function Call

穿精又带淫゛_ 提交于 2020-06-12 08:32:31

问题


I have function calls that look like this (for no apparent reason):

func
(
    a,
    b,
    c
)

Is there a way to make uncrustify collapse the function into a single line? I have been trying for two days not on and off...

I got it to work for function declarations, but I don't get it to work for function calls.

While we are at it I also have functions that look like so:

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

Is there a way to handle that case too, without breaking the code? Since uncrustify keeps comments, I think this is kind of impossible. With function declarations it collapses it to the first comment.


回答1:


Reading the docs, I came up with this:

# 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

As you anticipate, the comments kind-of ruin it, though. There is an option to change single-line comments (//) into block comments (/* ... */) though, which should make it easier for you to join lines manually (e.g. in vim v%J)

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

I tested it with prototypes, declarations and calls:

  • input http://ideone.com/VPOTF
  • output http://ideone.com/z6jYY

The calls are not affected. Note also the following related option:

# Whether to fully split long function protos/calls at commas
ls_func_split_full                       = false    # false/true



回答2:


After some LOOONG research I have come to the conclusion, that uncrustify can't do that. For my porposses I hacked a small perl script together:

$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);
}

I will look into if I can build a patch that integrates that feature into uncustify.



来源:https://stackoverflow.com/questions/12276328/uncrustify-collapse-multiline-function-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!