J2K: KtClassBody

This commit is contained in:
Alexey Sedunov
2015-10-20 20:20:53 +03:00
committed by Alexey Sedunov
parent d8864544d2
commit 98b3919824
4 changed files with 36 additions and 86 deletions
@@ -39,7 +39,7 @@ public open class KtClass : KtClassOrObject {
public fun getColon(): PsiElement? = findChildByType(KtTokens.COLON) public fun getColon(): PsiElement? = findChildByType(KtTokens.COLON)
public fun getProperties(): List<KtProperty> = getBody()?.getProperties().orEmpty() public fun getProperties(): List<KtProperty> = getBody()?.properties.orEmpty()
public fun isInterface(): Boolean = public fun isInterface(): Boolean =
_stub?.isInterface() ?: (findChildByType<PsiElement>(KtTokens.INTERFACE_KEYWORD) != null) _stub?.isInterface() ?: (findChildByType<PsiElement>(KtTokens.INTERFACE_KEYWORD) != null)
@@ -83,7 +83,7 @@ public open class KtClass : KtClassOrObject {
return StringUtil.join(parts, ".") return StringUtil.join(parts, ".")
} }
public fun getCompanionObjects(): List<KtObjectDeclaration> = getBody()?.getAllCompanionObjects().orEmpty() public fun getCompanionObjects(): List<KtObjectDeclaration> = getBody()?.allCompanionObjects.orEmpty()
public fun getClassOrInterfaceKeyword(): PsiElement? = findChildByType(TokenSet.create(KtTokens.CLASS_KEYWORD, KtTokens.INTERFACE_KEYWORD)) public fun getClassOrInterfaceKeyword(): PsiElement? = findChildByType(TokenSet.create(KtTokens.CLASS_KEYWORD, KtTokens.INTERFACE_KEYWORD))
} }
@@ -14,97 +14,47 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.psi; package org.jetbrains.kotlin.psi
import com.google.common.collect.Lists; import com.intellij.lang.ASTNode
import com.intellij.lang.ASTNode; import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElement; import com.intellij.psi.tree.TokenSet
import com.intellij.psi.tree.TokenSet; import org.jetbrains.kotlin.KtNodeTypes
import kotlin.CollectionsKt; import org.jetbrains.kotlin.lexer.KtTokens
import kotlin.jvm.functions.Function1; import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes.*
import org.jetbrains.kotlin.KtNodeTypes; import java.util.*
import org.jetbrains.kotlin.lexer.KtTokens;
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
import java.util.Arrays; class KtClassBody : KtElementImplStub<KotlinPlaceHolderStub<KtClassBody>>, KtDeclarationContainer {
import java.util.List; constructor(node: ASTNode) : super(node)
constructor(stub: KotlinPlaceHolderStub<KtClassBody>) : super(stub, CLASS_BODY)
import static org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes.*; override fun getDeclarations() = Arrays.asList(*getStubOrPsiChildren(DECLARATION_TYPES, KtDeclaration.ARRAY_FACTORY))
public class KtClassBody extends KtElementImplStub<KotlinPlaceHolderStub<KtClassBody>> implements KtDeclarationContainer { override fun <R, D> accept(visitor: KtVisitor<R, D>, data: D) = visitor.visitClassBody(this, data)
public KtClassBody(@NotNull ASTNode node) { val anonymousInitializers: List<KtClassInitializer>
super(node); get() = findChildrenByType(KtNodeTypes.ANONYMOUS_INITIALIZER)
}
public KtClassBody(@NotNull KotlinPlaceHolderStub<KtClassBody> stub) { internal val secondaryConstructors: List<KtSecondaryConstructor>
super(stub, CLASS_BODY); get() = getStubOrPsiChildrenAsList(KtStubElementTypes.SECONDARY_CONSTRUCTOR)
}
@Override val properties: List<KtProperty>
@NotNull get() = getStubOrPsiChildrenAsList(KtStubElementTypes.PROPERTY)
public List<KtDeclaration> getDeclarations() {
return Arrays.asList(getStubOrPsiChildren(DECLARATION_TYPES, KtDeclaration.ARRAY_FACTORY));
}
@Override val allCompanionObjects: List<KtObjectDeclaration>
public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) { get() = getStubOrPsiChildrenAsList(KtStubElementTypes.OBJECT_DECLARATION).filter { it.isCompanion() }
return visitor.visitClassBody(this, data);
}
@NotNull val rBrace: PsiElement?
public List<KtClassInitializer> getAnonymousInitializers() { get() = node.getChildren(TokenSet.create(KtTokens.RBRACE)).singleOrNull()?.psi
return findChildrenByType(KtNodeTypes.ANONYMOUS_INITIALIZER);
}
@NotNull val lBrace: PsiElement?
/* package-protected */ List<KtSecondaryConstructor> getSecondaryConstructors() { get() = node.getChildren(TokenSet.create(KtTokens.LBRACE)).singleOrNull()?.psi
return getStubOrPsiChildrenAsList(KtStubElementTypes.SECONDARY_CONSTRUCTOR);
}
@NotNull
public List<KtProperty> getProperties() {
return getStubOrPsiChildrenAsList(KtStubElementTypes.PROPERTY);
}
@NotNull
public List<KtObjectDeclaration> getAllCompanionObjects() {
List<KtObjectDeclaration> result = Lists.newArrayList();
for (KtObjectDeclaration declaration : getStubOrPsiChildrenAsList(KtStubElementTypes.OBJECT_DECLARATION)) {
if (declaration.isCompanion()) {
result.add(declaration);
}
}
return result;
}
@Nullable
public PsiElement getRBrace() {
ASTNode[] children = getNode().getChildren(TokenSet.create(KtTokens.RBRACE));
return children.length == 1 ? children[0].getPsi() : null;
}
@Nullable
public PsiElement getLBrace() {
ASTNode[] children = getNode().getChildren(TokenSet.create(KtTokens.LBRACE));
return children.length == 1 ? children[0].getPsi() : null;
}
/** /**
* @return annotations that do not belong to any declaration due to incomplete code or syntax errors * @return annotations that do not belong to any declaration due to incomplete code or syntax errors
*/ */
@NotNull val danglingAnnotations: List<KtAnnotationEntry>
public List<KtAnnotationEntry> getDanglingAnnotations() { get() = getStubOrPsiChildrenAsList(MODIFIER_LIST).flatMap { it.annotationEntries }
return CollectionsKt.flatMap(
getStubOrPsiChildrenAsList(MODIFIER_LIST),
new Function1<KtModifierList, Iterable<KtAnnotationEntry>>() {
@Override
public Iterable<KtAnnotationEntry> invoke(KtModifierList modifierList) {
return modifierList.getAnnotationEntries();
}
});
}
} }
@@ -61,7 +61,7 @@ abstract public class KtClassOrObject :
} }
} }
public fun getAnonymousInitializers(): List<KtClassInitializer> = getBody()?.getAnonymousInitializers().orEmpty() public fun getAnonymousInitializers(): List<KtClassInitializer> = getBody()?.anonymousInitializers.orEmpty()
public fun getNameAsDeclaration(): KtObjectDeclarationName? = public fun getNameAsDeclaration(): KtObjectDeclarationName? =
findChildByType<PsiElement>(KtNodeTypes.OBJECT_DECLARATION_NAME) as KtObjectDeclarationName? findChildByType<PsiElement>(KtNodeTypes.OBJECT_DECLARATION_NAME) as KtObjectDeclarationName?
@@ -72,7 +72,7 @@ abstract public class KtClassOrObject :
public fun addDeclaration(declaration: KtDeclaration): KtDeclaration { public fun addDeclaration(declaration: KtDeclaration): KtDeclaration {
val body = getOrCreateBody() val body = getOrCreateBody()
val anchor = PsiTreeUtil.skipSiblingsBackward(body.getRBrace() ?: body.getLastChild()!!, javaClass<PsiWhiteSpace>()) val anchor = PsiTreeUtil.skipSiblingsBackward(body.rBrace ?: body.getLastChild()!!, javaClass<PsiWhiteSpace>())
return body.addAfter(declaration, anchor) as KtDeclaration return body.addAfter(declaration, anchor) as KtDeclaration
} }
@@ -105,7 +105,7 @@ abstract public class KtClassOrObject :
public fun hasPrimaryConstructor(): Boolean = hasExplicitPrimaryConstructor() || !hasSecondaryConstructors() public fun hasPrimaryConstructor(): Boolean = hasExplicitPrimaryConstructor() || !hasSecondaryConstructors()
private fun hasSecondaryConstructors(): Boolean = !getSecondaryConstructors().isEmpty() private fun hasSecondaryConstructors(): Boolean = !getSecondaryConstructors().isEmpty()
public fun getSecondaryConstructors(): List<KtSecondaryConstructor> = getBody()?.getSecondaryConstructors().orEmpty() public fun getSecondaryConstructors(): List<KtSecondaryConstructor> = getBody()?.secondaryConstructors.orEmpty()
public fun isAnnotation(): Boolean = hasModifier(KtTokens.ANNOTATION_KEYWORD) public fun isAnnotation(): Boolean = hasModifier(KtTokens.ANNOTATION_KEYWORD)
@@ -608,12 +608,12 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
val classBody = classOrObject.getOrCreateBody() val classBody = classOrObject.getOrCreateBody()
return if (declaration is KtNamedFunction) { return if (declaration is KtNamedFunction) {
val anchor = PsiTreeUtil.skipSiblingsBackward( val anchor = PsiTreeUtil.skipSiblingsBackward(
classBody.getRBrace() ?: classBody.getLastChild()!!, classBody.rBrace ?: classBody.getLastChild()!!,
javaClass<PsiWhiteSpace>() javaClass<PsiWhiteSpace>()
) )
classBody.addAfter(declaration, anchor) as KtNamedDeclaration classBody.addAfter(declaration, anchor) as KtNamedDeclaration
} }
else classBody.addAfter(declaration, classBody.getLBrace()!!) as KtNamedDeclaration else classBody.addAfter(declaration, classBody.lBrace!!) as KtNamedDeclaration
} }
private fun getTypeParameterRenames(scope: KtScope): Map<TypeParameterDescriptor, String> { private fun getTypeParameterRenames(scope: KtScope): Map<TypeParameterDescriptor, String> {