在save上格式化C/ c++代码

时间:2023-01-14 10:50:26

The formatting options of Eclipse avoids me lots of conflicts with other users, and ensure that my code always looks "clean" (especially regarding my git commits).

Eclipse的格式选项避免了我与其他用户之间的许多冲突,并确保我的代码始终看起来“干净”(特别是对于我的git提交)。

I know about the "Format on save" option in Java editor, but I could not find one in C/C++.

我知道Java编辑器中的“保存格式”选项,但是在C/ c++中找不到。

Is this deliberate?

这是故意的吗?

May I expect to see such option one day? Or maybe I haven't looked enough?

我希望有一天能看到这样的选择吗?或者我看起来不够?

7 个解决方案

#1


3  

Format on save in CDT is now implemented in Eclipse Mars.

在CDT中保存的格式现在在Eclipse Mars中实现。

#2


16  

I know about the "Format on save" option in Java editor, but I could not find one in C/C++ ... Or maybe I haven't looked enough?

我知道Java编辑器中的“保存格式”选项,但是我在C/ c++中找不到。或者我看起来不够?

I'm afraid that option is simply not available for the CDT C++ editor (Eclipse CDT Juno Service Release 1):

恐怕CDT c++编辑器无法使用该选项(Eclipse CDT Juno服务发布1):

在save上格式化C/ c++代码

That's everything that's available at Eclipses IDE Level.

这就是在eclipse IDE级别上可用的所有东西。

There are two solutions you might consider to solve your concerns:

你可以考虑用两种方法来解决你的问题:

  1. You might consider adopting some discipline as mentioned in the other answers
  2. 你可以考虑采用其他答案中提到的一些学科。
  3. Running an appropriate script to format your source files before committing them. The AStyle tool might be helpful for this purpose.
  4. 在提交源文件之前运行适当的脚本来格式化它们。AStyle工具可能有助于达到这个目的。

One could also think about an automatic pre-checkin/-commit interception running a script for the actually used Revision Control System. Many revision/version control systems offer such feature.

还可以考虑为实际使用的修订控制系统运行脚本的自动预签/提交拦截。许多修订/版本控制系统都提供这样的特性。

May I expect to see such option one day?

我希望有一天能看到这样的选择吗?

Did you ask for this feature on the Eclipse/CDT community forum, or placed a feature request in the Eclipse CDT bugzilla tracking system? If no one knows about such feature demand and whether it is demanded by a majority of users, chances that this will be implemented by 'accident' are low.

您是在Eclipse/CDT社区论坛上要求这个特性,还是在Eclipse CDT bugzilla跟踪系统中提出了一个特性请求?如果没有人知道这种特性需求,也不知道大多数用户是否需要它,那么通过“意外”实现这种需求的可能性就很小。

#3


5  

You can use the keys "Ctrl+Shift+F" to format your edited c/c++ file.

您可以使用“Ctrl+Shift+F”键来格式化您编辑的c/c++文件。

#4


3  

I'm using AStyle program to format my code following a set of rules. At the moment I'm doing this from the CLI only but I know there's a plugin for eclipse, which I haven't used yet:

我正在使用AStyle程序来按照一组规则格式化我的代码。目前我只在CLI中做这个,但是我知道eclipse有一个插件,我还没有使用:

Astyle Eclipse is based on Astyle program, it aims to provide a C/C++ formatter for CDT plugin in eclipse. so all the supported format optional shall check astyle

Astyle Eclipse基于Astyle程序,它旨在为Eclipse中的CDT插件提供一个C/ c++格式化程序。所以所有支持的可选格式都应该检查astyle

#5


2  

There is no such default option in the eclipse cdt yet. You can try and write your own plugin This eclipse forum thread shows how to invoke the CDT formatter from an eclipse plugin. You only need to figure out how to trigger a plugin call on every save action.

在eclipse cdt中没有这样的默认选项。您可以尝试编写自己的插件,这个eclipse论坛线程演示如何从eclipse插件调用CDT格式化程序。您只需要知道如何在每次保存操作中触发一个插件调用。

#6


0  

Here is a plugin to fit the need: fos4cdt. Eclipse Mars may contain this feature, but many users will continue to use older versions for a while. So, it can be useful.

这里有一个插件可以满足需求:fos4cdt。Eclipse Mars可能包含这个特性,但是许多用户将在一段时间内继续使用旧版本。所以,它是有用的。

#7


-3  

You can trying to select all code,and use Alt + F8 to format. This is way of VC IDE,but I don't sure that Eclipse can work.

您可以尝试选择所有的代码,并使用Alt + F8格式。这是VC IDE的一种方式,但是我不确定Eclipse是否可以工作。

#1


3  

Format on save in CDT is now implemented in Eclipse Mars.

在CDT中保存的格式现在在Eclipse Mars中实现。

#2


16  

I know about the "Format on save" option in Java editor, but I could not find one in C/C++ ... Or maybe I haven't looked enough?

我知道Java编辑器中的“保存格式”选项,但是我在C/ c++中找不到。或者我看起来不够?

I'm afraid that option is simply not available for the CDT C++ editor (Eclipse CDT Juno Service Release 1):

恐怕CDT c++编辑器无法使用该选项(Eclipse CDT Juno服务发布1):

在save上格式化C/ c++代码

That's everything that's available at Eclipses IDE Level.

这就是在eclipse IDE级别上可用的所有东西。

There are two solutions you might consider to solve your concerns:

你可以考虑用两种方法来解决你的问题:

  1. You might consider adopting some discipline as mentioned in the other answers
  2. 你可以考虑采用其他答案中提到的一些学科。
  3. Running an appropriate script to format your source files before committing them. The AStyle tool might be helpful for this purpose.
  4. 在提交源文件之前运行适当的脚本来格式化它们。AStyle工具可能有助于达到这个目的。

One could also think about an automatic pre-checkin/-commit interception running a script for the actually used Revision Control System. Many revision/version control systems offer such feature.

还可以考虑为实际使用的修订控制系统运行脚本的自动预签/提交拦截。许多修订/版本控制系统都提供这样的特性。

May I expect to see such option one day?

我希望有一天能看到这样的选择吗?

Did you ask for this feature on the Eclipse/CDT community forum, or placed a feature request in the Eclipse CDT bugzilla tracking system? If no one knows about such feature demand and whether it is demanded by a majority of users, chances that this will be implemented by 'accident' are low.

您是在Eclipse/CDT社区论坛上要求这个特性,还是在Eclipse CDT bugzilla跟踪系统中提出了一个特性请求?如果没有人知道这种特性需求,也不知道大多数用户是否需要它,那么通过“意外”实现这种需求的可能性就很小。

#3


5  

You can use the keys "Ctrl+Shift+F" to format your edited c/c++ file.

您可以使用“Ctrl+Shift+F”键来格式化您编辑的c/c++文件。

#4


3  

I'm using AStyle program to format my code following a set of rules. At the moment I'm doing this from the CLI only but I know there's a plugin for eclipse, which I haven't used yet:

我正在使用AStyle程序来按照一组规则格式化我的代码。目前我只在CLI中做这个,但是我知道eclipse有一个插件,我还没有使用:

Astyle Eclipse is based on Astyle program, it aims to provide a C/C++ formatter for CDT plugin in eclipse. so all the supported format optional shall check astyle

Astyle Eclipse基于Astyle程序,它旨在为Eclipse中的CDT插件提供一个C/ c++格式化程序。所以所有支持的可选格式都应该检查astyle

#5


2  

There is no such default option in the eclipse cdt yet. You can try and write your own plugin This eclipse forum thread shows how to invoke the CDT formatter from an eclipse plugin. You only need to figure out how to trigger a plugin call on every save action.

在eclipse cdt中没有这样的默认选项。您可以尝试编写自己的插件,这个eclipse论坛线程演示如何从eclipse插件调用CDT格式化程序。您只需要知道如何在每次保存操作中触发一个插件调用。

#6


0  

Here is a plugin to fit the need: fos4cdt. Eclipse Mars may contain this feature, but many users will continue to use older versions for a while. So, it can be useful.

这里有一个插件可以满足需求:fos4cdt。Eclipse Mars可能包含这个特性,但是许多用户将在一段时间内继续使用旧版本。所以,它是有用的。

#7


-3  

You can trying to select all code,and use Alt + F8 to format. This is way of VC IDE,but I don't sure that Eclipse can work.

您可以尝试选择所有的代码,并使用Alt + F8格式。这是VC IDE的一种方式,但是我不确定Eclipse是否可以工作。

相关文章