Refactoring: Unify getClassOrObject() and getContainingClassOrObject() functions in constructor PSI
This commit is contained in:
@@ -30,7 +30,7 @@ public abstract class JetConstructor<T : JetConstructor<T>> : JetDeclarationStub
|
|||||||
protected constructor(node: ASTNode) : super(node)
|
protected constructor(node: ASTNode) : super(node)
|
||||||
protected constructor(stub: KotlinPlaceHolderStub<T>, nodeType: JetPlaceHolderStubElementType<T>) : super(stub, nodeType)
|
protected constructor(stub: KotlinPlaceHolderStub<T>, nodeType: JetPlaceHolderStubElementType<T>) : super(stub, nodeType)
|
||||||
|
|
||||||
public abstract fun getClassOrObject(): JetClassOrObject
|
public abstract fun getContainingClassOrObject(): JetClassOrObject
|
||||||
|
|
||||||
override fun isLocal() = false
|
override fun isLocal() = false
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ public abstract class JetConstructor<T : JetConstructor<T>> : JetDeclarationStub
|
|||||||
|
|
||||||
override fun getTypeParameters() = emptyList<JetTypeParameter>()
|
override fun getTypeParameters() = emptyList<JetTypeParameter>()
|
||||||
|
|
||||||
override fun getName(): String = getClassOrObject().getName()!!
|
override fun getName(): String = getContainingClassOrObject().getName()!!
|
||||||
|
|
||||||
override fun getNameAsSafeName() = Name.identifier(getName())
|
override fun getNameAsSafeName() = Name.identifier(getName())
|
||||||
|
|
||||||
|
|||||||
@@ -29,9 +29,7 @@ public class JetPrimaryConstructor : JetConstructor<JetPrimaryConstructor> {
|
|||||||
|
|
||||||
override fun <R, D> accept(visitor: JetVisitor<R, D>, data: D) = visitor.visitPrimaryConstructor(this, data)
|
override fun <R, D> accept(visitor: JetVisitor<R, D>, data: D) = visitor.visitPrimaryConstructor(this, data)
|
||||||
|
|
||||||
public fun getContainingClassOrObject(): JetClassOrObject = getParent() as JetClassOrObject
|
override fun getContainingClassOrObject() = getParent() as JetClassOrObject
|
||||||
|
|
||||||
override fun getClassOrObject() = getContainingClassOrObject()
|
|
||||||
|
|
||||||
override fun addModifier(modifier: JetModifierKeywordToken) {
|
override fun addModifier(modifier: JetModifierKeywordToken) {
|
||||||
val modifierList = getModifierList()
|
val modifierList = getModifierList()
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public class JetSecondaryConstructor : JetConstructor<JetSecondaryConstructor> {
|
|||||||
|
|
||||||
override fun <R, D> accept(visitor: JetVisitor<R, D>, data: D) = visitor.visitSecondaryConstructor(this, data)
|
override fun <R, D> accept(visitor: JetVisitor<R, D>, data: D) = visitor.visitSecondaryConstructor(this, data)
|
||||||
|
|
||||||
override fun getClassOrObject() = getParent().getParent() as JetClassOrObject
|
override fun getContainingClassOrObject() = getParent().getParent() as JetClassOrObject
|
||||||
|
|
||||||
override fun getBodyExpression() = findChildByClass(javaClass<JetBlockExpression>())
|
override fun getBodyExpression() = findChildByClass(javaClass<JetBlockExpression>())
|
||||||
|
|
||||||
|
|||||||
@@ -203,7 +203,7 @@ public class OverloadResolver {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
String containingClassName = jetDeclaration instanceof JetSecondaryConstructor ?
|
String containingClassName = jetDeclaration instanceof JetSecondaryConstructor ?
|
||||||
((JetSecondaryConstructor) jetDeclaration).getClassOrObject().getName() : null;
|
((JetSecondaryConstructor) jetDeclaration).getContainingClassOrObject().getName() : null;
|
||||||
|
|
||||||
trace.report(Errors.CONFLICTING_OVERLOADS.on(
|
trace.report(Errors.CONFLICTING_OVERLOADS.on(
|
||||||
jetDeclaration, memberDescriptor,
|
jetDeclaration, memberDescriptor,
|
||||||
|
|||||||
@@ -248,7 +248,7 @@ public class LightClassUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (declaration instanceof JetConstructor) {
|
if (declaration instanceof JetConstructor) {
|
||||||
return getPsiClass(((JetConstructor) declaration).getClassOrObject());
|
return getPsiClass(((JetConstructor) declaration).getContainingClassOrObject());
|
||||||
}
|
}
|
||||||
|
|
||||||
//noinspection unchecked
|
//noinspection unchecked
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ object KDocFinder {
|
|||||||
var psiDeclaration = (declaration.getSource() as? PsiSourceElement)?.psi?.getNavigationElement()
|
var psiDeclaration = (declaration.getSource() as? PsiSourceElement)?.psi?.getNavigationElement()
|
||||||
// KDoc for primary constructor is located inside of its class KDoc
|
// KDoc for primary constructor is located inside of its class KDoc
|
||||||
if (psiDeclaration is JetPrimaryConstructor) {
|
if (psiDeclaration is JetPrimaryConstructor) {
|
||||||
psiDeclaration = psiDeclaration.getClassOrObject()
|
psiDeclaration = psiDeclaration.getContainingClassOrObject()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (psiDeclaration is JetDeclaration) {
|
if (psiDeclaration is JetDeclaration) {
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ private fun PsiElement.isConstructorOf(unwrappedCandidate: PsiElement) =
|
|||||||
// call to Java constructor
|
// call to Java constructor
|
||||||
(this is PsiMethod && isConstructor() && getContainingClass() == unwrappedCandidate) ||
|
(this is PsiMethod && isConstructor() && getContainingClass() == unwrappedCandidate) ||
|
||||||
// call to Kotlin constructor
|
// call to Kotlin constructor
|
||||||
(this is JetConstructor<*> && getClassOrObject() == unwrappedCandidate)
|
(this is JetConstructor<*> && getContainingClassOrObject() == unwrappedCandidate)
|
||||||
|
|
||||||
fun AbstractJetReference<out JetExpression>.renameImplicitConventionalCall(newName: String?): JetExpression {
|
fun AbstractJetReference<out JetExpression>.renameImplicitConventionalCall(newName: String?): JetExpression {
|
||||||
if (newName == null) return expression
|
if (newName == null) return expression
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ fun PsiReference.isConstructorUsage(jetClassOrObject: JetClassOrObject): Boolean
|
|||||||
if (descriptor !is ConstructorDescriptor) return false
|
if (descriptor !is ConstructorDescriptor) return false
|
||||||
|
|
||||||
val declaration = DescriptorToSourceUtils.descriptorToDeclaration(descriptor.getContainingDeclaration())
|
val declaration = DescriptorToSourceUtils.descriptorToDeclaration(descriptor.getContainingDeclaration())
|
||||||
return declaration == jetClassOrObject || (declaration is JetConstructor<*> && declaration.getClassOrObject() == jetClassOrObject)
|
return declaration == jetClassOrObject || (declaration is JetConstructor<*> && declaration.getContainingClassOrObject() == jetClassOrObject)
|
||||||
}
|
}
|
||||||
|
|
||||||
checkJavaUsage() || checkKotlinUsage()
|
checkJavaUsage() || checkKotlinUsage()
|
||||||
@@ -120,7 +120,7 @@ public fun PsiElement.processDelegationCallConstructorUsages(scope: SearchScope,
|
|||||||
private fun PsiElement.processDelegationCallKotlinConstructorUsages(scope: SearchScope, process: (JetConstructorDelegationCall) -> Unit) {
|
private fun PsiElement.processDelegationCallKotlinConstructorUsages(scope: SearchScope, process: (JetConstructorDelegationCall) -> Unit) {
|
||||||
val element = unwrapped
|
val element = unwrapped
|
||||||
val klass = when (element) {
|
val klass = when (element) {
|
||||||
is JetConstructor<*> -> element.getClassOrObject()
|
is JetConstructor<*> -> element.getContainingClassOrObject()
|
||||||
is JetClass -> element
|
is JetClass -> element
|
||||||
else -> return
|
else -> return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ public class JetFunctionPresenter implements ItemPresentationProvider<JetFunctio
|
|||||||
@Override
|
@Override
|
||||||
public String getLocationString() {
|
public String getLocationString() {
|
||||||
if (function instanceof JetConstructor) {
|
if (function instanceof JetConstructor) {
|
||||||
FqName name = ((JetConstructor) function).getClassOrObject().getFqName();
|
FqName name = ((JetConstructor) function).getContainingClassOrObject().getFqName();
|
||||||
return name != null ? String.format("(in %s)", name) : "";
|
return name != null ? String.format("(in %s)", name) : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -54,7 +54,7 @@ public class AddOpenModifierToClassDeclarationFix extends JetIntentionAction<Jet
|
|||||||
if (reference != null) {
|
if (reference != null) {
|
||||||
PsiElement target = reference.resolve();
|
PsiElement target = reference.resolve();
|
||||||
if (target instanceof JetSecondaryConstructor) {
|
if (target instanceof JetSecondaryConstructor) {
|
||||||
target = ((JetSecondaryConstructor) target).getClassOrObject();
|
target = ((JetSecondaryConstructor) target).getContainingClassOrObject();
|
||||||
}
|
}
|
||||||
if (target instanceof JetClass && QuickFixUtil.canModifyElement(target)) {
|
if (target instanceof JetClass && QuickFixUtil.canModifyElement(target)) {
|
||||||
classDeclaration = (JetClass) target;
|
classDeclaration = (JetClass) target;
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ public class InsertDelegationCallQuickfix(val isThis: Boolean, element: JetSecon
|
|||||||
object InsertThisDelegationCallFactory : JetSingleIntentionActionFactory() {
|
object InsertThisDelegationCallFactory : JetSingleIntentionActionFactory() {
|
||||||
override fun createAction(diagnostic: Diagnostic) = diagnostic.createIntentionForFirstParentOfType<JetSecondaryConstructor> {
|
override fun createAction(diagnostic: Diagnostic) = diagnostic.createIntentionForFirstParentOfType<JetSecondaryConstructor> {
|
||||||
secondaryConstructor ->
|
secondaryConstructor ->
|
||||||
if (secondaryConstructor.getClassOrObject().getConstructorsCount() <= 1 ||
|
if (secondaryConstructor.getContainingClassOrObject().getConstructorsCount() <= 1 ||
|
||||||
!secondaryConstructor.hasImplicitDelegationCall()) return null
|
!secondaryConstructor.hasImplicitDelegationCall()) return null
|
||||||
|
|
||||||
return InsertDelegationCallQuickfix(isThis = true, element = secondaryConstructor)
|
return InsertDelegationCallQuickfix(isThis = true, element = secondaryConstructor)
|
||||||
@@ -75,7 +75,7 @@ public class InsertDelegationCallQuickfix(val isThis: Boolean, element: JetSecon
|
|||||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||||
val secondaryConstructor = diagnostic.getPsiElement().getNonStrictParentOfType<JetSecondaryConstructor>() ?: return null
|
val secondaryConstructor = diagnostic.getPsiElement().getNonStrictParentOfType<JetSecondaryConstructor>() ?: return null
|
||||||
if (!secondaryConstructor.hasImplicitDelegationCall()) return null
|
if (!secondaryConstructor.hasImplicitDelegationCall()) return null
|
||||||
val klass = secondaryConstructor.getClassOrObject() as? JetClass ?: return null
|
val klass = secondaryConstructor.getContainingClassOrObject() as? JetClass ?: return null
|
||||||
if (klass.hasPrimaryConstructor()) return null
|
if (klass.hasPrimaryConstructor()) return null
|
||||||
|
|
||||||
return InsertDelegationCallQuickfix(isThis = false, element = secondaryConstructor)
|
return InsertDelegationCallQuickfix(isThis = false, element = secondaryConstructor)
|
||||||
|
|||||||
Reference in New Issue
Block a user