Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle qualification when calculating relevance #1041

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions org.eclipse.jdt.core.tests.javac/projects/multiOut/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin
bin2
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@ private CompletionProposal toProposal(IBinding binding, String completion) {
binding instanceof IMethodBinding methodBinding ? methodBinding.getReturnType() :
binding instanceof IVariableBinding variableBinding ? variableBinding.getType() :
this.toComplete.getAST().resolveWellKnownType(Object.class.getName())) +
RelevanceConstants.R_UNQUALIFIED + // TODO: add logic
computeRelevanceForQualification(false) + // TODO: is this always false?
CompletionEngine.computeRelevanceForRestrictions(IAccessRule.K_ACCESSIBLE) //no access restriction for class field
//RelevanceConstants.R_NON_INHERITED // TODO: when is this active?
);
Expand Down Expand Up @@ -1460,12 +1460,30 @@ private CompletionProposal toProposal(IType type) {
} else if (this.toComplete instanceof MarkerAnnotation) {
res.setTokenRange(this.offset, this.offset);
}
boolean nodeInImports = DOMCompletionUtil.findParent(this.toComplete, new int[] { ASTNode.IMPORT_DECLARATION }) != null;
boolean fromCurrentCU = this.modelUnit.equals(type.getCompilationUnit());
boolean typeIsImported = false;
boolean inSamePackage = false;
try {
typeIsImported = Stream.of(this.modelUnit.getImports()).anyMatch(id -> {
return id.getElementName().equals(type.getFullyQualifiedName());
});
IPackageDeclaration[] packageDecls = this.modelUnit.getPackageDeclarations();
if (packageDecls != null && packageDecls.length > 0) {
inSamePackage = this.modelUnit.getPackageDeclarations()[0].getElementName().equals(type.getPackageFragment().getElementName());
} else {
inSamePackage = type.getPackageFragment().getElementName().isEmpty();
}
} catch (JavaModelException e) {
// there are sensible default set if accessing the model fails
}
res.completionEngine = this.nestedEngine;
res.nameLookup = this.nameEnvironment.nameLookup;
int relevance = RelevanceConstants.R_DEFAULT
+ RelevanceConstants.R_RESOLVED
+ RelevanceConstants.R_INTERESTING
+ RelevanceConstants.R_NON_RESTRICTED;
+ RelevanceConstants.R_NON_RESTRICTED
+ computeRelevanceForQualification(!nodeInImports && !fromCurrentCU && !inSamePackage && !typeIsImported);
relevance += computeRelevanceForCaseMatching(this.prefix.toCharArray(), simpleName, this.assistOptions);
try {
if (type.isAnnotation()) {
Expand Down Expand Up @@ -2059,6 +2077,17 @@ private CompletionProposal createLambdaExpressionProposal(IMethodBinding method)
return res;
}

private int computeRelevanceForQualification(boolean prefixRequired) {
boolean insideQualifiedReference = !this.prefix.equals(this.qualifiedPrefix);
if (!prefixRequired && !insideQualifiedReference) {
return RelevanceConstants.R_UNQUALIFIED;
}
if (prefixRequired && insideQualifiedReference) {
return RelevanceConstants.R_QUALIFIED;
}
return 0;
}

/**
* Sets the replace and token ranges of the completion based on the contents of the buffer.
*
Expand Down
Loading