寻求一个可以在线条元素之间插入空格的 C 美化器

发布于 2024-08-09 07:41:42 字数 1539 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赢得她心 2024-08-16 07:41:43
indent -prs -br -i 4 file

变成这样

#define f(x) ((x)+(x))

if((foo=bar(arg1,arg2,arg3))==NULL){
    printf("Error 42");
    f(42);
}

#define f(x) ((x)+(x))

if ( ( foo = bar ( arg1, arg2, arg3 ) ) == NULL ) {
    printf ( "Error 42" );
    f ( 42 );
}

-prs 选项在括号周围放置空格,运算符周围和逗号后面的空格是标准的。 -br 选项强制您的支撑样式,-i 4 使用 4 个空格来缩进。请注意,宏定义未修改,但代码中对类似函数的宏的调用是(大概是您想要的)。

indent -prs -br -i 4 file

Turns this:

#define f(x) ((x)+(x))

if((foo=bar(arg1,arg2,arg3))==NULL){
    printf("Error 42");
    f(42);
}

Into this:

#define f(x) ((x)+(x))

if ( ( foo = bar ( arg1, arg2, arg3 ) ) == NULL ) {
    printf ( "Error 42" );
    f ( 42 );
}

The -prs option puts spaces around parenthesis, the spaces around operators and after the commas come standard. The -br option enforces your bracing style, and -i 4 uses 4 spaces to indent. Note that the macro definition is not modified but the call to the function-like macro in the code is (presumably what you'd want).

明月夜 2024-08-16 07:41:43

您可能需要查看 GNU 缩进。我相信它可以满足您正在寻找的一切。

You may want to look at GNU Indent. I believe it can do everything you're looking for.

近箐 2024-08-16 07:41:43

Gnu Indent 或许可以做到这一点。不幸的是,缩进有大量选项,其中许多选项根本不直观,而且许多选项以极其奇怪的方式交互。我从来没有(甚至一次)设法让它以一种出来的方式不比进入的方式更难看的方式格式化代码。在某些情况下,它更统一。在其他情况下,我认为它确实是统一的,但它遵循的规则仍然很奇怪,以至于结果常常看起来很奇怪。

在与缩进斗争了一段时间后,我决定编写一个仅支持一种格式的更简单的程序会更容易,如果我想更改格式,只需编辑代码即可。

Gnu Indent can probably do that. Unfortunately, indent has a huge number of options, many of which aren't at all intuitive, and many of them interact in extremely strange ways. I've never (not even once) managed to get it to format code in a way that wasn't uglier coming out than going in. In some cases it was more uniform. In others I suppose it must really have been uniform, but the rules it was following were still strange enough that the result often just looked wierd.

After struggling with indent for a while, I decided that it was easier to write a much simpler program that only supported one format, and just edit the code if I wanted to change the format.

吲‖鸣 2024-08-16 07:41:43

我使用 vim 与选项 cindent 和 formatoptions 取得了令人满意的效果。
你可以缩进整个文件,

  gg=G
  gg  -- go to 1st line 
  =G  -- indent upto lastline

你可能想写一个 formatexpr (vim7) 或
您可能想使用 s///g 命令编写自定义函数并将其映射到某个键。

下面将在括号后添加一个空格,除了 #define 行

v/#define/ s/[()]/\1 /g

是的,您将学习正则表达式:)

i had used vim with options cindent and formatoptions to a satisfactory effect.
you can indent a whole file by

  gg=G
  gg  -- go to 1st line 
  =G  -- indent upto lastline

you migh want to write a formatexpr (vim7) or
you might want to write a custom function with s///g commands and map it to a key.

the follwing will put a space after parantheses except in #define lines

v/#define/ s/[()]/\1 /g

yes, you will be learning regexes :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文