From 4b2fc9a325a4db12b5021011b14e57227f76c95b Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Wed, 27 Sep 2017 18:41:59 +0300 Subject: [PATCH] Don't offer to remove empty primary constructor of `expect` class --- .../RemoveEmptyPrimaryConstructorIntention.kt | 15 ++++++++++++++- .../expectClassExplicitConstructor.kt | 4 ++++ .../expectClassPrimaryConstructor.kt | 4 ++++ .../nestedExpectClassPrimaryConstructor.kt | 6 ++++++ .../intentions/IntentionTestGenerated.java | 18 ++++++++++++++++++ 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 idea/testData/intentions/removeEmptyPrimaryConstructor/expectClassExplicitConstructor.kt create mode 100644 idea/testData/intentions/removeEmptyPrimaryConstructor/expectClassPrimaryConstructor.kt create mode 100644 idea/testData/intentions/removeEmptyPrimaryConstructor/nestedExpectClassPrimaryConstructor.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptyPrimaryConstructorIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptyPrimaryConstructorIntention.kt index de4772fcf31..77922a38d44 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptyPrimaryConstructorIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptyPrimaryConstructorIntention.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * Copyright 2010-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,9 +19,14 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.codeInspection.CleanupLocalInspectionTool import com.intellij.codeInspection.ProblemHighlightType import com.intellij.openapi.editor.Editor +import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection +import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor import org.jetbrains.kotlin.psi.KtPrimaryConstructor import org.jetbrains.kotlin.psi.psiUtil.containingClass +import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject +import org.jetbrains.kotlin.psi.psiUtil.hasExpectModifier +import org.jetbrains.kotlin.utils.addToStdlib.safeAs class RemoveEmptyPrimaryConstructorInspection : IntentionBasedInspection( RemoveEmptyPrimaryConstructorIntention::class @@ -39,6 +44,14 @@ class RemoveEmptyPrimaryConstructorIntention : SelfTargetingOffsetIndependentInt element.annotations.isNotEmpty() -> false element.modifierList?.text?.isBlank() == false -> false element.containingClass()?.secondaryConstructors?.isNotEmpty() == true -> false + element.isConstructorOfExpectClass() -> false else -> true } + + private fun KtPrimaryConstructor.isConstructorOfExpectClass(): Boolean { + val containingClass = containingClassOrObject ?: return false + if (containingClass.hasExpectModifier()) return true + + return containingClass.descriptor.safeAs()?.isExpect ?: false + } } diff --git a/idea/testData/intentions/removeEmptyPrimaryConstructor/expectClassExplicitConstructor.kt b/idea/testData/intentions/removeEmptyPrimaryConstructor/expectClassExplicitConstructor.kt new file mode 100644 index 00000000000..cfdd1cc5d8d --- /dev/null +++ b/idea/testData/intentions/removeEmptyPrimaryConstructor/expectClassExplicitConstructor.kt @@ -0,0 +1,4 @@ +// IS_APPLICABLE: false + +@Suppress("UNSUPPORTED_FEATURE") +expect class Foo constructor() \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptyPrimaryConstructor/expectClassPrimaryConstructor.kt b/idea/testData/intentions/removeEmptyPrimaryConstructor/expectClassPrimaryConstructor.kt new file mode 100644 index 00000000000..d4e75eddcc5 --- /dev/null +++ b/idea/testData/intentions/removeEmptyPrimaryConstructor/expectClassPrimaryConstructor.kt @@ -0,0 +1,4 @@ +// IS_APPLICABLE: false + +@Suppress("UNSUPPORTED_FEATURE") +expect class Foo() \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptyPrimaryConstructor/nestedExpectClassPrimaryConstructor.kt b/idea/testData/intentions/removeEmptyPrimaryConstructor/nestedExpectClassPrimaryConstructor.kt new file mode 100644 index 00000000000..a3d491b78e0 --- /dev/null +++ b/idea/testData/intentions/removeEmptyPrimaryConstructor/nestedExpectClassPrimaryConstructor.kt @@ -0,0 +1,6 @@ +// IS_APPLICABLE: false + +@Suppress("UNSUPPORTED_FEATURE") +expect class Foo { + class Nested() +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 2b59dad1718..17ef085de0c 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -12779,6 +12779,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("expectClassExplicitConstructor.kt") + public void testExpectClassExplicitConstructor() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyPrimaryConstructor/expectClassExplicitConstructor.kt"); + doTest(fileName); + } + + @TestMetadata("expectClassPrimaryConstructor.kt") + public void testExpectClassPrimaryConstructor() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyPrimaryConstructor/expectClassPrimaryConstructor.kt"); + doTest(fileName); + } + @TestMetadata("keyword.kt") public void testKeyword() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyPrimaryConstructor/keyword.kt"); @@ -12791,6 +12803,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("nestedExpectClassPrimaryConstructor.kt") + public void testNestedExpectClassPrimaryConstructor() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyPrimaryConstructor/nestedExpectClassPrimaryConstructor.kt"); + doTest(fileName); + } + @TestMetadata("secondary.kt") public void testSecondary() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyPrimaryConstructor/secondary.kt");