Eclipse格式化程序允许对齐=' s和Javadoc选项卡

时间:2014-07-17 16:44:40

标签: java eclipse formatter

我正在使用Eclipse格式化程序,我想要以下代码格式化。注意JavaDoc和=是如何对齐的。

我想要的是什么:

/**
 * Description.
 *
 * @param alpha this is what alpha does
 * @param beta  this is what beta does
 * @param gamma this is what gamma does
 * @param delta this is what delta does
 */
public Foo(Bar alpha, Bar beta, Bar gamma, Bar delta) {
    this.alpha = alpha;
    this.beta  = beta;
    this.gamma = gamma;
    this.delta = delta;
}

什么是eclipse格式:

/**
 *
 * @param alpha this is what alpha does
 * @param beta this is what beta does
 * @param gamma this is what gamma does
 * @param delta this is what delta does
 */
public Foo(Bar alpha, Bar beta, Bar gamma, Bar delta) {
    this.alpha = alpha;
    this.beta = beta;
    this.gamma = gamma;
    this.delta = delta;
}

有人知道这是否可行?

如果无法自动执行此操作,是否可以让格式化程序忽略这些行上的空格?

2 个答案:

答案 0 :(得分:1)

看起来太具体了,没有内置的解决方案。可能有一个插件可以帮助解决这个问题。

  

如果无法自动执行此操作,是否可以让格式化程序忽略这些行上的空格?

是的,使用@formatter标记。

  1. 转到Windows > Preferences
  2. 从那里Java > Code Style > Formatter > Edit
  3. 选择On/Off Tags标签,然后选中Enable复选框。
  4. 将它们应用于您的代码:

    //@formatter:off
    /**
    * Description.
    *
    * @param alpha this is what alpha does
    * @param beta  this is what beta does
    * @param gamma this is what gamma does
    * @param delta this is what delta does
    */
    //@formatter:on
    public Foo(Bar alpha, Bar beta, Bar gamma, Bar delta) {
        this.alpha = alpha;
        this.beta  = beta;
        this.gamma = gamma;
        this.delta = delta;
    }
    
  5. 或者,如果要禁用所有Javadoc格式:

    1. 选择Comments代码并取消选中Enable Javadoc comment formatting复选框。
    2. 以下不是您想要的,因为它添加了新行,但可能足够接近您,因为它将参数说明彼此对齐:

      /**
       * Description.
       *
       * @param alpha
       *            this is what alpha does
       * @param beta
       *            this is what beta does
       * @param gamma
       *            this is what gamma does
       * @param delta
       *            this is what delta does
       */
      

      这是通过以下方式实现的:

      1. 选择Comments代码,然后选中Indent Javadoc tags,(可选)Indent description after @paramNew line after @param tags复选框。

答案 1 :(得分:0)

您可以使用适用于最常见IDE的Jindent插件:     Eclipse,Netbeans,JDeveloper,IntelliJ IDEA,JBuilder

这将保留代码的格式

/**
 * Description.
 *
 * @param alpha this is what alpha does
 * @param beta  this is what beta does
 * @param gamma this is what gamma does
 * @param delta this is what delta does
 */
public Foo(Bar alpha, Bar beta, Bar gamma, Bar delta) {
    this.alpha = alpha;
    this.beta  = beta;
    this.gamma = gamma;
    this.delta = delta;
}

//~ Formatted by Jindent --- http://www.jindent.com

与Eclipse的默认格式化命令 Ctrl + Shift + F 不同,您需要使用
Ctrl + Shift + J ,使用Jindent格式化您的代码。

它只在代码末尾添加注释,您必须手动删除。

更新:要安装它,只需将图标拖到Eclipse窗口上即可 enter image description here