Don't run analysis in SpecifyTypeExplicitlyIntention.isAvailable()
If type is invalid, show error hint when action is invoked
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.intentions
|
package org.jetbrains.kotlin.idea.intentions
|
||||||
|
|
||||||
|
import com.intellij.codeInsight.hint.HintManager
|
||||||
import com.intellij.codeInsight.intention.LowPriorityAction
|
import com.intellij.codeInsight.intention.LowPriorityAction
|
||||||
import com.intellij.codeInsight.template.*
|
import com.intellij.codeInsight.template.*
|
||||||
import com.intellij.openapi.application.ApplicationManager
|
import com.intellij.openapi.application.ApplicationManager
|
||||||
@@ -53,8 +54,6 @@ class SpecifyTypeExplicitlyIntention :
|
|||||||
if (element is KtConstructor<*>) return null
|
if (element is KtConstructor<*>) return null
|
||||||
if (element.typeReference != null) return null
|
if (element.typeReference != null) return null
|
||||||
|
|
||||||
if (getTypeForDeclaration(element).isError) return null
|
|
||||||
|
|
||||||
if (element is KtNamedFunction && element.hasBlockBody()) return null
|
if (element is KtNamedFunction && element.hasBlockBody()) return null
|
||||||
|
|
||||||
text = if (element is KtFunction) "Specify return type explicitly" else "Specify type explicitly"
|
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?) {
|
override fun applyTo(element: KtCallableDeclaration, editor: Editor?) {
|
||||||
val type = getTypeForDeclaration(element)
|
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)
|
addTypeAnnotation(editor, element, type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
// IS_APPLICABLE: false
|
|
||||||
// WITH_RUNTIME
|
|
||||||
// ERROR: Unresolved reference: BadType
|
|
||||||
|
|
||||||
val <caret>x = BadType()
|
|
||||||
+2
-1
@@ -1,4 +1,5 @@
|
|||||||
// "Specify type explicitly" "false"
|
// "Specify type explicitly" "true"
|
||||||
|
// SHOULD_BE_AVAILABLE_AFTER_EXECUTION
|
||||||
// ACTION: Add getter
|
// ACTION: Add getter
|
||||||
// ACTION: Convert property to function
|
// ACTION: Convert property to function
|
||||||
// ACTION: Introduce backing property
|
// ACTION: Introduce backing property
|
||||||
|
|||||||
@@ -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 <caret>t = foo()
|
||||||
|
}
|
||||||
@@ -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
|
// ERROR: This property must either have a type annotation, be initialized or be delegated
|
||||||
// ACTION: Convert member to extension
|
// ACTION: Convert member to extension
|
||||||
// ACTION: Convert property to function
|
// ACTION: Convert property to function
|
||||||
|
|||||||
@@ -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 <caret>x
|
||||||
|
get() {
|
||||||
|
return 3.14
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
|
// ERROR: Type checking has run into a recursive problem. Easiest workaround: specify types of your declarations explicitly
|
||||||
// ACTION: Convert member to extension
|
// ACTION: Convert member to extension
|
||||||
// ACTION: Convert property to function
|
// ACTION: Convert property to function
|
||||||
|
|||||||
@@ -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<caret>
|
||||||
|
get() = a
|
||||||
|
}
|
||||||
@@ -15740,12 +15740,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/specifyTypeExplicitly/unitType.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/specifyTypeExplicitly/unitType.kt");
|
||||||
doTest(fileName);
|
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")
|
@TestMetadata("idea/testData/intentions/specifyTypeExplicitlyInDestructuringAssignment")
|
||||||
|
|||||||
Reference in New Issue
Block a user