### 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 11 Aug 2009 08:31:36 -0000 @@ -10849,4 +10849,60 @@ 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 +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 +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 +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); + } +} + } #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 11 Aug 2009 08:31:41 -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,13 +130,13 @@ * @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(); } @@ -149,8 +148,7 @@ 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++; @@ -161,7 +159,8 @@ if (spaceEquivs >= indentWidth) { end += characters; characters= 0; - spaceEquivs= spaceEquivs % indentWidth; + if(indentWidth == 0) spaceEquivs= 0; + else spaceEquivs = spaceEquivs = spaceEquivs % indentWidth; } } if (end == 0) { @@ -191,11 +190,11 @@ * */ public static String trimIndent(String line, int indentUnitsToRemove, int tabWidth, int indentWidth) { - if (tabWidth < 0 || indentWidth <= 0 || line == null) { + if (tabWidth < 0 || indentWidth < 0 || (indentWidth==0 && indentUnitsToRemove >0) || line == null) { throw new IllegalArgumentException(); } - if (indentUnitsToRemove <= 0) + if (indentUnitsToRemove <= 0||indentWidth == 0) return line; final int spaceEquivalentsToRemove= indentUnitsToRemove * indentWidth; @@ -207,8 +206,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,16 +255,17 @@ * @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 || (indentWidth == 0 && indentUnitsToRemove>0) || newIndentString == null || lineDelim == null) { throw new IllegalArgumentException(); } @@ -291,7 +290,8 @@ } 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,15 +316,16 @@ * @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 || (indentWidth == 0 && indentUnitsToRemove >0) || newIndentString == null) { throw new IllegalArgumentException(); } @@ -368,8 +369,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 +382,17 @@ return result + 1; } + /* + * Calculates space equivalents upto 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 11 Aug 2009 08:31:45 -0000 @@ -929,6 +929,8 @@ 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 +2162,10 @@ 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);