Bug 396686 - [Formatter] Better formatting of // comments (when line wrapping is on)
Summary: [Formatter] Better formatting of // comments (when line wrapping is on)
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.2   Edit
Hardware: All All
: P3 enhancement with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-12-16 09:28 EST by Palmer Eldritch CLA
Modified: 2017-02-03 08:44 EST (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 Palmer Eldritch CLA 2012-12-16 09:28:25 EST
When I hit Ctlr+Shift+F this :

		final Pattern doubleSlash = Pattern.compile("\\t|\\n|\\r");
		final Pattern slash = Pattern.compile("\\t|\\n|\\r");
		System.out.println(slash.equals(doubleSlash)); // Prints false ! uses Object's equals - even when the patterns are identical :slash = Pattern.compile("\\t|\\n|\\r");

becomes :


		final Pattern doubleSlash = Pattern.compile("\\t|\\n|\\r");
		final Pattern slash = Pattern.compile("\\t|\\n|\\r");
		System.out.println(slash.equals(doubleSlash)); // Prints false ! uses
														// Object's equals -
														// even when the
														// patterns are
														// identical :slash =
														// Pattern.compile("\\t|\\n|\\r");
It should become :


		final Pattern doubleSlash = Pattern.compile("\\t|\\n|\\r");
		final Pattern slash = Pattern.compile("\\t|\\n|\\r");
		System.out.println(slash.equals(doubleSlash)); // Prints false ! uses
		// Object's equals - even when the patterns are identical :slash =
		// Pattern.compile("\\t|\\n|\\r");

Please fix this - it is a constant annoyance

See also https://bugs.eclipse.org/bugs/show_bug.cgi?id=75516 for other changes in comments formatter
Comment 1 Dani Megert CLA 2012-12-17 07:34:27 EST
It's a matter of style/taste whether one wants to align those comments on the code or the comment start offset.
Comment 2 Palmer Eldritch CLA 2012-12-17 09:42:45 EST
(In reply to comment #1)
> It's a matter of style/taste whether one wants to align those comments on
> the code or the comment start offset.

Well yes - then solution would be to add a preference for it

Although the situation is really bad when a comment starts near the line wrapping column. I mean whatever your aesthetic dispositions are this :

long resetTime = operLimitStatus.getResetTimeInSeconds(); // I           
					// had       
					// int       
					// and       
					// it        
					// overflowed
					// -         
					// duh 

looks horrible (and is fairly common I assure you)
Comment 3 Palmer Eldritch CLA 2012-12-17 09:54:55 EST
Sorry for double posting - what I thought also when I first saw this is that a way to treat it would be to exclude // comments from line wrapping - could be an option. Needs some discussion - ideas ?
Comment 4 Dani Megert CLA 2012-12-18 02:46:28 EST
(In reply to comment #3)
> Sorry for double posting - what I thought also when I first saw this is that
> a way to treat it would be to exclude // comments from line wrapping - could
> be an option. Needs some discussion - ideas ?

As a first step you could just disable to format them.
Comment 5 Palmer Eldritch CLA 2012-12-18 06:30:46 EST
(In reply to comment #4)
> As a first step you could just disable to format them.
Not really - it is the line wrapping that I mind (when done as illustrated - when the comment is on a line on it's own it is very welcome and usually perfectly presentable). In general I need the comments formatted as anything else. And simply disabling it will break the style in several projects anyway.
Comment 6 Palmer Eldritch CLA 2015-06-08 19:50:44 EDT
Which class is responsible for formatting the comments in the jdt ? Have some difficulty finding it

Thanks
Comment 7 Mateusz Matela CLA 2015-06-09 10:19:59 EDT
(In reply to Palmer Eldritch from comment #6)
> Which class is responsible for formatting the comments in the jdt ? Have
> some difficulty finding it
That would be org.eclipse.jdt.internal.formatter.CommentsPreparator in jdt-core. Also, org.eclipse.jdt.internal.formatter.linewrap.CommentWrapExecutor. You may find this useful to understand the formatter in general: https://github.com/mateusz-matela/eclipse.jdt.core/wiki

Note that my intention while rewriting the formatter was that it would wrap the main code to leave a lot of space for the comment and minimize additional lines, for example:
System.out.println(slash.equals(
	doubleSlash)); /* Prints false ! uses Object's equals - even
			 * when the patterns are identical :slash =
			 * Pattern.compile("\\t|\\n|\\r"); */
But now I see this doesn't work for line comments or when "Never join lines" option is turned on, which may be considered a bug.
Comment 8 Palmer Eldritch CLA 2015-06-09 10:43:03 EDT
Thanks a lot for feedback !

Yes when line wrapping is on it ends up turning this:

someStatement(); // and a long comment that should be wrapped

to:

someStatement(); // and
                 // a long
                 // comment
                 // that
                 // should
                 // be wrapped

Which is a nono

Should become:


someStatement(); // and
// a long comment that
// should be wrapped

(or at least some sort of preference added)

Thanks again