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 (+95 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
//Test to make sure that measureIndentInSpaces and extractIndentString do not throw illegalArgumentException with indentWidth set to zero.
10855
public void testBug285565a() {
10856
	try {
10857
		assertEquals("Should be 0", 0, IndentManipulation.measureIndentInSpaces("", 0));
10858
		assertEquals("Should be 0", 0, IndentManipulation.measureIndentInSpaces("\t", 0));
10859
		assertEquals("Should be 1", 1, IndentManipulation.measureIndentInSpaces("\t ", 0));
10860
		assertEquals("Should be blank", "\t", IndentManipulation.extractIndentString("\tabc", 0, 0));
10861
	} catch (IllegalArgumentException e) {
10862
		assertTrue("Should not happen", false);
10863
	}
10864
}
10865
10866
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=285565
10867
//Test to make sure that a divide by zero exception isn't thrown when formatting with indent width and tab width set to zero
10868
public void testBug285565b() {		
10869
	this.formatterPrefs.indentation_size = 0;
10870
	this.formatterPrefs.tab_size = 0;
10871
	String source = "public class test {\n"
10872
			+ "    public static void main(String[] args) {\n"
10873
			+ "        int B= 12;\n" 
10874
			+ "        int C= B - 1;\n"
10875
			+ "        int K= 99;\n" 
10876
			+ "        int f1= K - 1 - C;\n"
10877
			+ "        int f2= K - C - C - C;\n" 
10878
			+ "    }\n" + "}\n";
10879
	formatSource(source, "public class test {\n"
10880
			+ "public static void main(String[] args) {\n"
10881
			+ "int B = 12;\n" 
10882
			+ "int C = B - 1;\n" 
10883
			+ "int K = 99;\n"
10884
			+ "int f1 = K - 1 - C;\n" 
10885
			+ "int f2 = K - C - C - C;\n" 
10886
			+ "}\n"
10887
			+ "}\n");
10888
}
10889
10890
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=285565
10891
//To make sure that changeIndent no longer throws an illegal argument exception with indentWidth set to 0.
10892
public void testBug285565c() {
10893
	String result = "int B = 12;\n" 
10894
		+ " int C = B - 1;\n" 
10895
		+ " int K = 99;\n"
10896
		+ " int f1 = K - 1 - C;\n" 
10897
		+ " int f2 = K - C - C - C;" ;
10898
		
10899
	try {
10900
		assertEquals("Should be as shown", result, IndentManipulation.changeIndent("int B = 12;\n" 
10901
			+ "int C = B - 1;\n" 
10902
			+ "int K = 99;\n"
10903
			+ "int f1 = K - 1 - C;\n" 
10904
			+ "int f2 = K - C - C - C;" ,0,0,0, " ","\n"));
10905
		
10906
	} catch (IllegalArgumentException e) {
10907
		assertTrue("Should not happen", false);
10908
	}
10909
}
10910
10911
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=285565
10912
//To make sure that trimIndent no longer throws an illegal argument exception with indentWidth set to 0.
10913
public void testBug285565d() {
10914
	String result = "int B = 12;\n" 
10915
		+ "int C = B - 1;\n" 
10916
		+ "int K = 99;\n"
10917
		+ "int f1 = K - 1 - C;\n" 
10918
		+ "int f2 = K - C - C - C;" ;
10919
		
10920
	try {
10921
		assertEquals("Should be as shown", result, IndentManipulation.trimIndent("int B = 12;\n" 
10922
			+ "int C = B - 1;\n" 
10923
			+ "int K = 99;\n"
10924
			+ "int f1 = K - 1 - C;\n" 
10925
			+ "int f2 = K - C - C - C;" , 0, 0, 0));
10926
		
10927
	} catch (IllegalArgumentException e) {
10928
		assertTrue("Should not happen", false);
10929
	}
10930
}
10931
10932
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=285565
10933
//To make sure that getChangeIndentEdits no longer throws an illegal argument exception with indentWidth set to 0.
10934
public void testBug285565e() {		
10935
	try {
10936
		IndentManipulation.getChangeIndentEdits("int B = 12;\n" 
10937
			+ "int C = B - 1;\n" 
10938
			+ "int K = 99;\n"
10939
			+ "int f1 = K - 1 - C;\n" 
10940
			+ "int f2 = K - C - C - C;", 0, 0, 0, " ");
10941
		
10942
	} catch (IllegalArgumentException e) {
10943
		assertTrue("Should not happen", false);
10944
	}
10945
}
10946
10852
}
10947
}
(-)formatter/org/eclipse/jdt/internal/formatter/Scribe.java (-5 / +24 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;
934
			}
932
			int rem = indent % this.indentationSize;
935
			int rem = indent % this.indentationSize;
933
			int addition = rem == 0 ? 0 : this.indentationSize - rem; // round to superior
936
			int addition = rem == 0 ? 0 : this.indentationSize - rem; // round to superior
934
			return indent + addition;
937
			return indent + addition;
Lines 2160-2166 Link Here
2160
		int indentLevel = this.indentationLevel;
2163
		int indentLevel = this.indentationLevel;
2161
		int indentations = this.numberOfIndentations;
2164
		int indentations = this.numberOfIndentations;
2162
		this.indentationLevel = getNextIndentationLevel(firstColumn);
2165
		this.indentationLevel = getNextIndentationLevel(firstColumn);
2163
		this.numberOfIndentations = this.indentationLevel / this.indentationSize;
2166
		if (this.indentationSize != 0) {
2167
			this.numberOfIndentations = this.indentationLevel / this.indentationSize;
2168
		}
2169
		else{
2170
			this.numberOfIndentations = 0;
2171
		}
2164
2172
2165
		// Consume the comment prefix
2173
		// Consume the comment prefix
2166
		this.scanner.resetTo(commentStart, commentEnd);
2174
		this.scanner.resetTo(commentStart, commentEnd);
Lines 2339-2351 Link Here
2339
				boolean useTabsForLeadingIndents = this.useTabsOnlyForLeadingIndents;
2347
				boolean useTabsForLeadingIndents = this.useTabsOnlyForLeadingIndents;
2340
				int numberOfLeadingIndents = this.numberOfIndentations;
2348
				int numberOfLeadingIndents = this.numberOfIndentations;
2341
				int indentationsAsTab = 0;
2349
				int indentationsAsTab = 0;
2350
				int complement = 0;
2342
				if (useTabsForLeadingIndents) {
2351
				if (useTabsForLeadingIndents) {
2343
					while (this.column <= this.indentationLevel) {
2352
					while (this.column <= this.indentationLevel) {
2344
						if (indentationsAsTab < numberOfLeadingIndents) {
2353
						if (indentationsAsTab < numberOfLeadingIndents) {
2345
							if (buffer != null) buffer.append('\t');
2354
							if (buffer != null) buffer.append('\t');
2346
							indentationsAsTab++;
2355
							indentationsAsTab++;
2347
							int complement = this.tabLength - ((this.column - 1) % this.tabLength); // amount of space
2356
							if(this.tabLength != 0) {
2348
							this.column += complement;
2357
								complement = this.tabLength - ((this.column - 1) % this.tabLength); // amount of space
2358
								this.column += complement;
2359
							}
2360
							else {
2361
								this.column++;
2362
							}
2349
							this.needSpace = false;
2363
							this.needSpace = false;
2350
						} else {
2364
						} else {
2351
							if (buffer != null) buffer.append(' ');
2365
							if (buffer != null) buffer.append(' ');
Lines 2356-2363 Link Here
2356
				} else {
2370
				} else {
2357
					while (this.column <= this.indentationLevel) {
2371
					while (this.column <= this.indentationLevel) {
2358
						if (buffer != null) buffer.append('\t');
2372
						if (buffer != null) buffer.append('\t');
2359
						int complement = this.tabLength - ((this.column - 1) % this.tabLength); // amount of space
2373
						if(this.tabLength != 0) {
2360
						this.column += complement;
2374
							complement = this.tabLength - ((this.column - 1) % this.tabLength); // amount of space
2375
							this.column += complement;
2376
						}
2377
						else {
2378
							this.column++;
2379
						}
2361
						this.needSpace = false;
2380
						this.needSpace = false;
2362
					}
2381
					}
2363
				}
2382
				}
(-)formatter/org/eclipse/jdt/core/formatter/IndentManipulation.java (-29 / +47 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-156 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
144
		int size= line.length();
143
		int size = line.length();
145
		int end= 0;
144
		int end = 0;
146
145
147
		int spaceEquivs= 0;
146
		int spaceEquivs = 0;
148
		int characters= 0;
147
		int characters = 0;
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 160-167 Link Here
160
			}
158
			}
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) {
163
					spaceEquivs = 0;
164
				} else {
165
					spaceEquivs = spaceEquivs % indentWidth;
166
				}
165
			}
167
			}
166
		}
168
		}
167
		if (end == 0) {
169
		if (end == 0) {
Lines 176-183 Link Here
176
178
177
	/**
179
	/**
178
	 * Removes the given number of indentation units from a given line. If the line
180
	 * Removes the given number of indentation units from a given line. If the line
179
	 * has less than the given indent, all the available indentation is removed.
181
	 * has less indent than the given indentUnitsToRemove, all the available indentation is removed.
180
	 * If <code>indentsToRemove <= 0</code> the line is returned.
182
	 * If <code>indentsToRemove <= 0 or indent == 0</code> the line is returned.
181
	 *
183
	 *
182
	 * @param line the line to trim
184
	 * @param line the line to trim
183
	 * @param tabWidth the width of one tab in space equivalents
185
	 * @param tabWidth the width of one tab in space equivalents
Lines 185-203 Link Here
185
	 * @return the trimmed string
187
	 * @return the trimmed string
186
	 * @exception IllegalArgumentException if:
188
	 * @exception IllegalArgumentException if:
187
	 * <ul>
189
	 * <ul>
188
	 * <li>the given <code>indentWidth</code> is lower or equals to zero</li>
190
	 * <li>the given <code>indentWidth</code> is lower than zero</li>
189
	 * <li>the given <code>tabWidth</code> is lower than zero</li>
191
	 * <li>the given <code>tabWidth</code> is lower than zero</li>
190
	 * <li>the given <code>line</code> is null</li>
192
	 * <li>the given <code>line</code> is null</li>
191
	 * </ul>
193
	 * </ul>
192
	 */
194
	 */
193
	public static String trimIndent(String line, int indentUnitsToRemove, int tabWidth, int indentWidth) {
195
	public static String trimIndent(String line, int indentUnitsToRemove, int tabWidth, int indentWidth) {
194
		if (tabWidth < 0 || indentWidth <= 0 || line == null) {
196
		if (tabWidth < 0 || indentWidth < 0 || line == null) {
195
			throw new IllegalArgumentException();
197
			throw new IllegalArgumentException();
196
		}
198
		}
197
199
198
		if (indentUnitsToRemove <= 0)
200
		if (indentUnitsToRemove <= 0 || indentWidth == 0) {
199
			return line;
201
			return line;
200
202
		}
201
		final int spaceEquivalentsToRemove= indentUnitsToRemove * indentWidth;
203
		final int spaceEquivalentsToRemove= indentUnitsToRemove * indentWidth;
202
204
203
		int start= 0;
205
		int start= 0;
Lines 207-214 Link Here
207
		for (int i= 0; i < size; i++) {
209
		for (int i= 0; i < size; i++) {
208
			char c= line.charAt(i);
210
			char c= line.charAt(i);
209
			if (c == '\t') {
211
			if (c == '\t') {
210
				int remainder= spaceEquivalents % tabWidth;
212
				spaceEquivalents = calculateSpaceEquivalents(tabWidth, spaceEquivalents);
211
				spaceEquivalents += tabWidth - remainder;
212
			} else if (isIndentChar(c)) {
213
			} else if (isIndentChar(c)) {
213
				spaceEquivalents++;
214
				spaceEquivalents++;
214
			} else {
215
			} else {
Lines 257-272 Link Here
257
	 * @return the newly indent code, containing only the given line delimiters.
258
	 * @return the newly indent code, containing only the given line delimiters.
258
	 * @exception IllegalArgumentException if:
259
	 * @exception IllegalArgumentException if:
259
	 * <ul>
260
	 * <ul>
260
	 * <li>the given <code>indentWidth</code> is lower or equals to zero</li>
261
	 * <li>the given <code>indentWidth</code> is lower than zero</li>
261
	 * <li>the given <code>tabWidth</code> is lower than zero</li>
262
	 * <li>the given <code>tabWidth</code> is lower than zero</li>
262
	 * <li>the given <code>code</code> is null</li>
263
	 * <li>the given <code>code</code> is null</li>
263
	 * <li>the given <code>indentUnitsToRemove</code> is lower than zero</li>
264
	 * <li>the given <code>indentUnitsToRemove</code> is lower than zero</li>
265
	 *  <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>
266
	 * <li>the given <code>newIndentString</code> is null</li>
265
	 * <li>the given <code>lineDelim</code> is null</li>
267
	 * <li>the given <code>lineDelim</code> is null</li>
266
	 * </ul>
268
	 * </ul>
267
	 */
269
	 */
268
	public static String changeIndent(String code, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString, String lineDelim) {
270
	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) {
271
		if (tabWidth < 0 || indentWidth < 0 || code == null || indentUnitsToRemove < 0 || (indentWidth == 0 && indentUnitsToRemove > 0) || newIndentString == null || lineDelim == null) {
270
			throw new IllegalArgumentException();
272
			throw new IllegalArgumentException();
271
		}
273
		}
272
274
Lines 291-297 Link Here
291
				} else { // no new line after last line
293
				} else { // no new line after last line
292
					buf.append(lineDelim);
294
					buf.append(lineDelim);
293
					buf.append(newIndentString);
295
					buf.append(newIndentString);
294
					buf.append(trimIndent(line, indentUnitsToRemove, tabWidth, indentWidth));
296
					if(indentWidth != 0) {
297
						buf.append(trimIndent(line, indentUnitsToRemove, tabWidth, indentWidth));
298
					} else {
299
						buf.append(line);
300
					}
295
				}
301
				}
296
			}
302
			}
297
			return buf.toString();
303
			return buf.toString();
Lines 316-330 Link Here
316
	 * @return returns the resulting text edits
322
	 * @return returns the resulting text edits
317
	 * @exception IllegalArgumentException if:
323
	 * @exception IllegalArgumentException if:
318
	 * <ul>
324
	 * <ul>
319
	 * <li>the given <code>indentWidth</code> is lower or equals to zero</li>
325
	 * <li>the given <code>indentWidth</code> is lower than zero</li>
320
	 * <li>the given <code>tabWidth</code> is lower than zero</li>
326
	 * <li>the given <code>tabWidth</code> is lower than zero</li>
321
	 * <li>the given <code>source</code> is null</li>
327
	 * <li>the given <code>source</code> is null</li>
322
	 * <li>the given <code>indentUnitsToRemove</code> is lower than zero</li>
328
	 * <li>the given <code>indentUnitsToRemove</code> is lower than zero</li>
329
	 * <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>
330
	 * <li>the given <code>newIndentString</code> is null</li>
324
	 * </ul>
331
	 * </ul>
325
	 */
332
	 */
326
	public static ReplaceEdit[] getChangeIndentEdits(String source, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString) {
333
	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) {
334
		if (tabWidth < 0 || indentWidth < 0 || source == null || indentUnitsToRemove < 0 || (indentWidth == 0 && indentUnitsToRemove > 0) || newIndentString == null) {
328
			throw new IllegalArgumentException();
335
			throw new IllegalArgumentException();
329
		}
336
		}
330
337
Lines 368-375 Link Here
368
		for (int i= 0; i < size && blanks < spaceEquivalents; i++) {
375
		for (int i= 0; i < size && blanks < spaceEquivalents; i++) {
369
			char c= line.charAt(i);
376
			char c= line.charAt(i);
370
			if (c == '\t') {
377
			if (c == '\t') {
371
				int remainder= blanks % tabWidth;
378
				blanks = calculateSpaceEquivalents(tabWidth, blanks);
372
				blanks += tabWidth - remainder;
373
			} else if (isIndentChar(c)) {
379
			} else if (isIndentChar(c)) {
374
				blanks++;
380
				blanks++;
375
			} else {
381
			} else {
Lines 382-387 Link Here
382
		return result + 1;
388
		return result + 1;
383
	}
389
	}
384
390
391
	/*
392
	 * Calculates space equivalents up to the next tab stop
393
	 */
394
	private static int calculateSpaceEquivalents(int tabWidth, int spaceEquivalents) {
395
		if (tabWidth == 0){
396
			return spaceEquivalents;
397
		}
398
		int remainder = spaceEquivalents % tabWidth;
399
		spaceEquivalents += tabWidth - remainder;
400
		return spaceEquivalents;
401
	}
402
385
	/**
403
	/**
386
	 * Returns the tab width as configured in the given map.
404
	 * 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>
405
	 * <p>Use {@link org.eclipse.jdt.core.IJavaProject#getOptions(boolean)} to get the most current project options.</p>

Return to bug 285565