J2K: KtClassBody
This commit is contained in:
committed by
Alexey Sedunov
parent
d8864544d2
commit
98b3919824
@@ -39,7 +39,7 @@ public open class KtClass : KtClassOrObject {
|
||||
|
||||
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 =
|
||||
_stub?.isInterface() ?: (findChildByType<PsiElement>(KtTokens.INTERFACE_KEYWORD) != null)
|
||||
@@ -83,7 +83,7 @@ public open class KtClass : KtClassOrObject {
|
||||
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))
|
||||
}
|
||||
|
||||
@@ -14,97 +14,47 @@
|
||||
* 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.psi.PsiElement;
|
||||
import com.intellij.psi.tree.TokenSet;
|
||||
import kotlin.CollectionsKt;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.KtNodeTypes;
|
||||
import org.jetbrains.kotlin.lexer.KtTokens;
|
||||
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
|
||||
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
|
||||
import com.intellij.lang.ASTNode
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.tree.TokenSet
|
||||
import org.jetbrains.kotlin.KtNodeTypes
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub
|
||||
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
|
||||
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes.*
|
||||
import java.util.*
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
class KtClassBody : KtElementImplStub<KotlinPlaceHolderStub<KtClassBody>>, KtDeclarationContainer {
|
||||
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) {
|
||||
super(node);
|
||||
}
|
||||
val anonymousInitializers: List<KtClassInitializer>
|
||||
get() = findChildrenByType(KtNodeTypes.ANONYMOUS_INITIALIZER)
|
||||
|
||||
public KtClassBody(@NotNull KotlinPlaceHolderStub<KtClassBody> stub) {
|
||||
super(stub, CLASS_BODY);
|
||||
}
|
||||
internal val secondaryConstructors: List<KtSecondaryConstructor>
|
||||
get() = getStubOrPsiChildrenAsList(KtStubElementTypes.SECONDARY_CONSTRUCTOR)
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<KtDeclaration> getDeclarations() {
|
||||
return Arrays.asList(getStubOrPsiChildren(DECLARATION_TYPES, KtDeclaration.ARRAY_FACTORY));
|
||||
}
|
||||
val properties: List<KtProperty>
|
||||
get() = getStubOrPsiChildrenAsList(KtStubElementTypes.PROPERTY)
|
||||
|
||||
@Override
|
||||
public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
|
||||
return visitor.visitClassBody(this, data);
|
||||
}
|
||||
val allCompanionObjects: List<KtObjectDeclaration>
|
||||
get() = getStubOrPsiChildrenAsList(KtStubElementTypes.OBJECT_DECLARATION).filter { it.isCompanion() }
|
||||
|
||||
@NotNull
|
||||
public List<KtClassInitializer> getAnonymousInitializers() {
|
||||
return findChildrenByType(KtNodeTypes.ANONYMOUS_INITIALIZER);
|
||||
}
|
||||
val rBrace: PsiElement?
|
||||
get() = node.getChildren(TokenSet.create(KtTokens.RBRACE)).singleOrNull()?.psi
|
||||
|
||||
@NotNull
|
||||
/* package-protected */ List<KtSecondaryConstructor> getSecondaryConstructors() {
|
||||
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;
|
||||
}
|
||||
val lBrace: PsiElement?
|
||||
get() = node.getChildren(TokenSet.create(KtTokens.LBRACE)).singleOrNull()?.psi
|
||||
|
||||
/**
|
||||
* @return annotations that do not belong to any declaration due to incomplete code or syntax errors
|
||||
*/
|
||||
@NotNull
|
||||
public List<KtAnnotationEntry> getDanglingAnnotations() {
|
||||
return CollectionsKt.flatMap(
|
||||
getStubOrPsiChildrenAsList(MODIFIER_LIST),
|
||||
new Function1<KtModifierList, Iterable<KtAnnotationEntry>>() {
|
||||
@Override
|
||||
public Iterable<KtAnnotationEntry> invoke(KtModifierList modifierList) {
|
||||
return modifierList.getAnnotationEntries();
|
||||
}
|
||||
});
|
||||
}
|
||||
val danglingAnnotations: List<KtAnnotationEntry>
|
||||
get() = getStubOrPsiChildrenAsList(MODIFIER_LIST).flatMap { it.annotationEntries }
|
||||
}
|
||||
|
||||
@@ -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? =
|
||||
findChildByType<PsiElement>(KtNodeTypes.OBJECT_DECLARATION_NAME) as KtObjectDeclarationName?
|
||||
@@ -72,7 +72,7 @@ abstract public class KtClassOrObject :
|
||||
|
||||
public fun addDeclaration(declaration: KtDeclaration): KtDeclaration {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ abstract public class KtClassOrObject :
|
||||
public fun hasPrimaryConstructor(): Boolean = hasExplicitPrimaryConstructor() || !hasSecondaryConstructors()
|
||||
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)
|
||||
|
||||
|
||||
+2
-2
@@ -608,12 +608,12 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
|
||||
val classBody = classOrObject.getOrCreateBody()
|
||||
return if (declaration is KtNamedFunction) {
|
||||
val anchor = PsiTreeUtil.skipSiblingsBackward(
|
||||
classBody.getRBrace() ?: classBody.getLastChild()!!,
|
||||
classBody.rBrace ?: classBody.getLastChild()!!,
|
||||
javaClass<PsiWhiteSpace>()
|
||||
)
|
||||
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> {
|
||||
|
||||
Reference in New Issue
Block a user