Bug 22313 - Formatter doesn't like some comment
Summary: Formatter doesn't like some comment
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.0   Edit
Hardware: PC Windows 98
: P3 minor (vote)
Target Milestone: 3.0 M4   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-08-09 11:45 EDT by jean-louis willems CLA
Modified: 2003-10-13 10:37 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description jean-louis willems CLA 2002-08-09 11:45:42 EDT
The formatter insert an unexpected tab before the { of the else-block.

Preferences  : Java -> Code Formatter -> New Lines 
--------------------------------------------------
   active  :  insert a line before an opening brace
   active  :  insert new lines in control statements
 inactive  :  Clear all blank lines
 inactive  :  insert new line between 'else-if'
   active  :  insert a new line inside an empty block
 
I know is a stupid code. The goal is to isolate the problem.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
public class FormatterProblem
{
	public static boolean isZero(int x)
	{
		if (x==0) 
		{
			return true;
		}
		else // here is the comment that the formatter doesn't like 
			{
			return false;
		}
	}
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Comment 1 jean-louis willems CLA 2002-09-01 10:02:52 EDT
The cause of the problem is the bad value in 'pendingNewLineAfterParen' in the 
format method when previous compilable token is a ELSE-token (C1) and when the 
lastToken is any comment (C2)
A solution is to modify pendingNewLineAfterParen when LBRACE token is the 
current token (C3) and when C1 and C2.

In the source of eclipse 2.0.1, 'pendingNewLineAfterParen' is updated by lines 
423-425.

pendingNewlineAfterParen =
   pendingNewlineAfterParen
   || (previousCompilableToken == TokenNameRPAREN && token == TokenNameLBRACE);

I propose to modify to 

pendingNewlineAfterParen =
   pendingNewlineAfterParen
   || (C1 && C3 && C2)
   || (previousCompilableToken == TokenNameRPAREN && token == TokenNameLBRACE);

where 

C1 : previousCompilableToken == TokenNameelse
C2 : previousToken == TokenNameCOMMENT_BLOCK 
     || previousToken == TokenNameCOMMENT_JAVADOC 
     || previousToken == TokenNameCOMMENT_LINE				
   Any other way to write C2 ?
C3 : token == TokenNameLBRACE

Finally, my proposal is...

pendingNewlineAfterParen =
   pendingNewlineAfterParen
// --- BEGIN PROPOSAL ADDED LINES ------------------------------------
   || (previousCompilableToken == TokenNameelse 
       && token == TokenNameLBRACE 
       && (previousToken == TokenNameCOMMENT_BLOCK 
            || previousToken == TokenNameCOMMENT_JAVADOC 
            || previousToken == TokenNameCOMMENT_LINE))				
// --- END PROPOSAL ADDED LINES --------------------------------------
   || (previousCompilableToken == TokenNameRPAREN && token == TokenNameLBRACE);



Comment 2 Olivier Thomann CLA 2002-10-11 09:33:19 EDT
Thanks for the proposal. Right now we are revisiting the whole implementation of
the code formatter. So for now we don't plan to apply bug fixes in the current
implementation.
Comment 3 Olivier Thomann CLA 2003-01-15 13:22:38 EST
We need to clear 2.1 bug reports that won't be addressed before 2.1. The new 
implementation is still in the works. Therefore we cannot include it for 2.1. 
Not enough testing and we need to polish the preferences. This will be address 
for 2.2 as stated in the JDT/Core plan.
Comment 4 Philipe Mulet CLA 2003-06-12 06:35:49 EDT
Resurrecting for 3.0
Comment 5 Olivier Thomann CLA 2003-06-12 15:59:01 EDT
Reopen for 3.0 consideration.
Comment 6 Olivier Thomann CLA 2003-10-03 10:03:15 EDT
This is improved with the new formatter.
Fixed and released in HEAD.
Comment 7 David Audel CLA 2003-10-13 10:37:43 EDT
Verified.