From af01f4320de576e4f5febccc769416cb598b18e1 Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Thu, 18 Oct 2007 14:20:59 +0000 Subject: [PATCH] Fix for 204575: [Formatter] Odd indentation effect with access specifiers and class members --- .../formatter/CodeFormatterVisitor.java | 17 ++++++- .../cdt/ui/tests/text/CodeFormatterTest.java | 51 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) 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 94c5108aa37..47a25370f0d 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 @@ -1058,6 +1058,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor { // storage class and other modifiers scribe.printModifiers(); + final int headerIndent= scribe.numberOfIndentations; switch (node.getKey()) { case IASTCompositeTypeSpecifier.k_struct: @@ -1087,6 +1088,10 @@ public class CodeFormatterVisitor extends CPPASTVisitor { // member declarations 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 (braceIndent > headerIndent) { + scribe.unIndent(); + } if (preferences.indent_access_specifier_compare_to_type_header) { scribe.indent(); } @@ -1118,6 +1123,9 @@ public class CodeFormatterVisitor extends CPPASTVisitor { if (preferences.indent_access_specifier_compare_to_type_header) { scribe.unIndent(); } + if (scribe.numberOfIndentations < braceIndent) { + scribe.indent(); + } formatClosingBrace(preferences.brace_position_for_type_declaration); return PROCESS_SKIP; } @@ -1899,6 +1907,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor { } private int visit(IASTSwitchStatement node) { + final int headerIndent= scribe.numberOfIndentations; scribe.printNextToken(Token.t_switch); scribe.printNextToken(Token.tLPAREN, preferences.insert_space_before_opening_paren_in_switch); @@ -1914,7 +1923,10 @@ public class CodeFormatterVisitor extends CPPASTVisitor { String switch_brace = preferences.brace_position_for_switch; formatOpeningBrace(switch_brace, preferences.insert_space_before_opening_brace_in_switch); scribe.startNewLine(); - + final int braceIndent= scribe.numberOfIndentations; + if (braceIndent > headerIndent) { + scribe.unIndent(); + } if (preferences.indent_switchstatements_compare_to_switch) { scribe.indent(); } @@ -2029,6 +2041,9 @@ public class CodeFormatterVisitor extends CPPASTVisitor { if (preferences.indent_switchstatements_compare_to_switch) { scribe.unIndent(); } + if (scribe.numberOfIndentations < braceIndent) { + scribe.indent(); + } scribe.startNewLine(); formatClosingBrace(switch_brace); 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 221a0477413..e8069d0cae9 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 @@ -223,4 +223,55 @@ public class CodeFormatterTest extends BaseUITestCase { assertFormatterResult(); } + //class A { + //public: + //A(); + //}; + + //class A + // { + //public: + // A(); + // }; + public void testWhiteSmithsAccessSpecifierIndentation1_Bug204575() throws Exception { + fOptions= DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap(); + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_COMPARE_TO_TYPE_HEADER, DefaultCodeFormatterConstants.FALSE); + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER, DefaultCodeFormatterConstants.TRUE); + assertFormatterResult(); + } + + //class A { + //public: + //A(); + //}; + + //class A + // { + // public: + // A(); + // }; + public void testWhiteSmithsAccessSpecifierIndentation2_Bug204575() throws Exception { + fOptions= DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap(); + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_COMPARE_TO_TYPE_HEADER, DefaultCodeFormatterConstants.TRUE); + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER, DefaultCodeFormatterConstants.FALSE); + assertFormatterResult(); + } + + //class A { + //public: + //A(); + //}; + + //class A + // { + // public: + // A(); + // }; + public void testWhiteSmithsAccessSpecifierIndentation3_Bug204575() throws Exception { + fOptions= DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap(); + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_COMPARE_TO_TYPE_HEADER, DefaultCodeFormatterConstants.TRUE); + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER, DefaultCodeFormatterConstants.TRUE); + assertFormatterResult(); + } + }