diff --git a/ChangeLog.md b/ChangeLog.md index ae6e9736e3f..3adfae1c50b 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -202,6 +202,7 @@ These artifacts include extensions for the types available in the latter JDKs, s - [`KT-13055`](https://youtrack.jetbrains.com/issue/KT-13055) Exception in "Specify Type Explicitly" intention - [`KT-12942`](https://youtrack.jetbrains.com/issue/KT-12942) "Replace 'when' with 'if'" intention changes semantics when 'if' statements are used - [`KT-12646`](https://youtrack.jetbrains.com/issue/KT-12646) 'Convert to block body' should use partial body resolve +- [`KT-12919`](https://youtrack.jetbrains.com/issue/KT-12919) Use simple class name in "Change function return type" quickfix - [`KT-13151`](https://youtrack.jetbrains.com/issue/KT-13151) Incorrect warning "Make variable immutable" - [`KT-13170`](https://youtrack.jetbrains.com/issue/KT-13170) "Declaration has platform type" inspection: by default should not be reported for platform type arguments - [`KT-13262`](https://youtrack.jetbrains.com/issue/KT-13262) "Wrap with safe let call" quickfix produces wrong result for qualified function diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeFunctionReturnTypeFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeFunctionReturnTypeFix.kt index 261503fee95..f94bed187be 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeFunctionReturnTypeFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeFunctionReturnTypeFix.kt @@ -21,16 +21,17 @@ import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project import com.intellij.psi.PsiFile import com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.diagnostics.Diagnostic import org.jetbrains.kotlin.diagnostics.Errors.COMPONENT_FUNCTION_RETURN_TYPE_MISMATCH import org.jetbrains.kotlin.idea.KotlinBundle import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor +import org.jetbrains.kotlin.idea.core.ShortenReferences import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil import org.jetbrains.kotlin.idea.project.builtIns import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers -import org.jetbrains.kotlin.idea.core.ShortenReferences import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext import org.jetbrains.kotlin.resolve.BindingContext @@ -68,7 +69,13 @@ class ChangeFunctionReturnTypeFix(element: KtFunction, type: KotlinType) : Kotli return changeFunctionLiteralReturnTypeFix.text } - val functionName = element.fqName?.asString() ?: element.name + val shortName = element.name + val functionName = if (shortName != null) { + val containingDescriptor = element.resolveToDescriptor().containingDeclaration as? ClassDescriptor + val containerName = containingDescriptor?.name + if (containerName != null && !containerName.isSpecial) "${containerName.asString()}.$shortName" else shortName + } + else null if (isUnitType && element.hasBlockBody()) { return if (functionName == null) diff --git a/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForAnonymousObject.kt b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForAnonymousObject.kt new file mode 100644 index 00000000000..ce60b937c9b --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForAnonymousObject.kt @@ -0,0 +1,10 @@ +// "Change 'foo' function return type to 'Int'" "true" +package foo.bar + +fun test() { + val o = object { + fun foo(): String { + return 1 + } + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForAnonymousObject.kt.after b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForAnonymousObject.kt.after new file mode 100644 index 00000000000..76de5e1d1c2 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForAnonymousObject.kt.after @@ -0,0 +1,10 @@ +// "Change 'foo' function return type to 'Int'" "true" +package foo.bar + +fun test() { + val o = object { + fun foo(): Int { + return 1 + } + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForCompanionObject.kt b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForCompanionObject.kt new file mode 100644 index 00000000000..0b7135ed0b6 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForCompanionObject.kt @@ -0,0 +1,11 @@ +// "Change 'Companion.foo' function return type to 'Int'" "true" +package foo.bar + +class A { + companion object { + fun foo(): String { + return 1 + } + } +} + diff --git a/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForCompanionObject.kt.after b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForCompanionObject.kt.after new file mode 100644 index 00000000000..01cce5d7657 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForCompanionObject.kt.after @@ -0,0 +1,11 @@ +// "Change 'Companion.foo' function return type to 'Int'" "true" +package foo.bar + +class A { + companion object { + fun foo(): Int { + return 1 + } + } +} + diff --git a/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForLocalClass.kt b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForLocalClass.kt new file mode 100644 index 00000000000..698ba6df23d --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForLocalClass.kt @@ -0,0 +1,10 @@ +// "Change 'A.foo' function return type to 'Int'" "true" +package foo.bar + +fun test() { + class A { + fun foo(): String { + return 1 + } + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForLocalClass.kt.after b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForLocalClass.kt.after new file mode 100644 index 00000000000..0ffeb1ad7ff --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForLocalClass.kt.after @@ -0,0 +1,10 @@ +// "Change 'A.foo' function return type to 'Int'" "true" +package foo.bar + +fun test() { + class A { + fun foo(): Int { + return 1 + } + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForNestedClass.kt b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForNestedClass.kt new file mode 100644 index 00000000000..73953c0cbb7 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForNestedClass.kt @@ -0,0 +1,11 @@ +// "Change 'B.foo' function return type to 'Int'" "true" +package foo.bar + +class A { + class B { + fun foo(): String { + return 1 + } + } +} + diff --git a/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForNestedClass.kt.after b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForNestedClass.kt.after new file mode 100644 index 00000000000..a83db80ae18 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForNestedClass.kt.after @@ -0,0 +1,11 @@ +// "Change 'B.foo' function return type to 'Int'" "true" +package foo.bar + +class A { + class B { + fun foo(): Int { + return 1 + } + } +} + diff --git a/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForTopLevelClass.kt b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForTopLevelClass.kt new file mode 100644 index 00000000000..bb45ed012d8 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForTopLevelClass.kt @@ -0,0 +1,9 @@ +// "Change 'A.foo' function return type to 'Int'" "true" +package foo.bar + +class A { + fun foo(): String { + return 1 + } +} + diff --git a/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForTopLevelClass.kt.after b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForTopLevelClass.kt.after new file mode 100644 index 00000000000..5afae86df5e --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForTopLevelClass.kt.after @@ -0,0 +1,9 @@ +// "Change 'A.foo' function return type to 'Int'" "true" +package foo.bar + +class A { + fun foo(): Int { + return 1 + } +} + diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index fc2fe3af9c6..172975b6d5a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -8137,6 +8137,36 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest(fileName); } + @TestMetadata("changeReturnTypeNoFqNameForAnonymousObject.kt") + public void testChangeReturnTypeNoFqNameForAnonymousObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForAnonymousObject.kt"); + doTest(fileName); + } + + @TestMetadata("changeReturnTypeNoFqNameForCompanionObject.kt") + public void testChangeReturnTypeNoFqNameForCompanionObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForCompanionObject.kt"); + doTest(fileName); + } + + @TestMetadata("changeReturnTypeNoFqNameForLocalClass.kt") + public void testChangeReturnTypeNoFqNameForLocalClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForLocalClass.kt"); + doTest(fileName); + } + + @TestMetadata("changeReturnTypeNoFqNameForNestedClass.kt") + public void testChangeReturnTypeNoFqNameForNestedClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForNestedClass.kt"); + doTest(fileName); + } + + @TestMetadata("changeReturnTypeNoFqNameForTopLevelClass.kt") + public void testChangeReturnTypeNoFqNameForTopLevelClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/changeReturnTypeNoFqNameForTopLevelClass.kt"); + doTest(fileName); + } + @TestMetadata("changeReturnTypeToSpecificNullable.kt") public void testChangeReturnTypeToSpecificNullable() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/changeReturnTypeToSpecificNullable.kt");