Implicit exhaustive when annotation / highlighting

This commit is contained in:
Mikhail Glukhikh
2015-12-25 15:18:52 +03:00
committed by Mikhail Glukhikh
parent 7d6ccc40c2
commit 0cc09872b6
8 changed files with 39 additions and 1 deletions
@@ -135,6 +135,7 @@ options.kotlin.attribute.descriptor.variable.as.function.like.call=Variable as f
options.kotlin.attribute.descriptor.smart.cast=Smart-cast value options.kotlin.attribute.descriptor.smart.cast=Smart-cast value
options.kotlin.attribute.descriptor.smart.constant=Smart constant options.kotlin.attribute.descriptor.smart.constant=Smart constant
options.kotlin.attribute.descriptor.smart.cast.receiver=Smart-cast implicit receiver options.kotlin.attribute.descriptor.smart.cast.receiver=Smart-cast implicit receiver
options.kotlin.attribute.descriptor.implicit.exhaustive.when=Implicitly exhaustive when
options.kotlin.attribute.descriptor.label=Label options.kotlin.attribute.descriptor.label=Label
change.to.function.invocation=Change to function invocation change.to.function.invocation=Change to function invocation
migrate.sure=Replace sure() calls by !! in project migrate.sure=Replace sure() calls by !! in project
@@ -85,6 +85,7 @@ public class KotlinHighlightingColors {
public static final TextAttributesKey SMART_CAST_VALUE = createTextAttributesKey("KOTLIN_SMART_CAST_VALUE"); public static final TextAttributesKey SMART_CAST_VALUE = createTextAttributesKey("KOTLIN_SMART_CAST_VALUE");
public static final TextAttributesKey SMART_CONSTANT = createTextAttributesKey("KOTLIN_SMART_CONSTANT"); public static final TextAttributesKey SMART_CONSTANT = createTextAttributesKey("KOTLIN_SMART_CONSTANT");
public static final TextAttributesKey SMART_CAST_RECEIVER = createTextAttributesKey("KOTLIN_SMART_CAST_RECEIVER"); public static final TextAttributesKey SMART_CAST_RECEIVER = createTextAttributesKey("KOTLIN_SMART_CAST_RECEIVER");
public static final TextAttributesKey IMPLICIT_EXHAUSTIVE_WHEN = createTextAttributesKey("KOTLIN_IMPLICIT_EXHAUSTIVE_WHEN");
public static final TextAttributesKey LABEL = createTextAttributesKey("KOTLIN_LABEL"); public static final TextAttributesKey LABEL = createTextAttributesKey("KOTLIN_LABEL");
public static final TextAttributesKey DEBUG_INFO = createTextAttributesKey("KOTLIN_DEBUG_INFO"); public static final TextAttributesKey DEBUG_INFO = createTextAttributesKey("KOTLIN_DEBUG_INFO");
public static final TextAttributesKey RESOLVED_TO_ERROR = createTextAttributesKey("KOTLIN_RESOLVED_TO_ERROR"); public static final TextAttributesKey RESOLVED_TO_ERROR = createTextAttributesKey("KOTLIN_RESOLVED_TO_ERROR");
@@ -110,6 +110,15 @@ class VariablesHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
.setTextAttributes(KotlinHighlightingColors.SMART_CAST_VALUE); .setTextAttributes(KotlinHighlightingColors.SMART_CAST_VALUE);
} }
if (expression instanceof KtWhenExpression) {
KtWhenExpression whenExpression = (KtWhenExpression) expression;
boolean implicitExhaustiveWhen = bindingContext.get(IMPLICIT_EXHAUSTIVE_WHEN, whenExpression) == Boolean.TRUE;
if (implicitExhaustiveWhen) {
holder.createInfoAnnotation(whenExpression.getWhenKeyword(), "When is implicitly exhaustive")
.setTextAttributes(KotlinHighlightingColors.IMPLICIT_EXHAUSTIVE_WHEN);
}
}
super.visitExpression(expression); super.visitExpression(expression);
} }
@@ -40,6 +40,11 @@
<option name="BACKGROUND" value="223c23" /> <option name="BACKGROUND" value="223c23" />
</value> </value>
</option> </option>
<option name="KOTLIN_IMPLICIT_EXHAUSTIVE_WHEN">
<value>
<option name="BACKGROUND" value="0321ab" />
</value>
</option>
<option name="KOTLIN_LABEL"> <option name="KOTLIN_LABEL">
<value> <value>
<option name="FOREGROUND" value="467cda" /> <option name="FOREGROUND" value="467cda" />
@@ -40,6 +40,11 @@
<option name="BACKGROUND" value="dbffdb" /> <option name="BACKGROUND" value="dbffdb" />
</value> </value>
</option> </option>
<option name="KOTLIN_IMPLICIT_EXHAUSTIVE_WHEN">
<value>
<option name="BACKGROUND" value="d3faf8" />
</value>
</option>
<option name="KOTLIN_LABEL"> <option name="KOTLIN_LABEL">
<value> <value>
<option name="FOREGROUND" value="4a86e8" /> <option name="FOREGROUND" value="4a86e8" />
@@ -63,13 +63,17 @@ public class KotlinColorSettingsPage : ColorSettingsPage {
} }
} }
fun Int?.bar() { fun Int?.bar(): Int {
if (this != null) { if (this != null) {
println(<SMART_CAST_RECEIVER>toString</SMART_CAST_RECEIVER>()) println(<SMART_CAST_RECEIVER>toString</SMART_CAST_RECEIVER>())
} }
else { else {
println(<SMART_CONSTANT>this</SMART_CONSTANT>.toString()) println(<SMART_CONSTANT>this</SMART_CONSTANT>.toString())
} }
<IMPLICIT_EXHAUSTIVE_WHEN>when</IMPLICIT_EXHAUSTIVE_WHEN> (this != null) {
true -> return 1
false -> return 0
}
} }
var <PROPERTY_WITH_BACKING_FIELD><PACKAGE_PROPERTY><MUTABLE_VARIABLE>globalCounter</MUTABLE_VARIABLE></PACKAGE_PROPERTY></PROPERTY_WITH_BACKING_FIELD> : Int = 5 var <PROPERTY_WITH_BACKING_FIELD><PACKAGE_PROPERTY><MUTABLE_VARIABLE>globalCounter</MUTABLE_VARIABLE></PACKAGE_PROPERTY></PROPERTY_WITH_BACKING_FIELD> : Int = 5
@@ -157,6 +161,7 @@ var <PROPERTY_WITH_BACKING_FIELD><PACKAGE_PROPERTY><MUTABLE_VARIABLE>globalCount
KotlinBundle.message("options.kotlin.attribute.descriptor.smart.cast") to KotlinHighlightingColors.SMART_CAST_VALUE, KotlinBundle.message("options.kotlin.attribute.descriptor.smart.cast") to KotlinHighlightingColors.SMART_CAST_VALUE,
KotlinBundle.message("options.kotlin.attribute.descriptor.smart.constant") to KotlinHighlightingColors.SMART_CONSTANT, KotlinBundle.message("options.kotlin.attribute.descriptor.smart.constant") to KotlinHighlightingColors.SMART_CONSTANT,
KotlinBundle.message("options.kotlin.attribute.descriptor.smart.cast.receiver") to KotlinHighlightingColors.SMART_CAST_RECEIVER, KotlinBundle.message("options.kotlin.attribute.descriptor.smart.cast.receiver") to KotlinHighlightingColors.SMART_CAST_RECEIVER,
KotlinBundle.message("options.kotlin.attribute.descriptor.implicit.exhaustive.when") to KotlinHighlightingColors.IMPLICIT_EXHAUSTIVE_WHEN,
KotlinBundle.message("options.kotlin.attribute.descriptor.label") to KotlinHighlightingColors.LABEL) KotlinBundle.message("options.kotlin.attribute.descriptor.label") to KotlinHighlightingColors.LABEL)
} }
+6
View File
@@ -0,0 +1,6 @@
fun foo(b: Boolean): Int {
<info descr="When is implicitly exhaustive">when</info> (b) {
true -> return 1
false -> return 0
}
}
@@ -766,6 +766,12 @@ public class PsiCheckerTestGenerated extends AbstractPsiCheckerTest {
doTestWithInfos(fileName); doTestWithInfos(fileName);
} }
@TestMetadata("WhenImplicitExhaustive.kt")
public void testWhenImplicitExhaustive() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/checker/infos/WhenImplicitExhaustive.kt");
doTestWithInfos(fileName);
}
@TestMetadata("WrapIntoRef.kt") @TestMetadata("WrapIntoRef.kt")
public void testWrapIntoRef() throws Exception { public void testWrapIntoRef() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/checker/infos/WrapIntoRef.kt"); String fileName = KotlinTestUtils.navigationMetadata("idea/testData/checker/infos/WrapIntoRef.kt");