如何阻止 Eclipse 在注释后移动我的行注释?

How do I stop Eclipse from moving my line comments after annotations?

我在一个class上设置了一些注解,和注解在同一行注释,比如:

@SampleAnnotation // sample comment
@AnotherAnnotation // another comment
public class SampleClass { }

但是,每次我格式化文件时,我的同行注释最终都会被移到下一行。这不仅是不可取的,因为它移动了我的评论,更糟糕的是,它把评论放在了一个混乱的地方。上面的例子变成:

@SampleAnnotation
// sample comment
@AnotherAnnotation
// another comment
public class SampleClass { }

如何阻止 Eclipse 在格式化后移动这些注释?或者,如何让 Eclipse 将注释移到上面的行,而不是下面的行?谢谢。

迄今为止最好的解决方案是通知 Eclipse 关闭此类块的格式化程序。也就是说,原始代码将变为:

// @formatter:off
@SampleAnnotation // sample comment
@AnotherAnnotation // another comment
// @formatter:on
public class SampleClass { }

确保您在 Eclipse 中启用了 off/on 标签:

  1. 在主菜单中,访问 WindowsPreferences
  2. 手边,进入JavaCode StyleFormatter.
  3. 右侧-手边,单击编辑
  4. 在出现的对话框中,单击最右侧的选项卡Off/On Tags
  5. 确保 Enable Off/On tags 选中
    • 如果您使用的是内置配置文件,则必须更改对话框顶部 Profile name 字段中的名称。
  6. 单击确定。对话框关闭后,再次单击 确定 以完成应用设置。