Skip to content

Commit

Permalink
completion returns no results with pattern matching
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <snjezana.peco@redhat.com>
  • Loading branch information
snjeza committed Mar 11, 2024
1 parent 1d93115 commit 663fb75
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3915,9 +3915,13 @@ private void completionOnQualifiedTypeReference(ASTNode astNode, ASTNode astNode
}
// alternatively interpret tokens in a misclassified LocalDeclaration like a QualifiedNameReference:
if (!(qualifiedBinding instanceof PackageBinding) && astNodeParent instanceof LocalDeclaration && enclosingNode != null) { // enclosingNode == null when called from completionOnProvidesInterfacesQualifiedTypeReference
if (scope instanceof BlockScope) {
if (scope instanceof BlockScope blockScope) {
// resolve tokens like it's done in CompletionOnQualifiedNameReference:
qualifiedBinding = ((BlockScope) scope).getBinding(ref.tokens, FakeInvocationSite);
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2106
Binding binding = blockScope.getBinding(ref.tokens, FakeInvocationSite);
if (binding.isValidBinding()) {
qualifiedBinding = binding;
}
boolean ignoreType = this.requestor.isIgnored(CompletionProposal.TYPE_REF);
try {
this.requestor.setIgnored(CompletionProposal.TYPE_REF, haveTypeProposals); // temp ignore types if already proposed above
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,19 @@ protected TypeBinding getTypeBinding(Scope scope) {
Binding binding = scope.parent.getTypeOrPackage(this.tokens); // step up from the ClassScope
if (!binding.isValidBinding()) {
scope.problemReporter().invalidType(this, (TypeBinding) binding);
LocalVariableBinding localVariableBinding;
if (scope instanceof BlockScope blockScope) {
localVariableBinding = blockScope.findVariable(this.tokens[0], null);
} else {
localVariableBinding = null;
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=568934#c6
if (localVariableBinding == null && scope.parent instanceof BlockScope blockScope) {
localVariableBinding = blockScope.findVariable(this.tokens[0], null);
}

if (binding.problemId() == ProblemReasons.NotFound) {
throw new CompletionNodeFound(this, binding, scope);
throw new CompletionNodeFound(this, (localVariableBinding == null ? binding : localVariableBinding), scope);
}

throw new CompletionNodeFound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
import org.eclipse.jdt.internal.compiler.parser.RecoveredModule;
import org.eclipse.jdt.internal.compiler.parser.RecoveredPackageVisibilityStatement;
import org.eclipse.jdt.internal.compiler.parser.RecoveredProvidesStatement;
import org.eclipse.jdt.internal.compiler.parser.RecoveredStatement;
import org.eclipse.jdt.internal.compiler.parser.RecoveredType;
import org.eclipse.jdt.internal.compiler.parser.RecoveredUnit;
import org.eclipse.jdt.internal.compiler.parser.Scanner;
Expand Down Expand Up @@ -734,8 +735,8 @@ protected void attachOrphanCompletionNode(){
|| (this.elementPtr >= 0 && stackHasInstanceOfExpression(this.elementObjectInfoStack, this.elementPtr))))
|| (expression instanceof AllocationExpression
&& ((AllocationExpression)expression).type == this.assistNode)
|| (expression instanceof AND_AND_Expression
&& (this.elementPtr >= 0 && this.elementObjectInfoStack[this.elementPtr] instanceof InstanceOfExpression))
|| (expression instanceof AND_AND_Expression // https://bugs.eclipse.org/bugs/show_bug.cgi?id=568934#c8
&& (this.elementPtr >= 0 && (this.elementObjectInfoStack[this.elementPtr] == null || this.elementObjectInfoStack[this.elementPtr] instanceof InstanceOfExpression)))
|| (expression instanceof ConditionalExpression
&& ((ConditionalExpression) expression).valueIfFalse == this.assistNode)){
buildMoreCompletionContext(expression);
Expand All @@ -755,19 +756,44 @@ protected void attachOrphanCompletionNode(){
}
}
}
if (this.astPtr > -1 && this.astStack[this.astPtr] instanceof LocalDeclaration) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=287939
if (this.astPtr > -1 && this.astStack[this.astPtr] instanceof LocalDeclaration local) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=287939
// To take care of: if (a instance of X) int i = a.|
LocalDeclaration local = (LocalDeclaration) this.astStack[this.astPtr];
if (local.initialization == this.assistNode) {
if (local.initialization == this.assistNode || local.type == this.assistNode) {
Statement enclosing = buildMoreCompletionEnclosingContext(local);
if (enclosing instanceof IfStatement) {
if (this.currentElement instanceof RecoveredBlock) {
if (this.currentElement instanceof RecoveredBlock recoveredBlock) {
// RecoveredLocalVariable must be removed from its parent because the IfStatement will be added instead
RecoveredBlock recoveredBlock = (RecoveredBlock) this.currentElement;
recoveredBlock.statements[--recoveredBlock.statementCount] = null;
this.currentElement = this.currentElement.add(enclosing, 0);
}
}
} else if (this.astPtr > 0) {
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2106
int index = this.astPtr - 1;
while (index >= 0) {
if (this.astStack[index] instanceof LocalDeclaration local2) {
if (local2.initialization == this.assistNode || local2.type == this.assistNode) {
Statement enclosing = buildMoreCompletionEnclosingContext(local2);
if (enclosing instanceof IfStatement) {
if (this.currentElement instanceof RecoveredBlock recoveredBlock) {
for (int i = 0; i < recoveredBlock.statementCount; i++) {
RecoveredStatement rs = recoveredBlock.statements[i];
if (rs instanceof RecoveredLocalVariable rlv) {
if (rlv.localDeclaration == local2) {
RecoveredStatement statement = new RecoveredStatement(enclosing,
recoveredBlock, 0);
recoveredBlock.statements[i] = statement;
index = -1;
break;
}
}
}
}
}
}
}
index = index - 1;
}
}
}
}
Expand Down

0 comments on commit 663fb75

Please sign in to comment.