Make JetClassOrObject an abstract class implementing common methods
This commit is contained in:
@@ -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<KotlinClassStub>, 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<JetDeclaration> {
|
||||
val body = getBody() ?: return emptyList<JetDeclaration>()
|
||||
|
||||
return body.getDeclarations()
|
||||
}
|
||||
override fun getStub(): KotlinClassStub? = super.getStub() as? KotlinClassStub
|
||||
|
||||
override fun <R, D> accept(visitor: JetVisitor<R, D>, data: D): R {
|
||||
return visitor.visitClass(this, data)
|
||||
@@ -82,38 +69,15 @@ public open class JetClass : JetTypeParameterListOwnerStub<KotlinClassStub>, Jet
|
||||
return constructor.add(JetPsiFactory(getProject()).createParameterList("()")) as JetParameterList
|
||||
}
|
||||
|
||||
override fun getDelegationSpecifierList(): JetDelegationSpecifierList? {
|
||||
return getStubOrPsiChild(JetStubElementTypes.DELEGATION_SPECIFIER_LIST)
|
||||
}
|
||||
|
||||
override fun getDelegationSpecifiers(): List<JetDelegationSpecifier> {
|
||||
val list = getDelegationSpecifierList()
|
||||
return if (list != null) list.getDelegationSpecifiers() else emptyList<JetDelegationSpecifier>()
|
||||
}
|
||||
|
||||
public fun getPrimaryConstructorModifierList(): JetModifierList? {
|
||||
val primaryConstructor = getPrimaryConstructor()
|
||||
return primaryConstructor?.getModifierList()
|
||||
}
|
||||
|
||||
override fun getAnonymousInitializers(): List<JetClassInitializer> {
|
||||
val body = getBody() ?: return emptyList<JetClassInitializer>()
|
||||
|
||||
return body.getAnonymousInitializers()
|
||||
}
|
||||
|
||||
public fun hasExplicitPrimaryConstructor(): Boolean {
|
||||
return getPrimaryConstructor() != null
|
||||
}
|
||||
|
||||
override fun getNameAsDeclaration(): JetObjectDeclarationName? {
|
||||
return findChildByType<PsiElement>(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<KotlinClassStub>, Jet
|
||||
}
|
||||
|
||||
override fun isEquivalentTo(another: PsiElement?): Boolean {
|
||||
if (super<JetTypeParameterListOwnerStub>.isEquivalentTo(another)) {
|
||||
if (super.isEquivalentTo(another)) {
|
||||
return true
|
||||
}
|
||||
if (another is JetClass) {
|
||||
@@ -180,22 +144,6 @@ public open class JetClass : JetTypeParameterListOwnerStub<KotlinClassStub>, 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<JetObjectDeclaration> {
|
||||
val body = getBody() ?: return emptyList<JetObjectDeclaration>()
|
||||
return body.getAllCompanionObjects()
|
||||
|
||||
@@ -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<KotlinClassOrObjectStub<out JetClassOrObject>>, JetDeclarationContainer, JetNamedDeclaration {
|
||||
public constructor(node: ASTNode) : super(node)
|
||||
public constructor(stub: KotlinClassOrObjectStub<out JetClassOrObject>, nodeType: IStubElementType<*, *>) : super(stub, nodeType)
|
||||
|
||||
public fun getDelegationSpecifiers(): List<JetDelegationSpecifier>
|
||||
public fun getDelegationSpecifierList(): JetDelegationSpecifierList? = getStubOrPsiChild(JetStubElementTypes.DELEGATION_SPECIFIER_LIST)
|
||||
open public fun getDelegationSpecifiers(): List<JetDelegationSpecifier> = getDelegationSpecifierList()?.getDelegationSpecifiers().orEmpty()
|
||||
|
||||
public fun getAnonymousInitializers(): List<JetClassInitializer>
|
||||
public fun getAnonymousInitializers(): List<JetClassInitializer> = getBody()?.getAnonymousInitializers().orEmpty()
|
||||
|
||||
override fun getNameAsName(): Name?
|
||||
public fun getNameAsDeclaration(): JetObjectDeclarationName? =
|
||||
findChildByType<PsiElement>(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)
|
||||
}
|
||||
|
||||
@@ -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<KotlinObjectStub>, 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<KotlinObjectStub>, 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<KotlinObjectStub>, J
|
||||
}
|
||||
}
|
||||
|
||||
override fun getNameAsDeclaration(): JetObjectDeclarationName? {
|
||||
return findChildByType<PsiElement>(JetNodeTypes.OBJECT_DECLARATION_NAME) as JetObjectDeclarationName
|
||||
}
|
||||
|
||||
public fun isCompanion(): Boolean {
|
||||
val stub = getStub()
|
||||
if (stub != null) {
|
||||
@@ -93,38 +76,6 @@ public class JetObjectDeclaration : JetNamedDeclarationStub<KotlinObjectStub>, 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<JetDelegationSpecifier> {
|
||||
val list = getDelegationSpecifierList()
|
||||
return if (list != null) list.getDelegationSpecifiers() else emptyList<JetDelegationSpecifier>()
|
||||
}
|
||||
|
||||
override fun getAnonymousInitializers(): List<JetClassInitializer> {
|
||||
val body = getBody() ?: return emptyList<JetClassInitializer>()
|
||||
|
||||
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<KotlinObjectStub>, J
|
||||
}
|
||||
}
|
||||
|
||||
override fun getDeclarations(): List<JetDeclaration> {
|
||||
val body = getBody() ?: return emptyList<JetDeclaration>()
|
||||
|
||||
return body.getDeclarations()
|
||||
}
|
||||
|
||||
override fun <R, D> accept(visitor: JetVisitor<R, D>, data: D): R {
|
||||
return visitor.visitObjectDeclaration(this, data)
|
||||
}
|
||||
@@ -156,8 +101,4 @@ public class JetObjectDeclaration : JetNamedDeclarationStub<KotlinObjectStub>, J
|
||||
public fun getObjectKeyword(): PsiElement {
|
||||
return findChildByType(JetTokens.OBJECT_KEYWORD)
|
||||
}
|
||||
|
||||
override fun getPresentation(): ItemPresentation? {
|
||||
return ItemPresentationProviders.getItemPresentation(this)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,7 +193,7 @@ public fun JetClass.isAbstract(): Boolean = isInterface() || hasModifier(JetToke
|
||||
*
|
||||
* @return the list of possible superclass names
|
||||
*/
|
||||
public fun <T: JetClassOrObject> StubBasedPsiElementBase<out KotlinClassOrObjectStub<T>>.getSuperNames(): List<String> {
|
||||
public fun StubBasedPsiElementBase<out KotlinClassOrObjectStub<out JetClassOrObject>>.getSuperNames(): List<String> {
|
||||
fun addSuperName(result: MutableList<String>, referencedName: String): Unit {
|
||||
result.add(referencedName)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user