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

Collapse All | Expand All

(-)src/org/eclipse/jdt/text/tests/JavaHeuristicScannerTest.java (-37 / +96 lines)
Lines 110-124 Link Here
110
		Assert.assertEquals(21, pos);
110
		Assert.assertEquals(21, pos);
111
	}
111
	}
112
112
113
	public void testPrevIndentationUnit4() {
114
		fDocument.set("\tint a;\n" +
115
			"\tif (true)\n" +
116
			"\t\treturn a\n" +
117
			"");
118
119
		int pos= fScanner.findReferencePosition(29);
120
		Assert.assertEquals(28, pos);
121
	}
122
113
123
	public void testPrevIndentationUnit5() {
114
	public void testPrevIndentationUnit5() {
124
		fDocument.set("\tint a;\n" +
115
		fDocument.set("\tint a;\n" +
Lines 325-358 Link Here
325
		Assert.assertEquals("\t\t", indent);
316
		Assert.assertEquals("\t\t", indent);
326
	}
317
	}
327
318
328
	public void testIndentation2() {
329
		fDocument.set("\tint a;\n" +
330
			"\tif (true)\n" +
331
			"\t\treturn a");
332
333
		String indent= fScanner.computeIndentation(28).toString();
334
		Assert.assertEquals("\t\t", indent);
335
	}
336
337
	public void testIndentation3() {
338
		fDocument.set("\tint a;\n" +
339
			"\tif (true)\n" +
340
			"\t\treturn a;");
341
342
		String indent= fScanner.computeIndentation(29).toString();
343
		Assert.assertEquals("\t\t", indent);
344
	}
345
346
	public void testIndentation4() {
347
		fDocument.set("\tint a;\n" +
348
			"\tif (true)\n" +
349
			"\t\treturn a\n" +
350
			"");
351
352
		String indent= fScanner.computeIndentation(29).toString();
353
		Assert.assertEquals("\t\t", indent);
354
	}
355
356
	public void testIndentation5() {
319
	public void testIndentation5() {
357
		fDocument.set("\tint a;\n" +
320
		fDocument.set("\tint a;\n" +
358
			"\tif (true)\n" +
321
			"\tif (true)\n" +
Lines 873-876 Link Here
873
    	Assert.assertEquals("					", indent);
836
    	Assert.assertEquals("					", indent);
874
837
875
    }
838
    }
839
840
	public void testContinuationIndentationOfForStatement() throws Exception {
841
		fDocument.set("\tfor (int i = (2 * 2); i < array.length; i++) {\n" +
842
				"\t\t}");
843
844
		String indent= fScanner.computeIndentation(22).toString();
845
		Assert.assertEquals("\t\t", indent);
846
		indent= fScanner.computeIndentation(27).toString();
847
		Assert.assertEquals("\t\t", indent);
848
		indent= fScanner.computeIndentation(39).toString();
849
		Assert.assertEquals("\t\t", indent);
850
		indent= fScanner.computeIndentation(40).toString();
851
		Assert.assertEquals("\t\t", indent);
852
		indent= fScanner.computeIndentation(5).toString();
853
		Assert.assertEquals("\t", indent);
854
		indent= fScanner.computeIndentation(45).toString();
855
		Assert.assertEquals("\t", indent);
856
	}
857
858
	public void testContinuationIndentationOfBooleanExpression() throws Exception {
859
		fDocument.set("\tboolean a = true || false;\n" +
860
				"\tboolean b = a || false;\n");
861
862
		String indent= fScanner.computeIndentation(20).toString();
863
		Assert.assertEquals("\t\t", indent);
864
		indent= fScanner.computeIndentation(40).toString();
865
		Assert.assertEquals("\t\t", indent);
866
	}
867
868
	public void testContinuationIndentationOfReturnStatement() throws Exception {
869
		fDocument.set("\t\treturn \"I'm such a long string that you have to split me to see the whole line without scrolling around\"\n");
870
871
		String indent= fScanner.computeIndentation(8).toString();
872
		Assert.assertEquals("\t\t\t", indent);
873
		indent= fScanner.computeIndentation(21).toString();
874
		Assert.assertEquals("\t\t\t", indent);
875
		indent= fScanner.computeIndentation(38).toString();
876
		Assert.assertEquals("\t\t\t", indent);
877
	}
878
879
	public void testContinuationIndentationOfAssignmentStatement() throws Exception {
880
		fDocument.set("\tint i= 5+");
881
882
		String indent= fScanner.computeIndentation(7).toString();
883
		Assert.assertEquals("\t\t", indent);
884
		indent= fScanner.computeIndentation(10).toString();
885
		Assert.assertEquals("\t\t", indent);
886
	}
887
888
	public void testContinuationIndentation1() throws Exception {
889
		fDocument.set("\treturn (thisIsAVeryLongName == 1 && anotherVeryLongName == 1)\n" +
890
				"\t\t|| thisIsAVeryLongName == 2;");
891
892
		String indent= fScanner.computeIndentation(68).toString();
893
		Assert.assertEquals("\t\t", indent);
894
		indent= fScanner.computeIndentation(88).toString();
895
		Assert.assertEquals("\t\t", indent);
896
	}
897
898
	public void testContinuationIndentation2() {
899
		fDocument.set("\tint a;\n" +
900
				"\tif (true)\n" +
901
				"\t\treturn a\n" +
902
				"");
903
904
		int pos= fScanner.findReferencePosition(29);
905
		Assert.assertEquals(21, pos);
906
	}
907
908
	public void testContinuationIndentation3() {
909
		fDocument.set("\tint a;\n" +
910
				"\tif (true)\n" +
911
				"\t\treturn a");
912
913
		String indent= fScanner.computeIndentation(28).toString();
914
		Assert.assertEquals("\t\t\t", indent);
915
	}
916
917
	public void testContinuationIndentation4() {
918
		fDocument.set("\tint a;\n" +
919
				"\tif (true)\n" +
920
				"\t\treturn a;");
921
922
		String indent= fScanner.computeIndentation(29).toString();
923
		Assert.assertEquals("\t\t\t", indent);
924
	}
925
926
	public void testContinuationIndentation5() {
927
		fDocument.set("\tint a;\n" +
928
				"\tif (true)\n" +
929
				"\t\treturn a\n" +
930
				"");
931
932
		String indent= fScanner.computeIndentation(29).toString();
933
		Assert.assertEquals("\t\t\t", indent);
934
	}
876
}
935
}
(-)ui/org/eclipse/jdt/internal/ui/text/JavaIndenter.java (-6 / +74 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 887-899 Link Here
887
				int pos= fPosition;
887
				int pos= fPosition;
888
				if (!skipScope())
888
				if (!skipScope())
889
					fPosition= pos;
889
					fPosition= pos;
890
				//$FALL-THROUGH$
890
				return skipToStatementStart(danglingElse, false);
891
			case Symbols.TokenSEMICOLON:
891
			case Symbols.TokenSEMICOLON:
892
				// this is the 90% case: after a statement block
892
				// this is the 90% case: after a statement block
893
				// the end of the previous statement / block previous.end
893
				// the end of the previous statement / block previous.end
894
				// search to the end of the statement / block before the previous; the token just after that is previous.start
894
				// search to the end of the statement / block before the previous; the token just after that is previous.start
895
				return skipToStatementStart(danglingElse, false);
895
				pos= fPosition;
896
896
				if (isForStatement()) {
897
					fIndent= fPrefs.prefContinuationIndent;
898
					return fPosition;
899
				} else {
900
					fPosition= pos;
901
					return skipToStatementStart(danglingElse, false);
902
				}
897
			// scope introduction: special treat who special is
903
			// scope introduction: special treat who special is
898
			case Symbols.TokenLPAREN:
904
			case Symbols.TokenLPAREN:
899
			case Symbols.TokenLBRACE:
905
			case Symbols.TokenLBRACE:
Lines 906-913 Link Here
906
912
907
			case Symbols.TokenEQUAL:
913
			case Symbols.TokenEQUAL:
908
				// indent assignments
914
				// indent assignments
909
				fIndent= fPrefs.prefAssignmentIndent;
915
				return handleEqual();
910
				return fPosition;
911
916
912
			case Symbols.TokenCOLON:
917
			case Symbols.TokenCOLON:
913
				// TODO handle ternary deep indentation
918
				// TODO handle ternary deep indentation
Lines 958-963 Link Here
958
				fLine= line;
963
				fLine= line;
959
964
960
				return skipToPreviousListItemOrListStart();
965
				return skipToPreviousListItemOrListStart();
966
			case Symbols.TokenRETURN:
967
				fIndent= fPrefs.prefContinuationIndent;
968
				return fPosition;
961
			case Symbols.TokenCOMMA:
969
			case Symbols.TokenCOMMA:
962
				// inside a list of some type
970
				// inside a list of some type
963
				// easy if there is already a list item before with its own indentation - we just align
971
				// easy if there is already a list item before with its own indentation - we just align
Lines 972-977 Link Here
972
	}
980
	}
973
981
974
	/**
982
	/**
983
	 * Checks if the statement at position is itself a continuation of the previous, else sets the
984
	 * indentation to Continuation Indent.
985
	 * 
986
	 * @return the position of the token
987
	 * @since 3.7
988
	 */
989
	private int handleEqual() {
990
		try {
991
			//If this line is itself continuation of the previous then do nothing
992
			IRegion line= fDocument.getLineInformationOfOffset(fPosition);
993
			int nonWS= fScanner.findNonWhitespaceBackward(line.getOffset(), JavaHeuristicScanner.UNBOUND);
994
			if (nonWS != Symbols.TokenEOF) {
995
				int tokenAtPreviousLine= fScanner.nextToken(nonWS, nonWS + 1);
996
				if (tokenAtPreviousLine != Symbols.TokenSEMICOLON && tokenAtPreviousLine != Symbols.TokenRBRACE && tokenAtPreviousLine != Symbols.TokenLBRACE
997
						&& tokenAtPreviousLine != Symbols.TokenEOF)
998
					return fPosition;
999
			}
1000
		} catch (BadLocationException e) {
1001
			return fPosition;
1002
		}
1003
1004
		fIndent= fPrefs.prefContinuationIndent;
1005
		return fPosition;
1006
	}
1007
1008
	/**
1009
	 * Checks if the semicolon at the current position is part of a for statement.
1010
	 * 
1011
	 * @return returns <code>true</code> if current position is part of for statement
1012
	 * @since 3.7
1013
	 */
1014
	private boolean isForStatement() {
1015
		int semiColonCount= 1;
1016
		while (true) {
1017
			nextToken();
1018
			switch (fToken) {
1019
				case Symbols.TokenFOR:
1020
					return true;
1021
				case Symbols.TokenSEMICOLON:
1022
					semiColonCount++;
1023
					if (semiColonCount > 2)
1024
						return false;
1025
					break;
1026
				case Symbols.TokenEOF:
1027
					return false;
1028
			}
1029
		}
1030
	}
1031
1032
	/**
975
	 * Skips to the start of a statement that ends at the current position.
1033
	 * Skips to the start of a statement that ends at the current position.
976
	 *
1034
	 *
977
	 * @param danglingElse whether to indent aligned with the last <code>if</code>
1035
	 * @param danglingElse whether to indent aligned with the last <code>if</code>
Lines 1235-1240 Link Here
1235
					return handleScopeIntroduction(startPosition + 1);
1293
					return handleScopeIntroduction(startPosition + 1);
1236
1294
1237
				case Symbols.TokenSEMICOLON:
1295
				case Symbols.TokenSEMICOLON:
1296
					int savedPosition= fPosition;
1297
					if (isForStatement())
1298
						fIndent= fPrefs.prefContinuationIndent;
1299
					else
1300
						fPosition= savedPosition;
1238
					return fPosition;
1301
					return fPosition;
1239
				case Symbols.TokenQUESTIONMARK:
1302
				case Symbols.TokenQUESTIONMARK:
1240
					if (fPrefs.prefTernaryDeepAlign) {
1303
					if (fPrefs.prefTernaryDeepAlign) {
Lines 1244-1249 Link Here
1244
						fIndent= fPrefs.prefTernaryIndent;
1307
						fIndent= fPrefs.prefTernaryIndent;
1245
						return fPosition;
1308
						return fPosition;
1246
					}
1309
					}
1310
				case Symbols.TokenRETURN:
1311
					fIndent= fPrefs.prefContinuationIndent;
1312
					return fPosition;
1313
				case Symbols.TokenEQUAL:
1314
					return handleEqual();
1247
				case Symbols.TokenEOF:
1315
				case Symbols.TokenEOF:
1248
					return 0;
1316
					return 0;
1249
1317
(-)ui/org/eclipse/jdt/internal/ui/text/java/JavaAutoIndentStrategy.java (-23 / +21 lines)
Lines 693-724 Link Here
693
				if (lineLength == 0) // don't modify empty lines
693
				if (lineLength == 0) // don't modify empty lines
694
					continue;
694
					continue;
695
695
696
				if (!isIndentDetected) {
697
696
698
					// indent the first pasted line
697
				// indent the first pasted line
699
					String current= getCurrentIndent(temp, l);
698
				String current= getCurrentIndent(temp, l); //$NON-NLS-1$
700
					StringBuffer correct= indenter.computeIndentation(lineOffset);
699
				StringBuffer correct= indenter.computeIndentation(lineOffset);
701
					if (correct == null)
700
				if (correct == null)
702
						return; // bail out
701
					return; // bail out
703
702
704
					insertLength= subtractIndent(correct, current, addition, tabLength);
703
				insertLength= subtractIndent(correct, current, addition, tabLength);
705
					if (l != first && temp.get(lineOffset, lineLength).trim().length() != 0) {
704
				if (!isIndentDetected && l != first && temp.get(lineOffset, lineLength).trim().length() != 0) {
706
						isIndentDetected= true;
705
					isIndentDetected= true;
707
						if (insertLength == 0) {
706
					if (insertLength == 0) {
708
							 // no adjustment needed, bail out
707
						// no adjustment needed, bail out
709
							if (firstLine == 0) {
708
						if (firstLine == 0) {
710
								// but we still need to adjust the first line
709
							// but we still need to adjust the first line
711
								command.offset= newOffset;
710
							command.offset= newOffset;
712
								command.length= newLength;
711
							command.length= newLength;
713
								if (changed)
712
							if (changed)
714
									break; // still need to get the leading indent of the first line
713
								break; // still need to get the leading indent of the first line
715
							}
716
							return;
717
						}
714
						}
718
						removeJavaStuff(temp);
715
						return;
719
					} else {
720
						changed= insertLength != 0;
721
					}
716
					}
717
					removeJavaStuff(temp);
718
				} else {
719
					changed= insertLength != 0;
722
				}
720
				}
723
721
724
				// relatively indent all pasted lines
722
				// relatively indent all pasted lines

Return to bug 65317