J2K KtAnonymousInitializer in one go

Rip history
This commit is contained in:
Pavel V. Talanov
2015-11-19 14:43:59 +03:00
parent 6bb89021dd
commit bc2a65b2ea
3 changed files with 53 additions and 76 deletions
@@ -1,74 +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.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.lexer.KtTokens;
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
public class KtAnonymousInitializer extends KtDeclarationStub<KotlinPlaceHolderStub<KtAnonymousInitializer>> implements KtStatementExpression {
public KtAnonymousInitializer(@NotNull ASTNode node) {
super(node);
}
public KtAnonymousInitializer(@NotNull KotlinPlaceHolderStub<KtAnonymousInitializer> stub) {
super(stub, KtStubElementTypes.ANONYMOUS_INITIALIZER);
}
@Override
public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
return visitor.visitAnonymousInitializer(this, data);
}
@Nullable
public KtExpression getBody() {
return findChildByClass(KtExpression.class);
}
@Nullable
public PsiElement getOpenBraceNode() {
KtExpression body = getBody();
return (body instanceof KtBlockExpression) ? ((KtBlockExpression) body).getLBrace() : null;
}
@NotNull
public PsiElement getOpenBraceNodeOrSelf() {
PsiElement result = getOpenBraceNode();
return result != null ? result : this;
}
@Nullable
public PsiElement getInitKeyword() {
return findChildByType(KtTokens.INIT_KEYWORD);
}
@NotNull
public KtDeclaration getContainingDeclaration() {
KtClassOrObject classOrObject = PsiTreeUtil.getParentOfType(this, KtClassOrObject.class, true);
if (classOrObject != null) {
return classOrObject;
}
KtScript type = PsiTreeUtil.getParentOfType(this, KtScript.class, true);
assert type != null : "Should only be present in class, object or script";
return type;
}
}
@@ -0,0 +1,51 @@
/*
* 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.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
import org.jetbrains.kotlin.utils.sure
class KtAnonymousInitializer : KtDeclarationStub<KotlinPlaceHolderStub<KtAnonymousInitializer>>, KtStatementExpression {
constructor(node: ASTNode) : super(node)
constructor(stub: KotlinPlaceHolderStub<KtAnonymousInitializer>) : super(stub, KtStubElementTypes.ANONYMOUS_INITIALIZER)
override fun <R, D> accept(visitor: KtVisitor<R, D>, data: D) = visitor.visitAnonymousInitializer(this, data)
val body: KtExpression?
get() = findChildByClass(KtExpression::class.java)
val openBraceNode: PsiElement?
get() {
val body = body
return if ((body is KtBlockExpression)) body.lBrace else null
}
val containingDeclaration: KtDeclaration
get() {
val classOrObject = PsiTreeUtil.getParentOfType(this, KtClassOrObject::class.java, true)
if (classOrObject != null) {
return classOrObject
}
val type = PsiTreeUtil.getParentOfType(this, KtScript::class.java, true)
return type.sure { "Should only be present in class, object or script" }
}
}
@@ -179,7 +179,7 @@ public fun PsiElement.getExtractionContainers(strict: Boolean = true, includeAll
else -> {
val targetContainer = when (enclosingDeclaration) {
is KtDeclarationWithBody -> enclosingDeclaration.getBodyExpression()
is KtAnonymousInitializer -> enclosingDeclaration.getBody()
is KtAnonymousInitializer -> enclosingDeclaration.body
else -> null
}
if (targetContainer is KtBlockExpression) Collections.singletonList(targetContainer) else Collections.emptyList()
@@ -328,7 +328,7 @@ public fun KtElement.getContextForContainingDeclarationBody(): BindingContext? {
is KtWithExpressionInitializer -> enclosingDeclaration.getInitializer()
is KtMultiDeclaration -> enclosingDeclaration.getInitializer()
is KtParameter -> enclosingDeclaration.getDefaultValue()
is KtAnonymousInitializer -> enclosingDeclaration.getBody()
is KtAnonymousInitializer -> enclosingDeclaration.body
is KtClass -> {
val delegationSpecifierList = enclosingDeclaration.getDelegationSpecifierList()
if (delegationSpecifierList.isAncestor(this)) this else null