View | Details | Raw Unified | Return to bug 229927
Collapse All | Expand All

(-)codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionParser.java (-19 / +28 lines)
Lines 1024-1053 Link Here
1024
	
1024
	
1025
	int blockIndex = this.lastIndexOfElement(K_BLOCK_DELIMITER);
1025
	int blockIndex = this.lastIndexOfElement(K_BLOCK_DELIMITER);
1026
	int controlIndex = this.lastIndexOfElement(K_CONTROL_STATEMENT_DELIMITER);
1026
	int controlIndex = this.lastIndexOfElement(K_CONTROL_STATEMENT_DELIMITER);
1027
	if (blockIndex != -1 && controlIndex < blockIndex) {
1027
	int index = blockIndex != -1 && controlIndex < blockIndex ? blockIndex : controlIndex;
1028
		if (this.elementInfoStack[blockIndex] == IF && this.elementObjectInfoStack[blockIndex] != null) {
1028
	
1029
			Expression condition = (Expression)this.elementObjectInfoStack[blockIndex];
1029
	if (index != -1 && this.elementInfoStack[index] == IF && this.elementObjectInfoStack[index] != null) {
1030
			IfStatement ifStatement =
1030
		Expression condition = (Expression)this.elementObjectInfoStack[index];
1031
				new IfStatement(
1031
		
1032
					condition, 
1032
		// If currentElement is a RecoveredLocalVariable then it can be contained in the if statement
1033
					statement, 
1033
		if (this.currentElement instanceof RecoveredLocalVariable &&
1034
					condition.sourceStart, 
1034
				this.currentElement.parent instanceof RecoveredBlock) {
1035
					statement.sourceEnd);
1035
			RecoveredLocalVariable recoveredLocalVariable = (RecoveredLocalVariable) this.currentElement;
1036
			this.enclosingNode = ifStatement;
1036
			if (recoveredLocalVariable.localDeclaration.initialization == null &&
1037
			return ifStatement;
1037
					statement instanceof Expression &&
1038
					condition.sourceStart < recoveredLocalVariable.localDeclaration.sourceStart) {
1039
				this.currentElement.add(statement, 0);
1040
				
1041
				statement = recoveredLocalVariable.updatedStatement();
1042
				
1043
				// RecoveredLocalVariable must be removed from his parent because the IfStatement will be added instead
1044
				RecoveredBlock recoveredBlock =  (RecoveredBlock) recoveredLocalVariable.parent;
1045
				recoveredBlock.statements[--recoveredBlock.statementCount] = null;
1046
				
1047
				this.currentElement = recoveredBlock;
1048
				
1049
			}
1038
		}
1050
		}
1039
	} else if (controlIndex != -1) {
1051
		
1040
		if (this.elementInfoStack[controlIndex] == IF && this.elementObjectInfoStack[controlIndex] != null) {
1052
		IfStatement ifStatement = 
1041
			Expression condition = (Expression)this.elementObjectInfoStack[controlIndex];
1053
			new IfStatement(
1042
			IfStatement ifStatement =
1043
				new IfStatement(
1044
					condition, 
1054
					condition, 
1045
					statement, 
1055
					statement, 
1046
					condition.sourceStart, 
1056
					condition.sourceStart, 
1047
					statement.sourceEnd);
1057
					statement.sourceEnd);
1048
			this.enclosingNode = ifStatement;
1058
		this.enclosingNode = ifStatement;
1049
			return ifStatement;
1059
		return ifStatement;
1050
		}
1051
	}
1060
	}
1052
	
1061
	
1053
	return statement;
1062
	return statement;
(-)src/org/eclipse/jdt/core/tests/compiler/parser/CompletionParserTest2.java (+43 lines)
Lines 11754-11757 Link Here
11754
			expectedReplacedSource,
11754
			expectedReplacedSource,
11755
	"diet ast");
11755
	"diet ast");
11756
}
11756
}
11757
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=229927
11758
public void test0178_Method() {
11759
11760
	String str = 
11761
		"package p;\n"+
11762
		"\n"+
11763
		"public class P {\n"+
11764
		"        private void foo(String key){\n"+
11765
		"                if (key != null) {\n"+
11766
		"                        String[] keys= { k };\n"+
11767
		"                }\n"+
11768
		"        }\n"+
11769
		"}\n"; 
11770
11771
	String completeBehind = "k";
11772
	int cursorLocation = str.lastIndexOf("k") + completeBehind.length() - 1;
11773
	String expectedCompletionNodeToString = "<CompleteOnName:k>";
11774
	String expectedParentNodeToString = "String[] keys = {<CompleteOnName:k>};";
11775
	String completionIdentifier = "k";
11776
	String expectedReplacedSource = "k";
11777
	String expectedUnitDisplayString =
11778
			"package p;\n" + 
11779
			"public class P {\n" + 
11780
			"  public P() {\n" + 
11781
			"  }\n" + 
11782
			"  private void foo(String key) {\n" + 
11783
			"    {\n" + 
11784
			"      if ((key != null))\n" + 
11785
			"          String[] keys = {<CompleteOnName:k>};\n" + 
11786
			"    }\n" + 
11787
			"  }\n" + 
11788
			"}\n";
11789
11790
	checkMethodParse(
11791
			str.toCharArray(),
11792
			cursorLocation,
11793
			expectedCompletionNodeToString,
11794
			expectedParentNodeToString,
11795
			expectedUnitDisplayString,
11796
			completionIdentifier,
11797
			expectedReplacedSource,
11798
			"full ast");
11799
}
11757
}
11800
}

Return to bug 229927