Eclipse: Lining up function arguments vertically
Asked Answered
B

2

25

I'm running Eclipse 3.7.2 on Windows 7 professional.

If I type a method declaration like this:

private void processCode(String codename,
                         boolean doSomethingElse,
                         int num_of_repeats){
}

Then hit Ctrl + I with the whole file selected, Eclipse will mess up the alignment of the function arguments like this

private void processCode(String codename,
     boolean doSomethingElse,
     int num_of_repeats){
}

How can I get Eclipse to stop doing that?

Billings answered 11/9, 2013 at 14:35 Comment(0)
B
33

Follow these steps to achieve what you want:

  1. Open the preferences dialog (located in Windows -> Preferences on Windows/Linux or in the Eclipse menu on Mac).
  2. Go to Java > Code Style > Formatter in the tree on the left
  3. You see the active formatter profile. You can edit it using the "Edit ..." button. Click that.
  4. Go to the tab "Line Wrapping", select "Method Declarations" > "Parameters" on the lower left.
  5. On the bottom left you see the "Settings for parameters" group. Set the Line wrapping policy to "Wrap all elements, except first element if not necessary". Set the Indentation policy to "Indent on column".
  6. Close all dialogs by clicking ok.

You should now have the behavior you intended. If it fits your needs, you might want to apply the same settings for constructor parameters as well.

Beatrizbeattie answered 11/9, 2013 at 15:36 Comment(3)
Thanks, Eclipse is now leaving that alone as I typed it :)Billings
Nice, I've been using eclipse for >10 years and hadn't noticed that the "settings for ..." section depended on what was selected above it.Rusert
Not sure if the implementation is changed or what, but it doesn't seem to do that. Instead it aligns all parameters on the next column (tab). It might insert spaces between the opening bracket and the parameter if needed which is ugly. It seems to be impossible to do :(Mcmichael
S
2

All, I understand this is an old thread, but the MARS eclipse editor wont line up this correctly and like to see if anyone experienced the same in eclipse MARS edition? The String u indentation is not lined up with String k:

public Properties fc(String k,
                    String u) {
    return new Properties();
}

EDIT: I found out the font I used was Lucida Console which is not fix-space font. Eclipse's Java editor has various syntax with BOLD on in which it made the aligned "looked" unaligned, but in fact it was due the bold syntax that widen the characters.

Satterfield answered 2/3, 2017 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.