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

Collapse All | Expand All

(-)codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionParser.java (-5 / +26 lines)
Lines 80-85 Link Here
80
	protected static final int K_CONTROL_STATEMENT_DELIMITER = COMPLETION_PARSER + 38;
80
	protected static final int K_CONTROL_STATEMENT_DELIMITER = COMPLETION_PARSER + 38;
81
	protected static final int K_INSIDE_ASSERT_EXCEPTION = COMPLETION_PARSER + 39;
81
	protected static final int K_INSIDE_ASSERT_EXCEPTION = COMPLETION_PARSER + 39;
82
	protected static final int K_INSIDE_FOR_CONDITIONAL = COMPLETION_PARSER + 40;
82
	protected static final int K_INSIDE_FOR_CONDITIONAL = COMPLETION_PARSER + 40;
83
	// added for https://bugs.eclipse.org/bugs/show_bug.cgi?id=261534
84
	protected static final int K_BETWEEN_INSTANCEOF_AND_RPAREN = COMPLETION_PARSER + 41;
83
85
84
	public final static char[] FAKE_TYPE_NAME = new char[]{' '};
86
	public final static char[] FAKE_TYPE_NAME = new char[]{' '};
85
	public final static char[] FAKE_METHOD_NAME = new char[]{' '};
87
	public final static char[] FAKE_METHOD_NAME = new char[]{' '};
Lines 562-568 Link Here
562
					&& ((Assignment)expression).expression == this.assistNode
564
					&& ((Assignment)expression).expression == this.assistNode
563
					&& (this.expressionPtr > 0 && this.expressionStack[this.expressionPtr - 1] instanceof InstanceOfExpression))
565
					&& (this.expressionPtr > 0 && this.expressionStack[this.expressionPtr - 1] instanceof InstanceOfExpression))
564
				|| (expression instanceof AllocationExpression
566
				|| (expression instanceof AllocationExpression
565
					&& ((AllocationExpression)expression).type == this.assistNode)){
567
					&& ((AllocationExpression)expression).type == this.assistNode)
568
				|| (expression instanceof BinaryExpression
569
					&& (this.elementPtr >= 0 && this.elementObjectInfoStack[this.elementPtr] instanceof InstanceOfExpression))){
566
				buildMoreCompletionContext(expression);
570
				buildMoreCompletionContext(expression);
567
				if (this.assistNodeParent == null
571
				if (this.assistNodeParent == null
568
					&& expression instanceof Assignment) {
572
					&& expression instanceof Assignment) {
Lines 1061-1068 Link Here
1061
1065
1062
	int blockIndex = lastIndexOfElement(K_BLOCK_DELIMITER);
1066
	int blockIndex = lastIndexOfElement(K_BLOCK_DELIMITER);
1063
	int controlIndex = lastIndexOfElement(K_CONTROL_STATEMENT_DELIMITER);
1067
	int controlIndex = lastIndexOfElement(K_CONTROL_STATEMENT_DELIMITER);
1064
	int index = blockIndex != -1 && controlIndex < blockIndex ? blockIndex : controlIndex;
1068
	int index;
1065
1069
	if (controlIndex != -1) {
1070
		index = blockIndex != -1 && controlIndex < blockIndex ? blockIndex : controlIndex;
1071
	}
1072
	else {
1073
		// To handle the case when the completion is requested before enclosing R_PAREN
1074
		// and an instanceof expression is also present
1075
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=261534
1076
		int instanceOfIndex = lastIndexOfElement(K_BETWEEN_INSTANCEOF_AND_RPAREN);
1077
		index = blockIndex != -1 && instanceOfIndex < blockIndex ? blockIndex : instanceOfIndex;
1078
	}
1066
	if (index != -1 && this.elementInfoStack[index] == IF && this.elementObjectInfoStack[index] != null) {
1079
	if (index != -1 && this.elementInfoStack[index] == IF && this.elementObjectInfoStack[index] != null) {
1067
		Expression condition = (Expression)this.elementObjectInfoStack[index];
1080
		Expression condition = (Expression)this.elementObjectInfoStack[index];
1068
1081
Lines 1085-1091 Link Here
1085
1098
1086
			}
1099
			}
1087
		}
1100
		}
1088
1101
		if (statement instanceof BinaryExpression && this.assistNode instanceof Statement) {
1102
			statement = (Statement) this.assistNode;
1103
		}
1089
		IfStatement ifStatement =
1104
		IfStatement ifStatement =
1090
			new IfStatement(
1105
			new IfStatement(
1091
					condition,
1106
					condition,
Lines 2605-2611 Link Here
2605
protected void consumeInstanceOfExpression() {
2620
protected void consumeInstanceOfExpression() {
2606
	super.consumeInstanceOfExpression();
2621
	super.consumeInstanceOfExpression();
2607
	popElement(K_BINARY_OPERATOR);
2622
	popElement(K_BINARY_OPERATOR);
2608
2623
	// to handle https://bugs.eclipse.org/bugs/show_bug.cgi?id=261534
2624
	if (topKnownElementKind(COMPLETION_OR_ASSIST_PARSER) == K_BETWEEN_IF_AND_RIGHT_PAREN) {
2625
		pushOnElementStack(K_BETWEEN_INSTANCEOF_AND_RPAREN, IF, this.expressionStack[this.expressionPtr]);
2626
	}
2609
	InstanceOfExpression exp = (InstanceOfExpression) this.expressionStack[this.expressionPtr];
2627
	InstanceOfExpression exp = (InstanceOfExpression) this.expressionStack[this.expressionPtr];
2610
	if(this.assistNode != null && exp.type == this.assistNode) {
2628
	if(this.assistNode != null && exp.type == this.assistNode) {
2611
		this.assistNodeParent = exp;
2629
		this.assistNodeParent = exp;
Lines 3443-3448 Link Here
3443
					case K_BETWEEN_CATCH_AND_RIGHT_PAREN :
3461
					case K_BETWEEN_CATCH_AND_RIGHT_PAREN :
3444
						popElement(K_BETWEEN_CATCH_AND_RIGHT_PAREN);
3462
						popElement(K_BETWEEN_CATCH_AND_RIGHT_PAREN);
3445
						break;
3463
						break;
3464
					case K_BETWEEN_INSTANCEOF_AND_RPAREN :
3465
						popElement(K_BETWEEN_INSTANCEOF_AND_RPAREN);
3466
						//$FALL-THROUGH$
3446
					case K_BETWEEN_IF_AND_RIGHT_PAREN :
3467
					case K_BETWEEN_IF_AND_RIGHT_PAREN :
3447
						if(topKnownElementInfo(COMPLETION_OR_ASSIST_PARSER) == this.bracketDepth) {
3468
						if(topKnownElementInfo(COMPLETION_OR_ASSIST_PARSER) == this.bracketDepth) {
3448
							popElement(K_BETWEEN_IF_AND_RIGHT_PAREN);
3469
							popElement(K_BETWEEN_IF_AND_RIGHT_PAREN);
(-)src/org/eclipse/jdt/core/tests/model/CompletionTests.java (+42 lines)
Lines 21043-21046 Link Here
21043
			"equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), 27}",
21043
			"equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), 27}",
21044
			requestor.getResults());
21044
			requestor.getResults());
21045
}
21045
}
21046
21047
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=261534
21048
// To verify that autocast works correctly even when instanceof expression
21049
// and completion node are in the same binary expression, related by &&
21050
public void testBug261534() throws JavaModelException {
21051
	this.workingCopies = new ICompilationUnit[1];
21052
	this.workingCopies[0] = getWorkingCopy(
21053
		"/Completion/src/test/CompletionAfterInstanceOf.java",
21054
		"package test;" +
21055
		"class MyString {\n" +
21056
		"	public String toWelcome() {\n" +
21057
		"		return \"welcome\";\n" +
21058
		"	}\n" +
21059
		"}\n" +
21060
		"public class CompletionAfterInstanceOf {\n" +
21061
		"	void foo() {\n" +
21062
		"	Object chars= null;\n" +
21063
		"       if (chars instanceof MyString && chars.to) {\n" +
21064
		"		}\n" +
21065
		"	}\n" +
21066
		"}\n");
21067
21068
	CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, true, true, true, true, true);
21069
	requestor.allowAllRequiredProposals();
21070
	String str = this.workingCopies[0].getSource();
21071
	String completeBehind = "chars instanceof MyString && chars.to";
21072
	int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
21073
	this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner);
21074
21075
	int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED;
21076
	int start1 = str.lastIndexOf("to") + "".length();
21077
	int end1 = start1 + "to".length();
21078
	int start2 = str.lastIndexOf("chars.to");
21079
	int end2 = start2 + "chars.to".length();
21080
	int start3 = str.lastIndexOf("chars.");
21081
	int end3 = start3 + "chars".length();
21082
	
21083
	assertResults(
21084
			"toString[METHOD_REF]{toString(), Ljava.lang.Object;, ()Ljava.lang.String;, null, null, toString, null, replace[" + start1 + ", " + end1 + "], token[" + start1 + ", " + end1 +"], " + relevance1 + "}\n" +
21085
			"toWelcome[METHOD_REF_WITH_CASTED_RECEIVER]{((MyString)chars).toWelcome(), Ltest.MyString;, ()Ljava.lang.String;, Ltest.MyString;, null, null, toWelcome, null, replace[" + start2 +", " + end2 + "], token[" + start1 + ", " + end1 + "], receiver[" + start3 + ", " + end3 + "], " + relevance1 + "}",
21086
			requestor.getResults());
21087
}
21046
}
21088
}

Return to bug 261534