Friday, April 15, 2016

Multiline string alignment / indenting in Eclipse

Leave a Comment

I'm using Eclipse Luna and I want to align / indent multi-line string literals so that that each line begins on the same column. But I'm having trouble convincing Eclipse to do it in the Code Style Formatter.

This is how it's currently formatting:

    final String string1 =         "abc" +             "def" +             "ghi";      System.out.println(         "abc" +             "def" +             "ghi");      System.out.println("" + // an ugly workaround         "abc" +         "def" +         "ghi");      method("xyz",         "" +           // Especially ugly in this case             "abc" +             "def" +             "ghi"); 

But this is what I really want:

    final String string2 =         "abc" +         "def" +         "ghi";      System.out.println(         "abc" +         "def" +         "ghi");      // Or something like this would be fine too     final String string3 =         "abc"       + "def"       + "ghi";      System.out.println(         "abc"       + "def"       + "ghi"); 

It seems like such a simple and desirable format. Just start each part of the broken-up line on the same column. But I haven't been able to find a way to do it anywhere.

1 Answers

Answers 1

The following settings sort of work for your 2 first examples. Copy paste the your example into a block and then reformat. With this setting however you need to manually break the lines, the formatter will then indent them and keep from joining them. You will probably need to tweek other settings since the "default indention for wrapped lines" is 0.

Under Line Wrapping:

  1. Check "Never join already wrapped lines"

  2. Set "Default indention for wrapped lines:" to 0

  3. Expressions / Assignments:

    • Wrap where necessary,
    • Indent by one
  4. Expressions / Binary expressions:

    • Wrap where necessary,
    • Check Wrap before operator,
    • Default indention

There is also the option to disable the formatter and then re-enable it after the code you want to format manually. This can be useful when you have tabular data in initializers you want to format for readability. This is under Off/On Tags in the formatter settings.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment