Skip to content

Commit 4dafcdf

Browse files
committed
Fix some qualified type relevances
It wasn't counting qualified types in case statements as qualified, for instance. Signed-off-by: David Thompson <davthomp@redhat.com>
1 parent e3efcd5 commit 4dafcdf

2 files changed

Lines changed: 101 additions & 66 deletions

File tree

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/codeassist/DOMCompletionEngine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,9 +1197,9 @@ public void complete(org.eclipse.jdt.internal.compiler.env.ICompilationUnit sour
11971197
boolean isTypeInVariableDeclaration = isTypeInVariableDeclaration(context);
11981198
SwitchCase switchCase = (SwitchCase)DOMCompletionUtil.findParent(this.toComplete, new int[] { ASTNode.SWITCH_CASE });
11991199

1200+
this.qualifyingType = qualifierTypeBinding;
12001201
processMembers(qualifiedName, qualifierTypeBinding, specificCompletionBindings, true);
12011202
if (this.extendsOrImplementsInfo == null && !isTypeInVariableDeclaration && switchCase == null) {
1202-
this.qualifyingType = qualifierTypeBinding;
12031203
publishFromScope(specificCompletionBindings);
12041204
} else if (switchCase != null) {
12051205
ITypeBinding switchBinding = null;
@@ -1230,7 +1230,7 @@ public void complete(org.eclipse.jdt.internal.compiler.env.ICompilationUnit sour
12301230
.map(binding -> toProposal(binding))
12311231
.map(proposal -> {
12321232
if (proposal.getKind() == CompletionProposal.FIELD_REF) {
1233-
proposal.setRelevance(proposal.getRelevance() + RelevanceConstants.R_FINAL + RelevanceConstants.R_QUALIFIED);
1233+
proposal.setRelevance(proposal.getRelevance() + RelevanceConstants.R_FINAL);
12341234
}
12351235
return proposal;
12361236
})

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/codeassist/RelevanceUtils.java

Lines changed: 99 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@
3232

3333
/**
3434
* Helper methods for calculating the relevance numbers of completion proposals.
35-
*
35+
*
3636
* Most of these are adapted from {@link CompletionEngine}
3737
*/
3838
class RelevanceUtils {
39-
39+
4040
/**
4141
* Returns the sum of the appropriate relevance constants based on how the
4242
* current name matches the given proposal name according to the enabled rules.
43-
*
43+
*
4444
* @param token the uncompleted content
4545
* @param proposalName the proposal text to check against the uncompleted
4646
* content
@@ -67,15 +67,22 @@ public static int computeRelevanceForCaseMatching(char[] token, char[] proposalN
6767
}
6868
return 0;
6969
}
70-
70+
7171
/**
72-
* Returns the appropriate "qualified" relevance constant based on if the current content is qualified and if the content is expected to be qualified.
73-
*
74-
* @param prefixRequired true if the current completion item should be qualified, false otherwise
75-
* @param prefix the completion prefix; the java identifier that completion was triggered on
76-
* @param qualifiedPrefix the completion prefix with any qualifiers (eg. package qualifiers, type qualifiers, nested member access, etc.). May contain `.`
72+
* Returns the appropriate "qualified" relevance constant based on if the
73+
* current content is qualified and if the content is expected to be qualified.
74+
*
75+
* @param prefixRequired true if the current completion item should be
76+
* qualified, false otherwise
77+
* @param prefix the completion prefix; the java identifier that
78+
* completion was triggered on
79+
* @param qualifiedPrefix the completion prefix with any qualifiers (eg. package
80+
* qualifiers, type qualifiers, nested member access,
81+
* etc.). May contain `.`
7782
* @see CompletionEngine#computeRelevanceForQualification(boolean)
78-
* @return the appropriate "qualified" relevance constant based on if the current content is qualified and if the content is expected to be qualified
83+
* @return the appropriate "qualified" relevance constant based on if the
84+
* current content is qualified and if the content is expected to be
85+
* qualified
7986
*/
8087
static int computeRelevanceForQualification(boolean prefixRequired, String prefix, String qualifiedPrefix) {
8188
boolean insideQualifiedReference = !prefix.equals(qualifiedPrefix);
@@ -87,49 +94,56 @@ static int computeRelevanceForQualification(boolean prefixRequired, String prefi
8794
}
8895
return 0;
8996
}
90-
97+
9198
/**
92-
* Returns the appropriate relevance constant based on if type associated with the proposal matches the expected types.
93-
*
94-
* @param proposalType the binding of the type associated with the proposal
99+
* Returns the appropriate relevance constant based on if type associated with
100+
* the proposal matches the expected types.
101+
*
102+
* @param proposalType the binding of the type associated with the proposal
95103
* @param expectedTypes the expected types
96-
* @return the appropriate relevance constant based on if type associated with the proposal matches the expected types
104+
* @return the appropriate relevance constant based on if type associated with
105+
* the proposal matches the expected types
97106
*/
98107
static int computeRelevanceForExpectingType(ITypeBinding proposalType, ExpectedTypes expectedTypes) {
99108
if (proposalType != null) {
100109
int relevance = 0;
101110
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=271296
102-
// If there is at least one expected type, then void proposal types attract a degraded relevance.
103-
if (!expectedTypes.getExpectedTypes().isEmpty() && PrimitiveType.VOID.toString().equals(proposalType.getName())) {
111+
// If there is at least one expected type, then void proposal types attract a
112+
// degraded relevance.
113+
if (!expectedTypes.getExpectedTypes().isEmpty()
114+
&& PrimitiveType.VOID.toString().equals(proposalType.getName())) {
104115
return RelevanceConstants.R_VOID;
105116
}
106117
for (ITypeBinding expectedType : expectedTypes.getExpectedTypes()) {
107-
if(expectedTypes.allowsSubtypes()
118+
if (expectedTypes.allowsSubtypes()
108119
&& proposalType.getErasure().isSubTypeCompatible(expectedType.getErasure())) {
109120

110-
if(Objects.equals(expectedType.getQualifiedName(), proposalType.getQualifiedName())) {
121+
if (Objects.equals(expectedType.getQualifiedName(), proposalType.getQualifiedName())) {
111122
return RelevanceConstants.R_EXACT_EXPECTED_TYPE;
112123
} else if (proposalType.getPackage() != null && proposalType.getPackage().isUnnamed()) {
113124
return RelevanceConstants.R_PACKAGE_EXPECTED_TYPE;
114125
}
115126
relevance = RelevanceConstants.R_EXPECTED_TYPE;
116127

117128
}
118-
if(expectedTypes.allowsSupertypes() && expectedType.isSubTypeCompatible(proposalType)) {
129+
if (expectedTypes.allowsSupertypes() && expectedType.isSubTypeCompatible(proposalType)) {
119130

120-
if(Objects.equals(expectedType.getQualifiedName(), proposalType.getQualifiedName())) {
131+
if (Objects.equals(expectedType.getQualifiedName(), proposalType.getQualifiedName())) {
121132
return RelevanceConstants.R_EXACT_EXPECTED_TYPE;
122133
}
123134
relevance = RelevanceConstants.R_EXPECTED_TYPE;
124135
}
125-
// Bug 84720 - [1.5][assist] proposal ranking by return value should consider auto(un)boxing
126-
// Just ensuring that the unitScope is not null, even though it's an unlikely case.
136+
// Bug 84720 - [1.5][assist] proposal ranking by return value should consider
137+
// auto(un)boxing
138+
// Just ensuring that the unitScope is not null, even though it's an unlikely
139+
// case.
127140
var oneErasure = expectedType.getErasure();
128141
var otherErasure = proposalType.getErasure();
129-
if ((oneErasure.isPrimitive() && otherErasure.getQualifiedName().startsWith("java.lang.")) ||
130-
(otherErasure.isPrimitive() && oneErasure.getQualifiedName().startsWith("java.lang."))) {
131-
if (oneErasure.getName().equalsIgnoreCase(otherErasure.getName()) ||
132-
Set.of(oneErasure.getName().toLowerCase(), otherErasure.getName().toLowerCase()).equals(Set.of("char", "character"))) {
142+
if ((oneErasure.isPrimitive() && otherErasure.getQualifiedName().startsWith("java.lang."))
143+
|| (otherErasure.isPrimitive() && oneErasure.getQualifiedName().startsWith("java.lang."))) {
144+
if (oneErasure.getName().equalsIgnoreCase(otherErasure.getName())
145+
|| Set.of(oneErasure.getName().toLowerCase(), otherErasure.getName().toLowerCase())
146+
.equals(Set.of("char", "character"))) {
133147
relevance = CompletionEngine.R_EXPECTED_TYPE;
134148
}
135149
}
@@ -142,40 +156,49 @@ static int computeRelevanceForExpectingType(ITypeBinding proposalType, ExpectedT
142156
}
143157
return 0;
144158
}
145-
146-
static int computeRelevanceForExpectingType(IType proposalType, ExpectedTypes expectedTypes, WorkingCopyOwner workingCopyOwner, Map<String, ITypeHierarchy> typeHierarchyCache) {
159+
160+
static int computeRelevanceForExpectingType(IType proposalType, ExpectedTypes expectedTypes,
161+
WorkingCopyOwner workingCopyOwner, Map<String, ITypeHierarchy> typeHierarchyCache) {
147162
if (proposalType != null) {
148-
IPackageFragment packageFragment = (IPackageFragment)proposalType.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
163+
IPackageFragment packageFragment = (IPackageFragment) proposalType
164+
.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
149165
int relevance = 0;
150166
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=271296
151-
// If there is at least one expected type, then void proposal types attract a degraded relevance.
152-
if (!expectedTypes.getExpectedTypes().isEmpty() && PrimitiveType.VOID.toString().equals(proposalType.getElementName())) {
167+
// If there is at least one expected type, then void proposal types attract a
168+
// degraded relevance.
169+
if (!expectedTypes.getExpectedTypes().isEmpty()
170+
&& PrimitiveType.VOID.toString().equals(proposalType.getElementName())) {
153171
return RelevanceConstants.R_VOID;
154172
}
155173
for (ITypeBinding expectedType : expectedTypes.getExpectedTypes()) {
156-
if(expectedTypes.allowsSubtypes()
157-
&& DOMCompletionUtil.findInSupers(proposalType, expectedType.getKey(), workingCopyOwner, typeHierarchyCache)) {
174+
if (expectedTypes.allowsSubtypes() && DOMCompletionUtil.findInSupers(proposalType,
175+
expectedType.getKey(), workingCopyOwner, typeHierarchyCache)) {
158176
if (expectedType.getKey().equals(proposalType.getKey())) {
159177
return RelevanceConstants.R_EXACT_EXPECTED_TYPE;
160-
// ??? I'm just guessing on the default packages name here, it might be the empty string
178+
// ??? I'm just guessing on the default packages name here, it might be the
179+
// empty string
161180
} else if (packageFragment != null && packageFragment.getElementName().equals("<default>")) {
162181
return RelevanceConstants.R_PACKAGE_EXPECTED_TYPE;
163182
}
164183
relevance = RelevanceConstants.R_EXPECTED_TYPE;
165184

166185
}
167-
if(expectedTypes.allowsSupertypes() && DOMCompletionUtil.findInSupers(expectedType, proposalType.getKey())) {
186+
if (expectedTypes.allowsSupertypes()
187+
&& DOMCompletionUtil.findInSupers(expectedType, proposalType.getKey())) {
168188
if (expectedType.getKey().equals(proposalType.getKey())) {
169189
return RelevanceConstants.R_EXACT_EXPECTED_TYPE;
170190
}
171191
relevance = RelevanceConstants.R_EXPECTED_TYPE;
172192
}
173-
// Bug 84720 - [1.5][assist] proposal ranking by return value should consider auto(un)boxing
174-
// Just ensuring that the unitScope is not null, even though it's an unlikely case.
193+
// Bug 84720 - [1.5][assist] proposal ranking by return value should consider
194+
// auto(un)boxing
195+
// Just ensuring that the unitScope is not null, even though it's an unlikely
196+
// case.
175197
var oneErasure = expectedType.getErasure();
176198
if ((oneErasure.isPrimitive() && proposalType.getFullyQualifiedName().startsWith("java.lang."))) {
177-
if (oneErasure.getName().equalsIgnoreCase(proposalType.getElementName()) ||
178-
Set.of(oneErasure.getName().toLowerCase(), proposalType.getElementName().toLowerCase()).equals(Set.of("char", "character"))) {
199+
if (oneErasure.getName().equalsIgnoreCase(proposalType.getElementName())
200+
|| Set.of(oneErasure.getName().toLowerCase(), proposalType.getElementName().toLowerCase())
201+
.equals(Set.of("char", "character"))) {
179202
relevance = CompletionEngine.R_EXPECTED_TYPE;
180203
}
181204
}
@@ -184,27 +207,34 @@ static int computeRelevanceForExpectingType(IType proposalType, ExpectedTypes ex
184207
}
185208
return 0;
186209
}
187-
210+
188211
/**
189-
* Returns the appropriate relevance based on if the given type is expected, without taking into account sub/super classes.
190-
*
191-
* @param type the type completion to get the relevance of
212+
* Returns the appropriate relevance based on if the given type is expected,
213+
* without taking into account sub/super classes.
214+
*
215+
* @param type the type completion to get the relevance of
192216
* @param expectedTypes the information on the expected type
193-
* @return the appropriate relevance based on if the given type is expected, without taking into account sub/super classes
217+
* @return the appropriate relevance based on if the given type is expected,
218+
* without taking into account sub/super classes
194219
*/
195220
static int simpleComputeRelevanceForExpectingType(IType type, ExpectedTypes expectedTypes) {
196-
return (expectedTypes.getExpectedTypes().stream().map(ITypeBinding::getQualifiedName).anyMatch(type.getFullyQualifiedName()::equals) ? RelevanceConstants.R_EXACT_EXPECTED_TYPE :
197-
expectedTypes.getExpectedTypes().stream().map(ITypeBinding::getQualifiedName).anyMatch(Object.class.getName()::equals) ? RelevanceConstants.R_EXPECTED_TYPE :
198-
0);
221+
return (expectedTypes.getExpectedTypes().stream().map(ITypeBinding::getQualifiedName)
222+
.anyMatch(type.getFullyQualifiedName()::equals) ? RelevanceConstants.R_EXACT_EXPECTED_TYPE
223+
: expectedTypes.getExpectedTypes().stream().map(ITypeBinding::getQualifiedName)
224+
.anyMatch(Object.class.getName()::equals) ? RelevanceConstants.R_EXPECTED_TYPE : 0);
199225
}
200226

201227
/**
202-
* Returns the appropriate relevance number based on if the given member is directly implemented in the qualifying type.
203-
*
204-
* @see CompletionEngine#computeRelevanceForInheritance(ReferenceBinding, ReferenceBinding)
228+
* Returns the appropriate relevance number based on if the given member is
229+
* directly implemented in the qualifying type.
230+
*
231+
* @see CompletionEngine#computeRelevanceForInheritance(ReferenceBinding,
232+
* ReferenceBinding)
205233
* @param qualifyingType the qualifying type
206-
* @param member the member to check if it's directly or indirectly inherited
207-
* @return the appropriate relevance number based on if the given member is directly implemented in the qualifying type
234+
* @param member the member to check if it's directly or indirectly
235+
* inherited
236+
* @return the appropriate relevance number based on if the given member is
237+
* directly implemented in the qualifying type
208238
*/
209239
static int computeRelevanceForInheritance(ITypeBinding qualifyingType, IBinding member) {
210240
if (qualifyingType == null) {
@@ -225,14 +255,18 @@ static int computeRelevanceForInheritance(ITypeBinding qualifyingType, IBinding
225255
}
226256
return 0;
227257
}
228-
258+
229259
/**
230-
* Returns the appropriate relevance number based on if the given member type is directly implemented in the qualifying type.
231-
*
232-
* @see CompletionEngine#computeRelevanceForInheritance(ReferenceBinding, ReferenceBinding)
260+
* Returns the appropriate relevance number based on if the given member type is
261+
* directly implemented in the qualifying type.
262+
*
263+
* @see CompletionEngine#computeRelevanceForInheritance(ReferenceBinding,
264+
* ReferenceBinding)
233265
* @param qualifyingType the qualifying type
234-
* @param memberType the member type to check if it's directly or indirectly inherited
235-
* @return the appropriate relevance number based on if the given member is directly implemented in the qualifying type
266+
* @param memberType the member type to check if it's directly or indirectly
267+
* inherited
268+
* @return the appropriate relevance number based on if the given member is
269+
* directly implemented in the qualifying type
236270
*/
237271
static int computeRelevanceForInheritance(ITypeBinding qualifyingType, IType memberType) {
238272
if (qualifyingType == null || memberType.getDeclaringType() == null) {
@@ -243,7 +277,7 @@ static int computeRelevanceForInheritance(ITypeBinding qualifyingType, IType mem
243277
}
244278
return 0;
245279
}
246-
280+
247281
static int computeRelevanceForInteresting(IType potentiallyInterestingType, ExpectedTypes expectedTypes) {
248282
String typeKey = potentiallyInterestingType.getKey();
249283
for (ITypeBinding uninterestingType : expectedTypes.getUninterestingTypes()) {
@@ -255,13 +289,14 @@ static int computeRelevanceForInteresting(IType potentiallyInterestingType, Expe
255289
}
256290

257291
/**
258-
* Returns the appropriate relevance number based on the access rule, taking into account if access warnings/errors are disabled.
292+
* Returns the appropriate relevance number based on the access rule, taking
293+
* into account if access warnings/errors are disabled.
259294
*
260-
* i.e. if the errors for accessing an inaccessible class are disabled,
261-
* the relevance number is the same as if there are no access restrictions.
295+
* i.e. if the errors for accessing an inaccessible class are disabled, the
296+
* relevance number is the same as if there are no access restrictions.
262297
*
263298
* @param accessRuleKind the access rule
264-
* @param settings the compiler settings
299+
* @param settings the compiler settings
265300
* @return the appropriate relevance number
266301
*/
267302
static int computeRelevanceForRestrictions(int accessRuleKind, Map<String, String> settings) {

0 commit comments

Comments
 (0)