diff options
| author | Olivier Thomann | 2013-04-08 16:53:33 +0000 |
|---|---|---|
| committer | Jayaprakash Arthanareeswaran | 2013-04-08 16:53:33 +0000 |
| commit | 6aed5c6026f35c6c9bf85a610d0a44078f65b752 (patch) | |
| tree | 80397c39d238eedb152702920dba46ac2e6f0abb | |
| parent | 5ccdb7e247bbbd1f8f7142380f221525938bf5a9 (diff) | |
| download | eclipse.jdt.core-6aed5c6026f35c6c9bf85a610d0a44078f65b752.tar.gz eclipse.jdt.core-6aed5c6026f35c6c9bf85a610d0a44078f65b752.tar.xz eclipse.jdt.core-6aed5c6026f35c6c9bf85a610d0a44078f65b752.zip | |
Fix for bug 405038 - [formatter] infix expression formatting broken
without spaces before/after binary operator
2 files changed, 116 insertions, 5 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java index 1ccb9f0272..87b5a73536 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2012 IBM Corporation and others. + * Copyright (c) 2000, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -12938,4 +12938,104 @@ public void testBug379793() throws Exception { "}\n" ); } +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=405038 +//To verify that the whitespace options for resources in try statement work correctly +public void testBug405038() throws Exception { + this.formatterPrefs = null; + this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_BINARY_OPERATOR, JavaCore.DO_NOT_INSERT); + this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_BINARY_OPERATOR, JavaCore.DO_NOT_INSERT); + String source = + "public class FormatterError {\n" + + " int foo(int a, int b, int c) {\n" + + " return a + b + ++c;\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class FormatterError {\n" + + " int foo(int a, int b, int c) {\n" + + " return a+b+ ++c;\n" + + " }\n" + + "}\n" + ); +} +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=405038 +//To verify that the whitespace options for resources in try statement work correctly +public void testBug405038_2() throws Exception { + this.formatterPrefs = null; + this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_BINARY_OPERATOR, JavaCore.DO_NOT_INSERT); + this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_BINARY_OPERATOR, JavaCore.DO_NOT_INSERT); + String source = + "public class FormatterError {\n" + + " int foo(int a, int b, int c) {\n" + + " return a + ++b + c;\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class FormatterError {\n" + + " int foo(int a, int b, int c) {\n" + + " return a+ ++b+c;\n" + + " }\n" + + "}\n" + ); +} +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=405038 +//To verify that the whitespace options for resources in try statement work correctly +public void testBug405038_3() throws Exception { + this.formatterPrefs = null; + this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_BINARY_OPERATOR, JavaCore.DO_NOT_INSERT); + this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_BINARY_OPERATOR, JavaCore.DO_NOT_INSERT); + String source = + "public class FormatterError {\n" + + " int foo(int a, int b, int c) {\n" + + " return a - --b + c;\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class FormatterError {\n" + + " int foo(int a, int b, int c) {\n" + + " return a- --b+c;\n" + + " }\n" + + "}\n" + ); +} +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=405038 +//To verify that the whitespace options for resources in try statement work correctly +public void testBug405038_4() throws Exception { + this.formatterPrefs = null; + this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_BINARY_OPERATOR, JavaCore.DO_NOT_INSERT); + this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_BINARY_OPERATOR, JavaCore.DO_NOT_INSERT); + String source = + "public class FormatterError {\n" + + " int foo(int a, int b, int c) {\n" + + " return a - -b + c;\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class FormatterError {\n" + + " int foo(int a, int b, int c) {\n" + + " return a- -b+c;\n" + + " }\n" + + "}\n" + ); +} +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=405038 +//To verify that the whitespace options for resources in try statement work correctly +public void testBug405038_5() throws Exception { + this.formatterPrefs = null; + this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_BINARY_OPERATOR, JavaCore.DO_NOT_INSERT); + this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_BINARY_OPERATOR, JavaCore.DO_NOT_INSERT); + String source = + "public class FormatterError {\n" + + " int foo(int a, int b, int c) {\n" + + " return a - -b + ++c;\n" + + " }\n" + + "}\n"; + formatSource(source, + "public class FormatterError {\n" + + " int foo(int a, int b, int c) {\n" + + " return a- -b+ ++c;\n" + + " }\n" + + "}\n" + ); +} } diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java index 30a8fced13..09cc4543ea 100644 --- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java +++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2002, 2012 IBM Corporation and others. + * Copyright (c) 2002, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -473,9 +473,20 @@ public class CodeFormatterVisitor extends ASTVisitor { this.scribe.printNextToken(operators[i], this.preferences.insert_space_before_binary_operator); this.scribe.alignFragment(binaryExpressionAlignment, i); } - if (operators[i] == TerminalTokens.TokenNameMINUS && isNextToken(TerminalTokens.TokenNameMINUS)) { - // the next character is a minus (unary operator) - this.scribe.space(); + switch(operators[i]) { + case TerminalTokens.TokenNameMINUS : + if (isNextToken(TerminalTokens.TokenNameMINUS) + || isNextToken(TerminalTokens.TokenNameMINUS_MINUS)) { + // the next character is a '-' or '--' (unary operator) + this.scribe.space(); + } + break; + case TerminalTokens.TokenNamePLUS : + if (isNextToken(TerminalTokens.TokenNamePLUS) + || isNextToken(TerminalTokens.TokenNamePLUS_PLUS)) { + // the next character is a + or ++ (unary operator) + this.scribe.space(); + } } if (this.preferences.insert_space_after_binary_operator) { this.scribe.space(); |
