Don't offer to remove empty primary constructor of expect class

This commit is contained in:
Mikhail Zarechenskiy
2017-09-27 18:41:59 +03:00
parent f3344ec2b2
commit 4b2fc9a325
5 changed files with 46 additions and 1 deletions
@@ -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<KtPrimaryConstructor>(
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<ClassDescriptor>()?.isExpect ?: false
}
}
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
@Suppress("UNSUPPORTED_FEATURE")
expect class Foo constructor()<caret>
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
@Suppress("UNSUPPORTED_FEATURE")
expect class Foo()<caret>
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
@Suppress("UNSUPPORTED_FEATURE")
expect class Foo {
class Nested()<caret>
}
@@ -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");