Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 400670 | Differences between
and this patch

Collapse All | Expand All

(-)a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/IndentActionTest.java (+31 lines)
Lines 207-210 Link Here
207
		selectAll();
207
		selectAll();
208
		assertIndentResult();
208
		assertIndentResult();
209
	}
209
	}
210
211
	public void testBug400670() throws Exception {
212
		String indentOnColumn= DefaultCodeFormatterConstants.createAlignmentValue(true, DefaultCodeFormatterConstants.WRAP_NEXT_PER_LINE, DefaultCodeFormatterConstants.INDENT_ON_COLUMN);
213
		IJavaProject project= IndentTestSetup.getProject();
214
		String value1= project.getOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, true);
215
		project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, indentOnColumn);
216
		String value2= project.getOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION, true);
217
		project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION, indentOnColumn);
218
		String value3= project.getOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_ARRAY_INITIALIZER, true);
219
		project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_ARRAY_INITIALIZER, indentOnColumn);
220
		try {
221
			selectAll();
222
			assertIndentResult();
223
		} finally {
224
			project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, value1);
225
			project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION, value2);
226
			project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_ARRAY_INITIALIZER, value3);
227
		}
228
	}
229
230
	public void testBug458763() throws Exception {
231
		IJavaProject project= IndentTestSetup.getProject();
232
		String value= project.getOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, true);
233
		project.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, DefaultCodeFormatterConstants.FALSE);
234
		try {
235
			selectAll();
236
			assertIndentResult();
237
		} finally {
238
			project.setOption(DefaultCodeFormatterConstants.FORMATTER_CONTINUATION_INDENTATION_FOR_ARRAY_INITIALIZER, value);
239
		}
240
	}
210
}
241
}
(-)a/org.eclipse.jdt.text.tests/testResources/indentation/bug400670/Before.java (+51 lines)
Added Link Here
1
package p;
2
3
public enum TestEnum {
4
                      FIRST_ENUM("first type",
5
                                 new SomeClass(),
6
                                 new OtherEnumType[] { OtherEnumType.FOO }),
7
                      SECOND_ENUM("second type",
8
                                  new SomeClassOtherClass(),
9
                                  new OtherEnumType[] { OtherEnumType.BAR }),
10
                      THIRD_ENUM("third type",
11
                                 new SomeThirdClass(),
12
                                 new OtherEnumType[] { OtherEnumType.BAZ }),
13
                      FOURTH_ENUM("fourth type",
14
                                  new YetAnotherClass(),
15
                                  new OtherEnumType[] { OtherEnumType.FOOBAR,
16
                                                        OtherEnumType.FOO,
17
                                                        OtherEnumType.FOOBARBAZ,
18
                                                        OtherEnumType.LONGERFOOBARBAZ,
19
                                                        OtherEnumType.REALLYLONGFOOBARBAZ,
20
                                                        OtherEnumType.MORELETTERSINHERE });
21
22
	/* data members and methods go here */
23
	TestEnum(String s, Cls s1, OtherEnumType[] e) {
24
	}
25
}
26
27
enum OtherEnumType {
28
                    FOOBAR,
29
                    FOOBARBAZ,
30
                    FOO,
31
                    LONGERFOOBARBAZ,
32
                    REALLYLONGFOOBARBAZ,
33
                    MORELETTERSINHERE,
34
                    BAR,
35
                    BAZ
36
}
37
38
class Cls {
39
}
40
41
class SomeClass extends Cls {
42
}
43
44
class SomeThirdClass extends Cls {
45
}
46
47
class SomeClassOtherClass extends Cls {
48
}
49
50
class YetAnotherClass extends Cls {
51
}
(-)a/org.eclipse.jdt.text.tests/testResources/indentation/bug400670/Modified.java (+51 lines)
Added Link Here
1
package p;
2
3
public enum TestEnum {
4
                      FIRST_ENUM("first type",
5
                                 new SomeClass(),
6
                                 new OtherEnumType[] { OtherEnumType.FOO }),
7
                      SECOND_ENUM("second type",
8
                                  new SomeClassOtherClass(),
9
                                  new OtherEnumType[] { OtherEnumType.BAR }),
10
                      THIRD_ENUM("third type",
11
                                 new SomeThirdClass(),
12
                                 new OtherEnumType[] { OtherEnumType.BAZ }),
13
                      FOURTH_ENUM("fourth type",
14
                                  new YetAnotherClass(),
15
                                  new OtherEnumType[] { OtherEnumType.FOOBAR,
16
                                                        OtherEnumType.FOO,
17
                                                        OtherEnumType.FOOBARBAZ,
18
                                                        OtherEnumType.LONGERFOOBARBAZ,
19
                                                        OtherEnumType.REALLYLONGFOOBARBAZ,
20
                                                        OtherEnumType.MORELETTERSINHERE });
21
22
	/* data members and methods go here */
23
	TestEnum(String s, Cls s1, OtherEnumType[] e) {
24
	}
25
}
26
27
enum OtherEnumType {
28
                    FOOBAR,
29
                    FOOBARBAZ,
30
                    FOO,
31
                    LONGERFOOBARBAZ,
32
                    REALLYLONGFOOBARBAZ,
33
                    MORELETTERSINHERE,
34
                    BAR,
35
                    BAZ
36
}
37
38
class Cls {
39
}
40
41
class SomeClass extends Cls {
42
}
43
44
class SomeThirdClass extends Cls {
45
}
46
47
class SomeClassOtherClass extends Cls {
48
}
49
50
class YetAnotherClass extends Cls {
51
}
(-)a/org.eclipse.jdt.text.tests/testResources/indentation/bug424772/Modified.java (-1 / +1 lines)
Lines 15-21 Link Here
15
15
16
	String t3(int i,
16
	String t3(int i,
17
			int j)
17
			int j)
18
			[] []
18
	[] []
19
	{
19
	{
20
		return new String[0][0];
20
		return new String[0][0];
21
	}
21
	}
(-)a/org.eclipse.jdt.text.tests/testResources/indentation/bug458763/Before.java (+17 lines)
Added Link Here
1
package p;
2
3
public enum Direction {NORTH, SOUTH, EAST, WEST; // note semicolon here
4
public Direction opposite() {
5
	switch (this) {
6
	case NORTH:
7
		return SOUTH;
8
	case SOUTH:
9
		return NORTH;
10
	case EAST:
11
		return WEST;
12
	case WEST:
13
		return EAST;
14
	default: return null;
15
	}
16
}
17
}
(-)a/org.eclipse.jdt.text.tests/testResources/indentation/bug458763/Modified.java (+17 lines)
Added Link Here
1
package p;
2
3
public enum Direction {NORTH, SOUTH, EAST, WEST; // note semicolon here
4
	public Direction opposite() {
5
		switch (this) {
6
		case NORTH:
7
			return SOUTH;
8
		case SOUTH:
9
			return NORTH;
10
		case EAST:
11
			return WEST;
12
		case WEST:
13
			return EAST;
14
		default: return null;
15
		}
16
	}
17
}
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/JavaIndenter.java (-9 / +123 lines)
Lines 71-76 Link Here
71
		final int prefContinuationIndent;
71
		final int prefContinuationIndent;
72
		final boolean prefHasGenerics;
72
		final boolean prefHasGenerics;
73
		final String prefTabChar;
73
		final String prefTabChar;
74
		final int prefEnumConstantIndent;
75
		final boolean prefEnumConstantDeepIndent;
74
76
75
		private final IJavaProject fProject;
77
		private final IJavaProject fProject;
76
78
Lines 128-133 Link Here
128
				prefIndentBracesForTypes= false;
130
				prefIndentBracesForTypes= false;
129
				prefHasGenerics= false;
131
				prefHasGenerics= false;
130
				prefTabChar= JavaCore.TAB;
132
				prefTabChar= JavaCore.TAB;
133
				prefEnumConstantDeepIndent= false;
134
				prefEnumConstantIndent= 1;
131
			} else {
135
			} else {
132
				prefUseTabs= prefUseTabs();
136
				prefUseTabs= prefUseTabs();
133
				prefTabSize= prefTabSize();
137
				prefTabSize= prefTabSize();
Lines 157-162 Link Here
157
				prefIndentBracesForTypes= prefIndentBracesForTypes();
161
				prefIndentBracesForTypes= prefIndentBracesForTypes();
158
				prefHasGenerics= hasGenerics();
162
				prefHasGenerics= hasGenerics();
159
				prefTabChar= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR);
163
				prefTabChar= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR);
164
				prefEnumConstantIndent= prefEnumConstantIndent();
165
				prefEnumConstantDeepIndent= prefEnumConstantDeepIndent();
160
			}
166
			}
161
		}
167
		}
162
168
Lines 362-367 Link Here
362
		}
368
		}
363
		private boolean hasGenerics() {
369
		private boolean hasGenerics() {
364
			return JavaCore.VERSION_1_5.compareTo(getCoreFormatterOption(JavaCore.COMPILER_SOURCE)) <= 0;
370
			return JavaCore.VERSION_1_5.compareTo(getCoreFormatterOption(JavaCore.COMPILER_SOURCE)) <= 0;
371
		}
372
373
		private boolean prefEnumConstantDeepIndent() {
374
			String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS);
375
			try {
376
				return DefaultCodeFormatterConstants.getIndentStyle(option) == DefaultCodeFormatterConstants.INDENT_ON_COLUMN;
377
			} catch (IllegalArgumentException e) {
378
				// ignore and return default
379
			}
380
			return false;
381
		}
382
383
		private int prefEnumConstantIndent() { // see bug 458208 comment 22
384
			String option= getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS);
385
			try {
386
				if (DefaultCodeFormatterConstants.getIndentStyle(option) == DefaultCodeFormatterConstants.INDENT_BY_ONE)
387
					return 2;
388
			} catch (IllegalArgumentException e) {
389
				// ignore and return default
390
			}
391
			return 1;
365
		}
392
		}
366
	}
393
	}
367
394
Lines 1037-1046 Link Here
1037
					if (isTryWithResources()) {
1064
					if (isTryWithResources()) {
1038
						fIndent= fPrefs.prefContinuationIndent;
1065
						fIndent= fPrefs.prefContinuationIndent;
1039
						return fPosition;
1066
						return fPosition;
1040
					} else {
1041
						fPosition= pos;
1042
						return skipToStatementStart(danglingElse, false);
1043
					}
1067
					}
1068
					fPosition= pos;
1069
					if (isSemicolonPartOfEnumBodyDeclaration()) {
1070
						fIndent= getBlockIndent(false, true);
1071
						return fPosition;
1072
					}
1073
					fPosition= pos;
1074
					return skipToStatementStart(danglingElse, false);
1044
				}
1075
				}
1045
			// scope introduction: special treat who special is
1076
			// scope introduction: special treat who special is
1046
			case Symbols.TokenLPAREN:
1077
			case Symbols.TokenLPAREN:
Lines 1537-1542 Link Here
1537
				case Symbols.TokenRBRACE:
1568
				case Symbols.TokenRBRACE:
1538
				case Symbols.TokenGREATERTHAN:
1569
				case Symbols.TokenGREATERTHAN:
1539
					skipScope();
1570
					skipScope();
1571
					startLine= fLine;
1572
					startPosition= fPosition;
1540
					break;
1573
					break;
1541
1574
1542
				// scope introduction: special treat who special is
1575
				// scope introduction: special treat who special is
Lines 1686-1691 Link Here
1686
			case Symbols.TokenLBRACE:
1719
			case Symbols.TokenLBRACE:
1687
				pos= fPosition; // store
1720
				pos= fPosition; // store
1688
1721
1722
				if (looksLikeEnumDeclaration()) {
1723
					if (fPrefs.prefEnumConstantDeepIndent) {
1724
						return setFirstElementAlignment(pos, bound);
1725
					} else {
1726
						fIndent= fPrefs.prefEnumConstantIndent;
1727
						return pos;
1728
					}
1729
				}
1730
				fPosition= pos; // restore
1731
1689
				// special: array initializer
1732
				// special: array initializer
1690
				if (looksLikeArrayInitializerIntro())
1733
				if (looksLikeArrayInitializerIntro())
1691
					if (fPrefs.prefArrayDeepIndent)
1734
					if (fPrefs.prefArrayDeepIndent)
Lines 1757-1769 Link Here
1757
	}
1800
	}
1758
1801
1759
	/**
1802
	/**
1760
	 * Skips over the next <code>if</code> keyword. The current token when calling
1803
	 * Returns <code>true</code> if the current position looks like a part of enum declaration
1761
	 * this method must be an <code>else</code> keyword. Returns <code>true</code>
1804
	 * header. It calls {@link #nextToken} to scan backwards.
1762
	 * if a matching <code>if</code> could be found, <code>false</code> otherwise.
1763
	 * The cursor (<code>fPosition</code>) is set to the offset of the <code>if</code>
1764
	 * token.
1765
	 *
1805
	 *
1766
	 * @return <code>true</code> if a matching <code>if</code> token was found, <code>false</code> otherwise
1806
	 * @return <code>true</code> if the current position looks like a part of enum declaration
1807
	 *         header
1808
	 * @since 3.11
1809
	 */
1810
	private boolean looksLikeEnumDeclaration() {
1811
		while (true) {
1812
			nextToken();
1813
			switch (fToken) {
1814
				case Symbols.TokenENUM:
1815
					return true;
1816
				case Symbols.TokenIDENT:
1817
				case Symbols.TokenCOMMA:
1818
				case Symbols.TokenAT:
1819
					break;
1820
				case Symbols.TokenOTHER:
1821
					try {
1822
						if (fDocument.getChar(fPosition) != '.') {
1823
							return false;
1824
						}
1825
					} catch (BadLocationException e) {
1826
						return false;
1827
					}
1828
					break;
1829
				case Symbols.TokenRPAREN:
1830
				case Symbols.TokenRBRACKET:
1831
				case Symbols.TokenRBRACE:
1832
				case Symbols.TokenGREATERTHAN:
1833
					skipScope();
1834
					break;
1835
				case Symbols.TokenEOF:
1836
					return false;
1837
				default:
1838
					return false;
1839
			}
1840
		}
1841
	}
1842
1843
	/**
1844
	 * Checks if the semicolon at the current position is part of enum body declaration.
1845
	 * 
1846
	 * @return returns <code>true</code> if the semicolon at the current position is part of enum
1847
	 *         body declaration
1848
	 * @since 3.11
1849
	 */
1850
	private boolean isSemicolonPartOfEnumBodyDeclaration() {
1851
		while (true) {
1852
			nextToken();
1853
			switch (fToken) {
1854
				case Symbols.TokenLBRACE:
1855
					return looksLikeEnumDeclaration();
1856
				case Symbols.TokenIDENT:
1857
				case Symbols.TokenCOMMA:
1858
					break;
1859
				case Symbols.TokenRPAREN:
1860
				case Symbols.TokenRBRACKET:
1861
				case Symbols.TokenRBRACE:
1862
				case Symbols.TokenGREATERTHAN:
1863
					skipScope();
1864
					break;
1865
				case Symbols.TokenEOF:
1866
					return false;
1867
				default:
1868
					return false;
1869
			}
1870
		}
1871
	}
1872
1873
	/**
1874
	 * Skips over the next <code>if</code> keyword. The current token when calling this method must
1875
	 * be an <code>else</code> keyword. Returns <code>true</code> if a matching <code>if</code>
1876
	 * could be found, <code>false</code> otherwise. The cursor (<code>fPosition</code>) is set to
1877
	 * the offset of the <code>if</code> token.
1878
	 *
1879
	 * @return <code>true</code> if a matching <code>if</code> token was found, <code>false</code>
1880
	 *         otherwise
1767
	 */
1881
	 */
1768
	private boolean skipNextIF() {
1882
	private boolean skipNextIF() {
1769
		Assert.isTrue(fToken == Symbols.TokenELSE);
1883
		Assert.isTrue(fToken == Symbols.TokenELSE);

Return to bug 400670