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

Collapse All | Expand All

(-)src/org/eclipse/jdt/text/tests/JavaAutoIndentStrategyTest.java (-1 / +43 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2009 IBM Corporation and others.
2
 * Copyright (c) 2009, 2011 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 19-27 Link Here
19
19
20
import org.eclipse.text.tests.Accessor;
20
import org.eclipse.text.tests.Accessor;
21
21
22
import org.eclipse.jface.text.BadLocationException;
22
import org.eclipse.jface.text.Document;
23
import org.eclipse.jface.text.Document;
23
import org.eclipse.jface.text.DocumentCommand;
24
import org.eclipse.jface.text.DocumentCommand;
24
import org.eclipse.jface.text.IDocument;
25
import org.eclipse.jface.text.IDocument;
26
import org.eclipse.jface.text.IRegion;
25
import org.eclipse.jface.text.rules.FastPartitioner;
27
import org.eclipse.jface.text.rules.FastPartitioner;
26
28
27
import org.eclipse.jdt.ui.text.IJavaPartitions;
29
import org.eclipse.jdt.ui.text.IJavaPartitions;
Lines 88-93 Link Here
88
90
89
	}
91
	}
90
92
93
	public void testPasteLongStringWithContinuations() {
94
		fDocument.set("public class Test2 {\n}");
95
		JavaAutoIndentStrategy javaAutoIndentStrategy= new JavaAutoIndentStrategy(IJavaPartitions.JAVA_PARTITIONING, null, null);
96
		DocumentCommand documentCommand= new DocumentCommand() {
97
		};
98
		documentCommand.doit= true;
99
		documentCommand.offset= 21;
100
		documentCommand.text= "String[]a=new String[] {\n" +
101
				"\"X.java\",\n" +
102
				"\"public class X extends B{\n\"\n" +
103
				"\"    public static int field1;\n\"\n" +
104
				"\"    public static X xfield;\n\"\n" +
105
				"\"    public void bar1(int i) {\n\"\n" +
106
				"\"        field1 = 1;\n\"\n" +
107
				"\"    }\n\"\n" +
108
				"\"    public void bar2(int i) {\n\"\n" +
109
				"\"        this.field1 = 1;\n\"\n" +
110
				"\"    }\n\"\n" +
111
				"\"}\n\"\n" +
112
				"\"class A{\n\"\n" +
113
				"\"    public static X xA;\n\"\n" +
114
				"\"}\n\"\n" +
115
				"\"class B{\n\"\n" +
116
				"\"    public static int b1;\n\"\n" +
117
				"\"}\"\n" +
118
				"};";
119
120
		Accessor accessor= new Accessor(javaAutoIndentStrategy, JavaAutoIndentStrategy.class);
121
		accessor.invoke("smartPaste", new Class[] { IDocument.class, DocumentCommand.class }, new Object[] { fDocument, documentCommand });
122
		System.out.println(documentCommand.text);
123
		try {
124
			IRegion lineDetails= fDocument.getLineInformation(0);
125
			//System.out.println(fDocument.get(lineDetails.getOffset(), lineDetails.getLength()));
126
		} catch (BadLocationException e) {
127
			// TODO Auto-generated catch block
128
			e.printStackTrace();
129
		}
130
131
	}
132
91
	/*
133
	/*
92
	 * @see junit.framework.TestCase#setUp()
134
	 * @see junit.framework.TestCase#setUp()
93
	 */
135
	 */
(-)src/org/eclipse/jdt/text/tests/JavaHeuristicScannerTest.java (-38 / +145 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 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 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
				"\tint i= 25;\n" +
843
				"\t}");
844
845
		String indent= fScanner.computeIndentation(22).toString();
846
		Assert.assertEquals("\t\t", indent);
847
		indent= fScanner.computeIndentation(27).toString();
848
		Assert.assertEquals("\t\t", indent);
849
		indent= fScanner.computeIndentation(39).toString();
850
		Assert.assertEquals("\t\t", indent);
851
		indent= fScanner.computeIndentation(40).toString();
852
		Assert.assertEquals("\t\t", indent);
853
		indent= fScanner.computeIndentation(5).toString();
854
		Assert.assertEquals("\t", indent);
855
		indent= fScanner.computeIndentation(45).toString();
856
		Assert.assertEquals("\t", indent);
857
		indent= fScanner.computeIndentation(60).toString();
858
		Assert.assertEquals("\t", indent);
859
	}
860
861
	public void testContinuationIndentationOfBooleanExpression() throws Exception {
862
		fDocument.set("\tboolean a = true || false;\n" +
863
				"\tboolean b = a || false;\n");
864
865
		String indent= fScanner.computeIndentation(20).toString();
866
		Assert.assertEquals("\t\t", indent);
867
		indent= fScanner.computeIndentation(40).toString();
868
		Assert.assertEquals("\t\t", indent);
869
	}
870
871
	public void testContinuationIndentationOfReturnStatement() throws Exception {
872
		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");
873
874
		String indent= fScanner.computeIndentation(8).toString();
875
		Assert.assertEquals("\t\t\t", indent);
876
		indent= fScanner.computeIndentation(21).toString();
877
		Assert.assertEquals("\t\t\t", indent);
878
		indent= fScanner.computeIndentation(38).toString();
879
		Assert.assertEquals("\t\t\t", indent);
880
	}
881
882
	public void testContinuationIndentationOfAssignmentStatement() throws Exception {
883
		fDocument.set("\tint i= 5+");
884
885
		String indent= fScanner.computeIndentation(7).toString();
886
		Assert.assertEquals("\t\t", indent);
887
		indent= fScanner.computeIndentation(10).toString();
888
		Assert.assertEquals("\t\t", indent);
889
	}
890
891
	public void testContinuationIndentationOfThrowsClause() throws Exception {
892
		fDocument.set("\tprivate void thrower() throws java.sql.SQLException, java.io.IOException {");
893
894
		String indent= fScanner.computeIndentation(23).toString();
895
		Assert.assertEquals("\t\t", indent);
896
		indent= fScanner.computeIndentation(24).toString();
897
		Assert.assertEquals("\t\t", indent);
898
	}
899
900
	public void testContinuationIndentationOfBrackets() throws Exception {
901
		fDocument.set("\tprivate void helper2(boolean[] booleans) {\n\t}");
902
903
		String indent= fScanner.computeIndentation(31).toString();
904
		Assert.assertEquals("\t\t", indent);
905
		indent= fScanner.computeIndentation(30).toString();
906
		Assert.assertEquals("\t                             ", indent);
907
908
		fDocument.set("\tif (booleans[0]) {\n\t\tString[] aString= new String[]{\"a\", \"b\"};\n\t\tbooleans[5]= true;\n\t}");
909
		indent= fScanner.computeIndentation(16).toString();
910
		Assert.assertEquals("\t\t", indent);
911
		indent= fScanner.computeIndentation(14).toString();
912
		Assert.assertEquals("\t             ", indent);
913
		indent= fScanner.computeIndentation(30).toString();
914
		Assert.assertEquals("\t\t\t", indent);
915
		indent= fScanner.computeIndentation(52).toString();
916
		Assert.assertEquals("\t\t\t", indent);
917
		indent= fScanner.computeIndentation(77).toString();
918
		Assert.assertEquals("\t\t\t", indent);
919
	}
920
921
	public void testContinuationIndentationOfStrings() throws Exception {
922
		fDocument.set("\tString[] i = new String[] {\n\t\t\"X.java\",\n\t\t\"public class X extends B{\"\n\t\t+ \"test\"\n\t\t+ \"    public \"};");
923
924
		String indent= fScanner.computeIndentation(73).toString();
925
		Assert.assertEquals("\t\t\t", indent);
926
		indent= fScanner.computeIndentation(84).toString();
927
		Assert.assertEquals("\t\t\t", indent);
928
929
		fDocument.set("\tString[] i = new String[] {\n\t\t\"X.java\",\n\t\t\"public class X extends B{\" +\n\t\t\"test\" +\n\t\t\"    public\"\n};");
930
931
		indent= fScanner.computeIndentation(75).toString();
932
		Assert.assertEquals("\t\t\t", indent);
933
	}
934
935
936
	public void testContinuationIndentation1() throws Exception {
937
		fDocument.set("\treturn (thisIsAVeryLongName == 1 && anotherVeryLongName == 1)\n" +
938
				"\t\t|| thisIsAVeryLongName == 2;");
939
940
		String indent= fScanner.computeIndentation(68).toString();
941
		Assert.assertEquals("\t\t", indent);
942
		indent= fScanner.computeIndentation(88).toString();
943
		Assert.assertEquals("\t\t", indent);
944
	}
945
946
	public void testContinuationIndentation2() {
947
		fDocument.set("\tint a;\n" +
948
				"\tif (true)\n" +
949
				"\t\treturn a\n" +
950
				"");
951
952
		int pos= fScanner.findReferencePosition(29);
953
		Assert.assertEquals(21, pos);
954
	}
955
956
	public void testContinuationIndentation3() {
957
		fDocument.set("\tint a;\n" +
958
				"\tif (true)\n" +
959
				"\t\treturn a");
960
961
		String indent= fScanner.computeIndentation(28).toString();
962
		Assert.assertEquals("\t\t\t", indent);
963
	}
964
965
	public void testContinuationIndentation4() {
966
		fDocument.set("\tint a;\n" +
967
				"\tif (true)\n" +
968
				"\t\treturn a;");
969
970
		String indent= fScanner.computeIndentation(29).toString();
971
		Assert.assertEquals("\t\t\t", indent);
972
	}
973
974
	public void testContinuationIndentation5() {
975
		fDocument.set("\tint a;\n" +
976
				"\tif (true)\n" +
977
				"\t\treturn a\n" +
978
				"");
979
980
		String indent= fScanner.computeIndentation(29).toString();
981
		Assert.assertEquals("\t\t\t", indent);
982
	}
876
}
983
}
(-)ui/org/eclipse/jdt/internal/ui/text/JavaHeuristicScanner.java (-6 / +34 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 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 59-64 Link Here
59
	private static final char EQUAL= '=';
59
	private static final char EQUAL= '=';
60
	private static final char LANGLE= '<';
60
	private static final char LANGLE= '<';
61
	private static final char RANGLE= '>';
61
	private static final char RANGLE= '>';
62
	private static final char PLUS= '+';
62
63
63
	/**
64
	/**
64
	 * Specifies the stop condition, upon which the <code>scanXXX</code> methods will decide whether
65
	 * Specifies the stop condition, upon which the <code>scanXXX</code> methods will decide whether
Lines 333-338 Link Here
333
				return TokenLESSTHAN;
334
				return TokenLESSTHAN;
334
			case RANGLE:
335
			case RANGLE:
335
				return TokenGREATERTHAN;
336
				return TokenGREATERTHAN;
337
			case PLUS:
338
				return TokenPLUS;
336
		}
339
		}
337
340
338
		// else
341
		// else
Lines 405-410 Link Here
405
				return TokenLESSTHAN;
408
				return TokenLESSTHAN;
406
			case RANGLE:
409
			case RANGLE:
407
				return TokenGREATERTHAN;
410
				return TokenGREATERTHAN;
411
			case PLUS:
412
				return TokenPLUS;
408
		}
413
		}
409
414
410
		// else
415
		// else
Lines 485-490 Link Here
485
					return TokenSTATIC;
490
					return TokenSTATIC;
486
				if ("switch".equals(s)) //$NON-NLS-1$
491
				if ("switch".equals(s)) //$NON-NLS-1$
487
					return TokenSWITCH;
492
					return TokenSWITCH;
493
				if ("throws".equals(s)) //$NON-NLS-1$
494
					return TokenTHROWS;
488
				break;
495
				break;
489
			case 7:
496
			case 7:
490
				if ("default".equals(s)) //$NON-NLS-1$
497
				if ("default".equals(s)) //$NON-NLS-1$
Lines 672-684 Link Here
672
	}
679
	}
673
680
674
	/**
681
	/**
675
	 * Finds the lowest position <code>p</code> in <code>fDocument</code> such that <code>start</code> &lt;= p &lt;
682
	 * Finds the highest position in <code>fDocument</code> such that the position is &lt;=
676
	 * <code>bound</code> and <code>condition.stop(fDocument.getChar(p), p)</code> evaluates to <code>true</code>.
683
	 * <code>position</code> and &gt; <code>bound</code> and
677
	 *
684
	 * <code>Character.isWhitespace(fDocument.getChar(pos))</code> evaluates to <code>false</code>
685
	 * and the position can be in any partition.
686
	 * 
687
	 * @param position the first character position in <code>fDocument</code> to be considered
688
	 * @param bound the first position in <code>fDocument</code> to not consider any more, with
689
	 *            <code>bound</code> &lt; <code>position</code>, or <code>UNBOUND</code>
690
	 * @return the highest position of a non-whitespace character in (<code>bound</code>,
691
	 *         <code>position</code>] that resides in a Java partition, or <code>NOT_FOUND</code> if
692
	 *         none can be found
693
	 * @since 3.7
694
	 */
695
	public int findNonWhitespaceBackwardInAnyPartition(int position, int bound) {
696
		return scanBackward(position, bound, fNonWS);
697
	}
698
699
	/**
700
	 * Finds the lowest position <code>p</code> in <code>fDocument</code> such that
701
	 * <code>start</code> &lt;= p &lt; <code>bound</code> and
702
	 * <code>condition.stop(fDocument.getChar(p), p)</code> evaluates to <code>true</code>.
703
	 * 
678
	 * @param start the first character position in <code>fDocument</code> to be considered
704
	 * @param start the first character position in <code>fDocument</code> to be considered
679
	 * @param bound the first position in <code>fDocument</code> to not consider any more, with <code>bound</code> &gt; <code>start</code>, or <code>UNBOUND</code>
705
	 * @param bound the first position in <code>fDocument</code> to not consider any more, with
706
	 *            <code>bound</code> &gt; <code>start</code>, or <code>UNBOUND</code>
680
	 * @param condition the <code>StopCondition</code> to check
707
	 * @param condition the <code>StopCondition</code> to check
681
	 * @return the lowest position in [<code>start</code>, <code>bound</code>) for which <code>condition</code> holds, or <code>NOT_FOUND</code> if none can be found
708
	 * @return the lowest position in [<code>start</code>, <code>bound</code>) for which
709
	 *         <code>condition</code> holds, or <code>NOT_FOUND</code> if none can be found
682
	 */
710
	 */
683
	public int scanForward(int start, int bound, StopCondition condition) {
711
	public int scanForward(int start, int bound, StopCondition condition) {
684
		Assert.isLegal(start >= 0);
712
		Assert.isLegal(start >= 0);
(-)ui/org/eclipse/jdt/internal/ui/text/JavaIndenter.java (-34 / +184 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 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 22-27 Link Here
22
22
23
import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
23
import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
24
24
25
import org.eclipse.jdt.internal.ui.JavaPlugin;
25
26
26
/**
27
/**
27
 * Uses the {@link org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner} to
28
 * Uses the {@link org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner} to
Lines 50-56 Link Here
50
		final boolean prefTernaryDeepAlign;
51
		final boolean prefTernaryDeepAlign;
51
		final int prefTernaryIndent;
52
		final int prefTernaryIndent;
52
		final int prefCaseIndent;
53
		final int prefCaseIndent;
53
		final int prefAssignmentIndent;
54
		final int prefCaseBlockIndent;
54
		final int prefCaseBlockIndent;
55
		final int prefSimpleIndent;
55
		final int prefSimpleIndent;
56
		final int prefBracketIndent;
56
		final int prefBracketIndent;
Lines 110-116 Link Here
110
				prefTernaryDeepAlign= false;
110
				prefTernaryDeepAlign= false;
111
				prefTernaryIndent= prefContinuationIndent;
111
				prefTernaryIndent= prefContinuationIndent;
112
				prefCaseIndent= 0;
112
				prefCaseIndent= 0;
113
				prefAssignmentIndent= prefBlockIndent;
114
				prefCaseBlockIndent= prefBlockIndent;
113
				prefCaseBlockIndent= prefBlockIndent;
115
				prefIndentBracesForBlocks= false;
114
				prefIndentBracesForBlocks= false;
116
				prefSimpleIndent= (prefIndentBracesForBlocks && prefBlockIndent == 0) ? 1 : prefBlockIndent;
115
				prefSimpleIndent= (prefIndentBracesForBlocks && prefBlockIndent == 0) ? 1 : prefBlockIndent;
Lines 140-146 Link Here
140
				prefTernaryDeepAlign= prefTernaryDeepAlign();
139
				prefTernaryDeepAlign= prefTernaryDeepAlign();
141
				prefTernaryIndent= prefTernaryIndent();
140
				prefTernaryIndent= prefTernaryIndent();
142
				prefCaseIndent= prefCaseIndent();
141
				prefCaseIndent= prefCaseIndent();
143
				prefAssignmentIndent= prefAssignmentIndent();
144
				prefCaseBlockIndent= prefCaseBlockIndent();
142
				prefCaseBlockIndent= prefCaseBlockIndent();
145
				prefIndentBracesForBlocks= prefIndentBracesForBlocks();
143
				prefIndentBracesForBlocks= prefIndentBracesForBlocks();
146
				prefSimpleIndent= prefSimpleIndent();
144
				prefSimpleIndent= prefSimpleIndent();
Lines 231-240 Link Here
231
				return 0;
229
				return 0;
232
		}
230
		}
233
231
234
		private int prefAssignmentIndent() {
235
			return prefBlockIndent();
236
		}
237
238
		private int prefCaseBlockIndent() {
232
		private int prefCaseBlockIndent() {
239
			if (DefaultCodeFormatterConstants.TRUE.equals(getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_CASES)))
233
			if (DefaultCodeFormatterConstants.TRUE.equals(getCoreFormatterOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_CASES)))
240
				return prefBlockIndent();
234
				return prefBlockIndent();
Lines 750-755 Link Here
750
		boolean matchBrace= false;
744
		boolean matchBrace= false;
751
		boolean matchParen= false;
745
		boolean matchParen= false;
752
		boolean matchCase= false;
746
		boolean matchCase= false;
747
		boolean throwsClause= false;
753
748
754
		// account for un-indentation characters already typed in, but after position
749
		// account for un-indentation characters already typed in, but after position
755
		// if they are on a line by themselves, the indentation gets adjusted
750
		// if they are on a line by themselves, the indentation gets adjusted
Lines 777-783 Link Here
777
					case Symbols.TokenLBRACE: // for opening-brace-on-new-line style
772
					case Symbols.TokenLBRACE: // for opening-brace-on-new-line style
778
						if (bracelessBlockStart && !fPrefs.prefIndentBracesForBlocks)
773
						if (bracelessBlockStart && !fPrefs.prefIndentBracesForBlocks)
779
							unindent= true;
774
							unindent= true;
780
						else if ((prevToken == Symbols.TokenCOLON || prevToken == Symbols.TokenEQUAL || prevToken == Symbols.TokenRBRACKET) && !fPrefs.prefIndentBracesForArrays)
775
						else if ((prevToken == Symbols.TokenCOLON || prevToken == Symbols.TokenEQUAL) && !fPrefs.prefIndentBracesForArrays)
781
							unindent= true;
776
							unindent= true;
782
						else if (!bracelessBlockStart && fPrefs.prefIndentBracesForMethods)
777
						else if (!bracelessBlockStart && fPrefs.prefIndentBracesForMethods)
783
							indent= true;
778
							indent= true;
Lines 790-795 Link Here
790
						if (isFirstTokenOnLine)
785
						if (isFirstTokenOnLine)
791
							matchParen= true;
786
							matchParen= true;
792
						break;
787
						break;
788
					case Symbols.TokenTHROWS:
789
						throwsClause= true;
790
						break;
791
					case Symbols.TokenPLUS:
792
						int position= handleStringContinuation(offset);
793
						if (position != JavaHeuristicScanner.NOT_FOUND) {
794
							fAlign= JavaHeuristicScanner.NOT_FOUND;
795
							fIndent= fPrefs.prefContinuationIndent;
796
							return position;
797
						}
798
						break;
793
				}
799
				}
794
			} catch (BadLocationException e) {
800
			} catch (BadLocationException e) {
795
			}
801
			}
Lines 798-804 Link Here
798
			danglingElse= false;
804
			danglingElse= false;
799
		}
805
		}
800
806
801
		int ref= findReferencePosition(offset, danglingElse, matchBrace, matchParen, matchCase);
807
		int ref= findReferencePosition(offset, danglingElse, matchBrace, matchParen, matchCase, throwsClause);
802
		if (unindent)
808
		if (unindent)
803
			fIndent--;
809
			fIndent--;
804
		if (indent)
810
		if (indent)
Lines 807-833 Link Here
807
	}
813
	}
808
814
809
	/**
815
	/**
810
	 * Returns the reference position regarding to indentation for <code>position</code>,
816
	 * Specifically handles the case of extra indentation for second line of string continuation.
811
	 * or <code>NOT_FOUND</code>.<code>fIndent</code> will contain the
817
	 * 
812
	 * relative indentation (in indentation units, not characters) after the
818
	 * @param offset the offset for which the reference is computed
813
	 * call. If there is a special alignment (e.g. for a method declaration
819
	 * @return the reference statement relative to which <code>offset</code> should be indented, or
814
	 * where parameters should be aligned), <code>fAlign</code> will contain
820
	 *         {@link JavaHeuristicScanner#NOT_FOUND}
815
	 * the absolute position of the alignment reference in <code>fDocument</code>,
821
	 * @since 3.7
816
	 * otherwise <code>fAlign</code> is set to <code>JavaHeuristicScanner.NOT_FOUND</code>.
822
	 */
817
	 *
823
	private int handleStringContinuation(int offset) {
824
		int prevNonWSCharPosition= fScanner.findNonWhitespaceBackwardInAnyPartition(offset - 1, JavaHeuristicScanner.UNBOUND);
825
		if (prevNonWSCharPosition != JavaHeuristicScanner.NOT_FOUND) {
826
			try {
827
				char c= fDocument.getChar(prevNonWSCharPosition);
828
				if (c == '"' || c == '+') {
829
					int initialLine= fDocument.getLineOfOffset(offset);
830
					nextToken(offset);
831
					while (fToken == Symbols.TokenPLUS) {
832
						if ((initialLine - fLine) > 1)
833
							return JavaHeuristicScanner.NOT_FOUND;
834
						nextToken();
835
					}
836
					int lineDiff= initialLine - fLine;
837
					if (lineDiff > 0) {
838
						int bound= fDocument.getLineOffset(fLine) + fDocument.getLineLength(fLine) - 1;
839
						int nextNonWSCharPosition= fScanner.findNonWhitespaceForwardInAnyPartition(fPosition + 1, bound);
840
						if (lineDiff < 3 && fPreviousPos != offset && nextNonWSCharPosition != JavaHeuristicScanner.NOT_FOUND && fDocument.getChar(nextNonWSCharPosition) != '"')
841
							return fPreviousPos;
842
						else
843
							return fPosition;
844
					}
845
				}
846
			} catch (BadLocationException e) {
847
				JavaPlugin.log(e);
848
			}
849
		}
850
		return JavaHeuristicScanner.NOT_FOUND;
851
	}
852
853
	/**
854
	 * Returns the reference position regarding to indentation for <code>position</code>, or
855
	 * <code>NOT_FOUND</code>.<code>fIndent</code> will contain the relative indentation (in
856
	 * indentation units, not characters) after the call. If there is a special alignment (e.g. for
857
	 * a method declaration where parameters should be aligned), <code>fAlign</code> will contain
858
	 * the absolute position of the alignment reference in <code>fDocument</code>, otherwise
859
	 * <code>fAlign</code> is set to <code>JavaHeuristicScanner.NOT_FOUND</code>. This method calls
860
	 * {@link #findReferencePosition(int, boolean, boolean, boolean, boolean, boolean)
861
	 * findReferencePosition(offset, danglingElse, matchBrace, matchParen, matchCase, throwsClause)}
862
	 * where <code>throwsClause</code> indicates whether a throws clause was found at
863
	 * <code>position</code>.
864
	 * 
818
	 * @param offset the offset for which the reference is computed
865
	 * @param offset the offset for which the reference is computed
819
	 * @param danglingElse whether a dangling else should be assumed at <code>position</code>
866
	 * @param danglingElse whether a dangling else should be assumed at <code>position</code>
820
	 * @param matchBrace whether the position of the matching brace should be
867
	 * @param matchBrace whether the position of the matching brace should be returned instead of
821
	 *            returned instead of doing code analysis
868
	 *            doing code analysis
822
	 * @param matchParen whether the position of the matching parenthesis
869
	 * @param matchParen whether the position of the matching parenthesis should be returned instead
823
	 *            should be returned instead of doing code analysis
870
	 *            of doing code analysis
824
	 * @param matchCase whether the position of a switch statement reference
871
	 * @param matchCase whether the position of a switch statement reference should be returned
825
	 *            should be returned (either an earlier case statement or the
872
	 *            (either an earlier case statement or the switch block brace)
826
	 *            switch block brace)
873
	 * @return the reference statement relative to which <code>position</code> should be indented,
827
	 * @return the reference statement relative to which <code>position</code>
874
	 *         or {@link JavaHeuristicScanner#NOT_FOUND}
828
	 *         should be indented, or {@link JavaHeuristicScanner#NOT_FOUND}
829
	 */
875
	 */
830
	public int findReferencePosition(int offset, boolean danglingElse, boolean matchBrace, boolean matchParen, boolean matchCase) {
876
	public int findReferencePosition(int offset, boolean danglingElse, boolean matchBrace, boolean matchParen, boolean matchCase) {
877
		return findReferencePosition(offset, danglingElse, matchBrace, matchParen, matchCase, false);
878
	}
879
880
	/**
881
	 * Returns the reference position regarding to indentation for <code>position</code>, or
882
	 * <code>NOT_FOUND</code>.<code>fIndent</code> will contain the relative indentation (in
883
	 * indentation units, not characters) after the call. If there is a special alignment (e.g. for
884
	 * a method declaration where parameters should be aligned), <code>fAlign</code> will contain
885
	 * the absolute position of the alignment reference in <code>fDocument</code>, otherwise
886
	 * <code>fAlign</code> is set to <code>JavaHeuristicScanner.NOT_FOUND</code>.
887
	 * 
888
	 * @param offset the offset for which the reference is computed
889
	 * @param danglingElse whether a dangling else should be assumed at <code>position</code>
890
	 * @param matchBrace whether the position of the matching brace should be returned instead of
891
	 *            doing code analysis
892
	 * @param matchParen whether the position of the matching parenthesis should be returned instead
893
	 *            of doing code analysis
894
	 * @param matchCase whether the position of a switch statement reference should be returned
895
	 *            (either an earlier case statement or the switch block brace)
896
	 * @param throwsClause whether a throws clause was found at <code>position</code>
897
	 * @return the reference statement relative to which <code>position</code> should be indented,
898
	 *         or {@link JavaHeuristicScanner#NOT_FOUND}
899
	 * @since 3.7
900
	 */
901
	public int findReferencePosition(int offset, boolean danglingElse, boolean matchBrace, boolean matchParen, boolean matchCase, boolean throwsClause) {
831
		fIndent= 0; // the indentation modification
902
		fIndent= 0; // the indentation modification
832
		fAlign= JavaHeuristicScanner.NOT_FOUND;
903
		fAlign= JavaHeuristicScanner.NOT_FOUND;
833
		fPosition= offset;
904
		fPosition= offset;
Lines 853-859 Link Here
853
			} else {
924
			} else {
854
				// if we can't find the matching brace, the heuristic is to unindent
925
				// if we can't find the matching brace, the heuristic is to unindent
855
				// by one against the normal position
926
				// by one against the normal position
856
				int pos= findReferencePosition(offset, danglingElse, false, matchParen, matchCase);
927
				int pos= findReferencePosition(offset, danglingElse, false, matchParen, matchCase, throwsClause);
857
				fIndent--;
928
				fIndent--;
858
				return pos;
929
				return pos;
859
			}
930
			}
Lines 866-872 Link Here
866
			else {
937
			else {
867
				// if we can't find the matching paren, the heuristic is to unindent
938
				// if we can't find the matching paren, the heuristic is to unindent
868
				// by one against the normal position
939
				// by one against the normal position
869
				int pos= findReferencePosition(offset, danglingElse, matchBrace, false, matchCase);
940
				int pos= findReferencePosition(offset, danglingElse, matchBrace, false, matchCase, throwsClause);
870
				fIndent--;
941
				fIndent--;
871
				return pos;
942
				return pos;
872
			}
943
			}
Lines 887-899 Link Here
887
				int pos= fPosition;
958
				int pos= fPosition;
888
				if (!skipScope())
959
				if (!skipScope())
889
					fPosition= pos;
960
					fPosition= pos;
890
				//$FALL-THROUGH$
961
				return skipToStatementStart(danglingElse, false);
891
			case Symbols.TokenSEMICOLON:
962
			case Symbols.TokenSEMICOLON:
892
				// this is the 90% case: after a statement block
963
				// this is the 90% case: after a statement block
893
				// the end of the previous statement / block previous.end
964
				// 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
965
				// search to the end of the statement / block before the previous; the token just after that is previous.start
895
				return skipToStatementStart(danglingElse, false);
966
				pos= fPosition;
896
967
				if (isForStatement()) {
968
					fIndent= fPrefs.prefContinuationIndent;
969
					return fPosition;
970
				} else {
971
					fPosition= pos;
972
					return skipToStatementStart(danglingElse, false);
973
				}
897
			// scope introduction: special treat who special is
974
			// scope introduction: special treat who special is
898
			case Symbols.TokenLPAREN:
975
			case Symbols.TokenLPAREN:
899
			case Symbols.TokenLBRACE:
976
			case Symbols.TokenLBRACE:
Lines 906-913 Link Here
906
983
907
			case Symbols.TokenEQUAL:
984
			case Symbols.TokenEQUAL:
908
				// indent assignments
985
				// indent assignments
909
				fIndent= fPrefs.prefAssignmentIndent;
986
				return handleEqual();
910
				return fPosition;
911
987
912
			case Symbols.TokenCOLON:
988
			case Symbols.TokenCOLON:
913
				// TODO handle ternary deep indentation
989
				// TODO handle ternary deep indentation
Lines 932-938 Link Here
932
1008
933
			case Symbols.TokenTRY:
1009
			case Symbols.TokenTRY:
934
				return skipToStatementStart(danglingElse, false);
1010
				return skipToStatementStart(danglingElse, false);
1011
1012
			case Symbols.TokenRBRACKET:
1013
				fIndent= fPrefs.prefContinuationIndent;
1014
				return fPosition;
1015
935
			case Symbols.TokenRPAREN:
1016
			case Symbols.TokenRPAREN:
1017
				if (throwsClause) {
1018
					fIndent= fPrefs.prefContinuationIndent;
1019
					return fPosition;
1020
				}
936
				int line= fLine;
1021
				int line= fLine;
937
				if (skipScope(Symbols.TokenLPAREN, Symbols.TokenRPAREN)) {
1022
				if (skipScope(Symbols.TokenLPAREN, Symbols.TokenRPAREN)) {
938
					int scope= fPosition;
1023
					int scope= fPosition;
Lines 958-963 Link Here
958
				fLine= line;
1043
				fLine= line;
959
1044
960
				return skipToPreviousListItemOrListStart();
1045
				return skipToPreviousListItemOrListStart();
1046
			case Symbols.TokenRETURN:
1047
				fIndent= fPrefs.prefContinuationIndent;
1048
				return fPosition;
961
			case Symbols.TokenCOMMA:
1049
			case Symbols.TokenCOMMA:
962
				// inside a list of some type
1050
				// inside a list of some type
963
				// easy if there is already a list item before with its own indentation - we just align
1051
				// easy if there is already a list item before with its own indentation - we just align
Lines 972-977 Link Here
972
	}
1060
	}
973
1061
974
	/**
1062
	/**
1063
	 * Checks if the statement at position is itself a continuation of the previous, else sets the
1064
	 * indentation to Continuation Indent.
1065
	 * 
1066
	 * @return the position of the token
1067
	 * @since 3.7
1068
	 */
1069
	private int handleEqual() {
1070
		try {
1071
			//If this line is itself continuation of the previous then do nothing
1072
			IRegion line= fDocument.getLineInformationOfOffset(fPosition);
1073
			int nonWS= fScanner.findNonWhitespaceBackward(line.getOffset(), JavaHeuristicScanner.UNBOUND);
1074
			if (nonWS != Symbols.TokenEOF) {
1075
				int tokenAtPreviousLine= fScanner.nextToken(nonWS, nonWS + 1);
1076
				if (tokenAtPreviousLine != Symbols.TokenSEMICOLON && tokenAtPreviousLine != Symbols.TokenRBRACE && tokenAtPreviousLine != Symbols.TokenLBRACE
1077
						&& tokenAtPreviousLine != Symbols.TokenEOF)
1078
					return fPosition;
1079
			}
1080
		} catch (BadLocationException e) {
1081
			return fPosition;
1082
		}
1083
1084
		fIndent= fPrefs.prefContinuationIndent;
1085
		return fPosition;
1086
	}
1087
1088
	/**
1089
	 * Checks if the semicolon at the current position is part of a for statement.
1090
	 * 
1091
	 * @return returns <code>true</code> if current position is part of for statement
1092
	 * @since 3.7
1093
	 */
1094
	private boolean isForStatement() {
1095
		int semiColonCount= 1;
1096
		while (true) {
1097
			nextToken();
1098
			switch (fToken) {
1099
				case Symbols.TokenFOR:
1100
					return true;
1101
				case Symbols.TokenLBRACE:
1102
					return false;
1103
				case Symbols.TokenSEMICOLON:
1104
					semiColonCount++;
1105
					if (semiColonCount > 2)
1106
						return false;
1107
					break;
1108
				case Symbols.TokenEOF:
1109
					return false;
1110
			}
1111
		}
1112
	}
1113
1114
	/**
975
	 * Skips to the start of a statement that ends at the current position.
1115
	 * Skips to the start of a statement that ends at the current position.
976
	 *
1116
	 *
977
	 * @param danglingElse whether to indent aligned with the last <code>if</code>
1117
	 * @param danglingElse whether to indent aligned with the last <code>if</code>
Lines 1235-1240 Link Here
1235
					return handleScopeIntroduction(startPosition + 1);
1375
					return handleScopeIntroduction(startPosition + 1);
1236
1376
1237
				case Symbols.TokenSEMICOLON:
1377
				case Symbols.TokenSEMICOLON:
1378
					int savedPosition= fPosition;
1379
					if (isForStatement())
1380
						fIndent= fPrefs.prefContinuationIndent;
1381
					else
1382
						fPosition= savedPosition;
1238
					return fPosition;
1383
					return fPosition;
1239
				case Symbols.TokenQUESTIONMARK:
1384
				case Symbols.TokenQUESTIONMARK:
1240
					if (fPrefs.prefTernaryDeepAlign) {
1385
					if (fPrefs.prefTernaryDeepAlign) {
Lines 1244-1249 Link Here
1244
						fIndent= fPrefs.prefTernaryIndent;
1389
						fIndent= fPrefs.prefTernaryIndent;
1245
						return fPosition;
1390
						return fPosition;
1246
					}
1391
					}
1392
				case Symbols.TokenRETURN:
1393
					fIndent= fPrefs.prefContinuationIndent;
1394
					return fPosition;
1395
				case Symbols.TokenEQUAL:
1396
					return handleEqual();
1247
				case Symbols.TokenEOF:
1397
				case Symbols.TokenEOF:
1248
					return 0;
1398
					return 0;
1249
1399
(-)ui/org/eclipse/jdt/internal/ui/text/java/JavaAutoIndentStrategy.java (-2 / +2 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 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 715-721 Link Here
715
							}
715
							}
716
							return;
716
							return;
717
						}
717
						}
718
						removeJavaStuff(temp);
719
					} else {
718
					} else {
720
						changed= insertLength != 0;
719
						changed= insertLength != 0;
721
					}
720
					}
Lines 729-734 Link Here
729
728
730
			}
729
			}
731
730
731
			removeJavaStuff(temp);
732
			temp.stopRewriteSession(session);
732
			temp.stopRewriteSession(session);
733
			newText= temp.get(prefix.length(), temp.getLength() - prefix.length());
733
			newText= temp.get(prefix.length(), temp.getLength() - prefix.length());
734
734

Return to bug 65317