【问题标题】:Uncrustify Collapse Multiline Function CallUncrustify Collapse 多行函数调用
【发布时间】:2012-09-05 07:16:18
【问题描述】:

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

func
(
    a,
    b,
    c
)

有没有办法让 uncrustify 将函数折叠成一行?我试了两天没断断续续...

我让它适用于函数声明,但我不让它适用于函数调用。

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

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

有没有办法在不破坏代码的情况下处理这种情况?由于 uncrustify 保留 cmets,我认为这是不可能的。使用函数声明,它会将其折叠到第一条评论。

【问题讨论】:

  • 感谢您告诉我有关 uncrustify 的信息。我想我可能会喜欢它(除了)/(过度)astyle,我通常使用它:)
  • 你可能想试试 UniversalIndentGUI,在那里你可以尝试一堆不同的格式化工具并立即看到结果。
  • 哦,我试用了所有 C++ 的免费格式化工具(我可以找到):rioki.org/2012/09/04/cleanup-yerr-code.html

标签: c++ c formatting uncrustify


【解决方案1】:

阅读文档后,我想到了这个:

# 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

不过,正如您所料,cmets 有点毁了它。 有一个选项可以将 单行 cmets (//) 更改为 block cmets (/* ... */ ) 不过,这应该使您更容易手动加入行(例如在 vim v%J 中)

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

我用原型、声明调用对其进行了测试:

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

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

【讨论】:

  • 好吧,我已经使用了 nl_func*** 选项。他们很好地处理函数的声明和定义,但不是调用。我知道 cmt_cpp_to_c 但我不想出于其他原因使用它,我可以忍受它。我需要一个“删除函数调用/定义中的 cmets”选项。如果调用合并可行,我只需使用编辑器 fu 删除 cmets 并重新运行 uncrustify...
  • 下次请在帖子中说明此信息。它可以节省您阅读您已经知道的内容的精力。专业提示:要删除 cmets,请考虑使用预处理器(例如在 vim 中)::'<,'>!cpp | grep -Ev '^\#'
  • “我让它适用于函数声明,但我不让它适用于函数调用。” - rioki 很抱歉,虽然很清楚,但还是感谢您的尝试。
【解决方案2】:

经过一些 LOOONG 研究后,我得出结论,uncrustify 无法做到这一点。对于我的 porposses,我一起编写了一个小的 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);
}

我会考虑是否可以构建一个补丁,将该功能集成到 uncustify 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    相关资源
    最近更新 更多