### Eclipse Workspace Patch 1.0 #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.108 diff -u -r1.108 Scribe.java --- formatter/org/eclipse/jdt/internal/formatter/Scribe.java 31 Oct 2007 12:48:48 -0000 1.108 +++ formatter/org/eclipse/jdt/internal/formatter/Scribe.java 15 Nov 2007 18:14:27 -0000 @@ -111,6 +111,53 @@ reset(); } + /** + * This method will adapt the selected regions if needed. + * If a region should be adapted (see isAdaptableRegion(IRegion)) + * retrieve correct upper and lower bounds and replace the region. + */ + private void adaptSelectedRegions() { + for (int i = 0, max = this.regions.length; i < max; i++) { + IRegion aRegion = this.regions[i]; + int offset = aRegion.getOffset(); + if (offset > 0) { + int length = aRegion.getLength(); + if (isAdaptableRegion(offset, length)) { + // if we have a selection, search for overlapping edits + int upperBound = offset; + int lowerBound = 0; + boolean upperFound = false; + int regionEnd = offset + length; + for (int j = 0, max2 = this.editsIndex; j < max2; j++) { + // search for lower bound + int editOffset = this.edits[j].offset; + if (upperFound) { + int editLength = this.edits[j].length; + if (lowerBound == 0 && editOffset + editLength < regionEnd) { + continue; + } else { + lowerBound = editOffset + editLength; + break; // found both bonds - leave the loop + } + // search for upper bound + } else { + if (this.edits[j+1].offset < offset) { + continue; + } else { + upperBound = editOffset; + upperFound = true; + } + } + } + if (lowerBound != 0) { + // store result if any + this.regions[i] = new Region(upperBound , lowerBound - upperBound); + } + } + } + } + } + private final void addDeleteEdit(int start, int end) { if (this.edits.length == this.editsIndex) { // resize @@ -558,6 +605,9 @@ } public TextEdit getRootEdit() { + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541 + adaptSelectedRegions(); + MultiTextEdit edit = null; int regionsLength = this.regions.length; int textRegionStart; @@ -661,8 +711,33 @@ this.scannerEndPosition = compilationUnitSource.length; this.scanner.resetTo(0, this.scannerEndPosition - 1); this.edits = new OptimizedReplaceEdit[INITIAL_SIZE]; - } - + } + + /** + * Returns whether the given region should be adpated of not. + * A region should be adapted only if: + * - region does not exceed the page width + * - on a single line when more than one line in CU + * @param offset the offset of the region to consider + * @param length the length of the region to consider + * @return boolean true if line should be adapted, false otherwhise + */ + private boolean isAdaptableRegion(int offset, int length) { + int span = offset + length; + // first check region width + if (span > this.pageWidth) { + return false; + } + // more than one line selected + if (span > this.getLineEnd(this.scanner.getLineNumber(offset) + 1)) { + return false; + // region is on a single line and CU has more than one line + } else if (this.lineEnds != null && this.lineEnds.length > 1) { + return true; + } + return false; + } + private boolean isOnFirstColumn(int start) { if (this.lineEnds == null) return start == 0; int index = Arrays.binarySearch(this.lineEnds, start); #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.218 diff -u -r1.218 FormatterRegressionTests.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java 31 Oct 2007 14:59:58 -0000 1.218 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java 15 Nov 2007 18:14:29 -0000 @@ -9586,4 +9586,45 @@ // }; // runTest(codeFormatter, "test677", "A.java", CodeFormatter.K_UNKNOWN, 0, false, regions, "\n");//$NON-NLS-1$ //$NON-NLS-2$ // } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541 + public void test685() { + final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); + DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); + DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); + IRegion[] regions = new IRegion[] { + new Region(18, 35) + }; + runTest(codeFormatter, "test685", "A.java", CodeFormatter.K_UNKNOWN, 0, false, regions, "\n");//$NON-NLS-1$ //$NON-NLS-2$ + } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541 + public void test686() { + final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); + DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); + DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); + runTest(codeFormatter, "test685", "A.java");//$NON-NLS-1$ //$NON-NLS-2$ + } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541 + public void test687() { + final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); + DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); + DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); + IRegion[] regions = new IRegion[] { + new Region(17, 25) + }; + runTest(codeFormatter, "test687", "A.java", CodeFormatter.K_UNKNOWN, 0, false, regions, "\n");//$NON-NLS-1$ //$NON-NLS-2$ + } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541 + public void test688() { + final Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); + DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options); + DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); + IRegion[] regions = new IRegion[] { + new Region(18, 48) + }; + runTest(codeFormatter, "test688", "A.java", CodeFormatter.K_UNKNOWN, 0, false, regions, "\n");//$NON-NLS-1$ //$NON-NLS-2$ + } } Index: workspace/Formatter/test688/A_out.java =================================================================== RCS file: workspace/Formatter/test688/A_out.java diff -N workspace/Formatter/test688/A_out.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ workspace/Formatter/test688/A_out.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,3 @@ +public class A { + int i = 1; +} Index: workspace/Formatter/test685/A_out.java =================================================================== RCS file: workspace/Formatter/test685/A_out.java diff -N workspace/Formatter/test685/A_out.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ workspace/Formatter/test685/A_out.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,3 @@ +public class A { + int i = 1; +} Index: workspace/Formatter/test687/A_out.java =================================================================== RCS file: workspace/Formatter/test687/A_out.java diff -N workspace/Formatter/test687/A_out.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ workspace/Formatter/test687/A_out.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,3 @@ +public class A { + int i=1; +} Index: workspace/Formatter/test688/A_in.java =================================================================== RCS file: workspace/Formatter/test688/A_in.java diff -N workspace/Formatter/test688/A_in.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ workspace/Formatter/test688/A_in.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,3 @@ +public class A { + int i=1; +} Index: workspace/Formatter/test685/A_in.java =================================================================== RCS file: workspace/Formatter/test685/A_in.java diff -N workspace/Formatter/test685/A_in.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ workspace/Formatter/test685/A_in.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,3 @@ +public class A { + int i=1; +} Index: workspace/Formatter/test687/A_in.java =================================================================== RCS file: workspace/Formatter/test687/A_in.java diff -N workspace/Formatter/test687/A_in.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ workspace/Formatter/test687/A_in.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,3 @@ +public class A { + int i=1; +}