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 (+38 lines)
Lines 207-210 Link Here
207
		selectAll();
207
		selectAll();
208
		assertIndentResult();
208
		assertIndentResult();
209
	}
209
	}
210
211
	public void testBug400670_1() throws Exception {
212
		// With formatter profile from https://bugs.eclipse.org/bugs/show_bug.cgi?id=400670#c0
213
		String indentOnColumn= DefaultCodeFormatterConstants.createAlignmentValue(true, DefaultCodeFormatterConstants.WRAP_NEXT_PER_LINE, DefaultCodeFormatterConstants.INDENT_ON_COLUMN);
214
		IJavaProject project= IndentTestSetup.getProject();
215
		String value1= project.getOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, true);
216
		project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, indentOnColumn);
217
		String value2= project.getOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION, true);
218
		project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION, indentOnColumn);
219
		String value3= project.getOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_ARRAY_INITIALIZER, true);
220
		project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_ARRAY_INITIALIZER, indentOnColumn);
221
		try {
222
			selectAll();
223
			assertIndentResult();
224
		} finally {
225
			project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, value1);
226
			project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION, value2);
227
			project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_ARRAY_INITIALIZER, value3);
228
		}
229
	}
230
231
	public void testBug400670_2() throws Exception {
232
		// With default formatter profile
233
		selectAll();
234
		assertIndentResult();
235
	}
236
237
	public void testBug458763() throws Exception {
238
		IJavaProject project= IndentTestSetup.getProject();
239
		String value= project.getOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, true);
240
		project.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, DefaultCodeFormatterConstants.FALSE);
241
		try {
242
			selectAll();
243
			assertIndentResult();
244
		} finally {
245
			project.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, value);
246
		}
247
	}
210
}
248
}
(-)a/org.eclipse.jdt.text.tests/testResources/indentation/bug400670_1/Before.java (+55 lines)
Added Link Here
1
package p;
2
3
public enum TestEnum {
4
5
	FIRST_ENUM("first type",
6
	           new SomeClass(),
7
	           new OtherEnumType[] { OtherEnumType.FOO }),
8
9
	SECOND_ENUM("second type",
10
	            new SomeClassOtherClass(),
11
	            new OtherEnumType[] { OtherEnumType.BAR }),
12
13
	THIRD_ENUM("third type",
14
	           new SomeThirdClass(),
15
	           new OtherEnumType[] { OtherEnumType.BAZ }),
16
17
	FOURTH_ENUM("fourth type",
18
	            new YetAnotherClass(),
19
	            new OtherEnumType[] { OtherEnumType.FOOBAR,
20
	                                  OtherEnumType.FOO,
21
	                                  OtherEnumType.FOOBARBAZ,
22
	                                  OtherEnumType.LONGERFOOBARBAZ,
23
	                                  OtherEnumType.REALLYLONGFOOBARBAZ,
24
	                                  OtherEnumType.MORELETTERSINHERE });
25
26
	/* data members and methods go here */
27
	TestEnum(String s, Cls s1, OtherEnumType[] e) {
28
	}
29
}
30
31
enum OtherEnumType {
32
                    FOOBAR,
33
                    FOOBARBAZ,
34
                    FOO,
35
                    LONGERFOOBARBAZ,
36
                    REALLYLONGFOOBARBAZ,
37
                    MORELETTERSINHERE,
38
                    BAR,
39
                    BAZ
40
}
41
42
class Cls {
43
}
44
45
class SomeClass extends Cls {
46
}
47
48
class SomeThirdClass extends Cls {
49
}
50
51
class SomeClassOtherClass extends Cls {
52
}
53
54
class YetAnotherClass extends Cls {
55
}
(-)a/org.eclipse.jdt.text.tests/testResources/indentation/bug400670_1/Modified.java (+55 lines)
Added Link Here
1
package p;
2
3
public enum TestEnum {
4
5
	FIRST_ENUM("first type",
6
	           new SomeClass(),
7
	           new OtherEnumType[] { OtherEnumType.FOO }),
8
9
	SECOND_ENUM("second type",
10
	            new SomeClassOtherClass(),
11
	            new OtherEnumType[] { OtherEnumType.BAR }),
12
13
	THIRD_ENUM("third type",
14
	           new SomeThirdClass(),
15
	           new OtherEnumType[] { OtherEnumType.BAZ }),
16
17
	FOURTH_ENUM("fourth type",
18
	            new YetAnotherClass(),
19
	            new OtherEnumType[] { OtherEnumType.FOOBAR,
20
	                                  OtherEnumType.FOO,
21
	                                  OtherEnumType.FOOBARBAZ,
22
	                                  OtherEnumType.LONGERFOOBARBAZ,
23
	                                  OtherEnumType.REALLYLONGFOOBARBAZ,
24
	                                  OtherEnumType.MORELETTERSINHERE });
25
26
	/* data members and methods go here */
27
	TestEnum(String s, Cls s1, OtherEnumType[] e) {
28
	}
29
}
30
31
enum OtherEnumType {
32
	FOOBAR,
33
	FOOBARBAZ,
34
	FOO,
35
	LONGERFOOBARBAZ,
36
	REALLYLONGFOOBARBAZ,
37
	MORELETTERSINHERE,
38
	BAR,
39
	BAZ
40
}
41
42
class Cls {
43
}
44
45
class SomeClass extends Cls {
46
}
47
48
class SomeThirdClass extends Cls {
49
}
50
51
class SomeClassOtherClass extends Cls {
52
}
53
54
class YetAnotherClass extends Cls {
55
}
(-)a/org.eclipse.jdt.text.tests/testResources/indentation/bug400670_2/Before.java (+38 lines)
Added Link Here
1
package p;
2
3
public enum TestEnum {
4
5
	FIRST_ENUM("first type", new SomeClass(), new OtherEnumType[] { OtherEnumType.FOO }),
6
7
	SECOND_ENUM("second type", new SomeClassOtherClass(), new OtherEnumType[] { OtherEnumType.BAR }),
8
9
	THIRD_ENUM("third type", new SomeThirdClass(), new OtherEnumType[] { OtherEnumType.BAZ }),
10
11
	FOURTH_ENUM("fourth type", new YetAnotherClass(),
12
			new OtherEnumType[] { OtherEnumType.FOOBAR, OtherEnumType.FOO, OtherEnumType.FOOBARBAZ,
13
					OtherEnumType.LONGERFOOBARBAZ, OtherEnumType.REALLYLONGFOOBARBAZ,
14
					OtherEnumType.MORELETTERSINHERE });
15
16
	/* data members and methods go here */
17
	TestEnum(String s, Cls s1, OtherEnumType[] e) {
18
	}
19
}
20
21
enum OtherEnumType {
22
	FOOBAR, FOOBARBAZ, FOO, LONGERFOOBARBAZ, REALLYLONGFOOBARBAZ, MORELETTERSINHERE, BAR, BAZ
23
}
24
25
class Cls {
26
}
27
28
class SomeClass extends Cls {
29
}
30
31
class SomeThirdClass extends Cls {
32
}
33
34
class SomeClassOtherClass extends Cls {
35
}
36
37
class YetAnotherClass extends Cls {
38
}
(-)a/org.eclipse.jdt.text.tests/testResources/indentation/bug400670_2/Modified.java (+38 lines)
Added Link Here
1
package p;
2
3
public enum TestEnum {
4
5
	FIRST_ENUM("first type", new SomeClass(), new OtherEnumType[] { OtherEnumType.FOO }),
6
7
	SECOND_ENUM("second type", new SomeClassOtherClass(), new OtherEnumType[] { OtherEnumType.BAR }),
8
9
	THIRD_ENUM("third type", new SomeThirdClass(), new OtherEnumType[] { OtherEnumType.BAZ }),
10
11
	FOURTH_ENUM("fourth type", new YetAnotherClass(),
12
			new OtherEnumType[] { OtherEnumType.FOOBAR, OtherEnumType.FOO, OtherEnumType.FOOBARBAZ,
13
					OtherEnumType.LONGERFOOBARBAZ, OtherEnumType.REALLYLONGFOOBARBAZ,
14
					OtherEnumType.MORELETTERSINHERE });
15
16
	/* data members and methods go here */
17
	TestEnum(String s, Cls s1, OtherEnumType[] e) {
18
	}
19
}
20
21
enum OtherEnumType {
22
	FOOBAR, FOOBARBAZ, FOO, LONGERFOOBARBAZ, REALLYLONGFOOBARBAZ, MORELETTERSINHERE, BAR, BAZ
23
}
24
25
class Cls {
26
}
27
28
class SomeClass extends Cls {
29
}
30
31
class SomeThirdClass extends Cls {
32
}
33
34
class SomeClassOtherClass extends Cls {
35
}
36
37
class YetAnotherClass extends Cls {
38
}
(-)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 (-16 / +111 lines)
Lines 1037-1046 Link Here
1037
					if (isTryWithResources()) {
1037
					if (isTryWithResources()) {
1038
						fIndent= fPrefs.prefContinuationIndent;
1038
						fIndent= fPrefs.prefContinuationIndent;
1039
						return fPosition;
1039
						return fPosition;
1040
					} else {
1041
						fPosition= pos;
1042
						return skipToStatementStart(danglingElse, false);
1043
					}
1040
					}
1041
					fPosition= pos;
1042
					if (isSemicolonPartOfEnumBodyDeclaration()) {
1043
						fIndent= getBlockIndent(false, true);
1044
						return fPosition;
1045
					}
1046
					fPosition= pos;
1047
					return skipToStatementStart(danglingElse, false);
1044
				}
1048
				}
1045
			// scope introduction: special treat who special is
1049
			// scope introduction: special treat who special is
1046
			case Symbols.TokenLPAREN:
1050
			case Symbols.TokenLPAREN:
Lines 1537-1542 Link Here
1537
				case Symbols.TokenRBRACE:
1541
				case Symbols.TokenRBRACE:
1538
				case Symbols.TokenGREATERTHAN:
1542
				case Symbols.TokenGREATERTHAN:
1539
					skipScope();
1543
					skipScope();
1544
					startLine= fLine;
1545
					startPosition= fPosition;
1540
					break;
1546
					break;
1541
1547
1542
				// scope introduction: special treat who special is
1548
				// scope introduction: special treat who special is
Lines 1687-1699 Link Here
1687
				pos= fPosition; // store
1693
				pos= fPosition; // store
1688
1694
1689
				// special: array initializer
1695
				// special: array initializer
1690
				if (looksLikeArrayInitializerIntro())
1696
				if (looksLikeArrayInitializerIntro()) {
1691
					if (fPrefs.prefArrayDeepIndent)
1697
					if (fPrefs.prefArrayDeepIndent) {
1692
						return setFirstElementAlignment(pos, bound);
1698
						return setFirstElementAlignment(pos, bound);
1693
					else
1699
					} else {
1694
						fIndent= fPrefs.prefArrayIndent;
1700
						fIndent= fPrefs.prefArrayIndent;
1695
				else
1701
						return pos;
1702
					}
1703
				} else {
1696
					fIndent= fPrefs.prefBlockIndent;
1704
					fIndent= fPrefs.prefBlockIndent;
1705
				}
1697
1706
1698
				// normal: skip to the statement start before the scope introducer
1707
				// normal: skip to the statement start before the scope introducer
1699
				// opening braces are often on differently ending indents than e.g. a method definition
1708
				// opening braces are often on differently ending indents than e.g. a method definition
Lines 1743-1769 Link Here
1743
1752
1744
1753
1745
	/**
1754
	/**
1746
	 * Returns <code>true</code> if the next token received after calling
1755
	 * Returns <code>true</code> if the next token received after calling <code>nextToken</code> is
1747
	 * <code>nextToken</code> is either an equal sign or an array designator ('[]').
1756
	 * either an equal sign or an array designator ('[]') preceded by array creation.
1748
	 *
1757
	 *
1749
	 * @return <code>true</code> if the next elements look like the start of an array definition
1758
	 * @return <code>true</code> if the next elements look like the start of an array definition
1750
	 */
1759
	 */
1751
	private boolean looksLikeArrayInitializerIntro() {
1760
	private boolean looksLikeArrayInitializerIntro() {
1752
		nextToken();
1761
		nextToken();
1753
		if (fToken == Symbols.TokenEQUAL || skipBrackets()) {
1762
		if (fToken == Symbols.TokenEQUAL) {
1754
			return true;
1763
			return true;
1764
		}
1765
1766
		if (!skipScope(Symbols.TokenLBRACKET, Symbols.TokenRBRACKET)) {
1767
			return false;
1768
		}
1769
		nextToken();
1770
		if (fToken == Symbols.TokenIDENT) { // type name
1771
			nextToken();
1772
			while (fToken == Symbols.TokenOTHER) { // dot of qualification
1773
				nextToken();
1774
				if (fToken != Symbols.TokenIDENT) // qualifying name
1775
					return false;
1776
				nextToken();
1777
			}
1778
			return fToken == Symbols.TokenNEW;
1755
		}
1779
		}
1756
		return false;
1780
		return false;
1757
	}
1781
	}
1758
1782
1759
	/**
1783
	/**
1760
	 * Skips over the next <code>if</code> keyword. The current token when calling
1784
	 * 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>
1785
	 * 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
	 *
1786
	 *
1766
	 * @return <code>true</code> if a matching <code>if</code> token was found, <code>false</code> otherwise
1787
	 * @return <code>true</code> if the current position looks like a part of enum declaration
1788
	 *         header
1789
	 * @since 3.11
1790
	 */
1791
	private boolean looksLikeEnumDeclaration() {
1792
		while (true) {
1793
			nextToken();
1794
			switch (fToken) {
1795
				case Symbols.TokenENUM:
1796
					return true;
1797
				case Symbols.TokenIDENT:
1798
				case Symbols.TokenCOMMA:
1799
				case Symbols.TokenAT:
1800
					break;
1801
				case Symbols.TokenOTHER:
1802
					try {
1803
						if (fDocument.getChar(fPosition) != '.') {
1804
							return false;
1805
						}
1806
					} catch (BadLocationException e) {
1807
						return false;
1808
					}
1809
					break;
1810
				case Symbols.TokenRPAREN:
1811
				case Symbols.TokenRBRACKET:
1812
				case Symbols.TokenRBRACE:
1813
				case Symbols.TokenGREATERTHAN:
1814
					skipScope();
1815
					break;
1816
				case Symbols.TokenEOF:
1817
					return false;
1818
				default:
1819
					return false;
1820
			}
1821
		}
1822
	}
1823
1824
	/**
1825
	 * Checks if the semicolon at the current position is part of enum body declaration.
1826
	 * 
1827
	 * @return returns <code>true</code> if the semicolon at the current position is part of enum
1828
	 *         body declaration
1829
	 * @since 3.11
1830
	 */
1831
	private boolean isSemicolonPartOfEnumBodyDeclaration() {
1832
		while (true) {
1833
			nextToken();
1834
			switch (fToken) {
1835
				case Symbols.TokenLBRACE:
1836
					return looksLikeEnumDeclaration();
1837
				case Symbols.TokenIDENT:
1838
				case Symbols.TokenCOMMA:
1839
					break;
1840
				case Symbols.TokenRPAREN:
1841
				case Symbols.TokenRBRACKET:
1842
				case Symbols.TokenRBRACE:
1843
				case Symbols.TokenGREATERTHAN:
1844
					skipScope();
1845
					break;
1846
				case Symbols.TokenEOF:
1847
					return false;
1848
				default:
1849
					return false;
1850
			}
1851
		}
1852
	}
1853
1854
	/**
1855
	 * Skips over the next <code>if</code> keyword. The current token when calling this method must
1856
	 * be an <code>else</code> keyword. Returns <code>true</code> if a matching <code>if</code>
1857
	 * could be found, <code>false</code> otherwise. The cursor (<code>fPosition</code>) is set to
1858
	 * the offset of the <code>if</code> token.
1859
	 *
1860
	 * @return <code>true</code> if a matching <code>if</code> token was found, <code>false</code>
1861
	 *         otherwise
1767
	 */
1862
	 */
1768
	private boolean skipNextIF() {
1863
	private boolean skipNextIF() {
1769
		Assert.isTrue(fToken == Symbols.TokenELSE);
1864
		Assert.isTrue(fToken == Symbols.TokenELSE);

Return to bug 400670