Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 463372 - Code formatting on JDT not respecting "Never join already wrapped lines" on some circumstances
Summary: Code formatting on JDT not respecting "Never join already wrapped lines" on s...
Status: CLOSED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.5   Edit
Hardware: PC Windows NT
: P3 normal with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Mateusz Matela CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-03-28 20:34 EDT by Fabio Zadrozny CLA
Modified: 2015-08-15 05:31 EDT (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fabio Zadrozny CLA 2015-03-28 20:34:27 EDT
In Eclipse 4.5m6, formatting the code below leaves all the contents of the line in a single line even if the setting "Never join already wrapped lines" is turned on.

Note that this does not happen if the '+' from the string concatenation is on the other side (i.e.: + "b = 20\n" instead of "b = 20\n" +)

package org.python.pydev.core.docutils;

public class Test {

    public static void main(String[] args) {
        String docContents = "" +
                "a = 10\n" +
                "b = 20\n" +
                "c = 30\n";
    }
}
Comment 1 Mateusz Matela CLA 2015-03-30 20:53:09 EDT
This will be fixed as part of bug 458208 comment 11 (I suggested a new setting, but it's too late for it).
I have it implemented already and I planned to upload the complete patch after bug 462945 is pushed in.
Comment 2 Fabio Zadrozny CLA 2015-03-31 06:49:51 EDT
Great! Thank you very much :)
Comment 3 Stephan Leicht Vogt CLA 2015-06-08 08:00:31 EDT
I'm also experiencing this problem with 

"Eclipse IDE for Java Developers"
Version: Mars Release Candidate 3 (4.5.0RC3)
Build id: 20150604-1155
Hardware "Macintosh" "Mac OS X"

I tested it with the example from Fabio and in both cases, '+' at the end or at the beginning of the line, it joins the lines to one line:

Start:
public class Test {
    public static void main(String[] args) {
        String docContents = ""
            + "a = 10\n"
            + "b = 20\n"
            + "c = 30\n";
    }
}


Result:
public class Test {
  public static void main(String[] args) {
    String docContents = "" + "a = 10\n" + "b = 20\n" + "c = 30\n";
  }
}
Comment 4 Stephan Leicht Vogt CLA 2015-06-08 08:01:53 EDT
And in my opinion it is a major bug as it is extremely annoying. It can rewrite a lot of lines before one realises it.
Comment 5 Jay Arthanareeswaran CLA 2015-06-08 10:44:45 EDT
(In reply to Stephan Leicht Vogt from comment #4)
> And in my opinion it is a major bug as it is extremely annoying. It can
> rewrite a lot of lines before one realises it.

Hi Mateusz, please weigh in with your opinion. Thanks!
Comment 6 Mateusz Matela CLA 2015-06-08 11:20:24 EDT
Well, my opinion is that it works :)
Stephan, are you sure you've checked the "Never join already wrapped lines"?

I just tested with RC4 (I20150603-2000):
1. start with completely new workspace
2. create a new formatter profile copying Eclipse[build-in], check "Never join already wrapped lines"
3. Create a new java project, paste example code from Fabio, add second main method from Stephan
4. Ctrl+Shift+F: none of the lines are joined
Comment 7 Stephan Leicht Vogt CLA 2015-06-09 09:54:17 EDT
(In reply to Mateusz Matela from comment #6)
> Well, my opinion is that it works :)
> Stephan, are you sure you've checked the "Never join already wrapped lines"?

You are right. I always tried it with my own project with a Formatter Profile heavily customised. If I try it your way it works. Comparing the two Formatter Profiles and beginning to set my options on the blank project I found the option which breaks the formatter: Expressions->Binary Expression. If I set this option to "Do not wrap" it joins the lines as described in this bug.
Comment 8 Mateusz Matela CLA 2015-06-09 10:54:07 EDT
(In reply to Stephan Leicht Vogt from comment #7)
> Expressions->Binary Expression.
> If I set this option to "Do not wrap" it joins the lines as described in
> this bug.
So do you still consider this a problem? To me the current behavior seems logical.
If you agree, I think we can close.
Comment 9 Stephan Leicht Vogt CLA 2015-06-10 00:42:27 EDT
No, we can't close it. Here is another example which affects me even more as similar code exists many times in our code.

public class Test {

  public static void main(String[] args) {
    StringBuilder docContentsBuilder = new StringBuilder().
        append("a = 10\n").
        append("b = 20\n").
        append("c = 30\n");
  }
}

I do not know how to set the formatter options so it doesn't join these lines to a single line.
Comment 10 Mateusz Matela CLA 2015-06-12 19:41:36 EDT
For custom formatting like that the general advice is to use @formatter:off tags. You can also consider switching back to the old formatter: http://eclipse-n-mati.blogspot.com/2015/06/eclipse-mars-how-to-switch-back-to.html

The reasons behind different behavior for "never join lines" is explained in bug 372801 and I don't think we're going to take a step back here.
Comment 11 Stephan Leicht Vogt CLA 2015-06-15 01:51:12 EDT
Thanks for pointing me to both, the old formatter and the bug. The old formatter worked fine as it did the same as in luna.
But reading the bug it pointed me to the other "solution". Line breaks will be kept intact "before '.' character in member access". This is great as I have to do only a small rewrite of my code so it will look like this:

public class Test {

  public static void main(String[] args) {
    StringBuilder docContentsBuilder = new StringBuilder()
        .append("a = 10\n")
        .append("b = 20\n")
        .append("c = 30\n");
  }
}


So for me all is fine and the bug can be closed.
Comment 12 Kaj Hejer CLA 2015-08-14 09:28:07 EDT
I have "Never join already wrapped lines" checked.

The following code:

        boolean retainRespondentAccessAfterDeliveryAndHasRespondentEmail = 
                delivery.getForm().getRetainRespondentAccessAfterDelivery() && 
                hasRespondentEmail;

get formatted to

        boolean retainRespondentAccessAfterDeliveryAndHasRespondentEmail = delivery.getForm()
                .getRetainRespondentAccessAfterDelivery() &&
                hasRespondentEmail;

I'm running Eclipse Java EE IDE for Web Developers. Version: Mars Release (4.5.0). Build id: 20150621-1200.
Comment 13 Mateusz Matela CLA 2015-08-14 12:51:42 EDT
(In reply to Kaj Hejer from comment #12)
> I have "Never join already wrapped lines" checked.
What about the wrapping policy for Expressions -> Assignments? If it's "Wrap where necessary", it should not remove the line break after the equal sign.
Comment 14 Kaj Hejer CLA 2015-08-15 05:31:18 EDT
Thanks! For Expressions -> Assignments I had "Do not wrap". When I changed it do "Wrap where necessary" it did NOT rewrap my code. I find it a bit strange that the code gets rewrapped when I have "Do not wrap" but not when I have "Wrap where necessary".