### 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.247 diff -u -r1.247 FormatterRegressionTests.java --- src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java 27 Jul 2009 18:20:49 -0000 1.247 +++ src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java 13 Aug 2009 09:53:40 -0000 @@ -10849,4 +10849,99 @@ DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences, compilerOptions); runTest(codeFormatter, "test723", "A.java", CodeFormatter.K_COMPILATION_UNIT, false);//$NON-NLS-1$ //$NON-NLS-2$ } + +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=285565 +//Test to make sure that measureIndentInSpaces and extractIndentString do not throw illegalArgumentException with indentWidth set to zero. +public void testBug285565a() { + try { + assertEquals("Should be 0", 0, IndentManipulation.measureIndentInSpaces("", 0)); + assertEquals("Should be 0", 0, IndentManipulation.measureIndentInSpaces("\t", 0)); + assertEquals("Should be 1", 1, IndentManipulation.measureIndentInSpaces("\t ", 0)); + assertEquals("Should be blank", "\t", IndentManipulation.extractIndentString("\tabc", 0, 0)); + } catch (IllegalArgumentException e) { + assertTrue("Should not happen", false); + } +} + +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=285565 +//Test to make sure that a divide by zero exception isn't thrown when formatting with indent width and tab width set to zero +public void testBug285565b() { + this.formatterPrefs.indentation_size = 0; + this.formatterPrefs.tab_size = 0; + String source = "public class test {\n" + + " public static void main(String[] args) {\n" + + " int B= 12;\n" + + " int C= B - 1;\n" + + " int K= 99;\n" + + " int f1= K - 1 - C;\n" + + " int f2= K - C - C - C;\n" + + " }\n" + "}\n"; + formatSource(source, "public class test {\n" + + "public static void main(String[] args) {\n" + + "int B = 12;\n" + + "int C = B - 1;\n" + + "int K = 99;\n" + + "int f1 = K - 1 - C;\n" + + "int f2 = K - C - C - C;\n" + + "}\n" + + "}\n"); +} + +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=285565 +//To make sure that changeIndent no longer throws an illegal argument exception with indentWidth set to 0. +public void testBug285565c() { + String result = "int B = 12;\n" + + " int C = B - 1;\n" + + " int K = 99;\n" + + " int f1 = K - 1 - C;\n" + + " int f2 = K - C - C - C;" ; + + try { + assertEquals("Should be as shown", result, IndentManipulation.changeIndent("int B = 12;\n" + + "int C = B - 1;\n" + + "int K = 99;\n" + + "int f1 = K - 1 - C;\n" + + "int f2 = K - C - C - C;" ,0,0,0, " ","\n")); + + } catch (IllegalArgumentException e) { + assertTrue("Should not happen", false); + } +} + +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=285565 +//To make sure that trimIndent no longer throws an illegal argument exception with indentWidth set to 0. +public void testBug285565d() { + String result = "int B = 12;\n" + + "int C = B - 1;\n" + + "int K = 99;\n" + + "int f1 = K - 1 - C;\n" + + "int f2 = K - C - C - C;" ; + + try { + assertEquals("Should be as shown", result, IndentManipulation.trimIndent("int B = 12;\n" + + "int C = B - 1;\n" + + "int K = 99;\n" + + "int f1 = K - 1 - C;\n" + + "int f2 = K - C - C - C;" , 0, 0, 0)); + + } catch (IllegalArgumentException e) { + assertTrue("Should not happen", false); + } +} + +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=285565 +//To make sure that getChangeIndentEdits no longer throws an illegal argument exception with indentWidth set to 0. +public void testBug285565e() { + try { + IndentManipulation.getChangeIndentEdits("int B = 12;\n" + + "int C = B - 1;\n" + + "int K = 99;\n" + + "int f1 = K - 1 - C;\n" + + "int f2 = K - C - C - C;", 0, 0, 0, " "); + + } catch (IllegalArgumentException e) { + assertTrue("Should not happen", false); + } +} + } #P org.eclipse.jdt.core Index: formatter/org/eclipse/jdt/core/formatter/IndentManipulation.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/IndentManipulation.java,v retrieving revision 1.9 diff -u -r1.9 IndentManipulation.java --- formatter/org/eclipse/jdt/core/formatter/IndentManipulation.java 14 Jul 2009 19:54:26 -0000 1.9 +++ formatter/org/eclipse/jdt/core/formatter/IndentManipulation.java 13 Aug 2009 09:53:43 -0000 @@ -109,8 +109,7 @@ for (int i= 0; i < max; i++) { char ch= line.charAt(i); if (ch == '\t') { - int reminder= length % tabWidth; - length += tabWidth - reminder; + length = calculateSpaceEquivalents(tabWidth, length); } else if (isIndentChar(ch)) { length++; } else { @@ -131,26 +130,25 @@ * @return the indent part of line, but no odd spaces * @exception IllegalArgumentException if: * */ public static String extractIndentString(String line, int tabWidth, int indentWidth) { - if (tabWidth < 0 || indentWidth <= 0 || line == null) { + if (tabWidth < 0 || indentWidth < 0 || line == null) { throw new IllegalArgumentException(); } - int size= line.length(); - int end= 0; + int size = line.length(); + int end = 0; - int spaceEquivs= 0; - int characters= 0; - for (int i= 0; i < size; i++) { - char c= line.charAt(i); + int spaceEquivs = 0; + int characters = 0; + for (int i = 0; i < size; i++) { + char c = line.charAt(i); if (c == '\t') { - int remainder= spaceEquivs % tabWidth; - spaceEquivs += tabWidth - remainder; + spaceEquivs = calculateSpaceEquivalents(tabWidth, spaceEquivs); characters++; } else if (isIndentChar(c)) { spaceEquivs++; @@ -160,8 +158,12 @@ } if (spaceEquivs >= indentWidth) { end += characters; - characters= 0; - spaceEquivs= spaceEquivs % indentWidth; + characters = 0; + if(indentWidth == 0) { + spaceEquivs = 0; + } else { + spaceEquivs = spaceEquivs % indentWidth; + } } } if (end == 0) { @@ -176,8 +178,8 @@ /** * Removes the given number of indentation units from a given line. If the line - * has less than the given indent, all the available indentation is removed. - * If indentsToRemove <= 0 the line is returned. + * has less indent than the given indentUnitsToRemove, all the available indentation is removed. + * If indentsToRemove <= 0 or indent == 0 the line is returned. * * @param line the line to trim * @param tabWidth the width of one tab in space equivalents @@ -185,19 +187,19 @@ * @return the trimmed string * @exception IllegalArgumentException if: * */ public static String trimIndent(String line, int indentUnitsToRemove, int tabWidth, int indentWidth) { - if (tabWidth < 0 || indentWidth <= 0 || line == null) { + if (tabWidth < 0 || indentWidth < 0 || line == null) { throw new IllegalArgumentException(); } - if (indentUnitsToRemove <= 0) + if (indentUnitsToRemove <= 0 || indentWidth == 0) { return line; - + } final int spaceEquivalentsToRemove= indentUnitsToRemove * indentWidth; int start= 0; @@ -207,8 +209,7 @@ for (int i= 0; i < size; i++) { char c= line.charAt(i); if (c == '\t') { - int remainder= spaceEquivalents % tabWidth; - spaceEquivalents += tabWidth - remainder; + spaceEquivalents = calculateSpaceEquivalents(tabWidth, spaceEquivalents); } else if (isIndentChar(c)) { spaceEquivalents++; } else { @@ -257,7 +258,7 @@ * @return the newly indent code, containing only the given line delimiters. * @exception IllegalArgumentException if: * */ public static String changeIndent(String code, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString, String lineDelim) { - if (tabWidth < 0 || indentWidth <= 0 || code == null || indentUnitsToRemove < 0 || newIndentString == null || lineDelim == null) { + if (tabWidth < 0 || indentWidth < 0 || code == null || indentUnitsToRemove < 0 || newIndentString == null || lineDelim == null) { throw new IllegalArgumentException(); } @@ -291,7 +292,11 @@ } else { // no new line after last line buf.append(lineDelim); buf.append(newIndentString); - buf.append(trimIndent(line, indentUnitsToRemove, tabWidth, indentWidth)); + if(indentWidth != 0) { + buf.append(trimIndent(line, indentUnitsToRemove, tabWidth, indentWidth)); + } else { + buf.append(line); + } } } return buf.toString(); @@ -316,7 +321,7 @@ * @return returns the resulting text edits * @exception IllegalArgumentException if: * */ public static ReplaceEdit[] getChangeIndentEdits(String source, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString) { - if (tabWidth < 0 || indentWidth <= 0 || source == null || indentUnitsToRemove < 0 || newIndentString == null) { + if (tabWidth < 0 || indentWidth < 0 || source == null || indentUnitsToRemove < 0 || newIndentString == null) { throw new IllegalArgumentException(); } @@ -368,8 +373,7 @@ for (int i= 0; i < size && blanks < spaceEquivalents; i++) { char c= line.charAt(i); if (c == '\t') { - int remainder= blanks % tabWidth; - blanks += tabWidth - remainder; + blanks = calculateSpaceEquivalents(tabWidth, blanks); } else if (isIndentChar(c)) { blanks++; } else { @@ -382,6 +386,18 @@ return result + 1; } + /* + * Calculates space equivalents up to the next tab stop + */ + private static int calculateSpaceEquivalents(int tabWidth, int spaceEquivalents) { + if (tabWidth == 0){ + return spaceEquivalents; + } + int remainder = spaceEquivalents % tabWidth; + spaceEquivalents += tabWidth - remainder; + return spaceEquivalents; + } + /** * Returns the tab width as configured in the given map. *

Use {@link org.eclipse.jdt.core.IJavaProject#getOptions(boolean)} to get the most current project options.

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.169 diff -u -r1.169 Scribe.java --- formatter/org/eclipse/jdt/internal/formatter/Scribe.java 20 Jul 2009 15:10:59 -0000 1.169 +++ formatter/org/eclipse/jdt/internal/formatter/Scribe.java 13 Aug 2009 09:53:45 -0000 @@ -929,6 +929,9 @@ if (this.useTabsOnlyForLeadingIndents) { return indent; } + if (this.indentationSize == 0) { + return indent; + } int rem = indent % this.indentationSize; int addition = rem == 0 ? 0 : this.indentationSize - rem; // round to superior return indent + addition; @@ -2160,7 +2163,12 @@ int indentLevel = this.indentationLevel; int indentations = this.numberOfIndentations; this.indentationLevel = getNextIndentationLevel(firstColumn); - this.numberOfIndentations = this.indentationLevel / this.indentationSize; + if (this.indentationSize != 0) { + this.numberOfIndentations = this.indentationLevel / this.indentationSize; + } + else{ + this.numberOfIndentations = 0; + } // Consume the comment prefix this.scanner.resetTo(commentStart, commentEnd); @@ -2339,13 +2347,19 @@ boolean useTabsForLeadingIndents = this.useTabsOnlyForLeadingIndents; int numberOfLeadingIndents = this.numberOfIndentations; int indentationsAsTab = 0; + int complement = 0; if (useTabsForLeadingIndents) { while (this.column <= this.indentationLevel) { if (indentationsAsTab < numberOfLeadingIndents) { if (buffer != null) buffer.append('\t'); indentationsAsTab++; - int complement = this.tabLength - ((this.column - 1) % this.tabLength); // amount of space - this.column += complement; + if(this.tabLength != 0) { + complement = this.tabLength - ((this.column - 1) % this.tabLength); // amount of space + this.column += complement; + } + else { + this.column++; + } this.needSpace = false; } else { if (buffer != null) buffer.append(' '); @@ -2356,8 +2370,13 @@ } else { while (this.column <= this.indentationLevel) { if (buffer != null) buffer.append('\t'); - int complement = this.tabLength - ((this.column - 1) % this.tabLength); // amount of space - this.column += complement; + if(this.tabLength != 0) { + complement = this.tabLength - ((this.column - 1) % this.tabLength); // amount of space + this.column += complement; + } + else { + this.column++; + } this.needSpace = false; } }