Bug 198074 - [formatter] the code formatter doesn't respect my new lines
Summary: [formatter] the code formatter doesn't respect my new lines
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3   Edit
Hardware: PC Windows XP
: P3 enhancement with 17 votes (vote)
Target Milestone: 3.5 M4   Edit
Assignee: Frederic Fusier CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 61511 91902 98898 99100 152444 201022 208451 213700 265192 (view as bug list)
Depends on:
Blocks: 239130
  Show dependency tree
 
Reported: 2007-07-27 04:19 EDT by Kristof Van Cleemput CLA
Modified: 2010-02-17 04:24 EST (History)
28 users (show)

See Also:
Olivier_Thomann: review+


Attachments
spam (3.16 KB, text/plain)
2007-11-21 12:41 EST, asarie CLA
no flags Details
Proposed patch (13.66 KB, patch)
2008-12-03 04:17 EST, Frederic Fusier CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Kristof Van Cleemput CLA 2007-07-27 04:19:35 EDT
Build ID:  I20070621-1340

Steps To Reproduce:
Simple example:
1. Enter in java the following:
String x = "select x "
         + "from y "
         + "where z=a";
2. format the code =>
Result: String x = "select x " + "from y " + "where z=a"; 


More information:
This is the only reason why we hate using the formatter.
It just doesn't respect the new lines entered by the developer.
A new option respect new lines should be added and set default to true (I think all developers would agree on this)
That's why I guess it's severity is high in regards of the formatter (if not even a formatter blocker)

I guess some other formatter problems in the comments can be solved by doing the same thing (Add respect new lines option). Now I just turn the formatter for block and line comments off.
Comment 1 Geoffrey De Smet CLA 2007-08-10 08:18:35 EDT
Note that only relates to wrapping, not the other parts of formatting.
When formatting if bracers shouldn't be on the next line, the newlines shouldn't be respected.
Only when applying the wrapping rule, newlines should be respected.
Each wrapping option (including those of comments too) should have a checkbox: "do not remove existing newlines when wrapping".

For example:

if (enabled)
{
   // we need x
   // we need a select
   return "select x "
   + "from X";}

would become under a Sun's java coding conventions formatter:


if (enabled) {
   // we need x
   // we need a select
   return "select x "
           + "from X";
}


Currently it becomes something like:

if (enabled) {
   // we need x we need a select
   return "select x " + "from X";
}
Comment 2 Geoffrey De Smet CLA 2007-08-10 08:19:41 EDT
PS: IntelliJ idea 3, 4, 5 and 6 do this correctly out of the box :)
Comment 3 Denis Ballant CLA 2007-09-11 09:50:28 EDT
I do totally agree with this request.  And this is THE reason I'm not using Eclipse to format my code.  I've been using IntelliJ IDEA for a long time before switching to Eclipse when I moved to another company.  IDEA is working just great:  reindents lines, preserves developer's line splits.

Here's another example :

-----BEFORE-----

public String toString() {
	return "YAD01: "
	+ " nommbr='"+getName()+"'"
	+ " nomgrp='"+getService().getArgtbl()+"'"
	+ " typmbr='"+getMemberType().getArgument()+"'"
	+ " srcpat='"+getPhysicalPath()+"'"
	+ " nommdl='"+getModel()+"'"
	;
}

-----AFTER (Ugly)-----

public String toString() {
	return "YAD01: " + " nommbr='" + getName() + "'" + " nomgrp='"
			+ getService().getArgtbl() + "'" + " typmbr='"
			+ getMemberType().getArgument() + "'" + " srcpat='" + getPhysicalPath()
			+ "'" + " nommdl='" + getModel() + "'";
}

-----AFTER (ok)-----

public String toString() {
    return "YAD01: "
	+ " nommbr='" + getName() + "'"
	+ " nomgrp='" + getService().getArgtbl() + "'"
	+ " typmbr='" + getMemberType().getArgument() + "'"
	+ " srcpat='" + getPhysicalPath() + "'"
	+ " nommdl='" + getModel() + "'"
	;
}
Comment 4 Denis Ballant CLA 2007-09-11 09:52:14 EDT
See also this request from someone else:

http://dev.eclipse.org/newslists/news.eclipse.platform/msg67607.html
Comment 5 Denis Ballant CLA 2007-10-02 12:07:29 EDT
Found same enhancement request: 99100

But this one is two years old and still in NEW status.
Is this one going to have a bit more success ?
Comment 6 John Goering CLA 2007-10-26 04:58:14 EDT
Wow, I thought I was just overlooking an option somewhere, wouldn't have thought this was actually a bug. I strongly agree, this is the #1 reason our developers are slightly hesitant to use the formatter.

Indenting is OK, but there should be an option to not only "do not wrap" but also "do not collapse".
Comment 7 Creo Ilbe CLA 2007-11-05 07:20:15 EST
This is a formatter show stopper for us!
Comment 8 Reinhold Fuereder CLA 2007-11-05 08:10:34 EST
*** Bug 208451 has been marked as a duplicate of this bug. ***
Comment 9 asarie CLA 2007-11-21 12:41:56 EST
Created attachment 83451 [details]
spam

my carbide ui can not open
Comment 10 Markus Keller CLA 2007-11-21 13:16:16 EST
Comment on attachment 83451 [details]
spam

(In reply to comment #9)
> Created an attachment (id=83451) [details]
> 1089048494500
> 
> my carbide ui can not open

Looks like a spammer, see bug 210560.
Comment 11 Immo Huneke CLA 2008-01-17 04:03:09 EST
Correcting this problem would also address another bugbear of mine. The formatter knows that it should not extend lines beyond a set width. However, it always puts the three elements of a "for" expression on a single line, even if they have been split by the programmer because the expression exceeds the allowed line length.
Comment 12 Jerome Lanneluc CLA 2008-02-13 10:42:11 EST
Time permitting for 3.4
Comment 13 Markus Keller CLA 2008-02-27 08:43:08 EST
*** Bug 213700 has been marked as a duplicate of this bug. ***
Comment 14 Jerome Lanneluc CLA 2008-05-12 07:03:38 EDT
We ran out of time and we didn't get any help on this one. Deferring post 3.4
Comment 15 Geoffrey De Smet CLA 2008-05-12 07:24:40 EDT
I hope this bug gets the attention it deserves for the next release.
For me, personally, this is the number #1 bug in eclipse at the moment.
Comment 16 Jerome Lanneluc CLA 2008-05-12 07:29:48 EDT
Geoffrey, the best way to get this bug fixed is to provide a good quality patch with the corresponding JUnit tests.
Comment 17 Patrick McCauley CLA 2008-05-12 07:37:48 EDT
(In reply to comment #14)
> We ran out of time and we didn't get any help on this one. Deferring post 3.4
> 

Our team of developers really wants to use the automatic formatting features to unify our code, but this bug is holding us up.  

Please make a serious effort to get this fixed as soon as possible.  Since it was promised for 3.4, I would hope at least a patch would be available before the next major release.
Comment 18 Geoffrey De Smet CLA 2008-05-12 08:03:42 EDT
(In reply to comment #16)
> Geoffrey, the best way to get this bug fixed is to provide a good quality patch
> with the corresponding JUnit tests.
> 

That's true, but to be honest: I don't have the time. All my spare time goes into drools-solver (also an open source project). One has to make choices (and I only have to use Eclipse at work...).
If anyone else does it, that person has my thanks :)
Comment 19 Jerome Lanneluc CLA 2008-05-29 06:54:42 EDT
Marking as enhancement as this will require a new mode (controlled by an option) in the formatter to take the user's new lines into account.
Comment 20 Frederic Fusier CLA 2008-07-08 07:41:25 EDT
*** Bug 201022 has been marked as a duplicate of this bug. ***
Comment 21 Markus Keller CLA 2008-08-13 10:59:10 EDT
*** Bug 61511 has been marked as a duplicate of this bug. ***
Comment 22 Jerome Lanneluc CLA 2008-08-27 06:47:45 EDT
*** Bug 196124 has been marked as a duplicate of this bug. ***
Comment 23 Jerome Lanneluc CLA 2008-09-11 10:50:31 EDT
Frederic, please investigate for M4
Comment 24 Frederic Fusier CLA 2008-09-11 11:06:01 EDT
(In reply to comment #23)
> Frederic, please investigate for M4
> 
Should I also include fix for bug 96696 (same issue for comments)?
Comment 25 Jerome Lanneluc CLA 2008-09-11 11:13:36 EDT
(In reply to comment #24)
> (In reply to comment #23)
> > Frederic, please investigate for M4
> > 
> Should I also include fix for bug 96696 (same issue for comments)?
> 
Yes, that makes sense
Comment 26 Frederic Fusier CLA 2008-12-02 10:41:04 EST
*** Bug 99100 has been marked as a duplicate of this bug. ***
Comment 27 Frederic Fusier CLA 2008-12-03 04:17:07 EST
Created attachment 119360 [details]
Proposed patch

Fix consists in hacking the Scribe method which preserve empty lines. When no line is to preserve (which means that the formatter tentatively removes an existing line break), then add a new line if the formatter is inside a wrapping alignment _and_ the new preference is set.

Note that the new preference default is false to preserve backward compatibility behavior. But does it make really sense although it seems that most of people think that the formatter is so bad without it...!?
Comment 28 Frederic Fusier CLA 2008-12-03 06:38:41 EST
Olivier, could you please review, thanks?
Comment 29 Dani Megert CLA 2008-12-03 07:16:25 EST
>But does it make really sense although it seems that
>most of people think that the formatter is so bad without it...!?
Is it currently possible to configure the formatter so that, e.g. this:
public String toString() {
        return "YAD01: "
        + " nommbr='"+getName()+"'"
        + " nomgrp='"+getService().getArgtbl()+"'"
        + " typmbr='"+getMemberType().getArgument()+"'"
        + " srcpat='"+getPhysicalPath()+"'"
        + " nommdl='"+getModel()+"'"
        ;
}

gets produced? If so, I would expect that if I have such code:
public String toString() {
        return "YAD01: "
     + " nommbr='"+getName()+"'"
        + " nomgrp='"+getService().getArgtbl()+"'"
             + " typmbr='"+getMemberType().getArgument()+"'"
      + " srcpat='"+getPhysicalPath()+"'"
 + " nommdl='"+getModel()+"'"
        ;
and format it it would be formatted as configured and not left alone.

Comment 30 Frederic Fusier CLA 2008-12-03 07:19:34 EST
*** Bug 61511 has been marked as a duplicate of this bug. ***
Comment 31 Frederic Fusier CLA 2008-12-03 07:21:45 EST
*** Bug 201022 has been marked as a duplicate of this bug. ***
Comment 32 Frederic Fusier CLA 2008-12-03 07:22:45 EST
*** Bug 208451 has been marked as a duplicate of this bug. ***
Comment 33 Frederic Fusier CLA 2008-12-03 07:23:31 EST
*** Bug 213700 has been marked as a duplicate of this bug. ***
Comment 34 Frederic Fusier CLA 2008-12-03 07:33:11 EST
(In reply to comment #29)
Preserving line breaks does not mean preserve wrong indentation. These are two separated points. Indentation is always fixed by the formatter; its value of course depends on statement and corresponding preferences.

For example, for your test case:
public String toString() {
        return "YAD01: "
     + " nommbr='"+getName()+"'"
        + " nomgrp='"+getService().getArgtbl()+"'"
             + " typmbr='"+getMemberType().getArgument()+"'"
      + " srcpat='"+getPhysicalPath()+"'"
 + " nommdl='"+getModel()+"'"
        ;

it's possible to have the following formatter output:
	public String toString() {
		return "YAD01: "
		+ " nommbr='" + getName() + "'"
		+ " nomgrp='" + getService().getArgtbl() + "'"
		+ " typmbr='" + getMemberType().getArgument() + "'"
		+ " srcpat='" + getPhysicalPath() + "'"
		+ " nommdl='" + getModel() + "'";
	}
by using the proposed patch and seeting '0' for 'Default indentation for wrapped lines'...
Comment 35 Dani Megert CLA 2008-12-03 08:25:46 EST
OK, so we could try to enable this per default - at least in the 'Eclipse' Profile.
Comment 36 Olivier Thomann CLA 2008-12-04 11:24:18 EST
+1. Fix looks good.
Comment 37 Frederic Fusier CLA 2008-12-04 11:38:57 EST
Released for 3.5M4 in HEAD stream.
Comment 38 Kent Johnson CLA 2008-12-09 14:06:33 EST
Verified for 3.5M4 using I20081209-0100
Comment 39 Adrian Sampaleanu CLA 2008-12-09 14:14:20 EST
Will this be backported to 3.4.x?
Comment 40 Philipe Mulet CLA 2008-12-09 16:31:15 EST
The change comes with an API addition (pref) which is not something we consider in maintenance streams. So the answer would be no go for 3.4.x (it isn't a critical bug fix, but a feature addition). 
Comment 41 Frederic Fusier CLA 2009-06-22 10:45:51 EDT
*** Bug 265192 has been marked as a duplicate of this bug. ***
Comment 42 Frederic Fusier CLA 2009-12-17 10:23:35 EST
I would like to know what all people interested in the 'Never join lines' option think about bug 258049...

Do you think that the formatter should preserve such rare code patterns when the option is activated or should it ignore it instead and always join the lines in that case?

Thanks in advance to let us know your mind about this issue by adding a comment to bug 258049 :-)
Comment 43 Frederic Fusier CLA 2010-01-08 10:46:42 EST
*** Bug 152444 has been marked as a duplicate of this bug. ***
Comment 44 Frederic Fusier CLA 2010-02-17 04:15:41 EST
*** Bug 76435 has been marked as a duplicate of this bug. ***
Comment 45 Frederic Fusier CLA 2010-02-17 04:22:18 EST
*** Bug 91902 has been marked as a duplicate of this bug. ***
Comment 46 Frederic Fusier CLA 2010-02-17 04:24:25 EST
*** Bug 98898 has been marked as a duplicate of this bug. ***