View | Details | Raw Unified | Return to bug 285565 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java (+56 lines)
Lines 10849-10852 Link Here
10849
	DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences, compilerOptions);
10849
	DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences, compilerOptions);
10850
	runTest(codeFormatter, "test723", "A.java", CodeFormatter.K_COMPILATION_UNIT, false);//$NON-NLS-1$ //$NON-NLS-2$
10850
	runTest(codeFormatter, "test723", "A.java", CodeFormatter.K_COMPILATION_UNIT, false);//$NON-NLS-1$ //$NON-NLS-2$
10851
}
10851
}
10852
10853
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=285565
10854
public void testBug285565a() {
10855
	try {
10856
		assertEquals("Should be 0", 0, IndentManipulation.measureIndentInSpaces("", 0));
10857
		assertEquals("Should be 0", 0, IndentManipulation.measureIndentInSpaces("\t", 0));
10858
		assertEquals("Should be 1", 1, IndentManipulation.measureIndentInSpaces("\t ", 0));
10859
		assertEquals("Should be blank", "\t", IndentManipulation.extractIndentString("\tabc", 0, 0));
10860
	} catch (IllegalArgumentException e) {
10861
		assertTrue("Should not happen", false);
10862
	}
10863
}
10864
10865
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=285565
10866
public void testBug285565b() {
10867
	this.formatterPrefs.indentation_size = 0;
10868
	this.formatterPrefs.tab_size = 0;
10869
	String source = "public class test {\n"
10870
			+ "    public static void main(String[] args) {\n"
10871
			+ "        int B= 12;\n" 
10872
			+ "        int C= B - 1;\n"
10873
			+ "        int K= 99;\n" 
10874
			+ "        int f1= K - 1 - C;\n"
10875
			+ "        int f2= K - C - C - C;\n" 
10876
			+ "    }\n" + "}\n";
10877
	formatSource(source, "public class test {\n"
10878
			+ "public static void main(String[] args) {\n"
10879
			+ "int B = 12;\n" 
10880
			+ "int C = B - 1;\n" 
10881
			+ "int K = 99;\n"
10882
			+ "int f1 = K - 1 - C;\n" 
10883
			+ "int f2 = K - C - C - C;\n" 
10884
			+ "}\n"
10885
			+ "}\n");
10886
}
10887
10888
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=285565
10889
public void testBug285565c() {
10890
	String result = "int B = 12;\n" 
10891
		+ " int C = B - 1;\n" 
10892
		+ " int K = 99;\n"
10893
		+ " int f1 = K - 1 - C;\n" 
10894
		+ " int f2 = K - C - C - C;" ;
10895
		
10896
	try {
10897
		assertEquals("Should be as shown", result, IndentManipulation.changeIndent("int B = 12;\n" 
10898
			+ "int C = B - 1;\n" 
10899
			+ "int K = 99;\n"
10900
			+ "int f1 = K - 1 - C;\n" 
10901
			+ "int f2 = K - C - C - C;" ,0,0,0, " ","\n"));
10902
		
10903
	} catch (IllegalArgumentException e) {
10904
		assertTrue("Should not happen", false);
10905
	}
10906
}
10907
10852
}
10908
}
(-)formatter/org/eclipse/jdt/core/formatter/IndentManipulation.java (-18 / +29 lines)
Lines 109-116 Link Here
109
		for (int i= 0; i < max; i++) {
109
		for (int i= 0; i < max; i++) {
110
			char ch= line.charAt(i);
110
			char ch= line.charAt(i);
111
			if (ch == '\t') {
111
			if (ch == '\t') {
112
				int reminder= length % tabWidth;
112
				length = calculateSpaceEquivalents(tabWidth, length);
113
				length += tabWidth - reminder;
114
			} else if (isIndentChar(ch)) {
113
			} else if (isIndentChar(ch)) {
115
				length++;
114
				length++;
116
			} else {
115
			} else {
Lines 131-143 Link Here
131
	 * @return the indent part of <code>line</code>, but no odd spaces
130
	 * @return the indent part of <code>line</code>, but no odd spaces
132
	 * @exception IllegalArgumentException if:
131
	 * @exception IllegalArgumentException if:
133
	 * <ul>
132
	 * <ul>
134
	 * <li>the given <code>indentWidth</code> is lower or equals to zero</li>
133
	 * <li>the given <code>indentWidth</code> is lower than zero</li>
135
	 * <li>the given <code>tabWidth</code> is lower than zero</li>
134
	 * <li>the given <code>tabWidth</code> is lower than zero</li>
136
	 * <li>the given <code>line</code> is null</li>
135
	 * <li>the given <code>line</code> is null</li>
137
	 * </ul>
136
	 * </ul>
138
	 */
137
	 */
139
	public static String extractIndentString(String line, int tabWidth, int indentWidth) {
138
	public static String extractIndentString(String line, int tabWidth, int indentWidth) {
140
		if (tabWidth < 0 || indentWidth <= 0 || line == null) {
139
		if (tabWidth < 0 || indentWidth < 0 || line == null) {
141
			throw new IllegalArgumentException();
140
			throw new IllegalArgumentException();
142
		}
141
		}
143
142
Lines 149-156 Link Here
149
		for (int i= 0; i < size; i++) {
148
		for (int i= 0; i < size; i++) {
150
			char c= line.charAt(i);
149
			char c= line.charAt(i);
151
			if (c == '\t') {
150
			if (c == '\t') {
152
				int remainder= spaceEquivs % tabWidth;
151
				spaceEquivs = calculateSpaceEquivalents(tabWidth, spaceEquivs);
153
				spaceEquivs += tabWidth - remainder;
154
				characters++;
152
				characters++;
155
			} else if (isIndentChar(c)) {
153
			} else if (isIndentChar(c)) {
156
				spaceEquivs++;
154
				spaceEquivs++;
Lines 161-167 Link Here
161
			if (spaceEquivs >= indentWidth) {
159
			if (spaceEquivs >= indentWidth) {
162
				end += characters;
160
				end += characters;
163
				characters= 0;
161
				characters= 0;
164
				spaceEquivs= spaceEquivs % indentWidth;
162
				if(indentWidth == 0) spaceEquivs= 0;
163
				else spaceEquivs = spaceEquivs = spaceEquivs % indentWidth;
165
			}
164
			}
166
		}
165
		}
167
		if (end == 0) {
166
		if (end == 0) {
Lines 191-201 Link Here
191
	 * </ul>
190
	 * </ul>
192
	 */
191
	 */
193
	public static String trimIndent(String line, int indentUnitsToRemove, int tabWidth, int indentWidth) {
192
	public static String trimIndent(String line, int indentUnitsToRemove, int tabWidth, int indentWidth) {
194
		if (tabWidth < 0 || indentWidth <= 0 || line == null) {
193
		if (tabWidth < 0 || indentWidth < 0 || (indentWidth==0 && indentUnitsToRemove >0) || line == null) {
195
			throw new IllegalArgumentException();
194
			throw new IllegalArgumentException();
196
		}
195
		}
197
196
198
		if (indentUnitsToRemove <= 0)
197
		if (indentUnitsToRemove <= 0||indentWidth == 0)
199
			return line;
198
			return line;
200
199
201
		final int spaceEquivalentsToRemove= indentUnitsToRemove * indentWidth;
200
		final int spaceEquivalentsToRemove= indentUnitsToRemove * indentWidth;
Lines 207-214 Link Here
207
		for (int i= 0; i < size; i++) {
206
		for (int i= 0; i < size; i++) {
208
			char c= line.charAt(i);
207
			char c= line.charAt(i);
209
			if (c == '\t') {
208
			if (c == '\t') {
210
				int remainder= spaceEquivalents % tabWidth;
209
				spaceEquivalents = calculateSpaceEquivalents(tabWidth, spaceEquivalents);
211
				spaceEquivalents += tabWidth - remainder;
212
			} else if (isIndentChar(c)) {
210
			} else if (isIndentChar(c)) {
213
				spaceEquivalents++;
211
				spaceEquivalents++;
214
			} else {
212
			} else {
Lines 257-272 Link Here
257
	 * @return the newly indent code, containing only the given line delimiters.
255
	 * @return the newly indent code, containing only the given line delimiters.
258
	 * @exception IllegalArgumentException if:
256
	 * @exception IllegalArgumentException if:
259
	 * <ul>
257
	 * <ul>
260
	 * <li>the given <code>indentWidth</code> is lower or equals to zero</li>
258
	 * <li>the given <code>indentWidth</code> is lower than zero</li>
261
	 * <li>the given <code>tabWidth</code> is lower than zero</li>
259
	 * <li>the given <code>tabWidth</code> is lower than zero</li>
262
	 * <li>the given <code>code</code> is null</li>
260
	 * <li>the given <code>code</code> is null</li>
263
	 * <li>the given <code>indentUnitsToRemove</code> is lower than zero</li>
261
	 * <li>the given <code>indentUnitsToRemove</code> is lower than zero</li>
262
	 *  <li>the given <code>indentUnitsToRemove</code> is greater than zero when indentWidth equals zero</li>
264
	 * <li>the given <code>newIndentString</code> is null</li>
263
	 * <li>the given <code>newIndentString</code> is null</li>
265
	 * <li>the given <code>lineDelim</code> is null</li>
264
	 * <li>the given <code>lineDelim</code> is null</li>
266
	 * </ul>
265
	 * </ul>
267
	 */
266
	 */
268
	public static String changeIndent(String code, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString, String lineDelim) {
267
	public static String changeIndent(String code, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString, String lineDelim) {
269
		if (tabWidth < 0 || indentWidth <= 0 || code == null || indentUnitsToRemove < 0 || newIndentString == null || lineDelim == null) {
268
		if (tabWidth < 0 || indentWidth < 0 || code == null || indentUnitsToRemove < 0 || (indentWidth == 0 && indentUnitsToRemove>0) || newIndentString == null || lineDelim == null) {
270
			throw new IllegalArgumentException();
269
			throw new IllegalArgumentException();
271
		}
270
		}
272
271
Lines 291-297 Link Here
291
				} else { // no new line after last line
290
				} else { // no new line after last line
292
					buf.append(lineDelim);
291
					buf.append(lineDelim);
293
					buf.append(newIndentString);
292
					buf.append(newIndentString);
294
					buf.append(trimIndent(line, indentUnitsToRemove, tabWidth, indentWidth));
293
					if(indentWidth!=0) buf.append(trimIndent(line, indentUnitsToRemove, tabWidth, indentWidth));
294
					else buf.append(line);
295
				}
295
				}
296
			}
296
			}
297
			return buf.toString();
297
			return buf.toString();
Lines 316-330 Link Here
316
	 * @return returns the resulting text edits
316
	 * @return returns the resulting text edits
317
	 * @exception IllegalArgumentException if:
317
	 * @exception IllegalArgumentException if:
318
	 * <ul>
318
	 * <ul>
319
	 * <li>the given <code>indentWidth</code> is lower or equals to zero</li>
319
	 * <li>the given <code>indentWidth</code> is lower than zero</li>
320
	 * <li>the given <code>tabWidth</code> is lower than zero</li>
320
	 * <li>the given <code>tabWidth</code> is lower than zero</li>
321
	 * <li>the given <code>source</code> is null</li>
321
	 * <li>the given <code>source</code> is null</li>
322
	 * <li>the given <code>indentUnitsToRemove</code> is lower than zero</li>
322
	 * <li>the given <code>indentUnitsToRemove</code> is lower than zero</li>
323
	 * <li>the given <code>indentUnitsToRemove</code> is greater than zero when indentWidth equals zero</li>
323
	 * <li>the given <code>newIndentString</code> is null</li>
324
	 * <li>the given <code>newIndentString</code> is null</li>
324
	 * </ul>
325
	 * </ul>
325
	 */
326
	 */
326
	public static ReplaceEdit[] getChangeIndentEdits(String source, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString) {
327
	public static ReplaceEdit[] getChangeIndentEdits(String source, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString) {
327
		if (tabWidth < 0 || indentWidth <= 0 || source == null || indentUnitsToRemove < 0 || newIndentString == null) {
328
		if (tabWidth < 0 || indentWidth < 0 || source == null || indentUnitsToRemove < 0 || (indentWidth == 0 && indentUnitsToRemove >0) || newIndentString == null) {
328
			throw new IllegalArgumentException();
329
			throw new IllegalArgumentException();
329
		}
330
		}
330
331
Lines 368-375 Link Here
368
		for (int i= 0; i < size && blanks < spaceEquivalents; i++) {
369
		for (int i= 0; i < size && blanks < spaceEquivalents; i++) {
369
			char c= line.charAt(i);
370
			char c= line.charAt(i);
370
			if (c == '\t') {
371
			if (c == '\t') {
371
				int remainder= blanks % tabWidth;
372
				blanks = calculateSpaceEquivalents(tabWidth, blanks);
372
				blanks += tabWidth - remainder;
373
			} else if (isIndentChar(c)) {
373
			} else if (isIndentChar(c)) {
374
				blanks++;
374
				blanks++;
375
			} else {
375
			} else {
Lines 382-387 Link Here
382
		return result + 1;
382
		return result + 1;
383
	}
383
	}
384
384
385
	/*
386
	 * Calculates space equivalents upto the next tab stop
387
	 */
388
	private static int calculateSpaceEquivalents(int tabWidth, int spaceEquivalents) {
389
		if (tabWidth == 0)
390
			return spaceEquivalents;
391
		int remainder = spaceEquivalents % tabWidth;
392
		spaceEquivalents += tabWidth - remainder;
393
		return spaceEquivalents;
394
	}
395
385
	/**
396
	/**
386
	 * Returns the tab width as configured in the given map.
397
	 * Returns the tab width as configured in the given map.
387
	 * <p>Use {@link org.eclipse.jdt.core.IJavaProject#getOptions(boolean)} to get the most current project options.</p>
398
	 * <p>Use {@link org.eclipse.jdt.core.IJavaProject#getOptions(boolean)} to get the most current project options.</p>
(-)formatter/org/eclipse/jdt/internal/formatter/Scribe.java (-1 / +6 lines)
Lines 929-934 Link Here
929
			if (this.useTabsOnlyForLeadingIndents) {
929
			if (this.useTabsOnlyForLeadingIndents) {
930
				return indent;
930
				return indent;
931
			}
931
			}
932
			if (this.indentationSize == 0)
933
				return indent;
932
			int rem = indent % this.indentationSize;
934
			int rem = indent % this.indentationSize;
933
			int addition = rem == 0 ? 0 : this.indentationSize - rem; // round to superior
935
			int addition = rem == 0 ? 0 : this.indentationSize - rem; // round to superior
934
			return indent + addition;
936
			return indent + addition;
Lines 2160-2166 Link Here
2160
		int indentLevel = this.indentationLevel;
2162
		int indentLevel = this.indentationLevel;
2161
		int indentations = this.numberOfIndentations;
2163
		int indentations = this.numberOfIndentations;
2162
		this.indentationLevel = getNextIndentationLevel(firstColumn);
2164
		this.indentationLevel = getNextIndentationLevel(firstColumn);
2163
		this.numberOfIndentations = this.indentationLevel / this.indentationSize;
2165
		if (this.indentationSize != 0)
2166
			this.numberOfIndentations = this.indentationLevel / this.indentationSize;
2167
		else
2168
			this.numberOfIndentations = 0;
2164
2169
2165
		// Consume the comment prefix
2170
		// Consume the comment prefix
2166
		this.scanner.resetTo(commentStart, commentEnd);
2171
		this.scanner.resetTo(commentStart, commentEnd);

Return to bug 285565