5

I am working with an Eclipse formatter and I want the following code formatting. Note how the JavaDoc and ='s are aligned.

What I want:

/**
 * 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;
}

What eclipse formats:

/**
 *
 * @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;
}

Does anyone know if this is possible?

If it is not possible to do this automatically, is it possible to have the formatter ignore the whitespace on those lines?

Dan Grahn
  • 9,044
  • 4
  • 37
  • 74

2 Answers2

0

Looks too specific to have a built in solution. There might be a plugin to help with that.

If it is not possible to do this automatically, is it possible to have the formatter ignore the whitespace on those lines?

Yes, with @formatter tags.

  1. Go to Windows > Preferences.
  2. From there Java > Code Style > Formatter > Edit
  3. Choose the On/Off Tags tab and check the Enable checkbox.
  4. Apply them to your code:

    //@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;
    }
    

Alternatively, if you want to disable all Javadoc formatting:

  1. Choose the Comments tag and uncheck the Enable Javadoc comment formatting checkbox.

The following is not what you want because it adds new lines, but might be close enough for you since it aligns the parameters descriptions with each other:

/**
 * 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
 */

This is achieved by:

  1. Choose the Comments tag and check the Indent Javadoc tags, (optionally) Indent description after @param and New line after @param tags checkboxes.
user1803551
  • 12,965
  • 5
  • 47
  • 74
0

You can use Jindent plugin which is available for the most common IDEs: Eclipse, Netbeans, JDeveloper, IntelliJ IDEA, JBuilder

This will preserve the formatting for your code

/**
 * 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

Unlike Eclipse's default formatting command Ctrl + Shift + F, you need to use
Ctrl + Shift + J to format your code using Jindent.

It only adds a comment at the end of the code which you would have to manually remove.


Update: To install it, simply drag the icon over your Eclipse Window enter image description here

Devendra Lattu
  • 2,732
  • 2
  • 18
  • 27