Formatting Field Declarations in Eclipse
Asked Answered
S

3

4

Greetings Overflowers,

Problem (names of variables are aligned by not their types):

public abstract class Filter {
    private long     id;
    protected String expression;
}

how can I align fields automatically in Eclipse, such in:

public abstract class Filter {
    private   long   id;
    protected String expression;
}

Note the space before long, thank you !

UPDATE: I cannot find a customization option to align types of class members (e.g.: long and String) under Eclipse formatter options. Yes, there is one to align names of class members (e.g.: id and expression) but not their types. Please, take a look at this issue in my examples above. Any solution ?

Saran answered 31/12, 2010 at 13:39 Comment(0)
O
2

There is no formatting option to do what you suggest. I have two possibilities, neither of which you are going to like very much:

  1. Raise an enhancement request on jdt.ui and describe the feature that you want. Here is the bugzilla link: https://bugs.eclipse.org/bugs/ Or, better yet, submit a patch for the code formatter.
  2. Create your own Eclipse plugin that contributes a custom action to do this special kind of formatting.
Obadiah answered 2/1, 2011 at 21:54 Comment(1)
I'm not aware of any. Unfortunately, writing a source code formatter is a lot more difficult than it would seem on first glance. To make everyone happy, you would need to include thousands of options. However, in most cases (yours excluded) the Eclipse formatter is good enough, so I'm guessing that not many people have tried to make an alternative.Obadiah
M
7

Window - Preferences - Java - Code Style - Formatter -> New... ->Indentation -> Align fields in column.

Now, pressing ctrl-shift-f will fix your code :)

or

Window - Preferences - Java - Code Style - Clean Up -> New...

Here you can change how you would like your code to be. Then, doing Source - Clean Up will make sure it looks that way..

Marpet answered 31/12, 2010 at 14:16 Comment(0)
O
2

There is no formatting option to do what you suggest. I have two possibilities, neither of which you are going to like very much:

  1. Raise an enhancement request on jdt.ui and describe the feature that you want. Here is the bugzilla link: https://bugs.eclipse.org/bugs/ Or, better yet, submit a patch for the code formatter.
  2. Create your own Eclipse plugin that contributes a custom action to do this special kind of formatting.
Obadiah answered 2/1, 2011 at 21:54 Comment(1)
I'm not aware of any. Unfortunately, writing a source code formatter is a lot more difficult than it would seem on first glance. To make everyone happy, you would need to include thousands of options. However, in most cases (yours excluded) the Eclipse formatter is good enough, so I'm guessing that not many people have tried to make an alternative.Obadiah
H
1

You could write a really complicated formatter extension for eclipse. But you really shouldn't.

Ctrl-shift F will format your code in beautiful Java Spec compliant format.

Hux answered 31/12, 2010 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.