From 51534f4592e62534dc0567c986a7a6abef81046a Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Wed, 27 May 2015 19:03:51 +0300 Subject: [PATCH] Make JetClassOrObject an abstract class implementing common methods --- .../src/org/jetbrains/kotlin/psi/JetClass.kt | 62 ++--------------- .../jetbrains/kotlin/psi/JetClassOrObject.kt | 36 ++++++---- .../kotlin/psi/JetObjectDeclaration.kt | 67 ++----------------- .../kotlin/psi/psiUtil/jetPsiUtil.kt | 2 +- 4 files changed, 33 insertions(+), 134 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetClass.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetClass.kt index a760263dc3a..43ed738977b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetClass.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetClass.kt @@ -21,32 +21,19 @@ import com.intellij.navigation.ItemPresentation import com.intellij.navigation.ItemPresentationProviders import com.intellij.openapi.util.text.StringUtil import com.intellij.psi.PsiElement -import com.intellij.psi.PsiFile import com.intellij.psi.tree.TokenSet import com.intellij.psi.util.PsiTreeUtil -import com.intellij.util.IncorrectOperationException -import org.jetbrains.kotlin.JetNodeTypes import org.jetbrains.kotlin.lexer.JetTokens -import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi.stubs.KotlinClassStub import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes - import java.util.ArrayList import java.util.Collections -public open class JetClass : JetTypeParameterListOwnerStub, JetClassOrObject { +public open class JetClass : JetClassOrObject { + public constructor(node: ASTNode) : super(node) + public constructor(stub: KotlinClassStub) : super(stub, JetStubElementTypes.CLASS) - public constructor(node: ASTNode) : super(node) { - } - - public constructor(stub: KotlinClassStub) : super(stub, JetStubElementTypes.CLASS) { - } - - override fun getDeclarations(): List { - val body = getBody() ?: return emptyList() - - return body.getDeclarations() - } + override fun getStub(): KotlinClassStub? = super.getStub() as? KotlinClassStub override fun accept(visitor: JetVisitor, data: D): R { return visitor.visitClass(this, data) @@ -82,38 +69,15 @@ public open class JetClass : JetTypeParameterListOwnerStub, Jet return constructor.add(JetPsiFactory(getProject()).createParameterList("()")) as JetParameterList } - override fun getDelegationSpecifierList(): JetDelegationSpecifierList? { - return getStubOrPsiChild(JetStubElementTypes.DELEGATION_SPECIFIER_LIST) - } - - override fun getDelegationSpecifiers(): List { - val list = getDelegationSpecifierList() - return if (list != null) list.getDelegationSpecifiers() else emptyList() - } - public fun getPrimaryConstructorModifierList(): JetModifierList? { val primaryConstructor = getPrimaryConstructor() return primaryConstructor?.getModifierList() } - override fun getAnonymousInitializers(): List { - val body = getBody() ?: return emptyList() - - return body.getAnonymousInitializers() - } - public fun hasExplicitPrimaryConstructor(): Boolean { return getPrimaryConstructor() != null } - override fun getNameAsDeclaration(): JetObjectDeclarationName? { - return findChildByType(JetNodeTypes.OBJECT_DECLARATION_NAME) as JetObjectDeclarationName - } - - override fun getBody(): JetClassBody? { - return getStubOrPsiChild(JetStubElementTypes.CLASS_BODY) - } - public fun getColon(): PsiElement? { return findChildByType(JetTokens.COLON) } @@ -146,7 +110,7 @@ public open class JetClass : JetTypeParameterListOwnerStub, Jet } override fun isEquivalentTo(another: PsiElement?): Boolean { - if (super.isEquivalentTo(another)) { + if (super.isEquivalentTo(another)) { return true } if (another is JetClass) { @@ -180,22 +144,6 @@ public open class JetClass : JetTypeParameterListOwnerStub, Jet return StringUtil.join(parts, ".") } - override fun getPresentation(): ItemPresentation? { - return ItemPresentationProviders.getItemPresentation(this) - } - - override fun isTopLevel(): Boolean { - return getContainingFile() == getParent() - } - - override fun isLocal(): Boolean { - val stub = getStub() - if (stub != null) { - return stub.isLocal() - } - return JetPsiUtil.isLocal(this) - } - public fun getCompanionObjects(): List { val body = getBody() ?: return emptyList() return body.getAllCompanionObjects() diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetClassOrObject.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetClassOrObject.kt index 432c2c02973..1df87e009f5 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetClassOrObject.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetClassOrObject.kt @@ -16,25 +16,35 @@ package org.jetbrains.kotlin.psi -import com.intellij.psi.PsiNameIdentifierOwner -import org.jetbrains.kotlin.name.Name +import com.intellij.lang.ASTNode +import com.intellij.navigation.ItemPresentation +import com.intellij.navigation.ItemPresentationProviders +import com.intellij.psi.PsiElement +import com.intellij.psi.stubs.IStubElementType +import org.jetbrains.kotlin.JetNodeTypes +import org.jetbrains.kotlin.lexer.JetTokens +import org.jetbrains.kotlin.psi.stubs.KotlinClassOrObjectStub +import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes -public interface JetClassOrObject : PsiNameIdentifierOwner, JetDeclarationContainer, JetElement, JetModifierListOwner, JetNamedDeclaration { - public fun getDelegationSpecifierList(): JetDelegationSpecifierList? +abstract public class JetClassOrObject : JetTypeParameterListOwnerStub>, JetDeclarationContainer, JetNamedDeclaration { + public constructor(node: ASTNode) : super(node) + public constructor(stub: KotlinClassOrObjectStub, nodeType: IStubElementType<*, *>) : super(stub, nodeType) - public fun getDelegationSpecifiers(): List + public fun getDelegationSpecifierList(): JetDelegationSpecifierList? = getStubOrPsiChild(JetStubElementTypes.DELEGATION_SPECIFIER_LIST) + open public fun getDelegationSpecifiers(): List = getDelegationSpecifierList()?.getDelegationSpecifiers().orEmpty() - public fun getAnonymousInitializers(): List + public fun getAnonymousInitializers(): List = getBody()?.getAnonymousInitializers().orEmpty() - override fun getNameAsName(): Name? + public fun getNameAsDeclaration(): JetObjectDeclarationName? = + findChildByType(JetNodeTypes.OBJECT_DECLARATION_NAME) as JetObjectDeclarationName? - override fun getModifierList(): JetModifierList? + public fun getBody(): JetClassBody? = getStubOrPsiChild(JetStubElementTypes.CLASS_BODY) - public fun getNameAsDeclaration(): JetObjectDeclarationName? + public fun isTopLevel(): Boolean = getStub()?.isTopLevel() ?: (getParent() is JetFile) - public fun getBody(): JetClassBody? + public fun isLocal(): Boolean = getStub()?.isLocal() ?: JetPsiUtil.isLocal(this) + + override fun getDeclarations() = getBody()?.getDeclarations().orEmpty() - public fun isTopLevel(): Boolean - - public fun isLocal(): Boolean + override fun getPresentation(): ItemPresentation? = ItemPresentationProviders.getItemPresentation(this) } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetObjectDeclaration.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetObjectDeclaration.kt index 75ae7f1578b..a9894b75fc2 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetObjectDeclaration.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetObjectDeclaration.kt @@ -22,21 +22,17 @@ import com.intellij.navigation.ItemPresentationProviders import com.intellij.psi.PsiElement import com.intellij.util.IncorrectOperationException import org.jetbrains.annotations.NonNls -import org.jetbrains.kotlin.JetNodeTypes import org.jetbrains.kotlin.lexer.JetModifierKeywordToken import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.name.SpecialNames import org.jetbrains.kotlin.psi.stubs.KotlinObjectStub import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes -import java.util.Collections +public class JetObjectDeclaration : JetClassOrObject { + public constructor(node: ASTNode) : super(node) + public constructor(stub: KotlinObjectStub) : super(stub, JetStubElementTypes.OBJECT_DECLARATION) -public class JetObjectDeclaration : JetNamedDeclarationStub, JetClassOrObject { - public constructor(node: ASTNode) : super(node) { - } - - public constructor(stub: KotlinObjectStub) : super(stub, JetStubElementTypes.OBJECT_DECLARATION) { - } + override fun getStub(): KotlinObjectStub? = super.getStub() as? KotlinObjectStub override fun getName(): String? { val stub = getStub() @@ -52,15 +48,6 @@ public class JetObjectDeclaration : JetNamedDeclarationStub, J return nameAsDeclaration?.getName() } - override fun isTopLevel(): Boolean { - val stub = getStub() - if (stub != null) { - return stub.isTopLevel() - } - - return getParent() is JetFile - } - override fun getNameIdentifier(): PsiElement? { val nameAsDeclaration = getNameAsDeclaration() return nameAsDeclaration?.getNameIdentifier() @@ -81,10 +68,6 @@ public class JetObjectDeclaration : JetNamedDeclarationStub, J } } - override fun getNameAsDeclaration(): JetObjectDeclarationName? { - return findChildByType(JetNodeTypes.OBJECT_DECLARATION_NAME) as JetObjectDeclarationName - } - public fun isCompanion(): Boolean { val stub = getStub() if (stub != null) { @@ -93,38 +76,6 @@ public class JetObjectDeclaration : JetNamedDeclarationStub, J return hasModifier(JetTokens.COMPANION_KEYWORD) } - override fun hasModifier(modifier: JetModifierKeywordToken): Boolean { - val modifierList = getModifierList() - return modifierList != null && modifierList.hasModifier(modifier) - } - - override fun getDelegationSpecifierList(): JetDelegationSpecifierList? { - return getStubOrPsiChild(JetStubElementTypes.DELEGATION_SPECIFIER_LIST) - } - - override fun getDelegationSpecifiers(): List { - val list = getDelegationSpecifierList() - return if (list != null) list.getDelegationSpecifiers() else emptyList() - } - - override fun getAnonymousInitializers(): List { - val body = getBody() ?: return emptyList() - - return body.getAnonymousInitializers() - } - - override fun getBody(): JetClassBody? { - return getStubOrPsiChild(JetStubElementTypes.CLASS_BODY) - } - - override fun isLocal(): Boolean { - val stub = getStub() - if (stub != null) { - return stub.isLocal() - } - return JetPsiUtil.isLocal(this) - } - override fun getTextOffset(): Int { val nameIdentifier = getNameIdentifier() if (nameIdentifier != null) { @@ -135,12 +86,6 @@ public class JetObjectDeclaration : JetNamedDeclarationStub, J } } - override fun getDeclarations(): List { - val body = getBody() ?: return emptyList() - - return body.getDeclarations() - } - override fun accept(visitor: JetVisitor, data: D): R { return visitor.visitObjectDeclaration(this, data) } @@ -156,8 +101,4 @@ public class JetObjectDeclaration : JetNamedDeclarationStub, J public fun getObjectKeyword(): PsiElement { return findChildByType(JetTokens.OBJECT_KEYWORD) } - - override fun getPresentation(): ItemPresentation? { - return ItemPresentationProviders.getItemPresentation(this) - } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/psiUtil/jetPsiUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/psiUtil/jetPsiUtil.kt index 95600b7b442..6dfbaa35f5b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/psiUtil/jetPsiUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/psiUtil/jetPsiUtil.kt @@ -193,7 +193,7 @@ public fun JetClass.isAbstract(): Boolean = isInterface() || hasModifier(JetToke * * @return the list of possible superclass names */ -public fun StubBasedPsiElementBase>.getSuperNames(): List { +public fun StubBasedPsiElementBase>.getSuperNames(): List { fun addSuperName(result: MutableList, referencedName: String): Unit { result.add(referencedName)