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 330556 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jdt/text/tests/JavaHeuristicScannerTest.java (+35 lines)
Lines 895-900 Link Here
895
		Assert.assertEquals("\t\t", indent);
895
		Assert.assertEquals("\t\t", indent);
896
	}
896
	}
897
897
898
	public void testContinuationIndentationOfBrackets() throws Exception {
899
		fDocument.set("\tprivate void helper2(boolean[] booleans) {\n\t}");
900
901
		String indent= fScanner.computeIndentation(31).toString();
902
		Assert.assertEquals("\t\t", indent);
903
		indent= fScanner.computeIndentation(30).toString();
904
		Assert.assertEquals("\t                             ", indent);
905
906
		fDocument.set("\tif (booleans[0]) {\n\t\tString[] aString= new String[]{\"a\", \"b\"};\n\t\tbooleans[5]= true;\n\t}");
907
		indent= fScanner.computeIndentation(16).toString();
908
		Assert.assertEquals("\t\t", indent);
909
		indent= fScanner.computeIndentation(14).toString();
910
		Assert.assertEquals("\t             ", indent);
911
		indent= fScanner.computeIndentation(30).toString();
912
		Assert.assertEquals("\t\t\t", indent);
913
		indent= fScanner.computeIndentation(52).toString();
914
		Assert.assertEquals("\t\t\t", indent);
915
		indent= fScanner.computeIndentation(77).toString();
916
		Assert.assertEquals("\t\t\t", indent);
917
	}
918
919
	public void testContinuationIndentationOfStrings() throws Exception {
920
		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 \"};");
921
922
		String indent= fScanner.computeIndentation(73).toString();
923
		Assert.assertEquals("\t\t\t", indent);
924
		indent= fScanner.computeIndentation(84).toString();
925
		Assert.assertEquals("\t\t\t", indent);
926
927
		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};");
928
929
		indent= fScanner.computeIndentation(75).toString();
930
		Assert.assertEquals("\t\t\t", indent);
931
	}
932
898
	public void testContinuationIndentation1() throws Exception {
933
	public void testContinuationIndentation1() throws Exception {
899
		fDocument.set("\treturn (thisIsAVeryLongName == 1 && anotherVeryLongName == 1)\n" +
934
		fDocument.set("\treturn (thisIsAVeryLongName == 1 && anotherVeryLongName == 1)\n" +
900
				"\t\t|| thisIsAVeryLongName == 2;");
935
				"\t\t|| thisIsAVeryLongName == 2;");
(-)ui/org/eclipse/jdt/internal/ui/text/JavaHeuristicScanner.java (+24 lines)
Lines 61-66 Link Here
61
	private static final char LANGLE= '<';
61
	private static final char LANGLE= '<';
62
	private static final char RANGLE= '>';
62
	private static final char RANGLE= '>';
63
63
64
	private static final char PLUS= '+';
65
64
	/**
66
	/**
65
	 * Specifies the stop condition, upon which the <code>scanXXX</code> methods will decide whether
67
	 * Specifies the stop condition, upon which the <code>scanXXX</code> methods will decide whether
66
	 * to keep scanning or not. This interface may implemented by clients.
68
	 * to keep scanning or not. This interface may implemented by clients.
Lines 334-339 Link Here
334
				return TokenLESSTHAN;
336
				return TokenLESSTHAN;
335
			case RANGLE:
337
			case RANGLE:
336
				return TokenGREATERTHAN;
338
				return TokenGREATERTHAN;
339
			case PLUS:
340
				return TokenPLUS;
337
		}
341
		}
338
342
339
		// else
343
		// else
Lines 406-411 Link Here
406
				return TokenLESSTHAN;
410
				return TokenLESSTHAN;
407
			case RANGLE:
411
			case RANGLE:
408
				return TokenGREATERTHAN;
412
				return TokenGREATERTHAN;
413
			case PLUS:
414
				return TokenPLUS;
409
		}
415
		}
410
416
411
		// else
417
		// else
Lines 675-680 Link Here
675
	}
681
	}
676
682
677
	/**
683
	/**
684
	 * Finds the highest position in <code>fDocument</code> such that the position is &lt;=
685
	 * <code>position</code> and &gt; <code>bound</code> and
686
	 * <code>Character.isWhitespace(fDocument.getChar(pos))</code> evaluates to <code>false</code>
687
	 * and the position can be in any partition.
688
	 * 
689
	 * @param position the first character position in <code>fDocument</code> to be considered
690
	 * @param bound the first position in <code>fDocument</code> to not consider any more, with
691
	 *            <code>bound</code> &lt; <code>position</code>, or <code>UNBOUND</code>
692
	 * @return the highest position of a non-whitespace character in (<code>bound</code>,
693
	 *         <code>position</code>] that resides in a Java partition, or <code>NOT_FOUND</code> if
694
	 *         none can be found
695
	 * @since 3.7
696
	 */
697
	public int findNonWhitespaceBackwardInAnyPartition(int position, int bound) {
698
		return scanBackward(position, bound, fNonWS);
699
	}
700
701
	/**
678
	 * Finds the lowest position <code>p</code> in <code>fDocument</code> such that <code>start</code> &lt;= p &lt;
702
	 * Finds the lowest position <code>p</code> in <code>fDocument</code> such that <code>start</code> &lt;= p &lt;
679
	 * <code>bound</code> and <code>condition.stop(fDocument.getChar(p), p)</code> evaluates to <code>true</code>.
703
	 * <code>bound</code> and <code>condition.stop(fDocument.getChar(p), p)</code> evaluates to <code>true</code>.
680
	 *
704
	 *
(-)ui/org/eclipse/jdt/internal/ui/text/JavaIndenter.java (-1 / +54 lines)
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 771-777 Link Here
771
					case Symbols.TokenLBRACE: // for opening-brace-on-new-line style
772
					case Symbols.TokenLBRACE: // for opening-brace-on-new-line style
772
						if (bracelessBlockStart && !fPrefs.prefIndentBracesForBlocks)
773
						if (bracelessBlockStart && !fPrefs.prefIndentBracesForBlocks)
773
							unindent= true;
774
							unindent= true;
774
						else if ((prevToken == Symbols.TokenCOLON || prevToken == Symbols.TokenEQUAL || prevToken == Symbols.TokenRBRACKET) && !fPrefs.prefIndentBracesForArrays)
775
						//Bracket Indentation Fix
776
						else if ((prevToken == Symbols.TokenCOLON || prevToken == Symbols.TokenEQUAL) && !fPrefs.prefIndentBracesForArrays)
775
							unindent= true;
777
							unindent= true;
776
						else if (!bracelessBlockStart && fPrefs.prefIndentBracesForMethods)
778
						else if (!bracelessBlockStart && fPrefs.prefIndentBracesForMethods)
777
							indent= true;
779
							indent= true;
Lines 787-792 Link Here
787
					case Symbols.TokenTHROWS:
789
					case Symbols.TokenTHROWS:
788
						throwsClause= true;
790
						throwsClause= true;
789
						break;
791
						break;
792
					case Symbols.TokenPLUS:
793
						int position= handleStringContinuation(offset);
794
						if (position != JavaHeuristicScanner.NOT_FOUND) {
795
							fAlign= JavaHeuristicScanner.NOT_FOUND;
796
							fIndent= fPrefs.prefContinuationIndent;
797
							return position;
798
						}
799
						break;
790
				}
800
				}
791
			} catch (BadLocationException e) {
801
			} catch (BadLocationException e) {
792
			}
802
			}
Lines 804-809 Link Here
804
	}
814
	}
805
815
806
	/**
816
	/**
817
	 * Specifically handles the case of extra indentation for second line of string continuation.
818
	 * 
819
	 * @param offset the offset for which the reference is computed
820
	 * @return the reference statement relative to which <code>offset</code> should be indented, or
821
	 *         {@link JavaHeuristicScanner#NOT_FOUND}
822
	 * @since 3.7
823
	 */
824
	private int handleStringContinuation(int offset) {
825
		int prevNonWSCharPosition= fScanner.findNonWhitespaceBackwardInAnyPartition(offset - 1, JavaHeuristicScanner.UNBOUND);
826
		if (prevNonWSCharPosition != JavaHeuristicScanner.NOT_FOUND) {
827
			try {
828
				char c= fDocument.getChar(prevNonWSCharPosition);
829
				if (c == '"' || c == '+') {
830
					int initialLine= fDocument.getLineOfOffset(offset);
831
					nextToken(offset);
832
					while (fToken == Symbols.TokenPLUS) {
833
						if ((initialLine - fLine) > 1)
834
							return JavaHeuristicScanner.NOT_FOUND;
835
						nextToken();
836
					}
837
					int lineDiff= initialLine - fLine;
838
					if (lineDiff > 0) {
839
						int bound= fDocument.getLineOffset(fLine) + fDocument.getLineLength(fLine) - 1;
840
						int nextNonWSCharPosition= fScanner.findNonWhitespaceForwardInAnyPartition(fPosition + 1, bound);
841
						if (lineDiff < 3 && fPreviousPos != offset && nextNonWSCharPosition != JavaHeuristicScanner.NOT_FOUND && fDocument.getChar(nextNonWSCharPosition) != '"')
842
							return fPreviousPos;
843
						else
844
							return fPosition;
845
					}
846
				}
847
			} catch (BadLocationException e) {
848
				JavaPlugin.log(e);
849
			}
850
		}
851
		return JavaHeuristicScanner.NOT_FOUND;
852
	}
853
854
	/**
807
	 * Returns the reference position regarding to indentation for <code>position</code>, or
855
	 * Returns the reference position regarding to indentation for <code>position</code>, or
808
	 * <code>NOT_FOUND</code>.<code>fIndent</code> will contain the relative indentation (in
856
	 * <code>NOT_FOUND</code>.<code>fIndent</code> will contain the relative indentation (in
809
	 * indentation units, not characters) after the call. If there is a special alignment (e.g. for
857
	 * indentation units, not characters) after the call. If there is a special alignment (e.g. for
Lines 961-966 Link Here
961
1009
962
			case Symbols.TokenTRY:
1010
			case Symbols.TokenTRY:
963
				return skipToStatementStart(danglingElse, false);
1011
				return skipToStatementStart(danglingElse, false);
1012
			//Bracket Indentation Fix	
1013
			case Symbols.TokenRBRACKET:
1014
				fIndent= fPrefs.prefContinuationIndent;
1015
				return fPosition;
1016
964
			case Symbols.TokenRPAREN:
1017
			case Symbols.TokenRPAREN:
965
				if (throwsClause) {
1018
				if (throwsClause) {
966
					fIndent= fPrefs.prefContinuationIndent;
1019
					fIndent= fPrefs.prefContinuationIndent;
(-)ui/org/eclipse/jdt/internal/ui/text/Symbols.java (+1 lines)
Lines 32-37 Link Here
32
	int TokenEQUAL= 12;
32
	int TokenEQUAL= 12;
33
	int TokenLESSTHAN= 13;
33
	int TokenLESSTHAN= 13;
34
	int TokenGREATERTHAN= 14;
34
	int TokenGREATERTHAN= 14;
35
	int TokenPLUS= 15;
35
	int TokenIF= 109;
36
	int TokenIF= 109;
36
	int TokenDO= 1010;
37
	int TokenDO= 1010;
37
	int TokenFOR= 1011;
38
	int TokenFOR= 1011;
(-)ui/org/eclipse/jdt/internal/ui/text/java/JavaAutoIndentStrategy.java (-1 / +1 lines)
Lines 714-720 Link Here
714
						}
714
						}
715
						return;
715
						return;
716
					}
716
					}
717
					removeJavaStuff(temp);
718
				} else {
717
				} else {
719
					changed= insertLength != 0;
718
					changed= insertLength != 0;
720
				}
719
				}
Lines 727-732 Link Here
727
726
728
			}
727
			}
729
728
729
			removeJavaStuff(temp);
730
			temp.stopRewriteSession(session);
730
			temp.stopRewriteSession(session);
731
			newText= temp.get(prefix.length(), temp.getLength() - prefix.length());
731
			newText= temp.get(prefix.length(), temp.getLength() - prefix.length());
732
732

Return to bug 330556