Community
Participate
Working Groups
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; } } } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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);
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.
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.
Resurrecting for 3.0
Reopen for 3.0 consideration.
This is improved with the new formatter. Fixed and released in HEAD.
Verified.