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.ElementDescriptionProvider
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import com.intellij.psi.PsiNamedElement
|
import com.intellij.psi.PsiNamedElement
|
||||||
|
import com.intellij.refactoring.util.CommonRefactoringUtil
|
||||||
import com.intellij.refactoring.util.RefactoringDescriptionLocation
|
import com.intellij.refactoring.util.RefactoringDescriptionLocation
|
||||||
import com.intellij.usageView.UsageViewLongNameLocation
|
import com.intellij.usageView.UsageViewLongNameLocation
|
||||||
import org.jetbrains.kotlin.psi.*
|
|
||||||
import org.jetbrains.kotlin.asJava.unwrapped
|
import org.jetbrains.kotlin.asJava.unwrapped
|
||||||
|
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||||
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
||||||
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||||
import com.intellij.refactoring.util.CommonRefactoringUtil
|
|
||||||
|
|
||||||
public class JetElementDescriptionProvider : ElementDescriptionProvider {
|
public class JetElementDescriptionProvider : ElementDescriptionProvider {
|
||||||
public override fun getElementDescription(element: PsiElement, location: ElementDescriptionLocation): String? {
|
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
|
if (targetElement !is PsiNamedElement || targetElement !is JetElement) return null
|
||||||
|
|
||||||
val name = (targetElement as PsiNamedElement).getName()
|
val name = (targetElement : PsiNamedElement).getName()
|
||||||
|
|
||||||
return when(location) {
|
return when(location) {
|
||||||
is UsageViewLongNameLocation ->
|
is UsageViewLongNameLocation ->
|
||||||
name
|
name
|
||||||
is RefactoringDescriptionLocation -> {
|
is RefactoringDescriptionLocation -> {
|
||||||
val kind = elementKind()
|
val kind = elementKind() ?: return null
|
||||||
if (kind != null) {
|
var descriptor = (targetElement as JetDeclaration).descriptor ?: return null
|
||||||
val descriptor = (targetElement as JetDeclaration).descriptor
|
if (descriptor is ConstructorDescriptor) {
|
||||||
if (descriptor != null) {
|
descriptor = (descriptor as ConstructorDescriptor).getContainingDeclaration()
|
||||||
val desc = if (location.includeParent() && targetElement !is JetTypeParameter && targetElement !is JetParameter) {
|
}
|
||||||
|
val desc =
|
||||||
|
if (location.includeParent() && targetElement !is JetTypeParameter && targetElement !is JetParameter) {
|
||||||
DescriptorUtils.getFqName(descriptor).asString()
|
DescriptorUtils.getFqName(descriptor).asString()
|
||||||
}
|
}
|
||||||
else descriptor.getName().asString()
|
else {
|
||||||
|
descriptor.getName().asString()
|
||||||
|
}
|
||||||
|
|
||||||
"$kind ${CommonRefactoringUtil.htmlEmphasize(desc)}"
|
"$kind ${CommonRefactoringUtil.htmlEmphasize(desc)}"
|
||||||
}
|
|
||||||
else null
|
|
||||||
}
|
|
||||||
else null
|
|
||||||
}
|
}
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
|
|||||||
+12
@@ -24,11 +24,16 @@ import com.intellij.openapi.util.Conditions
|
|||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import com.intellij.psi.PsiMethod
|
import com.intellij.psi.PsiMethod
|
||||||
import com.intellij.psi.PsiParameter
|
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.psi.search.searches.ReferencesSearch
|
||||||
import com.intellij.refactoring.safeDelete.JavaSafeDeleteProcessor
|
import com.intellij.refactoring.safeDelete.JavaSafeDeleteProcessor
|
||||||
import com.intellij.refactoring.safeDelete.NonCodeUsageSearchInfo
|
import com.intellij.refactoring.safeDelete.NonCodeUsageSearchInfo
|
||||||
import com.intellij.refactoring.safeDelete.usageInfo.*
|
import com.intellij.refactoring.safeDelete.usageInfo.*
|
||||||
import com.intellij.usageView.UsageInfo
|
import com.intellij.usageView.UsageInfo
|
||||||
|
import com.intellij.util.Processor
|
||||||
import org.jetbrains.kotlin.asJava.*
|
import org.jetbrains.kotlin.asJava.*
|
||||||
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
||||||
import org.jetbrains.kotlin.descriptors.Modality
|
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 -> {
|
is JetNamedFunction -> {
|
||||||
if (element.isLocal()) {
|
if (element.isLocal()) {
|
||||||
findKotlinDeclarationUsages(element)
|
findKotlinDeclarationUsages(element)
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ public fun PsiElement.canDeleteElement(): Boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return this is JetClassOrObject
|
return this is JetClassOrObject
|
||||||
|
|| this is JetSecondaryConstructor
|
||||||
|| this is JetNamedFunction
|
|| this is JetNamedFunction
|
||||||
|| this is PsiMethod
|
|| this is PsiMethod
|
||||||
|| this is JetProperty
|
|| 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 {
|
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 {
|
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 {
|
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");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/safeDelete/deleteFunction/kotlinFunctionWithJava/overrideAndImplement2.kt");
|
||||||
doFunctionTestWithJava(fileName);
|
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")
|
@TestMetadata("idea/testData/safeDelete/deleteFunction/javaFunctionWithKotlin")
|
||||||
|
|||||||
Reference in New Issue
Block a user