如何配置 Eclipse 格式化程序以不在字段声明之间插入空行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2101955/
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-09-19 14:19:47  来源:igfitidea点击:

How to configure Eclipse formatter to not insert blank line between field declarations?

eclipseconfigurationformatting

提问by Grundlefleck

I am using Eclipse 3.5.1 (Galileo) and am having trouble with one of the auto-formatting settings.

我正在使用 Eclipse 3.5.1 (Galileo) 并且在使用自动格式化设置之一时遇到问题。

At the moment when I auto-format a blank line is inserted between each and every field declaration. The behaviour I'm looking for is to organise field declarations without a blank line between. Except in the case where there is already one or more blank lines between the declarations, in that case I'd like the formatter to respect the space, and reduce >=1 lines to 1 line.

在我自动格式化时,在每个字段声明之间插入一个空行。我正在寻找的行为是组织字段声明,中间没有空行。除非声明之间已经有一个或多个空行,在这种情况下,我希望格式化程序尊重空格,并将 >=1 行减少到 1 行。

I know this was possible at some point with Eclipse (can't remember the version but I have used a setting for this), but I can't seem to find the correct settings in Window->Preferences->Java->Code Style->Formatter.

我知道在某些时候使用 Eclipse 是可能的(不记得版本,但我为此使用了一个设置),但我似乎无法在Window->Preferences->Java->Code Style->Formatter.

Which setting do I configure to enable this behaviour?

我配置哪个设置来启用此行为?



EDIT: It seems the behaviour I'm describing is the default, and for some reason I couldn't find a way to return to that without removing the .settingsdirectory. It also couldn't be reproduced, which suggests that it's something weird with the project settings (it's checked out from an open source project). Although I've found a hacky way to fix this, I'm still looking for suggestions and will be able to verify and accept an answer.

编辑:似乎我所描述的行为是默认行为,出于某种原因,我无法找到一种方法来返回该.settings目录而不删除目录。它也无法复制,这表明项目设置有些奇怪(它是从开源项目中检出的)。尽管我找到了一种解决此问题的黑客方法,但我仍在寻找建议,并且能够验证并接受答案。

采纳答案by Grundlefleck

I found a poor solution to my problem.

我找到了一个糟糕的解决方案。

I went to the project and removed the .settingsdirectory and restarted Eclipse. In the process I lost all my other formatting rules of course, which is why it sucks. But the blank lines are no longer are added, and the behaviour I described is the default behaviour (probably why I was used to it).

我去了项目并删除了.settings目录并重新启动了Eclipse。在这个过程中,我当然丢失了所有其他格式规则,这就是它糟糕的原因。但是不再添加空行,我描述的行为是默认行为(可能是我习惯的原因)。

I still hope to accept a better solution for this, I have kept the old settings around to verify any suggestions.

我仍然希望为此接受更好的解决方案,我保留了旧设置以验证任何建议。



EDIT: I think I've found the problem.

编辑:我想我已经找到了问题所在。

For some reason, possibly related to the project settings being checked out, any settings which I selected through Preferences were not actually being honoured. So although the preferences suggested by Von Cwere right, changing to them had no effect. I had the same problem trying to change the auto-generated comments, but didn't put two and two together (what actually alerted me to this was finally seeing "ARGH" being generated within a comment I couldn't seem to remove :-p).

出于某种原因,可能与被检出的项目设置有关,我通过首选项选择的任何设置实际上都没有得到尊重。因此,尽管Von C建议的偏好是正确的,但更改为它们没有任何效果。我在尝试更改自动生成的评论时遇到了同样的问题,但没有将两个和两个放在一起(实际上提醒我的是终于看到在我似乎无法删除的评论中生成了“ARGH”:- p)。

So in this case, the problem was not actually the settings, but something else within Eclipse I don't even want to investigate.

因此,在这种情况下,问题实际上不是设置,而是 Eclipse 中的其他问题,我什至不想调查。

回答by George

Format-Config

Format-Config

The Eclipse i am using is:

我正在使用的 Eclipse 是:

Eclipse Java EE IDE for Web Developers.
Version: Indigo Service Release 2
Build id: 20120216-1857

As the picture above shows, there is a 'Between import groups' under the Blank Lines tab and it works for me. You can check it out.

如上图所示,空白行选项卡下有一个“导入组之间”,它对我有用。你可以检查一下。

回答by VonC

Strange, I cannot reproduce that behavior with my own formatter profile (eclipse3.5.1).

奇怪的是,我无法使用自己的格式化程序配置文件 (eclipse3.5.1) 重现该行为。

There was a setting back in 2003 (bug 38523) to "Insert new line after each parameter if line is too long", but this has been integrated into the "Line Wrapping" panel a long time ago.

2003 年有一个设置(错误 38523)“如果行太长,则在每个参数后插入新行”,但很久以前这已集成到“换行”面板中。

Are you sure the newlines are not added because of a wrapping issue (due to a maximum line length too short)?

您确定由于换行问题(由于最大行长度太短)而没有添加换行符吗?

The tab "Blank Lines" does have an "Existing blank lines" section, with a "Number of empty lines to preserve" field which can be set to 1, and should address the later part of your requirements.
In that same tab "Blank Lines", check also the value of the "Before field declarations": "1" could explain the blank lines you see.

“空白行”选项卡确实有一个“现有空白行”部分,带有“要保留的空行数”字段,该字段可以设置为 1,并且应该满足您后面的要求。
在同一个选项卡“Blank Lines”中,还要检查“Before field declarations”的值:“1”可以解释您看到的空行

alt text
(source: comscigate.com)

alt text
(来源:comscigate.com



The formatter options are in the org.eclipse.jdt.ui.prefsfile, which is:

格式化程序选项位于org.eclipse.jdt.ui.prefs文件中,即:

  • either in your workspace (.metadata.plugins\org.eclipse.core.runtime.settings)
  • or in the .settingsof your project.
  • 无论是在您的工作区 (.metadata.plugins\org.eclipse.core.runtime.settings)
  • 或在.settings您的项目中。

You can try and empty that file to check if the problem remains. It that clear up the issue, try adding some lines into that file, up to the point where the problem comes back.

您可以尝试清空该文件以检查问题是否仍然存在。它解决了问题,尝试在该文件中添加一些行,直到问题再次出现。