### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core.tests.model Index: src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java,v retrieving revision 1.214 diff -u -r1.214 FormatterRegressionTests.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java 22 Jun 2007 15:43:19 -0000 1.214 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java 13 Jul 2007 07:15:58 -0000 @@ -54,7 +54,7 @@ private long time; static { -// TESTS_NUMBERS = new int[] { 667 }; +// TESTS_NUMBERS = new int[] { 668 }; // TESTS_RANGE = new int[] { 658, -1 }; } public static Test suite() { @@ -9361,4 +9361,12 @@ JavaCore.setOptions(javaCoreOptions); } } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=128630 + public void test668() { + final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); + DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); + DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); + runTest(codeFormatter, "test668", "A.java", CodeFormatter.K_COMPILATION_UNIT);//$NON-NLS-1$ //$NON-NLS-2$ + } } Index: workspace/Formatter/test668/A_in.java =================================================================== RCS file: workspace/Formatter/test668/A_in.java diff -N workspace/Formatter/test668/A_in.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ workspace/Formatter/test668/A_in.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,37 @@ +public +class +FormatTest { + + int + foo() + { + // foo or bar? +int i = 3; + return i + +1; + } + + // DO NOT REFORMAT BELOW THIS LINE + int + bar() + { + // foo or bar? +// DO NOT REFORMAT BELOW THIS LINE +int i = 3; +// DO NOT REFORMAT ABOVE THIS LINE + return i + +1; + } + // DO NOT REFORMAT ABOVE THIS LINE + + int + baz() + { + // foo or bar? +int i = 3; + return i + +1; + } + + +} Index: workspace/Formatter/test668/A_out.java =================================================================== RCS file: workspace/Formatter/test668/A_out.java diff -N workspace/Formatter/test668/A_out.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ workspace/Formatter/test668/A_out.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,28 @@ +public class FormatTest { + + int foo() { + // foo or bar? + int i = 3; + return i + 1; + } + + // DO NOT REFORMAT BELOW THIS LINE + int + bar() + { + // foo or bar? +// DO NOT REFORMAT BELOW THIS LINE +int i = 3; +// DO NOT REFORMAT ABOVE THIS LINE + return i + +1; + } + // DO NOT REFORMAT ABOVE THIS LINE + + int baz() { + // foo or bar? + int i = 3; + return i + 1; + } + +} #P org.eclipse.jdt.core Index: formatter/org/eclipse/jdt/internal/formatter/Scribe.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Scribe.java,v retrieving revision 1.107 diff -u -r1.107 Scribe.java --- formatter/org/eclipse/jdt/internal/formatter/Scribe.java 29 May 2007 15:12:40 -0000 1.107 +++ formatter/org/eclipse/jdt/internal/formatter/Scribe.java 13 Jul 2007 07:16:02 -0000 @@ -35,6 +35,8 @@ */ public class Scribe { private static final int INITIAL_SIZE = 100; + private static final String BEGIN_NOFORMAT = "DO NOT REFORMAT BELOW THIS LINE"; //$NON-NLS-1$ + private static final String END_NOFORMAT = "DO NOT REFORMAT ABOVE THIS LINE"; //$NON-NLS-1$ private boolean checkLineWrapping; /** one-based column */ @@ -46,6 +48,7 @@ public int currentToken; // edits management + private int editsSuppressedCount = 0; private OptimizedReplaceEdit[] edits; public int editsIndex; @@ -110,7 +113,22 @@ reset(); } + private final boolean editsSuppressed() { + return this.editsSuppressedCount > 0; + } + + private final void suppressEdits() { + this.editsSuppressedCount++; + } + + private final void allowEdits() { + if (this.editsSuppressedCount > 0) { + this.editsSuppressedCount--; + } + } + private final void addDeleteEdit(int start, int end) { + if (this.editsSuppressed()) return; if (this.edits.length == this.editsIndex) { // resize resize(); @@ -119,6 +137,7 @@ } public final void addInsertEdit(int insertPosition, String insertedString) { + if (this.editsSuppressed()) return; if (this.edits.length == this.editsIndex) { // resize resize(); @@ -127,6 +146,7 @@ } private final void addOptimizedReplaceEdit(int offset, int length, String replacement) { + if (this.editsSuppressed()) return; if (this.editsIndex > 0) { // try to merge last two edits final OptimizedReplaceEdit previous = this.edits[this.editsIndex-1]; @@ -209,6 +229,7 @@ } public final void addReplaceEdit(int start, int end, String replacement) { + if (this.editsSuppressed()) return; if (this.edits.length == this.editsIndex) { // resize resize(); @@ -1153,7 +1174,15 @@ } } this.scanner.resetTo(currentTokenEndPosition, this.scannerEndPosition - 1); + + final String comment = String.valueOf(s); + if (comment.contains(BEGIN_NOFORMAT)) { + suppressEdits(); + } else if (comment.contains(END_NOFORMAT)) { + allowEdits(); + } } + public void printEmptyLines(int linesNumber) { this.printEmptyLines(linesNumber, this.scanner.getCurrentTokenEndPosition() + 1); }