From 3eddfeb8c977e9bb67bea169f0f8239cd0cdcef1 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Wed, 1 Mar 2023 12:30:46 +0100 Subject: [PATCH] [psi] make collection literal stub based ^ KT-57035 --- .../org/jetbrains/kotlin/ElementTypeUtils.kt | 2 + .../src/org/jetbrains/kotlin/KtNodeTypes.java | 2 +- .../psi/KtCollectionLiteralExpression.java | 72 +++++++++++++++++++ .../psi/KtCollectionLiteralExpression.kt | 42 ----------- .../jetbrains/kotlin/psi/KtValueArgument.java | 6 +- .../kotlin/psi/stubs/StubInterfaces.kt | 1 + ...tCollectionLiteralExpressionElementType.kt | 34 +++++++++ .../stubs/elements/KtStubElementTypes.java | 1 + ...tlinCollectionLiteralExpressionStubImpl.kt | 17 +++++ 9 files changed, 132 insertions(+), 45 deletions(-) create mode 100644 compiler/psi/src/org/jetbrains/kotlin/psi/KtCollectionLiteralExpression.java delete mode 100644 compiler/psi/src/org/jetbrains/kotlin/psi/KtCollectionLiteralExpression.kt create mode 100644 compiler/psi/src/org/jetbrains/kotlin/psi/stubs/elements/KtCollectionLiteralExpressionElementType.kt create mode 100644 compiler/psi/src/org/jetbrains/kotlin/psi/stubs/impl/KotlinCollectionLiteralExpressionStubImpl.kt diff --git a/compiler/psi/src/org/jetbrains/kotlin/ElementTypeUtils.kt b/compiler/psi/src/org/jetbrains/kotlin/ElementTypeUtils.kt index 9962944a2d6..774a5817ea5 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/ElementTypeUtils.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/ElementTypeUtils.kt @@ -14,6 +14,7 @@ import org.jetbrains.kotlin.lexer.KtSingleValueToken import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.parsing.KotlinExpressionParsing import org.jetbrains.kotlin.psi.stubs.elements.KtClassLiteralExpressionElementType +import org.jetbrains.kotlin.psi.stubs.elements.KtCollectionLiteralExpressionElementType import org.jetbrains.kotlin.psi.stubs.elements.KtConstantExpressionElementType import org.jetbrains.kotlin.psi.stubs.elements.KtStringTemplateExpressionElementType @@ -62,6 +63,7 @@ object ElementTypeUtils { is KtConstantExpressionElementType, is KtStringTemplateExpressionElementType, is KtClassLiteralExpressionElementType, + is KtCollectionLiteralExpressionElementType, in expressionSet -> true else -> false } diff --git a/compiler/psi/src/org/jetbrains/kotlin/KtNodeTypes.java b/compiler/psi/src/org/jetbrains/kotlin/KtNodeTypes.java index bb87b10dded..e2262b6a97d 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/KtNodeTypes.java +++ b/compiler/psi/src/org/jetbrains/kotlin/KtNodeTypes.java @@ -158,7 +158,7 @@ public interface KtNodeTypes { IElementType WHEN_CONDITION_IS_PATTERN = new KtNodeType("WHEN_CONDITION_IS_PATTERN", KtWhenConditionIsPattern.class); IElementType WHEN_CONDITION_EXPRESSION = new KtNodeType("WHEN_CONDITION_WITH_EXPRESSION", KtWhenConditionWithExpression.class); - IElementType COLLECTION_LITERAL_EXPRESSION = new KtNodeType("COLLECTION_LITERAL_EXPRESSION", KtCollectionLiteralExpression.class); + IElementType COLLECTION_LITERAL_EXPRESSION = KtStubElementTypes.COLLECTION_LITERAL_EXPRESSION; IElementType PACKAGE_DIRECTIVE = KtStubElementTypes.PACKAGE_DIRECTIVE; diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtCollectionLiteralExpression.java b/compiler/psi/src/org/jetbrains/kotlin/psi/KtCollectionLiteralExpression.java new file mode 100644 index 00000000000..cb1737f7ad6 --- /dev/null +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtCollectionLiteralExpression.java @@ -0,0 +1,72 @@ +/* + * Copyright 2010-2017 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.KotlinCollectionLiteralExpressionStub; +import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes; + +import java.util.Arrays; +import java.util.List; + +import static org.jetbrains.kotlin.psi.psiUtil.KtPsiUtilKt.getTrailingCommaByClosingElement; + +public class KtCollectionLiteralExpression extends KtElementImplStub implements KtReferenceExpression { + public KtCollectionLiteralExpression(@NotNull KotlinCollectionLiteralExpressionStub stub) { + super(stub, KtStubElementTypes.COLLECTION_LITERAL_EXPRESSION); + } + + public KtCollectionLiteralExpression(@NotNull ASTNode node) { + super(node); + } + + @Override + public R accept(@NotNull KtVisitor visitor, D data) { + return visitor.visitCollectionLiteralExpression(this, data); + } + + @Nullable + public PsiElement getLeftBracket() { + ASTNode astNode = getNode().findChildByType(KtTokens.LBRACKET); + return astNode != null ? astNode.getPsi() : null; + } + + @Nullable + public PsiElement getRightBracket() { + ASTNode astNode = getNode().findChildByType(KtTokens.RBRACKET); + return astNode != null ? astNode.getPsi() : null; + } + + @Nullable + public PsiElement getTrailingComma() { + PsiElement rightBracket = getRightBracket(); + return getTrailingCommaByClosingElement(rightBracket); + } + + public List getInnerExpressions() { + KotlinCollectionLiteralExpressionStub stub = getStub(); + if (stub != null) { + return Arrays.asList(stub.getChildrenByType(KtValueArgument.CONSTANT_EXPRESSIONS_TYPES, KtExpression.EMPTY_ARRAY)); + } + return PsiTreeUtil.getChildrenOfTypeAsList(this, KtExpression.class); + } +} \ No newline at end of file diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtCollectionLiteralExpression.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/KtCollectionLiteralExpression.kt deleted file mode 100644 index 20588be5bc8..00000000000 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtCollectionLiteralExpression.kt +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2010-2017 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.lexer.KtTokens -import org.jetbrains.kotlin.psi.psiUtil.getTrailingCommaByClosingElement - -class KtCollectionLiteralExpression(node: ASTNode) : KtExpressionImpl(node), KtReferenceExpression { - override fun accept(visitor: KtVisitor, data: D): R { - return visitor.visitCollectionLiteralExpression(this, data) - } - - val leftBracket: PsiElement? - get() = findChildByType(KtTokens.LBRACKET) - - val rightBracket: PsiElement? - get() = findChildByType(KtTokens.RBRACKET) - - val trailingComma: PsiElement? - get() = getTrailingCommaByClosingElement(rightBracket) - - fun getInnerExpressions(): List { - return PsiTreeUtil.getChildrenOfTypeAsList(this, KtExpression::class.java) - } -} \ No newline at end of file diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtValueArgument.java b/compiler/psi/src/org/jetbrains/kotlin/psi/KtValueArgument.java index ecfb0f19b22..7a5b3ca1c73 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtValueArgument.java +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtValueArgument.java @@ -46,7 +46,7 @@ public class KtValueArgument extends KtElementImplStub { } interface KotlinClassLiteralExpressionStub : StubElement +interface KotlinCollectionLiteralExpressionStub : StubElement interface KotlinTypeProjectionStub : StubElement { fun getProjectionKind(): KtProjectionKind diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/elements/KtCollectionLiteralExpressionElementType.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/elements/KtCollectionLiteralExpressionElementType.kt new file mode 100644 index 00000000000..3a093d0d9f8 --- /dev/null +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/elements/KtCollectionLiteralExpressionElementType.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.psi.stubs.elements + +import com.intellij.psi.stubs.StubElement +import com.intellij.psi.stubs.StubInputStream +import com.intellij.psi.stubs.StubOutputStream +import org.jetbrains.annotations.NonNls +import org.jetbrains.kotlin.psi.KtClassLiteralExpression +import org.jetbrains.kotlin.psi.KtCollectionLiteralExpression +import org.jetbrains.kotlin.psi.stubs.KotlinClassLiteralExpressionStub +import org.jetbrains.kotlin.psi.stubs.KotlinCollectionLiteralExpressionStub +import org.jetbrains.kotlin.psi.stubs.impl.KotlinClassLiteralExpressionStubImpl +import org.jetbrains.kotlin.psi.stubs.impl.KotlinCollectionLiteralExpressionStubImpl + +class KtCollectionLiteralExpressionElementType(@NonNls debugName: String) : + KtStubElementType( + debugName, + KtCollectionLiteralExpression::class.java, + KotlinCollectionLiteralExpressionStub::class.java + ) { + override fun serialize(stub: KotlinCollectionLiteralExpressionStub, dataStream: StubOutputStream) {} + + override fun deserialize(dataStream: StubInputStream, parentStub: StubElement<*>?): KotlinCollectionLiteralExpressionStub { + return KotlinCollectionLiteralExpressionStubImpl(parentStub) + } + + override fun createStub(psi: KtCollectionLiteralExpression, parentStub: StubElement<*>?): KotlinCollectionLiteralExpressionStub { + return KotlinCollectionLiteralExpressionStubImpl(parentStub) + } +} \ No newline at end of file diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/elements/KtStubElementTypes.java b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/elements/KtStubElementTypes.java index bfdeb87504a..4497d686170 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/elements/KtStubElementTypes.java +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/elements/KtStubElementTypes.java @@ -160,6 +160,7 @@ public interface KtStubElementTypes { KtConstantExpressionElementType CHARACTER_CONSTANT = new KtConstantExpressionElementType("CHARACTER_CONSTANT"); KtConstantExpressionElementType INTEGER_CONSTANT = new KtConstantExpressionElementType("INTEGER_CONSTANT"); KtClassLiteralExpressionElementType CLASS_LITERAL_EXPRESSION = new KtClassLiteralExpressionElementType("CLASS_LITERAL_EXPRESSION"); + KtCollectionLiteralExpressionElementType COLLECTION_LITERAL_EXPRESSION = new KtCollectionLiteralExpressionElementType("COLLECTION_LITERAL_EXPRESSION"); KtPlaceHolderStubElementType STRING_TEMPLATE = new KtStringTemplateExpressionElementType("STRING_TEMPLATE"); diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/impl/KotlinCollectionLiteralExpressionStubImpl.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/impl/KotlinCollectionLiteralExpressionStubImpl.kt new file mode 100644 index 00000000000..0383bfafb97 --- /dev/null +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/impl/KotlinCollectionLiteralExpressionStubImpl.kt @@ -0,0 +1,17 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.psi.stubs.impl + +import com.intellij.psi.PsiElement +import com.intellij.psi.stubs.StubElement +import org.jetbrains.kotlin.psi.KtCollectionLiteralExpression +import org.jetbrains.kotlin.psi.stubs.KotlinCollectionLiteralExpressionStub +import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes + +class KotlinCollectionLiteralExpressionStubImpl( + parent: StubElement? +) : KotlinStubBaseImpl(parent, KtStubElementTypes.COLLECTION_LITERAL_EXPRESSION), + KotlinCollectionLiteralExpressionStub \ No newline at end of file