强制“同一条线” Eclipse 格式化程序中的大括号

发布于 2024-12-10 14:45:58 字数 750 浏览 0 评论 0原文

在我的java代码格式化程序中,在我的配置文件中,我将“大括号”选项卡中的所有选项(实际上是Eclipse的默认设置)设置为“同一行”。然而,当我有一段这样的代码时:

interface TestI
    {
}

并且我调用格式化程序(键绑定、上下文菜单等),我得到的只是

interface TestI
{
}

它不会将左大括号移动到同一行,这同样适用于方法、控制语句等等然后当我手动将其更改为

interface TestI      {
}

它然后将其格式化为:

interface TestI {
}

因此它通常遵循我手动执行的格式,但仅格式化大括号和其他元素之间的空格。

我的格式化程序中的其他格式化选项按指定工作。我使用的是最新的 Eclipse Indigo 版本 3.7.1,我使用的是 3.7,我的朋友尝试使用 3.5 版本,所以这让我认为这是故意的,但话又说回来,为什么会有这个“大括号” “如果我无法使用此规则重新格式化代码,请根本不使用选项卡...

我希望我错过了一些东西,并且您会让我瞬间变得愚蠢,问题就会消失,因为我有无数的类到清理,我不想手动完成..:)

更新:当我将规则设置为“同一行”而不是“下一行”时,格式化将按预期工作,无论我如何执行,它都会强制执行该规则编写代码后,一旦我将其改回“同一行”,格式化程序就不会强制执行此规则。

In my java code formatter, in my profile I have (actually it's the eclipse's default) all choices in the "braces" tab set to "same line". Nevertheless when I have a piece of code like this:

interface TestI
    {
}

and I invoke the formatter (key binding, context menu, whatever) all I get is

interface TestI
{
}

it doesn't move the opening braces to the same line, the same applies to methods, control statements etc. then when I manually change it to

interface TestI      {
}

it then formats it to:

interface TestI {
}

So it generally respects the format I did manually but only formats the spaces between the braces and other elements.

Other formatting options in my formatter work as as specified. I'm using the latest Eclipse Indigo release 3.7.1, I had the same with 3.7, and my friend tried it with a 3.5 release, so it makes me think that this is on purpose, but then again why is there this "braces" tab at all if I can't reformat the code using this rule ...

I hope I'm missing something and that you will make me fell stupid in a moment and the problem will go away, because I have a gazillion of classes to cleanup and I don't want to do it by hand .. :)

UPDATE: when I set the rule not to "same line" but "next line" then formatting works as expected, it forces the rule regardless of how I've written the code, as soon as I change it back to "same line" the the formatter doesn't force this rule.

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

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

发布评论

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

评论(2

迷爱 2024-12-17 14:45:58

找到它...它是由以下原因引起的:

<setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="false"/> 

在格式化程序设置 GUI 中位于“换行/从不加入已换行的行”下是的,

现在我记得设置它,但我从未想过这会覆盖大括号设置...

a格式化程序的一个不错的功能是,如果您设置了无操作选项,因为您设置的其他选项覆盖了它们,那么它会发出警告。

Found it... it was caused by:

<setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="false"/> 

which in the Formatter setting GUI is under "Line Wrapping/Never join already wrapped lines"

yes now I remember setting it, but I never thought that this would override the braces setting ...

a nice-to-have feature of the formatter would be to have warnings if you set options that are no-ops because of other options that you've set override them.

你怎么敢 2024-12-17 14:45:58

对于那些想知道如何将其应用于数组初始值设定项的人,设置为

  • New Lines >打开数组初始值设定项的大括号后
  • New Lines >在数组初始值设定项的右大括号之前

For those wondering how to make this apply to array initializers, the settings are

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