Bug 287833 - [formatter] Formatter removes the first character after the * in the <pre> tag
Summary: [formatter] Formatter removes the first character after the * in the <pre> tag
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: 3.5.2   Edit
Assignee: Frederic Fusier CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 303957 (view as bug list)
Depends on:
Blocks:
 
Reported: 2009-08-27 10:20 EDT by Satyam Kandula CLA
Modified: 2010-02-26 06:53 EST (History)
4 users (show)

See Also:
Olivier_Thomann: review+


Attachments
Proposed patch (5.32 KB, patch)
2009-09-02 09:56 EDT, Satyam Kandula CLA
no flags Details | Diff
Proposed patch (6.14 KB, patch)
2009-09-18 02:45 EDT, Satyam Kandula CLA
no flags Details | Diff
Other proposed patch (7.42 KB, patch)
2009-09-18 06:43 EDT, Frederic Fusier CLA
no flags Details | Diff
Patch for R3_5_maintenance stream (7.44 KB, patch)
2009-09-23 08:26 EDT, 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 Satyam Kandula CLA 2009-08-27 10:20:06 EDT
Build ID:  M20090826-1100

Steps To Reproduce:
Formatting the below code 
#######
public class test1 {
	/**
	 *<pre>
	 *void foo() {
	 *}
	 *</pre>
	 */
	void foo() {

	}
}
#####
becomes
#######
public class test1 {
	/**
	 *<pre>
	 * oid foo() {
	 * 
	 *</pre>
	 */
	void foo() {

	}
}
#########
Note the 'v' in the void and the '{' have gone. 

More information:
This is even reproducible in 3.6M1 and not reproducible in 3.5
Comment 1 Olivier Thomann CLA 2009-08-28 12:40:05 EDT
I reproduced with 3.5.1RC2.

Reassigning to Frédéric as I would like a fix for next week.
Comment 2 Olivier Thomann CLA 2009-08-28 12:44:16 EDT
It looks like the character that follows the '*' is removed. If a space is inserted between 'void' and the '*', it works.
Same for the '}' and the '*'.
Comment 3 Olivier Thomann CLA 2009-08-28 12:44:49 EDT
A fix is needed for 3.5.1.
Comment 4 Frederic Fusier CLA 2009-08-28 15:03:45 EDT
Olivier,

This is not a regression, I reproduce the same behavior using 3.5.0, 3.4.2 and 3.3.2. Hence, this is even not a regression introduced while rewriting the comment formatter, so I guess this issue exists since day 1...

IMO, you may reset the target (at least set to 3.5.2), reduce the severity to normal and put back this bug to Satyam to let him investigate and learn around this code.
Comment 5 Olivier Thomann CLA 2009-08-28 16:59:52 EDT
Agreed. In this case, we can fix it only for 3.6.
Targetting 3.6M2.
I still leave it as major as there is data loss in the process.

Reassigning to Satyam so he can learn how to fix issues in the comment formatter.
Comment 6 Satyam Kandula CLA 2009-08-31 03:54:08 EDT
My mistake in reproducing - the problem is reproducible in 3.5 too.
Comment 7 Satyam Kandula CLA 2009-09-02 09:56:46 EDT
Created attachment 146280 [details]
Proposed patch

Frederic,
Can you review the changes? 

Thanks,
Satyam
Comment 8 Frederic Fusier CLA 2009-09-14 11:52:08 EDT
(In reply to comment #7)
> Created an attachment (id=146280) [details]
> Proposed patch
> 
> Frederic,
> Can you review the changes? 
> 
> Thanks,
> Satyam

The changes look good but need some minor improvements:

1 - Tests are usually put in the FormatterCommentsBugsTest test suite.
    JavaDocTestCase was used to test the comment formatter before it was 
    rewritten in 3.4). It's not wrong to write a test there but it's easier
    to have all tests at the same place...

2 - change in Scribe.printCodeSnippet(int,int) should be improved:
    a - do not call a method twice when its result can be put in local variable
    b - not only ' ' and '\t' are space characters
    c - use a switch as soon as there are several comparisons to constants

    Then the code would become:
	int offsetEnd = prefixOffset + 1;
	char ch = inputBuffer.charAt(offsetEnd);
	switch (ch) {
		case '\u000c' :	/* FORM FEED               */
		case ' ':
		case '\t':
			offsetEnd++;
			break;
		default:
			if (ScannerHelper.isWhitespace(ch)) {
				offsetEnd++;
			}
			break;
	}
	inputBuffer.delete(lineOffset, offsetEnd);

3 - in printJavadocHtmlTag(FormatJavadocText, FormatJavadocBlock, boolean),
    the change from nextStart to realEnd as a side effect on existing behavior.
    I observed it while running the FormatterCommentsMassiveTests test suite
    on a Galileo full source workspace. Undoing this change makes the behavior
    b and with your patch I see a lot of difference
    vs. the output produced by HEAD... And I strongly suspect that this is
Comment 9 Frederic Fusier CLA 2009-09-14 12:02:08 EDT
Sorry, a unexpected hit on the Enter key before I actually finish my comment...
So, point 3 should be read:

3 - Can you explain a little bit why you change nextStart to realEnd?
    At least by specific test showing that it was necessary.

4 - The patch has side effects on existing comment formatter behavior.
    I observed it while running the FormatterCommentsMassiveTests test suite
    on a Galileo full source workspace. You need to understand first if these
    side effects are acceptable (i.e. fix incorrect previous behaviors) or
    if they are regressions...
Comment 10 Frederic Fusier CLA 2009-09-14 12:21:56 EDT
There's another issue with your patch, it does not fix the following test case:
public class X {
	/**
	 * <pre>   {@code
	 * 
	 *   public class X {
	 *   }}</pre>
	 */
	void foo() {}
}

which is both formatted with or without your patch as:
public class X02b {
	/**
	 * <pre>
	 * @code
	 * 
	 *   public class X {
	 *   }}
	 * </pre>
	 */
	void foo() {
	}
}

The brace of the inline tag @code still disappears when inserting the new line after the <pre> html tag...
Comment 11 Olivier Thomann CLA 2009-09-14 14:31:26 EDT
Moving to 3.6M3 as this won't be part of the next I-build.
Comment 12 Satyam Kandula CLA 2009-09-15 02:52:42 EDT
Frederic, Thank you for your comments.

1. I would move the test accordingly.

2. I completely agree for your comment of the duplicate call. I would fix it. 

3. The main reason we are running into this problem is because we are setting the nextStart  to '*'position+1, even if it running into the next separator. Hence, I am trying to use one more variable realEnd so that we make sure that we add '1' for those that are really having atleast one space. 

4. Can you tell me how to run the FormatterCommentsMassiveTests test suite?

5. I feel that the '{' disappearance problem is slightly different and probably better handled as part of another bug. Do you think otherwise?
Comment 13 Frederic Fusier CLA 2009-09-15 08:01:27 EDT
(In reply to comment #12)
> Frederic, Thank you for your comments.
> 
> 1. I would move the test accordingly.
> 
> 2. I completely agree for your comment of the duplicate call. I would fix it. 
> 
> 3. The main reason we are running into this problem is because we are setting
> the nextStart  to '*'position+1, even if it running into the next separator.
> Hence, I am trying to use one more variable realEnd so that we make sure that
> we add '1' for those that are really having atleast one space. 
> 
OK, but I do not think this new variable is necessary, the spaceFound flag sounds to be enough for me. If you did find some cases where it's not true, then you should add specific tests to highlight them...

> 4. Can you tell me how to run the FormatterCommentsMassiveTests test suite?
> 
Of course. The FormatterCommentsBugsTest test suite is in the org.eclipse.jdt.core.tests.formatter package.

Create a launch configuration for this test suite and add the following VM arguments:
-Xmx256M -DinputDir=<source directory> -DoutputDir=<output directory>

When running this test suite, the source directory is parsed recursively to find all the java files, format each of them twice and write the output result in the output directory.

Typically, the source directory may be a workspace where you import the sources of all the target platform plugins, eg. Import > Plug-ins and Fragments > Projects with source folders. Personally, I import sources of all Galileo plugins and get more than 41,000 java units to format...

So, the process to test your patch is:
1 - create and fill the source directory
2 - run the FormatterCommentsBugsTest test suite against this workspace using
    HEAD contents; this will create the outputs which will be used as reference
3 - repeat step 2 and store the console output in a text file
4 - apply your patch
5 - run the FormatterCommentsBugsTest test suite against this workspace
6 - compare the console output with the one stored in step 3, only date and 
    time should differ. If there are differences, then they must be analyzed
    to understand whether there are justified (incorrect behavior fixed by the
    patch) or regressions...


> 5. I feel that the '{' disappearance problem is slightly different and probably
> better handled as part of another bug. Do you think otherwise?
>
I really don't know. If you think it's a separated issue then open a specific bug for it looks the good thing to do.
Comment 14 Satyam Kandula CLA 2009-09-17 02:32:14 EDT
I have analyzed the failures. It is just that the blank lines in the pre tag were not getting deleted with this patch. Fixing them!
Comment 15 Satyam Kandula CLA 2009-09-18 02:45:26 EDT
Created attachment 147524 [details]
Proposed patch

Apart from your earlier comments, I have added two more case statements for \r and \n in the switch statement. I have also added two more test cases to make sure that we catch those changes observed in the massive tests.
Comment 16 Frederic Fusier CLA 2009-09-18 06:41:31 EDT
(In reply to comment #15)
> Created an attachment (id=147524) [details]
> Proposed patch
> 
> Apart from your earlier comments, I have added two more case statements for \r
> and \n in the switch statement. I have also added two more test cases to make
> sure that we catch those changes observed in the massive tests.

This patch sounds OK, however I'm still annoyed by the local variable realEnd.
It's not clear why you need an extra variable instead of only nextStart.

So, I've try to rewrite entirely this part of the algorithm to make it clearer.
I'll attach it to this bug then we can chose the one appearing to be the best...
Comment 17 Frederic Fusier CLA 2009-09-18 06:43:50 EDT
Created attachment 147536 [details]
Other proposed patch

The first thing it appears to me is that rescan the text was not necessary as we already knows the number of empty lines (linesGap), hence it's only necessary to scan the beginning of the line before the code start (nextStart) to see whether a space is needed or not...
Comment 18 Satyam Kandula CLA 2009-09-18 08:22:17 EDT
This patch looks much better. I believe this could be final!
Comment 19 Olivier Thomann CLA 2009-09-18 08:53:03 EDT
Since there is data loss, please investigate a patch for 3.5.2.
Thanks.
Comment 20 Frederic Fusier CLA 2009-09-18 09:00:03 EDT
(In reply to comment #18)
> This patch looks much better. I believe this could be final!

Thanks Satyam. So, as I'm back to JDT/Core team and keep the formatter stuff in
my area, I take this bug back to me...

I really appreciated your helpful contribution :-)
Comment 21 Frederic Fusier CLA 2009-09-18 09:01:41 EDT
Comment on attachment 147524 [details]
Proposed patch

mark this patch as obsolete as we agreed to take the other patch instead
Comment 22 Frederic Fusier CLA 2009-09-21 03:36:24 EDT
Released for 3.6M3 in HEAD stream.

(In reply to comment #19)
> Since there is data loss, please investigate a patch for 3.5.2.
> Thanks.

=> set the target to 3.5.2. I do not think there will be any problem to backport it to the R3_5_maintenance stream...
Comment 23 Frederic Fusier CLA 2009-09-23 08:26:04 EDT
Created attachment 147885 [details]
Patch for R3_5_maintenance stream
Comment 24 Frederic Fusier CLA 2009-09-23 08:27:59 EDT
Olivier, could you please review?
Comment 25 Olivier Thomann CLA 2009-09-23 13:46:04 EDT
Patch looks good.
Comment 26 Olivier Thomann CLA 2009-09-24 16:28:40 EDT
This seems to be released in the 3.5 maintenance stream.
Please close as RESOLVED/FIXED.
Comment 27 Frederic Fusier CLA 2009-09-24 16:40:28 EDT
(In reply to comment #26)
> This seems to be released in the 3.5 maintenance stream.
> Please close as RESOLVED/FIXED.

Ooops that's right, sorry I forgot to update the bug...

Released for 3.5.2 in R3_5_maintenance stream.
Comment 28 Dani Megert CLA 2009-09-25 03:06:03 EDT
Frédéric, please update the bundle version to 3.5.2.
Comment 29 Frederic Fusier CLA 2009-09-25 04:46:06 EDT
(In reply to comment #28)
> Frédéric, please update the bundle version to 3.5.2.

done
Comment 30 Olivier Thomann CLA 2009-10-27 12:56:47 EDT
Verified for 3.6M3 using I20091027-0100.
Comment 31 Olivier Thomann CLA 2010-01-21 10:14:53 EST
Verified for 3.5.2RC2 using M20100120-0800.
Opened bug 300379 for problem reported in comment 10.
Comment 32 Markus Keller CLA 2010-02-26 06:53:16 EST
*** Bug 303957 has been marked as a duplicate of this bug. ***