Fixed KT-5158 Smart completion for when selector: do not include subject variable
#KT-5158 Fixed
This commit is contained in:
@@ -151,10 +151,19 @@ class SmartCompletion(val expression: JetSimpleNameExpression,
|
||||
val entry = parent.getParent() as JetWhenEntry
|
||||
val whenExpression = entry.getParent() as JetWhenExpression
|
||||
val subject = whenExpression.getSubjectExpression() ?: return setOf()
|
||||
|
||||
val itemsToSkip = HashSet<DeclarationDescriptor>()
|
||||
|
||||
if (subject is JetSimpleNameExpression) {
|
||||
val variable = bindingContext[BindingContext.REFERENCE_TARGET, subject] as? VariableDescriptor
|
||||
if (variable != null) {
|
||||
itemsToSkip.add(variable)
|
||||
}
|
||||
}
|
||||
|
||||
val subjectType = bindingContext[BindingContext.EXPRESSION_TYPE, subject] ?: return setOf()
|
||||
val classDescriptor = TypeUtils.getClassDescriptor(subjectType)
|
||||
if (classDescriptor != null && DescriptorUtils.isEnumClass(classDescriptor)) {
|
||||
val usedEnumEntries = HashSet<ClassDescriptor>()
|
||||
val conditions = whenExpression.getEntries()
|
||||
.flatMap { it.getConditions().toList() }
|
||||
.filterIsInstance(javaClass<JetWhenConditionWithExpression>())
|
||||
@@ -163,11 +172,12 @@ class SmartCompletion(val expression: JetSimpleNameExpression,
|
||||
?.getSelectorExpression() as? JetReferenceExpression ?: continue
|
||||
val target = bindingContext[BindingContext.REFERENCE_TARGET, selectorExpr] as? ClassDescriptor ?: continue
|
||||
if (DescriptorUtils.isEnumEntry(target)) {
|
||||
usedEnumEntries.add(target)
|
||||
itemsToSkip.add(target)
|
||||
}
|
||||
}
|
||||
return usedEnumEntries
|
||||
}
|
||||
|
||||
return itemsToSkip
|
||||
}
|
||||
}
|
||||
return setOf()
|
||||
|
||||
@@ -9,3 +9,4 @@ fun foo(e: ElementType) {
|
||||
|
||||
// ABSENT: ElementType.FIELD
|
||||
// EXIST: ElementType.TYPE
|
||||
// ABSENT: e
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
fun foo(p1: String, p2: String) {
|
||||
when (p1) {
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
|
||||
// ABSENT: p1
|
||||
// EXIST: p2
|
||||
@@ -516,6 +516,11 @@ public class JvmSmartCompletionTestGenerated extends AbstractJvmSmartCompletionT
|
||||
doTest("idea/testData/completion/smart/WhenEntryValue8.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("WhenEntryValue9.kt")
|
||||
public void testWhenEntryValue9() throws Exception {
|
||||
doTest("idea/testData/completion/smart/WhenEntryValue9.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("WhenWithNoSubjectEntryValue1.kt")
|
||||
public void testWhenWithNoSubjectEntryValue1() throws Exception {
|
||||
doTest("idea/testData/completion/smart/WhenWithNoSubjectEntryValue1.kt");
|
||||
|
||||
Reference in New Issue
Block a user