Introduce Variable: Avoid explicit types for expression of anonymous type

#KT-12078 Fixed
This commit is contained in:
Alexey Sedunov
2018-04-10 19:32:33 +03:00
parent c7e72af170
commit 62f654b3d2
4 changed files with 44 additions and 3 deletions
@@ -49,7 +49,6 @@ import org.jetbrains.kotlin.idea.refactoring.*
import org.jetbrains.kotlin.idea.refactoring.introduce.*
import org.jetbrains.kotlin.idea.resolve.ResolutionFacade
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.idea.core.ShortenReferences
import org.jetbrains.kotlin.idea.util.application.executeCommand
import org.jetbrains.kotlin.idea.util.application.executeWriteCommand
import org.jetbrains.kotlin.idea.util.application.runWriteAction
@@ -67,7 +66,10 @@ import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.resolve.source.getPsi
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeConstructor
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.checker.NewKotlinTypeChecker
import org.jetbrains.kotlin.types.checker.TypeCheckerContext
import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.utils.ifEmpty
import org.jetbrains.kotlin.utils.sure
@@ -82,6 +84,20 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
private var KtExpression.isOccurrence: Boolean by NotNullablePsiCopyableUserDataProperty(Key.create("OCCURRENCE"), false)
private class TypeCheckerImpl(private val project: Project) : KotlinTypeChecker by KotlinTypeChecker.DEFAULT {
private inner class ContextImpl : TypeCheckerContext(false) {
override fun areEqualTypeConstructors(a: TypeConstructor, b: TypeConstructor): Boolean {
return compareDescriptors(project, a.declarationDescriptor, b.declarationDescriptor)
}
}
override fun equalTypes(a: KotlinType, b: KotlinType): Boolean {
return with(NewKotlinTypeChecker) {
ContextImpl().equalTypes(a.unwrap(), b.unwrap())
}
}
}
private class IntroduceVariableContext(
private val expression: KtExpression,
private val nameSuggestions: List<Collection<String>>,
@@ -495,8 +511,8 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
val typeNoExpectedType = substringInfo?.type
?: physicalExpression.computeTypeInfoInContext(scope, physicalExpression, bindingTrace, dataFlowInfo).type
val noTypeInference = expressionType != null
&& typeNoExpectedType != null
&& !KotlinTypeChecker.DEFAULT.equalTypes(expressionType, typeNoExpectedType)
&& typeNoExpectedType != null
&& !TypeCheckerImpl(project).equalTypes(expressionType, typeNoExpectedType)
if (expressionType == null && bindingContext.get(BindingContext.QUALIFIER, physicalExpression) != null) {
return showErrorHint(project, editor, KotlinRefactoringBundle.message("cannot.refactor.package.expression"))
@@ -0,0 +1,9 @@
fun foo() {
bar(<selection>object: Runnable {
override fun run() {
}
}</selection>)
}
fun bar(runnable: Runnable) {
}
@@ -0,0 +1,10 @@
fun foo() {
val runnable = object : Runnable {
override fun run() {
}
}
bar(runnable)
}
fun bar(runnable: Runnable) {
}
@@ -27,6 +27,12 @@ public class ExtractionTestGenerated extends AbstractExtractionTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/refactoring/introduceVariable"), Pattern.compile("^(.+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("AnonymousType.kt")
public void testAnonymousType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/AnonymousType.kt");
doIntroduceVariableTest(fileName);
}
@TestMetadata("ArrayAccessExpr.kt")
public void testArrayAccessExpr() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/ArrayAccessExpr.kt");