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

Fixes to completing types #1070

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public class DOMCompletionEngine implements Runnable {

private static final String FAKE_IDENTIFIER = new String(RecoveryScanner.FAKE_IDENTIFIER);
private static final char[] VOID = PrimitiveType.VOID.toString().toCharArray();
private static final List<char[]> TYPE_KEYWORDS_EXCEPT_VOID = List.of(
PrimitiveType.BOOLEAN.toString().toCharArray(),
PrimitiveType.BYTE.toString().toCharArray(),
PrimitiveType.SHORT.toString().toCharArray(),
PrimitiveType.INT.toString().toCharArray(),
PrimitiveType.LONG.toString().toCharArray(),
PrimitiveType.DOUBLE.toString().toCharArray(),
PrimitiveType.FLOAT.toString().toCharArray(),
PrimitiveType.CHAR.toString().toCharArray());

private final int offset;
private final CompilationUnit unit;
Expand Down Expand Up @@ -342,6 +351,7 @@ public void run() {
ITypeBinding typeDeclBinding = typeDecl.resolveBinding();
findOverridableMethods(typeDeclBinding, this.modelUnit.getJavaProject(), context);
ExtendsOrImplementsInfo extendsOrImplementsInfo = isInExtendsOrImplements(this.toComplete);
suggestTypeKeywords(true);
if (!this.requestor.isIgnored(CompletionProposal.TYPE_REF)) {
findTypes(this.prefix, IJavaSearchConstants.TYPE, null)
// don't care about annotations
Expand Down Expand Up @@ -688,13 +698,16 @@ public void run() {
scrapeAccessibleBindings(defaultCompletionBindings);

if (suggestDefaultCompletions) {
ExtendsOrImplementsInfo extendsOrImplementsInfo = isInExtendsOrImplements(this.toComplete);
statementLikeKeywords();
if (!this.prefix.isEmpty() && extendsOrImplementsInfo == null) {
suggestTypeKeywords(DOMCompletionUtil.findParent(this.toComplete, new int[] { ASTNode.BLOCK }) == null);
}
publishFromScope(defaultCompletionBindings);
if (!completeAfter.isBlank()) {
final int typeMatchRule = this.toComplete.getParent() instanceof Annotation
? IJavaSearchConstants.ANNOTATION_TYPE
: IJavaSearchConstants.TYPE;
ExtendsOrImplementsInfo extendsOrImplementsInfo = isInExtendsOrImplements(this.toComplete);
if (!this.requestor.isIgnored(CompletionProposal.TYPE_REF)) {
findTypes(completeAfter, typeMatchRule, null)
.filter(type -> {
Expand All @@ -721,6 +734,17 @@ public void run() {
}
}

private void suggestTypeKeywords(boolean includeVoid) {
for (char[] keyword : TYPE_KEYWORDS_EXCEPT_VOID) {
if (!this.isFailedMatch(this.prefix.toCharArray(), keyword)) {
this.requestor.accept(createKeywordProposal(keyword, -1, -1));
}
}
if (includeVoid && !this.isFailedMatch(this.prefix.toCharArray(), VOID)) {
this.requestor.accept(createKeywordProposal(VOID, -1, -1));
}
}

private void scrapeAccessibleBindings(Bindings scope) {
ASTNode current = this.toComplete;
while (current != null) {
Expand Down Expand Up @@ -1142,6 +1166,9 @@ private void completeConstructor(ITypeBinding typeBinding, ASTNode referencedFro
ILog.get().error("Unable to compute type hierarchy while performing completion", e); //$NON-NLS-1$
}
} else if (enclosingTypeElement != null) {
if (isArray) {
suggestTypeKeywords(false);
}
// for some reason the enclosing type is almost always suggested
if (!this.isFailedMatch(this.prefix.toCharArray(), enclosingTypeElement.getElementName().toCharArray())) {
List<CompletionProposal> proposals = toConstructorProposals(enclosingTypeElement, referencedFrom, true);
Expand Down Expand Up @@ -1710,15 +1737,20 @@ private CompletionProposal toProposal(IType type) {
+ RelevanceConstants.R_NON_RESTRICTED
+ computeRelevanceForQualification(!nodeInImports && !fromCurrentCU && !inSamePackage && !typeIsImported);
relevance += computeRelevanceForCaseMatching(this.prefix.toCharArray(), simpleName, this.assistOptions);
try {
if (type.isAnnotation()) {
relevance += RelevanceConstants.R_ANNOTATION;
}
if (type.isInterface()) {
relevance += RelevanceConstants.R_INTERFACE;
if (isInExtendsOrImplements(this.toComplete) != null) {
try {
if (type.isAnnotation()) {
relevance += RelevanceConstants.R_ANNOTATION;
}
if (type.isInterface()) {
relevance += RelevanceConstants.R_INTERFACE;
}
if (type.isClass()) {
relevance += RelevanceConstants.R_CLASS;
}
} catch (JavaModelException e) {
// do nothing
}
} catch (JavaModelException e) {
// do nothing
}
res.setRelevance(relevance);
if (parentType != null) {
Expand Down
Loading