diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java index f23842f833c..fb71249cb16 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java @@ -970,6 +970,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor { // declarator final IASTFunctionDeclarator declarator= node.getDeclarator(); + skipNonWhitespaceToNode(declarator); boolean needSpace= declarator.getPointerOperators().length > 0 && scribe.printComment(); if (needSpace) { scribe.space(); @@ -1523,6 +1524,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor { scribe.space(); } + final int headerIndent= scribe.numberOfIndentations; scribe.printNextToken(Token.t_enum, true); final IASTName name= node.getName(); if (name != null) { @@ -1532,20 +1534,23 @@ public class CodeFormatterVisitor extends CPPASTVisitor { formatLeftCurlyBrace(line, preferences.brace_position_for_type_declaration); formatOpeningBrace(preferences.brace_position_for_type_declaration, preferences.insert_space_before_opening_brace_in_type_declaration); + final int braceIndent= scribe.numberOfIndentations; -// if (preferences.insert_new_line_after_opening_brace_in_enumerator_list) { -// scribe.printNewLine(); -// } -// if (preferences.insert_space_after_opening_brace_in_enumerator_list) { -// scribe.space(); -// } - + if (true /* preferences.insert_new_line_after_opening_brace_in_enumerator_list */) { + scribe.startNewLine(); + } else if (true /* preferences.insert_space_after_opening_brace_in_enumerator_list */) { + scribe.space(); + } + if (braceIndent == headerIndent) { + scribe.indent(); + } + final int enumIndent= scribe.numberOfIndentations; final IASTEnumerator[] enumerators= node.getEnumerators(); final ListAlignment align= new ListAlignment(preferences.alignment_for_enumerator_list); align.fSpaceBeforeComma= preferences.insert_space_before_comma_in_enum_declarations; align.fSpaceAfterComma= preferences.insert_space_after_comma_in_enum_declarations; - align.fContinuationIndentation= 1; + align.fContinuationIndentation= enumIndent == headerIndent ? 1 : 0; formatList(Arrays.asList(enumerators), align, false, false); // handle trailing comma @@ -1556,13 +1561,14 @@ public class CodeFormatterVisitor extends CPPASTVisitor { } } -// if (preferences.insert_new_line_before_closing_brace_in_enumerator_list) { -// scribe.startNewLine(); -// } -// if (preferences.insert_space_before_closing_brace_in_enumerator_list) { -// scribe.space(); -// } - scribe.startNewLine(); + if (enumIndent > braceIndent) { + scribe.unIndent(); + } + if (true /* preferences.insert_new_line_before_closing_brace_in_enumerator_list */) { + scribe.startNewLine(); + } else if (true /* preferences.insert_space_before_closing_brace_in_enumerator_list */) { + scribe.space(); + } formatClosingBrace(preferences.brace_position_for_type_declaration); return PROCESS_SKIP; } @@ -2858,6 +2864,21 @@ public class CodeFormatterVisitor extends CPPASTVisitor { } } + private void skipNonWhitespaceToNode(IASTNode node) { + final IASTNodeLocation fileLocation= node.getFileLocation(); + if (fileLocation != null) { + final int startOffset= fileLocation.getNodeOffset(); + final int nextTokenOffset= getNextTokenOffset(); + if (nextTokenOffset < startOffset) { + final int currentOffset= scribe.scanner.getCurrentTokenEndPosition() + 1; + final int restLength= startOffset - currentOffset; + if (restLength > 0) { + scribe.printRaw(currentOffset, restLength); + } + } + } + } + /** * Format an expression nested in parenthesis. If the operand is * null, empty parenthesis are expected. diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java index 4335d6d62e3..0f3c3bce9b8 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java @@ -561,4 +561,44 @@ public class CodeFormatterTest extends BaseUITestCase { assertFormatterResult(); } + //NOT_DEFINED void foo() + // { + // } + // + //enum T1 + // { + // E1 = 1 + // }; + + //NOT_DEFINED void foo() { + //} + // + //enum T1 { + // E1 = 1 + //}; + public void testPreserveWhitespace_Bug225326() throws Exception { + assertFormatterResult(); + } + + //NOT_DEFINED void foo() + // { + // } + // + //enum T1 + // { + // E1 = 1 + // }; + + //NOT_DEFINED void foo() + // { + // } + // + //enum T1 + // { + // E1 = 1 + // }; + public void testPreserveWhitespace2_Bug225326() throws Exception { + fOptions= DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap(); + assertFormatterResult(); + } }