Bug 179796 - [formatter] Format joins lines causing them to break an 80 character limit
Summary: [formatter] Format joins lines causing them to break an 80 character limit
Status: VERIFIED DUPLICATE of bug 303519
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3   Edit
Hardware: PC Windows XP
: P5 normal with 5 votes (vote)
Target Milestone: 4.7 M2   Edit
Assignee: Mateusz Matela CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-03-28 13:38 EDT by Mark Davis CLA
Modified: 2016-09-15 01:08 EDT (History)
6 users (show)

See Also:


Attachments
copy of requested preferences (56.44 KB, text/plain)
2007-04-10 16:01 EDT, Mark Davis CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Mark Davis CLA 2007-03-28 13:38:58 EDT
In Preferences>Java>Code Style>Formatter>Edit
Set Maximum line width to 80

Format (Ctrl-Shift-F) a file with lines such as:

    reordered = new TreeSet(
         new DeprecatedAndLengthFirstComparator("territory");

This line gets joined into a single line, which exceeds 80 characters, breaking the limit that was just set.

This makes it unusable on systems that have an 80 character limit, like ours, since you then have to go back by hand and repair all of the lines that Format has screwed up.

(What makes this worse is that Format operates on the whole file, instead of the the selection. If the command were limited to the selection, at least the user could apply Format to a single method or part of a method, and not have to go back and fix everything else in the file.)
Comment 1 Dani Megert CLA 2007-03-29 06:00:55 EDT
>What makes this worse is that Format operates on the whole file, instead of
>the the selection
It DOES operate on the selection but when none is present it formats the file. no plans to change this.
Comment 2 Olivier Thomann CLA 2007-03-29 08:27:52 EDT
Could you please provide your formatter settings?
Comment 3 Mark Davis CLA 2007-03-29 13:04:16 EDT
(In reply to comment #2)
> Could you please provide your formatter settings?

Is there a way to dump them all at once into a format that you can use, or do I need to go through the UI and record each and every setting? If the latter, are there particular ones that I can concentrate on?

As to Format working on selections, that's not what I see happening. Take the following:

 * Note that it is quite common for the separator to be "_" instead of "-". Either separator can be used when creating a LanguageCode. When getting a
 * string from the LanguageCode, the "-" form is produced by {@link #toString}.
 * For more details, see 
 * <a href='http://www.unicode.org/reports/tr35/'>LDML</a>.

If I select the line starting "Note:" and Format, I get:

 * Note that it is quite common for the separator to be "_" instead of "-".
 * Either separator can be used when creating a LanguageCode. When getting a
 * string from the LanguageCode, the "-" form is produced by {@link #toString}.
 * For more details, see <a href='http://www.unicode.org/reports/tr35/'>LDML</a>.

That is, it correctly breaks the line with Note, but then *joins* the line with the <a...>, incorrectly creating a line with 81 characters. (the lines might end up being broken by the bugs form, but you should be able to see what I mean)

Here is another examples of incorrect formatting, where the result beforehand was legal 80 characters, and after formatting was too long.

      StringBuilder buffer = new StringBuilder();
      buffer
        .append(
      "Long String Long String Long String Long String Long String Long String")
        .append(
      "Long String Long String Long String Long String Long String Long String")
        .append(
      "Long String Long String Long String Long String Long String Long String")
      ;
    }

Format. Causes the lines to exceed 80 characters.

      buffer
        .append(
            "Long String Long String Long String Long String Long String Long String")
        .append(
            "Long String Long String Long String Long String Long String Long String")

Even without breaking strings, the 80 character limit could be observed by not indenting so deeply. (And strings could also be broken by inserting ..." + "... if necessary.)

Comment 4 Mark Davis CLA 2007-04-10 16:01:37 EDT
Created attachment 63425 [details]
copy of requested preferences
Comment 5 Mark Davis CLA 2007-04-10 16:02:33 EDT
(In reply to comment #2)
> Could you please provide your formatter settings?
> 

I'm attaching them (with xxxxxx replacing my company's name)

Comment 6 Olivier Thomann CLA 2007-06-21 14:16:11 EDT

*** This bug has been marked as a duplicate of bug 59891 ***
Comment 7 Mark Davis CLA 2007-06-21 14:30:55 EDT
This is not a duplicate. The other is "merely" aesthetics -- this is a serious problem, since not only is it breaking an 80 character limit, it is actively screwing up lines that *do* satisfy an 80 character limit, by joining them incorrectly.
Comment 8 Creo Ilbe CLA 2007-11-05 07:51:17 EST
See and vote for Bug
198074 https://bugs.eclipse.org/bugs/show_bug.cgi?id=198074
Comment 9 Nathan McDonald CLA 2008-03-13 02:05:00 EDT
I've just came across this issue as well.

You can even see the same behaviour in the Formatter.

Go to Java -> Code Style -> Formatter in Preferences and Edit the active profile.

Indentation set to Tabs only, tab size set to 4.

Click on Line Wrapping tab.

Select Method Declarations -> Parameters.

In the "Set line width for preview window:" set it to 70.  Line will wrap.

Now set it to 71.  Line will not wrap, scroll to end of window, you will see the line where the break should occur running between the arg6 and closing bracket ).

I thought this was explained by the tab character being counted as a single character rather than 4 spaces in regards to line length, so I tried going back to the Indentation tab and changing tab size to 8.  This is an increase in tab size by 4 spaces.

Going back to Line Wrapping, we see that the wrapping is now different (if tab counted as a single character wouldn't matter how long it is).  Setting line width for preview to 74/75 has the same effect as before.



Comment 10 Frederic Fusier CLA 2008-08-18 08:07:45 EDT
Ownership has changed for the formatter, but I surely will not have enough time to fix your bug during the 3.5 development process, hence set its priority to P5.
Please provide a patch if you definitely need the bug to be fixed in this version and I'll have a look at it...
TIA
Comment 11 Mateusz Matela CLA 2016-09-08 15:44:41 EDT
This problem no longer occurs after the formatter redesign.

*** This bug has been marked as a duplicate of bug 303519 ***
Comment 12 Manoj N Palat CLA 2016-09-13 02:56:03 EDT
(In reply to Mateusz Matela from comment #11)
> This problem no longer occurs after the formatter redesign.
> 
> *** This bug has been marked as a duplicate of bug 303519 ***

@Mateusz: I still see the issue reported in comment 3. Can you please take a look?
Comment 13 Jay Arthanareeswaran CLA 2016-09-14 01:58:34 EDT
I don't see the problems from comment #3 anymore. I played around with various options including tab size set to 2 and 4 and don't see any wrapping issue.
Comment 14 Manoj N Palat CLA 2016-09-14 09:30:01 EDT
(In reply to Jay Arthanareeswaran from comment #13)
> I don't see the problems from comment #3 anymore. I played around with
> various options including tab size set to 2 and 4 and don't see any wrapping
> issue.

Jay- try this from comment 3
If I select the line starting "Note:" and Format, I get:

 * Note that it is quite common for the separator to be "_" instead of "-".
 * Either separator can be used when creating a LanguageCode. When getting a
 * string from the LanguageCode, the "-" form is produced by {@link #toString}.
..
That is, it correctly breaks the line with Note, but then *joins* the line with the <a...>, incorrectly creating a line with 81 characters. .

if you don't see this you can set this to verified
Comment 15 Jay Arthanareeswaran CLA 2016-09-15 00:43:58 EDT
(In reply to Manoj Palat from comment #14)
> if you don't see this you can set this to verified

For me, the output is same when I select the first lone alone or format the entire class. This is what I get after formatting:

	 * Note that it is quite common for the separator to be "_" instead of "-".
	 * Either separator can be used when creating a LanguageCode. When getting a
	 * string from the LanguageCode, the "-" form is produced by
	 * {@link #toString}.

The @link corrected pushed to a new line.

I20160914-0430