是否有缩进 C++ 程序的实用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1813897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:13:56  来源:igfitidea点击:

Is there a utility to indent C++ programs

c++indentation

提问by user855

I am trying to use "indent" program to indent C++ programs. But it does not seem to work fine. It is messing up the indentation much more.

我正在尝试使用“缩进”程序来缩进 C++ 程序。但它似乎不能正常工作。它更多地弄乱了缩进。

It is a Class file. Can you please suggest the right options for it or another program that works?

它是一个类文件。您能否为它或其他有效的程序建议正确的选项?

Thanks

谢谢

采纳答案by Andrew Hare

Try Artistic Style:

尝试艺术风格

Artistic Style is a source code indenter, formatter, and beautifier for the C, C++, C# and Java programming languages.

Artistic Style 是 C、C++、C# 和 Java 编程语言的源代码缩进器、格式化器和美化器。

回答by Rantaak

in visual studio, CTRL+a, CTRL+k, CTRL+fwill auto-indent the entire file you're working in.

在 Visual Studio 中,CTRL+ aCTRL+ kCTRL+f将自动缩进您正在处理的整个文件。

回答by greyfade

Many editors like Vim and Emacs have a feature that reindents code according to its built-in indentation standard.

许多像 Vim 和 Emacs 这样的编辑器都有一个功能,可以根据其内置的缩进标准重新缩进代码。

In Vim, you can type =with any motion. (Like =%at any block delimiter or S-vto select a region and then =to reformat.) The gqcommand can be used to reformat comments and other text.

在 Vim 中,您可以输入=任何动作。(就像=%在任何块分隔符处或S-v选择一个区域然后=重新格式化。)该gq命令可用于重新格式化注释和其他文本。

In Emacs, you can type M-xindent-regionafter selecting a region. (Such as with M-<and M->.) You can also reformat comments with M-xfill-paragraph.

在 Emacs 中,您可以M-xindent-region在选择区域后键入。(例如使用M-<M->。)您还可以使用 重新格式化注释M-xfill-paragraph

回答by Philipp

Another tool for automated formatting of C++ code is clang-format.

另一个用于自动格式化 C++ 代码的工具是clang-format

回答by cxxl

This is in reply to the OP and other posters: I work under Windows and was looking for an indent as well. This is what I learned:

这是对 OP 和其他海报的回复:我在 Windows 下工作并且也在寻找缩进。这是我学到的:

I tried bcpp. It seems to be quite old (more or less from 1996) and you need to find a windows exe. I found one here: http://www.zeusedit.com/zforum/viewtopic.php?t=1067. Still I think it's not really worth the bother, since it can only do space/tab, indentation level and brace conversion.

我试过bcpp。它似乎很旧(或多或少从 1996 年开始),您需要找到一个 Windows exe。我在这里找到了一个:http: //www.zeusedit.com/zforum/viewtopic.php?t=1067。我仍然认为这并不值得打扰,因为它只能进行空格/制表符、缩进级别和大括号转换。

Then there is Artistic Style, which is newer and on sourceforge. It seems to be quite capable, but it can not, for example, reformat spaces around function names and parameters (like, turn if ( a ( b ) )into if (a(b)).

然后是Artistic Style,它是较新的并且在 sourceforge 上。它似乎很有能力,但它不能,例如,重新格式化函数名称和参数周围的空格(例如,if ( a ( b ) )变成if (a(b)).

I like Cygwin indent( http://www.cygwin.com/) best, but it works only well on newline separated files, so you have to use it like "dos2unix < infile | indent ...". But indent will not reformat boxed comments... :)

我最喜欢Cygwin indent( http://www.cygwin.com/),但它只适用于换行符分隔的文件,所以你必须像“dos2unix < infile | indent ...”一样使用它。但缩进不会重新格式化盒装评论...... :)

Maybe a combi of Artistic Style and Cygwin indent works best. Now I use this command line: astyle --style=kr -s3 --break-closing-brackets < infile.cpp | dos2unix | indent --no-tabs --k-and-r-style --indent-level 3 --dont-cuddle-else | unix2dos. I bit long, I agree, but apart from that it produces a very good result. :)

也许艺术风格和 Cygwin 缩进的组合效果最好。现在我使用这个命令行:astyle --style=kr -s3 --break-closing-brackets < infile.cpp | dos2unix | indent --no-tabs --k-and-r-style --indent-level 3 --dont-cuddle-else | unix2dos. 我有点长,我同意,但除此之外,它产生了非常好的结果。:)

回答by David Rodríguez - dribeas

bcpp, available in any linux distro (and probably for mingw/cygwin, but I have not checked it) is a C++ indenter and beautifier. You can parametrize how it works with the code.

bcpp,可在任何 linux 发行版中使用(可能适用于 mingw/cygwin,但我还没有检查过)是 C++ 压头和美化器。您可以参数化它如何与代码一起工作。

回答by Vadakkumpadath

Another short key in Visual Studio 2008:

Visual Studio 2008 中的另一个快捷键:

Ctrl+A, Alt+F8

Ctrl+A、Alt+F8

回答by Anish Sheela

In netbeans, you can simply do

netbeans中,你可以简单地做

Alt + Shift + F

Alt + Shift + F

Please make sure that the plugins are installed for corresponding language. For example, for PHP code indentation, you have to choose netbeans PHP or install PHP plugins from repository.

请确保安装了相应语言的插件。例如,对于 PHP 代码缩进,您必须选择 netbeans PHP 或从存储库安装 PHP 插件。