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

4
我正在为一个类设置一些注解,并将注释放在与注解相同的行上,例如:
@SampleAnnotation // sample comment
@AnotherAnnotation // another comment
public class SampleClass { }

然而,每次我格式化文件时,我的同行注释都会被移动到下一行。这不仅不理想,因为它移动了我的注释,更糟糕的是它把注释放在了一个令人困惑的位置。上面的示例变成了:

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

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

不要一开始就写注释。相反,专注于使方法/注解名称更易读。 - adarshr
1个回答

0
到目前为止,最好的解决方案是向Eclipse发出信号,让其关闭这些块的格式化程序。也就是说,原始代码将变成:
// @formatter:off
@SampleAnnotation // sample comment
@AnotherAnnotation // another comment
// @formatter:on
public class SampleClass { }

请确保在Eclipse中启用了开/关标签:

  1. 在主菜单中,选择 WindowsPreferences
  2. 左侧,选择 JavaCode StyleFormatter
  3. 右侧,点击 EditJava → Code Style → Formatter → Edit
  4. 在弹出的对话框中,点击最右边的选项卡,Off/On Tags
  5. 确保 Enable Off/On tags 已经勾选Off/On Tags → Enable Off/On tags
    • 如果您正在使用内置配置文件,则必须在对话框顶部的 Profile name 字段中更改名称。
  6. 点击 OK。当对话框关闭时,再次点击 OK 完成应用设置。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接