Safe Delete: Support secondary constructors
This commit is contained in:
@@ -20,13 +20,14 @@ import com.intellij.psi.ElementDescriptionLocation
|
||||
import com.intellij.psi.ElementDescriptionProvider
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiNamedElement
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil
|
||||
import com.intellij.refactoring.util.RefactoringDescriptionLocation
|
||||
import com.intellij.usageView.UsageViewLongNameLocation
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.asJava.unwrapped
|
||||
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil
|
||||
|
||||
public class JetElementDescriptionProvider : ElementDescriptionProvider {
|
||||
public override fun getElementDescription(element: PsiElement, location: ElementDescriptionLocation): String? {
|
||||
@@ -45,26 +46,26 @@ public class JetElementDescriptionProvider : ElementDescriptionProvider {
|
||||
|
||||
if (targetElement !is PsiNamedElement || targetElement !is JetElement) return null
|
||||
|
||||
val name = (targetElement as PsiNamedElement).getName()
|
||||
val name = (targetElement : PsiNamedElement).getName()
|
||||
|
||||
return when(location) {
|
||||
is UsageViewLongNameLocation ->
|
||||
name
|
||||
is RefactoringDescriptionLocation -> {
|
||||
val kind = elementKind()
|
||||
if (kind != null) {
|
||||
val descriptor = (targetElement as JetDeclaration).descriptor
|
||||
if (descriptor != null) {
|
||||
val desc = if (location.includeParent() && targetElement !is JetTypeParameter && targetElement !is JetParameter) {
|
||||
val kind = elementKind() ?: return null
|
||||
var descriptor = (targetElement as JetDeclaration).descriptor ?: return null
|
||||
if (descriptor is ConstructorDescriptor) {
|
||||
descriptor = (descriptor as ConstructorDescriptor).getContainingDeclaration()
|
||||
}
|
||||
val desc =
|
||||
if (location.includeParent() && targetElement !is JetTypeParameter && targetElement !is JetParameter) {
|
||||
DescriptorUtils.getFqName(descriptor).asString()
|
||||
}
|
||||
else descriptor.getName().asString()
|
||||
else {
|
||||
descriptor.getName().asString()
|
||||
}
|
||||
|
||||
"$kind ${CommonRefactoringUtil.htmlEmphasize(desc)}"
|
||||
}
|
||||
else null
|
||||
}
|
||||
else null
|
||||
"$kind ${CommonRefactoringUtil.htmlEmphasize(desc)}"
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
|
||||
+12
@@ -24,11 +24,16 @@ import com.intellij.openapi.util.Conditions
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiMethod
|
||||
import com.intellij.psi.PsiParameter
|
||||
import com.intellij.psi.PsiReference
|
||||
import com.intellij.psi.impl.search.ConstructorReferencesSearchHelper
|
||||
import com.intellij.psi.search.SearchRequestCollector
|
||||
import com.intellij.psi.search.SearchSession
|
||||
import com.intellij.psi.search.searches.ReferencesSearch
|
||||
import com.intellij.refactoring.safeDelete.JavaSafeDeleteProcessor
|
||||
import com.intellij.refactoring.safeDelete.NonCodeUsageSearchInfo
|
||||
import com.intellij.refactoring.safeDelete.usageInfo.*
|
||||
import com.intellij.usageView.UsageInfo
|
||||
import com.intellij.util.Processor
|
||||
import org.jetbrains.kotlin.asJava.*
|
||||
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
@@ -173,6 +178,13 @@ public class KotlinSafeDeleteProcessor : JavaSafeDeleteProcessor() {
|
||||
}
|
||||
}
|
||||
|
||||
is JetSecondaryConstructor -> {
|
||||
element.getRepresentativeLightMethod()?.let { method ->
|
||||
findDelegationCallUsages(method)
|
||||
findUsagesByJavaProcessor(method, false)
|
||||
}
|
||||
}
|
||||
|
||||
is JetNamedFunction -> {
|
||||
if (element.isLocal()) {
|
||||
findKotlinDeclarationUsages(element)
|
||||
|
||||
@@ -33,6 +33,7 @@ public fun PsiElement.canDeleteElement(): Boolean {
|
||||
}
|
||||
|
||||
return this is JetClassOrObject
|
||||
|| this is JetSecondaryConstructor
|
||||
|| this is JetNamedFunction
|
||||
|| this is PsiMethod
|
||||
|| this is JetProperty
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
class J extends B {
|
||||
B b = new B(1);
|
||||
|
||||
public J() {
|
||||
super(1);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
open class B {
|
||||
<caret>constructor(a: Int) {
|
||||
|
||||
}
|
||||
|
||||
constructor(): this(1) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
open class A: B {
|
||||
constructor(a: Int): super(a) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
open class C: B(1) {
|
||||
|
||||
}
|
||||
|
||||
fun test() {
|
||||
B(1)
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
constructor B has 5 usages that are not safe to delete.
|
||||
@@ -63,11 +63,11 @@ public abstract class AbstractJetSafeDeleteTest extends JetLightCodeInsightFixtu
|
||||
}
|
||||
|
||||
public void doFunctionTest(@NotNull String path) throws Exception {
|
||||
doTest(path, JetNamedFunction.class, false);
|
||||
doTest(path, JetFunction.class, false);
|
||||
}
|
||||
|
||||
public void doFunctionTestWithJava(@NotNull String path) throws Exception {
|
||||
doTest(path, JetNamedFunction.class, true);
|
||||
doTest(path, JetFunction.class, true);
|
||||
}
|
||||
|
||||
public void doJavaMethodTest(@NotNull String path) throws Exception {
|
||||
|
||||
@@ -376,6 +376,12 @@ public class JetSafeDeleteTestGenerated extends AbstractJetSafeDeleteTest {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/safeDelete/deleteFunction/kotlinFunctionWithJava/overrideAndImplement2.kt");
|
||||
doFunctionTestWithJava(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("secondaryConstructor.kt")
|
||||
public void testSecondaryConstructor() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/safeDelete/deleteFunction/kotlinFunctionWithJava/secondaryConstructor.kt");
|
||||
doFunctionTestWithJava(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/safeDelete/deleteFunction/javaFunctionWithKotlin")
|
||||
|
||||
Reference in New Issue
Block a user