From a7d189cd1c196976f531c1178e5b69a072161163 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 24 Jul 2018 16:16:00 +0300 Subject: [PATCH] Branch 173: delete unnecessary duplicate of KtClassOrObject --- .../kotlin/psi/KtClassOrObject.kt.173 | 139 ------------------ 1 file changed, 139 deletions(-) delete mode 100644 compiler/psi/src/org/jetbrains/kotlin/psi/KtClassOrObject.kt.173 diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtClassOrObject.kt.173 b/compiler/psi/src/org/jetbrains/kotlin/psi/KtClassOrObject.kt.173 deleted file mode 100644 index 359e1d2b943..00000000000 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtClassOrObject.kt.173 +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.psi - -import com.intellij.lang.ASTNode -import com.intellij.navigation.ItemPresentation -import com.intellij.navigation.ItemPresentationProviders -import com.intellij.psi.PsiElement -import com.intellij.psi.PsiWhiteSpace -import com.intellij.psi.impl.CheckUtil -import com.intellij.psi.stubs.IStubElementType -import com.intellij.psi.tree.TokenSet -import com.intellij.psi.util.PsiTreeUtil -import org.jetbrains.kotlin.lexer.KtTokens -import org.jetbrains.kotlin.psi.stubs.KotlinClassOrObjectStub -import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes - -abstract class KtClassOrObject : - KtTypeParameterListOwnerStub>, KtDeclarationContainer, KtNamedDeclaration, - KtPureClassOrObject { - constructor(node: ASTNode) : super(node) - constructor(stub: KotlinClassOrObjectStub, nodeType: IStubElementType<*, *>) : super(stub, nodeType) - - fun getSuperTypeList(): KtSuperTypeList? = getStubOrPsiChild(KtStubElementTypes.SUPER_TYPE_LIST) - - override fun getSuperTypeListEntries(): List = getSuperTypeList()?.entries.orEmpty() - - fun addSuperTypeListEntry(superTypeListEntry: KtSuperTypeListEntry): KtSuperTypeListEntry { - getSuperTypeList()?.let { - val single = it.entries.singleOrNull() - if (single != null && single.typeReference?.typeElement == null) { - return single.replace(superTypeListEntry) as KtSuperTypeListEntry - } - return EditCommaSeparatedListHelper.addItem(it, superTypeListEntries, superTypeListEntry) - } - - val psiFactory = KtPsiFactory(this) - val specifierListToAdd = psiFactory.createSuperTypeCallEntry("A()").replace(superTypeListEntry).parent - val colon = addBefore(psiFactory.createColon(), getBody()) - return (addAfter(specifierListToAdd, colon) as KtSuperTypeList).entries.first() - } - - fun removeSuperTypeListEntry(superTypeListEntry: KtSuperTypeListEntry) { - val specifierList = getSuperTypeList() ?: return - assert(superTypeListEntry.parent === specifierList) - - if (specifierList.entries.size > 1) { - EditCommaSeparatedListHelper.removeItem(superTypeListEntry) - } else { - deleteChildRange(findChildByType(KtTokens.COLON) ?: specifierList, specifierList) - } - } - - fun getAnonymousInitializers(): List = getBody()?.anonymousInitializers.orEmpty() - - fun getBody(): KtClassBody? = getStubOrPsiChild(KtStubElementTypes.CLASS_BODY) - - inline fun addDeclaration(declaration: T): T { - val body = getOrCreateBody() - val anchor = PsiTreeUtil.skipSiblingsBackward(body.rBrace ?: body.lastChild!!, PsiWhiteSpace::class.java) - return body.addAfter(declaration, anchor) as T - } - - inline fun addDeclarationAfter(declaration: T, anchor: PsiElement?): T { - val anchorBefore = anchor ?: declarations.lastOrNull() ?: return addDeclaration(declaration) - return getOrCreateBody().addAfter(declaration, anchorBefore) as T - } - - inline fun addDeclarationBefore(declaration: T, anchor: PsiElement?): T { - val anchorAfter = anchor ?: declarations.firstOrNull() ?: return addDeclaration(declaration) - return getOrCreateBody().addBefore(declaration, anchorAfter) as T - } - - fun isTopLevel(): Boolean = stub?.isTopLevel() ?: (parent is KtFile) - - override fun isLocal(): Boolean = stub?.isLocal() ?: KtPsiUtil.isLocal(this) - - override fun getDeclarations(): List = getBody()?.declarations.orEmpty() - - override fun getPresentation(): ItemPresentation? = ItemPresentationProviders.getItemPresentation(this) - - override fun getPrimaryConstructor(): KtPrimaryConstructor? = getStubOrPsiChild(KtStubElementTypes.PRIMARY_CONSTRUCTOR) - - override fun getPrimaryConstructorModifierList(): KtModifierList? = primaryConstructor?.modifierList - - fun getPrimaryConstructorParameterList(): KtParameterList? = primaryConstructor?.valueParameterList - - override fun getPrimaryConstructorParameters(): List = getPrimaryConstructorParameterList()?.parameters.orEmpty() - - override fun hasExplicitPrimaryConstructor(): Boolean = primaryConstructor != null - - override fun hasPrimaryConstructor(): Boolean = hasExplicitPrimaryConstructor() || !hasSecondaryConstructors() - - private fun hasSecondaryConstructors(): Boolean = !secondaryConstructors.isEmpty() - - override fun getSecondaryConstructors(): List = getBody()?.secondaryConstructors.orEmpty() - - fun isAnnotation(): Boolean = hasModifier(KtTokens.ANNOTATION_KEYWORD) - - fun getDeclarationKeyword(): PsiElement? = - findChildByType( - TokenSet.create( - KtTokens.CLASS_KEYWORD, KtTokens.INTERFACE_KEYWORD, KtTokens.OBJECT_KEYWORD - ) - ) - - override fun delete() { - CheckUtil.checkWritable(this) - - val file = containingKtFile - if (!isTopLevel() || file.declarations.size > 1) { - super.delete() - } else { - file.delete() - } - } -} - -fun KtClassOrObject.getOrCreateBody(): KtClassBody { - getBody()?.let { return it } - - val newBody = KtPsiFactory(this).createEmptyClassBody() - if (this is KtEnumEntry) return addAfter(newBody, initializerList ?: nameIdentifier) as KtClassBody - return add(newBody) as KtClassBody -}