From bc2a65b2ea3303ee3075cf7a40bb76e062b8a5de Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Thu, 19 Nov 2015 14:43:59 +0300 Subject: [PATCH] J2K KtAnonymousInitializer in one go Rip history --- .../kotlin/psi/KtAnonymousInitializer.java | 74 ------------------- .../kotlin/psi/KtAnonymousInitializer.kt | 51 +++++++++++++ .../idea/refactoring/jetRefactoringUtil.kt | 4 +- 3 files changed, 53 insertions(+), 76 deletions(-) delete mode 100644 compiler/frontend/src/org/jetbrains/kotlin/psi/KtAnonymousInitializer.java create mode 100644 compiler/frontend/src/org/jetbrains/kotlin/psi/KtAnonymousInitializer.kt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtAnonymousInitializer.java b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtAnonymousInitializer.java deleted file mode 100644 index 9a4d45c8ded..00000000000 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtAnonymousInitializer.java +++ /dev/null @@ -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> implements KtStatementExpression { - public KtAnonymousInitializer(@NotNull ASTNode node) { - super(node); - } - - public KtAnonymousInitializer(@NotNull KotlinPlaceHolderStub stub) { - super(stub, KtStubElementTypes.ANONYMOUS_INITIALIZER); - } - - @Override - public R accept(@NotNull KtVisitor 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; - } -} diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtAnonymousInitializer.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtAnonymousInitializer.kt new file mode 100644 index 00000000000..84b5bc84f85 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtAnonymousInitializer.kt @@ -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>, KtStatementExpression { + constructor(node: ASTNode) : super(node) + + constructor(stub: KotlinPlaceHolderStub) : super(stub, KtStubElementTypes.ANONYMOUS_INITIALIZER) + + override fun accept(visitor: KtVisitor, 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" } + } +} diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/jetRefactoringUtil.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/jetRefactoringUtil.kt index af542aa4c3b..c36b039cf05 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/jetRefactoringUtil.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/jetRefactoringUtil.kt @@ -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