From a495c2e5ea4bac1473aa4cb62cab8040c45d4613 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 29 Mar 2018 16:12:44 +0200 Subject: [PATCH] Don't run analysis in SpecifyTypeExplicitlyIntention.isAvailable() If type is invalid, show error hint when action is invoked --- .../intentions/SpecifyTypeExplicitlyIntention.kt | 10 ++++++++-- .../intentions/specifyTypeExplicitly/unknownType.kt | 5 ----- .../quickfix/typeAddition/noAddErrorType.kt | 3 ++- .../quickfix/typeAddition/noAddErrorType.kt.after | 12 ++++++++++++ .../typeAddition/propertyWithGetterWithBlockBody.kt | 3 ++- .../propertyWithGetterWithBlockBody.kt.after | 13 +++++++++++++ .../typeAddition/propertyWithRecursiveGetter.kt | 3 ++- .../propertyWithRecursiveGetter.kt.after | 11 +++++++++++ .../idea/intentions/IntentionTestGenerated.java | 6 ------ 9 files changed, 50 insertions(+), 16 deletions(-) delete mode 100644 idea/testData/intentions/specifyTypeExplicitly/unknownType.kt create mode 100644 idea/testData/quickfix/typeAddition/noAddErrorType.kt.after create mode 100644 idea/testData/quickfix/typeAddition/propertyWithGetterWithBlockBody.kt.after create mode 100644 idea/testData/quickfix/typeAddition/propertyWithRecursiveGetter.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt index 453f99d05c2..eed85b392d7 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt @@ -16,6 +16,7 @@ package org.jetbrains.kotlin.idea.intentions +import com.intellij.codeInsight.hint.HintManager import com.intellij.codeInsight.intention.LowPriorityAction import com.intellij.codeInsight.template.* import com.intellij.openapi.application.ApplicationManager @@ -53,8 +54,6 @@ class SpecifyTypeExplicitlyIntention : if (element is KtConstructor<*>) return null if (element.typeReference != null) return null - if (getTypeForDeclaration(element).isError) return null - if (element is KtNamedFunction && element.hasBlockBody()) return null text = if (element is KtFunction) "Specify return type explicitly" else "Specify type explicitly" @@ -70,6 +69,13 @@ class SpecifyTypeExplicitlyIntention : override fun applyTo(element: KtCallableDeclaration, editor: Editor?) { val type = getTypeForDeclaration(element) + if (type.isError) { + if (editor != null) { + HintManager.getInstance().showErrorHint(editor, "Cannot infer type for this declaration") + } + return + } + addTypeAnnotation(editor, element, type) } diff --git a/idea/testData/intentions/specifyTypeExplicitly/unknownType.kt b/idea/testData/intentions/specifyTypeExplicitly/unknownType.kt deleted file mode 100644 index 2446e7d9d60..00000000000 --- a/idea/testData/intentions/specifyTypeExplicitly/unknownType.kt +++ /dev/null @@ -1,5 +0,0 @@ -// IS_APPLICABLE: false -// WITH_RUNTIME -// ERROR: Unresolved reference: BadType - -val x = BadType() \ No newline at end of file diff --git a/idea/testData/quickfix/typeAddition/noAddErrorType.kt b/idea/testData/quickfix/typeAddition/noAddErrorType.kt index e05dd48156c..5a03294a9ff 100644 --- a/idea/testData/quickfix/typeAddition/noAddErrorType.kt +++ b/idea/testData/quickfix/typeAddition/noAddErrorType.kt @@ -1,4 +1,5 @@ -// "Specify type explicitly" "false" +// "Specify type explicitly" "true" +// SHOULD_BE_AVAILABLE_AFTER_EXECUTION // ACTION: Add getter // ACTION: Convert property to function // ACTION: Introduce backing property diff --git a/idea/testData/quickfix/typeAddition/noAddErrorType.kt.after b/idea/testData/quickfix/typeAddition/noAddErrorType.kt.after new file mode 100644 index 00000000000..5a03294a9ff --- /dev/null +++ b/idea/testData/quickfix/typeAddition/noAddErrorType.kt.after @@ -0,0 +1,12 @@ +// "Specify type explicitly" "true" +// SHOULD_BE_AVAILABLE_AFTER_EXECUTION +// ACTION: Add getter +// ACTION: Convert property to function +// ACTION: Introduce backing property +// ACTION: Move to companion object +// ACTION: Move to constructor +// ERROR: Unresolved reference: foo + +class A() { + public val t = foo() +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeAddition/propertyWithGetterWithBlockBody.kt b/idea/testData/quickfix/typeAddition/propertyWithGetterWithBlockBody.kt index a68610f40ba..f52f4cb7367 100644 --- a/idea/testData/quickfix/typeAddition/propertyWithGetterWithBlockBody.kt +++ b/idea/testData/quickfix/typeAddition/propertyWithGetterWithBlockBody.kt @@ -1,4 +1,5 @@ -// "Specify type explicitly" "false" +// "Specify type explicitly" "true" +// SHOULD_BE_AVAILABLE_AFTER_EXECUTION // ERROR: This property must either have a type annotation, be initialized or be delegated // ACTION: Convert member to extension // ACTION: Convert property to function diff --git a/idea/testData/quickfix/typeAddition/propertyWithGetterWithBlockBody.kt.after b/idea/testData/quickfix/typeAddition/propertyWithGetterWithBlockBody.kt.after new file mode 100644 index 00000000000..f52f4cb7367 --- /dev/null +++ b/idea/testData/quickfix/typeAddition/propertyWithGetterWithBlockBody.kt.after @@ -0,0 +1,13 @@ +// "Specify type explicitly" "true" +// SHOULD_BE_AVAILABLE_AFTER_EXECUTION +// ERROR: This property must either have a type annotation, be initialized or be delegated +// ACTION: Convert member to extension +// ACTION: Convert property to function +// ACTION: Move to companion object + +class My { + val x + get() { + return 3.14 + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeAddition/propertyWithRecursiveGetter.kt b/idea/testData/quickfix/typeAddition/propertyWithRecursiveGetter.kt index 5a6172d61a3..830a3c8a1f9 100644 --- a/idea/testData/quickfix/typeAddition/propertyWithRecursiveGetter.kt +++ b/idea/testData/quickfix/typeAddition/propertyWithRecursiveGetter.kt @@ -1,4 +1,5 @@ -// "Specify type explicitly" "false" +// "Specify type explicitly" "true" +// SHOULD_BE_AVAILABLE_AFTER_EXECUTION // ERROR: Type checking has run into a recursive problem. Easiest workaround: specify types of your declarations explicitly // ACTION: Convert member to extension // ACTION: Convert property to function diff --git a/idea/testData/quickfix/typeAddition/propertyWithRecursiveGetter.kt.after b/idea/testData/quickfix/typeAddition/propertyWithRecursiveGetter.kt.after new file mode 100644 index 00000000000..830a3c8a1f9 --- /dev/null +++ b/idea/testData/quickfix/typeAddition/propertyWithRecursiveGetter.kt.after @@ -0,0 +1,11 @@ +// "Specify type explicitly" "true" +// SHOULD_BE_AVAILABLE_AFTER_EXECUTION +// ERROR: Type checking has run into a recursive problem. Easiest workaround: specify types of your declarations explicitly +// ACTION: Convert member to extension +// ACTION: Convert property to function +// ACTION: Move to companion object + +class A { + val a + get() = a +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index c65698ef77d..81f2b7b6b2f 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -15740,12 +15740,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/specifyTypeExplicitly/unitType.kt"); doTest(fileName); } - - @TestMetadata("unknownType.kt") - public void testUnknownType() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/specifyTypeExplicitly/unknownType.kt"); - doTest(fileName); - } } @TestMetadata("idea/testData/intentions/specifyTypeExplicitlyInDestructuringAssignment")