From efa59bfc7e7d34981f31cd81ecd863ad034039be Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Wed, 17 Oct 2018 14:53:54 +0300 Subject: [PATCH] Make stubs for string entries (KT-23738) --- .../src/org/jetbrains/kotlin/KtNodeTypes.java | 10 +++--- .../psi/KotlinStringLiteralTextEscaper.kt | 2 +- .../psi/KtBlockStringTemplateEntry.java | 6 ++++ .../psi/KtEscapeStringTemplateEntry.java | 6 ++++ .../psi/KtLiteralStringTemplateEntry.java | 6 ++++ .../psi/KtSimpleNameStringTemplateEntry.java | 6 ++++ .../kotlin/psi/KtStringTemplateEntry.java | 8 ++++- .../KtStringTemplateEntryWithExpression.java | 8 +++++ .../kotlin/psi/stubs/KotlinStubVersions.kt | 2 +- .../kotlin/psi/stubs/StubInterfaces.kt | 4 +++ .../KtPlaceHolderWithTextStubElementType.kt | 31 +++++++++++++++++++ .../stubs/elements/KtStubElementTypes.java | 12 +++++++ .../impl/KotlinPlaceHolderWithTextStubImpl.kt | 19 ++++++++++++ idea/testData/stubs/AnnotationValues.expected | 5 +++ idea/testData/stubs/Contracts.expected | 1 + 15 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 compiler/psi/src/org/jetbrains/kotlin/psi/stubs/elements/KtPlaceHolderWithTextStubElementType.kt create mode 100644 compiler/psi/src/org/jetbrains/kotlin/psi/stubs/impl/KotlinPlaceHolderWithTextStubImpl.kt diff --git a/compiler/psi/src/org/jetbrains/kotlin/KtNodeTypes.java b/compiler/psi/src/org/jetbrains/kotlin/KtNodeTypes.java index c89f242b20d..deaa43751e1 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/KtNodeTypes.java +++ b/compiler/psi/src/org/jetbrains/kotlin/KtNodeTypes.java @@ -92,11 +92,11 @@ public interface KtNodeTypes { IElementType CHARACTER_CONSTANT = KtStubElementTypes.CHARACTER_CONSTANT; IElementType INTEGER_CONSTANT = KtStubElementTypes.INTEGER_CONSTANT; - IElementType STRING_TEMPLATE = KtStubElementTypes.STRING_TEMPLATE; - KtNodeType LONG_STRING_TEMPLATE_ENTRY = new KtNodeType("LONG_STRING_TEMPLATE_ENTRY", KtBlockStringTemplateEntry.class); - KtNodeType SHORT_STRING_TEMPLATE_ENTRY = new KtNodeType("SHORT_STRING_TEMPLATE_ENTRY", KtSimpleNameStringTemplateEntry.class); - KtNodeType LITERAL_STRING_TEMPLATE_ENTRY = new KtNodeType("LITERAL_STRING_TEMPLATE_ENTRY", KtLiteralStringTemplateEntry.class); - KtNodeType ESCAPE_STRING_TEMPLATE_ENTRY = new KtNodeType("ESCAPE_STRING_TEMPLATE_ENTRY", KtEscapeStringTemplateEntry.class); + IElementType STRING_TEMPLATE = KtStubElementTypes.STRING_TEMPLATE; + IElementType LONG_STRING_TEMPLATE_ENTRY = KtStubElementTypes.LONG_STRING_TEMPLATE_ENTRY; + IElementType SHORT_STRING_TEMPLATE_ENTRY = KtStubElementTypes.SHORT_STRING_TEMPLATE_ENTRY; + IElementType LITERAL_STRING_TEMPLATE_ENTRY = KtStubElementTypes.LITERAL_STRING_TEMPLATE_ENTRY; + IElementType ESCAPE_STRING_TEMPLATE_ENTRY = KtStubElementTypes.ESCAPE_STRING_TEMPLATE_ENTRY; KtNodeType PARENTHESIZED = new KtNodeType("PARENTHESIZED", KtParenthesizedExpression.class); KtNodeType RETURN = new KtNodeType("RETURN", KtReturnExpression.class); diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KotlinStringLiteralTextEscaper.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/KotlinStringLiteralTextEscaper.kt index 18325a2e046..f3c182a5b2c 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KotlinStringLiteralTextEscaper.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KotlinStringLiteralTextEscaper.kt @@ -16,8 +16,8 @@ package org.jetbrains.kotlin.psi -import com.intellij.psi.LiteralTextEscaper import com.intellij.openapi.util.TextRange +import com.intellij.psi.LiteralTextEscaper import gnu.trove.TIntArrayList import org.jetbrains.kotlin.psi.psiUtil.getContentRange import org.jetbrains.kotlin.psi.psiUtil.isSingleQuoted diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtBlockStringTemplateEntry.java b/compiler/psi/src/org/jetbrains/kotlin/psi/KtBlockStringTemplateEntry.java index e97f62836a5..14a063fbec0 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtBlockStringTemplateEntry.java +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtBlockStringTemplateEntry.java @@ -18,12 +18,18 @@ package org.jetbrains.kotlin.psi; import com.intellij.lang.ASTNode; import org.jetbrains.annotations.NotNull; +import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderWithTextStub; +import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes; public class KtBlockStringTemplateEntry extends KtStringTemplateEntryWithExpression { public KtBlockStringTemplateEntry(@NotNull ASTNode node) { super(node); } + public KtBlockStringTemplateEntry(@NotNull KotlinPlaceHolderWithTextStub stub) { + super(stub, KtStubElementTypes.LONG_STRING_TEMPLATE_ENTRY); + } + @Override public R accept(@NotNull KtVisitor visitor, D data) { return visitor.visitBlockStringTemplateEntry(this, data); diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtEscapeStringTemplateEntry.java b/compiler/psi/src/org/jetbrains/kotlin/psi/KtEscapeStringTemplateEntry.java index 46c5f864ac8..36b86cec5a2 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtEscapeStringTemplateEntry.java +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtEscapeStringTemplateEntry.java @@ -19,12 +19,18 @@ package org.jetbrains.kotlin.psi; import com.intellij.lang.ASTNode; import com.intellij.openapi.util.text.StringUtil; import org.jetbrains.annotations.NotNull; +import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderWithTextStub; +import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes; public class KtEscapeStringTemplateEntry extends KtStringTemplateEntry { public KtEscapeStringTemplateEntry(@NotNull ASTNode node) { super(node); } + public KtEscapeStringTemplateEntry(@NotNull KotlinPlaceHolderWithTextStub stub) { + super(stub, KtStubElementTypes.ESCAPE_STRING_TEMPLATE_ENTRY); + } + @Override public R accept(@NotNull KtVisitor visitor, D data) { return visitor.visitEscapeStringTemplateEntry(this, data); diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtLiteralStringTemplateEntry.java b/compiler/psi/src/org/jetbrains/kotlin/psi/KtLiteralStringTemplateEntry.java index 7d25eeaccca..4c2e09f3f19 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtLiteralStringTemplateEntry.java +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtLiteralStringTemplateEntry.java @@ -18,12 +18,18 @@ package org.jetbrains.kotlin.psi; import com.intellij.lang.ASTNode; import org.jetbrains.annotations.NotNull; +import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderWithTextStub; +import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes; public class KtLiteralStringTemplateEntry extends KtStringTemplateEntry { public KtLiteralStringTemplateEntry(@NotNull ASTNode node) { super(node); } + public KtLiteralStringTemplateEntry(@NotNull KotlinPlaceHolderWithTextStub stub) { + super(stub, KtStubElementTypes.LITERAL_STRING_TEMPLATE_ENTRY); + } + @Override public R accept(@NotNull KtVisitor visitor, D data) { return visitor.visitLiteralStringTemplateEntry(this, data); diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtSimpleNameStringTemplateEntry.java b/compiler/psi/src/org/jetbrains/kotlin/psi/KtSimpleNameStringTemplateEntry.java index d7ab9f000c8..5a4710663c4 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtSimpleNameStringTemplateEntry.java +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtSimpleNameStringTemplateEntry.java @@ -18,12 +18,18 @@ package org.jetbrains.kotlin.psi; import com.intellij.lang.ASTNode; import org.jetbrains.annotations.NotNull; +import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderWithTextStub; +import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes; public class KtSimpleNameStringTemplateEntry extends KtStringTemplateEntryWithExpression { public KtSimpleNameStringTemplateEntry(@NotNull ASTNode node) { super(node); } + public KtSimpleNameStringTemplateEntry(@NotNull KotlinPlaceHolderWithTextStub stub) { + super(stub, KtStubElementTypes.SHORT_STRING_TEMPLATE_ENTRY); + } + @Override public R accept(@NotNull KtVisitor visitor, D data) { return visitor.visitSimpleNameStringTemplateEntry(this, data); diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtStringTemplateEntry.java b/compiler/psi/src/org/jetbrains/kotlin/psi/KtStringTemplateEntry.java index b9b59b21980..9a0ee00e659 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtStringTemplateEntry.java +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtStringTemplateEntry.java @@ -17,14 +17,20 @@ package org.jetbrains.kotlin.psi; import com.intellij.lang.ASTNode; +import com.intellij.psi.stubs.IStubElementType; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderWithTextStub; -public abstract class KtStringTemplateEntry extends KtElementImpl { +public abstract class KtStringTemplateEntry extends KtElementImplStub> { public KtStringTemplateEntry(@NotNull ASTNode node) { super(node); } + public KtStringTemplateEntry(@NotNull KotlinPlaceHolderWithTextStub stub, @NotNull IStubElementType elementType) { + super(stub, elementType); + } + @Nullable public KtExpression getExpression() { return findChildByClass(KtExpression.class); diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtStringTemplateEntryWithExpression.java b/compiler/psi/src/org/jetbrains/kotlin/psi/KtStringTemplateEntryWithExpression.java index 179a1be4810..5381220774c 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtStringTemplateEntryWithExpression.java +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtStringTemplateEntryWithExpression.java @@ -17,13 +17,21 @@ package org.jetbrains.kotlin.psi; import com.intellij.lang.ASTNode; +import com.intellij.psi.stubs.IStubElementType; import org.jetbrains.annotations.NotNull; +import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderWithTextStub; public abstract class KtStringTemplateEntryWithExpression extends KtStringTemplateEntry { public KtStringTemplateEntryWithExpression(@NotNull ASTNode node) { super(node); } + public KtStringTemplateEntryWithExpression( + @NotNull KotlinPlaceHolderWithTextStub stub, + @NotNull IStubElementType elementType) { + super(stub, elementType); + } + @Override public R accept(@NotNull KtVisitor visitor, D data) { return visitor.visitStringTemplateEntryWithExpression(this, data); diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/KotlinStubVersions.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/KotlinStubVersions.kt index dbdfac02c8f..0064033bcb3 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/KotlinStubVersions.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/KotlinStubVersions.kt @@ -23,7 +23,7 @@ object KotlinStubVersions { // Though only kotlin declarations (no code in the bodies) are stubbed, please do increase this version // if you are not 100% sure it can be avoided. // Increasing this version will lead to reindexing of all kotlin source files on the first IDE startup with the new version. - const val SOURCE_STUB_VERSION = 132 + const val SOURCE_STUB_VERSION = 133 // Binary stub version should be increased if stub format (org.jetbrains.kotlin.psi.stubs.impl) is changed // or changes are made to the core stub building code (org.jetbrains.kotlin.idea.decompiler.stubBuilder). diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/StubInterfaces.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/StubInterfaces.kt index 6858844dc60..3d5857c10d2 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/StubInterfaces.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/StubInterfaces.kt @@ -32,6 +32,10 @@ interface KotlinFileStub : PsiFileStub { interface KotlinPlaceHolderStub : StubElement +interface KotlinPlaceHolderWithTextStub : KotlinPlaceHolderStub { + fun text(): String +} + interface KotlinStubWithFqName : NamedStub { fun getFqName(): FqName? } diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/elements/KtPlaceHolderWithTextStubElementType.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/elements/KtPlaceHolderWithTextStubElementType.kt new file mode 100644 index 00000000000..b7c739819ed --- /dev/null +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/elements/KtPlaceHolderWithTextStubElementType.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. 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.KtElementImplStub +import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderWithTextStub +import org.jetbrains.kotlin.psi.stubs.impl.KotlinPlaceHolderWithTextStubImpl + +class KtPlaceHolderWithTextStubElementType>>(@NonNls debugName: String, psiClass: Class) : + KtStubElementType, T>(debugName, psiClass, KotlinPlaceHolderWithTextStub::class.java) { + + override fun createStub(psi: T, parentStub: StubElement<*>): KotlinPlaceHolderWithTextStub { + return KotlinPlaceHolderWithTextStubImpl(parentStub, this, psi.text) + } + + override fun serialize(stub: KotlinPlaceHolderWithTextStub, dataStream: StubOutputStream) { + dataStream.writeUTFFast(stub.text()) + } + + override fun deserialize(dataStream: StubInputStream, parentStub: StubElement<*>): KotlinPlaceHolderWithTextStub { + val text = dataStream.readUTFFast() + return KotlinPlaceHolderWithTextStubImpl(parentStub, this, text) + } +} 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 a34fd57874e..123fe4a1608 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 @@ -138,6 +138,18 @@ public interface KtStubElementTypes { KtPlaceHolderStubElementType STRING_TEMPLATE = new KtStringTemplateExpressionElementType("STRING_TEMPLATE"); + KtPlaceHolderWithTextStubElementType LONG_STRING_TEMPLATE_ENTRY = + new KtPlaceHolderWithTextStubElementType<>("LONG_STRING_TEMPLATE_ENTRY", KtBlockStringTemplateEntry.class); + + KtPlaceHolderWithTextStubElementType SHORT_STRING_TEMPLATE_ENTRY = + new KtPlaceHolderWithTextStubElementType<>("SHORT_STRING_TEMPLATE_ENTRY", KtSimpleNameStringTemplateEntry.class); + + KtPlaceHolderWithTextStubElementType LITERAL_STRING_TEMPLATE_ENTRY = + new KtPlaceHolderWithTextStubElementType<>("LITERAL_STRING_TEMPLATE_ENTRY", KtLiteralStringTemplateEntry.class); + + KtPlaceHolderWithTextStubElementType ESCAPE_STRING_TEMPLATE_ENTRY = + new KtPlaceHolderWithTextStubElementType<>("ESCAPE_STRING_TEMPLATE_ENTRY", KtEscapeStringTemplateEntry.class); + KtScriptElementType SCRIPT = new KtScriptElementType("SCRIPT"); TokenSet DECLARATION_TYPES = diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/impl/KotlinPlaceHolderWithTextStubImpl.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/impl/KotlinPlaceHolderWithTextStubImpl.kt new file mode 100644 index 00000000000..a72ea39940a --- /dev/null +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/impl/KotlinPlaceHolderWithTextStubImpl.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. 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.stubs.IStubElementType +import com.intellij.psi.stubs.StubElement +import org.jetbrains.kotlin.psi.KtElementImplStub +import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderWithTextStub + +class KotlinPlaceHolderWithTextStubImpl>>( + parent: StubElement<*>, + elementType: IStubElementType<*, *>, + private val text: String +) : KotlinStubBaseImpl(parent, elementType), KotlinPlaceHolderWithTextStub { + override fun text(): String = text +} diff --git a/idea/testData/stubs/AnnotationValues.expected b/idea/testData/stubs/AnnotationValues.expected index cd6176bfdcb..07a75d448d0 100644 --- a/idea/testData/stubs/AnnotationValues.expected +++ b/idea/testData/stubs/AnnotationValues.expected @@ -110,10 +110,14 @@ PsiJetFileStubImpl[package=test] VALUE_ARGUMENT_LIST VALUE_ARGUMENT STRING_TEMPLATE + LITERAL_STRING_TEMPLATE_ENTRY[text=some] VALUE_ARGUMENT STRING_TEMPLATE VALUE_ARGUMENT STRING_TEMPLATE + LITERAL_STRING_TEMPLATE_ENTRY[text=H] + SHORT_STRING_TEMPLATE_ENTRY[text=$CONSTANT] + REFERENCE_EXPRESSION[referencedName=CONSTANT] CLASS[fqName=test.E, isEnumEntry=false, isInterface=false, isLocal=false, isTopLevel=true, name=E, superNames=[]] MODIFIER_LIST[enum] CLASS_BODY @@ -283,6 +287,7 @@ PsiJetFileStubImpl[package=test] VALUE_ARGUMENT_LIST VALUE_ARGUMENT STRING_TEMPLATE + LITERAL_STRING_TEMPLATE_ENTRY[text=value] VALUE_ARGUMENT VALUE_ARGUMENT_NAME REFERENCE_EXPRESSION[referencedName=nested] diff --git a/idea/testData/stubs/Contracts.expected b/idea/testData/stubs/Contracts.expected index 748762f93b4..985864a0ac6 100644 --- a/idea/testData/stubs/Contracts.expected +++ b/idea/testData/stubs/Contracts.expected @@ -9,6 +9,7 @@ PsiJetFileStubImpl[package=test] VALUE_ARGUMENT_LIST VALUE_ARGUMENT STRING_TEMPLATE + LITERAL_STRING_TEMPLATE_ENTRY[text=INVISIBLE_MEMBER] PACKAGE_DIRECTIVE REFERENCE_EXPRESSION[referencedName=test] IMPORT_LIST