How do I get IntelliJ IDEA to align adjacent variable assignments on the "=" sign?

Intellij Idea

Intellij Idea Problem Overview


Environment is IntelliJ IDEA 12.

I've got Wrapping and Braces > Assignment Statement > Align when multiline ticked. When I have adjacent properties, they're aligned correctly:

private final CustomerFactory  customerFactory  = FactoryBuilder.getDefaultCustomerFactory ( );
private final RepFactory       repFactory       = FactoryBuilder.getDefaultRepFactory ( );
private final WarehouseFactory warehouseFactory = FactoryBuilder.getDefaultWarehouseFactory ( );

but when I've got adjacent variable assignments, they're not:

final Integer account30 = parseCurrency ( fields.get ( FIELD_OVERDUE_BALANCE_1 ) );
final Integer account60 = parseCurrency ( fields.get ( FIELD_OVERDUE_BALANCE_2 ) );
final Integer account90 = parseCurrency ( fields.get ( FIELD_OVERDUE_BALANCE_3 ) );
final Integer account120 = parseCurrency ( fields.get ( FIELD_OVERDUE_BALANCE_4 ) );
final Integer priceNumber = parseInteger ( fields.get ( FIELD_PRICE_NUMBER ), 1 );

This is minor but it annoys me. Can anyone tell me what I can do to get these adjacent variable declarations aligned? What I want is:

final Integer account30   = parseCurrency ( fields.get ( FIELD_OVERDUE_BALANCE_1 ) );
final Integer account60   = parseCurrency ( fields.get ( FIELD_OVERDUE_BALANCE_2 ) );
final Integer account90   = parseCurrency ( fields.get ( FIELD_OVERDUE_BALANCE_3 ) );
final Integer account120  = parseCurrency ( fields.get ( FIELD_OVERDUE_BALANCE_4 ) );
final Integer priceNumber = parseInteger ( fields.get ( FIELD_PRICE_NUMBER ), 1 );

Intellij Idea Solutions


Solution 1 - Intellij Idea

You can also do this using built-in key bindings without any changes to Intellij options:

How to align any text using keyboard strokes in Intelli

How this is done:

Use the following (or equivalent) GNOME key bindings:

  1. Highlight the first = (or any delimiter of your choosing)
  2. Press Alt+j once for each additional = to Add Selection for Next Occurrence
  3. Press Left once then make room by pressing Tab per the above GIF
  4. Press Alt+Shift+Insert to enter Column Selection mode
  5. Press Alt+Shift+Insert again to exit Column Selection mode (notice selection is aligned).
  6. Press Left twice then presss Ctrl+Shift+Right to select all white space to each =
  7. Press Shift+Left to exclude = from selection
  8. Press Backspace and that will cause all of the uneven whitespace to truncate

After backspacing to where you want the columns aligned, you should now have something like:

someVar           = someVal
someOtherVar      = someVal
someOtherOtherVar = someVal

The nice thing about this approach is it's general purpose; The above steps will align any text within whitespace so long as you choose a distinct delimiter each line contains only once, such as = in the example given.

As noted previously, this example was performed using GNOME key bindings. You are free to pick a different scheme in Settings > Keymap: enter image description here

Solution 2 - Intellij Idea

I think you should be using the Field groups => Align in columns

Align when multiline has some other effect that doesn't apply here.

enter image description here

Solution 3 - Intellij Idea

Under "Code Style"->"Java" in the Tab "Wrapping and Braces" find following entry and tick "Align Variable in columns". Done!

Setting to align variable declaration

Solution 4 - Intellij Idea

To align adjacent variable assignments on the “=” sign, do this:

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrewView Question on Stackoverflow
Solution 1 - Intellij IdeaecoeView Answer on Stackoverflow
Solution 2 - Intellij IdeamabaView Answer on Stackoverflow
Solution 3 - Intellij IdeaRaphMcleeView Answer on Stackoverflow
Solution 4 - Intellij IdeagadolfView Answer on Stackoverflow