From 0674d2495cf1dfcf9873f8c1e4c25d08ac87e2fc Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Thu, 27 Mar 2014 19:21:53 +0400 Subject: [PATCH] Stubs for JetNameReferenceExpression --- .../src/org/jetbrains/jet/JetNodeTypes.java | 3 +- .../lang/psi/JetLabelReferenceExpression.kt | 5 +- .../lang/psi/JetNameReferenceExpression.java | 82 +++++++++++++++++++ .../psi/JetOperationReferenceExpression.kt | 5 +- .../jet/lang/psi/JetSimpleNameExpression.kt | 45 +++++----- .../PsiJetNameReferenceExpressionStub.java} | 12 ++- ...JetNameReferenceExpressionElementType.java | 52 ++++++++++++ .../stubs/elements/JetStubElementTypes.java | 2 + ...PsiJetNameReferenceExpressionStubImpl.java | 41 ++++++++++ 9 files changed, 221 insertions(+), 26 deletions(-) create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/psi/JetNameReferenceExpression.java rename compiler/frontend/src/org/jetbrains/jet/lang/psi/{JetNameReferenceExpression.kt => stubs/PsiJetNameReferenceExpressionStub.java} (61%) create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetNameReferenceExpressionElementType.java create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/impl/PsiJetNameReferenceExpressionStubImpl.java diff --git a/compiler/frontend/src/org/jetbrains/jet/JetNodeTypes.java b/compiler/frontend/src/org/jetbrains/jet/JetNodeTypes.java index f2411fe2334..a76666e8054 100644 --- a/compiler/frontend/src/org/jetbrains/jet/JetNodeTypes.java +++ b/compiler/frontend/src/org/jetbrains/jet/JetNodeTypes.java @@ -19,6 +19,7 @@ package org.jetbrains.jet; import com.intellij.psi.tree.IElementType; import com.intellij.psi.tree.IFileElementType; import org.jetbrains.jet.lang.psi.*; +import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementType; import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes; import org.jetbrains.jet.plugin.JetLanguage; @@ -114,7 +115,7 @@ public interface JetNodeTypes { JetNodeType FUNCTION_LITERAL = new JetNodeType("FUNCTION_LITERAL", JetFunctionLiteral.class); JetNodeType ANNOTATED_EXPRESSION = new JetNodeType("ANNOTATED_EXPRESSION", JetAnnotatedExpression.class); - JetNodeType REFERENCE_EXPRESSION = new JetNodeType("REFERENCE_EXPRESSION", JetNameReferenceExpression.class); + IElementType REFERENCE_EXPRESSION = JetStubElementTypes.REFERENCE_EXPRESSION; JetNodeType OPERATION_REFERENCE = new JetNodeType("OPERATION_REFERENCE", JetOperationReferenceExpression.class); JetNodeType LABEL = new JetNodeType("LABEL", JetLabelReferenceExpression.class); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetLabelReferenceExpression.kt b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetLabelReferenceExpression.kt index bc085c4e2d0..805463bc4c9 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetLabelReferenceExpression.kt +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetLabelReferenceExpression.kt @@ -17,5 +17,8 @@ package org.jetbrains.jet.lang.psi import com.intellij.lang.ASTNode +import org.jetbrains.jet.lexer.JetTokens -public class JetLabelReferenceExpression(node: ASTNode) : JetSimpleNameExpressionImpl(node) +public class JetLabelReferenceExpression(node: ASTNode) : JetSimpleNameExpressionImpl(node) { + public override fun getReferencedNameElement() = findChildByType(JetTokens.LABEL_IDENTIFIER) ?: this +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetNameReferenceExpression.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetNameReferenceExpression.java new file mode 100644 index 00000000000..68ffbcd04eb --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetNameReferenceExpression.java @@ -0,0 +1,82 @@ +/* + * Copyright 2010-2014 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.jet.lang.psi; + +import com.intellij.lang.ASTNode; +import com.intellij.psi.PsiElement; +import com.intellij.psi.tree.IElementType; +import com.intellij.psi.tree.TokenSet; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.psi.stubs.PsiJetNameReferenceExpressionStub; +import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes; +import org.jetbrains.jet.lang.resolve.name.Name; +import org.jetbrains.jet.lexer.JetTokens; + +import static org.jetbrains.jet.lexer.JetTokens.*; + +public class JetNameReferenceExpression extends JetElementImplStub implements JetSimpleNameExpression { + + private static final TokenSet NAME_REFERENCE_EXPRESSIONS = TokenSet.create(IDENTIFIER, FIELD_IDENTIFIER, THIS_KEYWORD, SUPER_KEYWORD); + + public JetNameReferenceExpression(@NotNull ASTNode node) { + super(node); + } + + public JetNameReferenceExpression(@NotNull PsiJetNameReferenceExpressionStub stub) { + super(stub, JetStubElementTypes.REFERENCE_EXPRESSION); + } + + @Override + @NotNull + public String getReferencedName() { + return JetSimpleNameExpressionImpl.object$.getReferencedNameImpl(this); + } + + @Override + @NotNull + public Name getReferencedNameAsName() { + return JetSimpleNameExpressionImpl.object$.getReferencedNameAsNameImpl(this); + } + + @Override + @NotNull + public PsiElement getReferencedNameElement() { + PsiElement element = findChildByType(NAME_REFERENCE_EXPRESSIONS); + if (element == null) { + return this; + } + return element; + } + + @Override + @Nullable + public PsiElement getIdentifier() { + return findChildByType(JetTokens.IDENTIFIER); + } + + @NotNull + @Override + public IElementType getReferencedNameElementType() { + return JetSimpleNameExpressionImpl.object$.getReferencedNameElementTypeImpl(this); + } + + @Override + public R accept(@NotNull JetVisitor visitor, D data) { + return visitor.visitSimpleNameExpression(this, data); + } +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetOperationReferenceExpression.kt b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetOperationReferenceExpression.kt index 89e347dc9d7..f3cd6b2a041 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetOperationReferenceExpression.kt +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetOperationReferenceExpression.kt @@ -17,5 +17,8 @@ package org.jetbrains.jet.lang.psi import com.intellij.lang.ASTNode +import org.jetbrains.jet.lang.parsing.JetExpressionParsing -public class JetOperationReferenceExpression(node: ASTNode) : JetSimpleNameExpressionImpl(node) +public class JetOperationReferenceExpression(node: ASTNode) : JetSimpleNameExpressionImpl(node) { + override fun getReferencedNameElement() = findChildByType(JetExpressionParsing.ALL_OPERATIONS) ?: this +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetSimpleNameExpression.kt b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetSimpleNameExpression.kt index 613c65d19ec..bfd379c1ce1 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetSimpleNameExpression.kt +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetSimpleNameExpression.kt @@ -28,41 +28,46 @@ import org.jetbrains.jet.lexer.JetTokens.* public trait JetSimpleNameExpression : JetReferenceExpression { - public fun getReferencedName(): String { - val text = getReferencedNameElement().getNode()!!.getText() - return JetPsiUtil.unquoteIdentifierOrFieldReference(text) - } + public fun getReferencedName(): String - public fun getReferencedNameAsName(): Name { - val name = getReferencedName() - return Name.identifierNoValidate(name) - } + public fun getReferencedNameAsName(): Name public fun getReferencedNameElement(): PsiElement public fun getIdentifier(): PsiElement? - public fun getReferencedNameElementType(): IElementType { - return getReferencedNameElement().getNode()!!.getElementType()!! - } + public fun getReferencedNameElementType(): IElementType } abstract class JetSimpleNameExpressionImpl(node: ASTNode) : JetExpressionImpl(node), JetSimpleNameExpression { - public override fun getIdentifier(): PsiElement? { - return findChildByType(JetTokens.IDENTIFIER) - } + + override fun getIdentifier(): PsiElement? = findChildByType(JetTokens.IDENTIFIER) + + override fun getReferencedNameElementType() = getReferencedNameElementTypeImpl() override fun accept(visitor: JetVisitor, data: D?): R { return visitor.visitSimpleNameExpression(this, data) as R } - public override fun getReferencedNameElement(): PsiElement { - return findChildByType(REFERENCE_TOKENS) ?: - findChildByType(JetExpressionParsing.ALL_OPERATIONS) ?: - this - } + override fun getReferencedNameAsName() = getReferencedNameAsNameImpl() + override fun getReferencedName() = getReferencedNameImpl() + + //NOTE: an unfortunate way to share an implementation between stubbed and not stubbed tree class object { - public val REFERENCE_TOKENS: TokenSet = TokenSet.create(LABEL_IDENTIFIER, IDENTIFIER, FIELD_IDENTIFIER, THIS_KEYWORD, SUPER_KEYWORD) + + fun JetSimpleNameExpression.getReferencedNameElementTypeImpl(): IElementType { + return this.getReferencedNameElement().getNode()!!.getElementType()!! + } + + fun JetSimpleNameExpression.getReferencedNameAsNameImpl(): Name { + val name = this.getReferencedName() + return Name.identifierNoValidate(name) + } + + fun JetSimpleNameExpression.getReferencedNameImpl(): String { + val text = this.getReferencedNameElement().getNode()!!.getText() + return JetPsiUtil.unquoteIdentifierOrFieldReference(text) + } } } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetNameReferenceExpression.kt b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/PsiJetNameReferenceExpressionStub.java similarity index 61% rename from compiler/frontend/src/org/jetbrains/jet/lang/psi/JetNameReferenceExpression.kt rename to compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/PsiJetNameReferenceExpressionStub.java index d28bf89fea1..f32751947df 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetNameReferenceExpression.kt +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/PsiJetNameReferenceExpressionStub.java @@ -14,8 +14,14 @@ * limitations under the License. */ -package org.jetbrains.jet.lang.psi +package org.jetbrains.jet.lang.psi.stubs; -import com.intellij.lang.ASTNode +import com.intellij.psi.stubs.StubElement; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.psi.JetNameReferenceExpression; +import org.jetbrains.jet.lang.resolve.name.Name; -public class JetNameReferenceExpression(node: ASTNode) : JetSimpleNameExpressionImpl(node) \ No newline at end of file +public interface PsiJetNameReferenceExpressionStub extends StubElement { + @NotNull + String getReferencedName(); +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetNameReferenceExpressionElementType.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetNameReferenceExpressionElementType.java new file mode 100644 index 00000000000..80f54175938 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetNameReferenceExpressionElementType.java @@ -0,0 +1,52 @@ +/* + * Copyright 2010-2014 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.jet.lang.psi.stubs.elements; + +import com.intellij.psi.stubs.StubElement; +import com.intellij.psi.stubs.StubInputStream; +import com.intellij.psi.stubs.StubOutputStream; +import com.intellij.util.io.StringRef; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.psi.JetNameReferenceExpression; +import org.jetbrains.jet.lang.psi.stubs.PsiJetNameReferenceExpressionStub; +import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetNameReferenceExpressionStubImpl; + +import java.io.IOException; + +public class JetNameReferenceExpressionElementType extends JetStubElementType { + public JetNameReferenceExpressionElementType(@NotNull @NonNls String debugName) { + super(debugName, JetNameReferenceExpression.class, PsiJetNameReferenceExpressionStub.class); + } + + @Override + public PsiJetNameReferenceExpressionStub createStub(@NotNull JetNameReferenceExpression psi, StubElement parentStub) { + return new PsiJetNameReferenceExpressionStubImpl(parentStub, StringRef.fromString(psi.getReferencedName())); + } + + @Override + public void serialize(@NotNull PsiJetNameReferenceExpressionStub stub, @NotNull StubOutputStream dataStream) throws IOException { + dataStream.writeName(stub.getReferencedName()); + } + + @NotNull + @Override + public PsiJetNameReferenceExpressionStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException { + StringRef referencedName = dataStream.readName(); + return new PsiJetNameReferenceExpressionStubImpl(parentStub, referencedName); + } +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetStubElementTypes.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetStubElementTypes.java index b493622a79e..916349d3def 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetStubElementTypes.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetStubElementTypes.java @@ -60,4 +60,6 @@ public interface JetStubElementTypes { new JetPlaceHolderStubElementType("TYPE_REFERENCE", JetTypeReference.class);; JetUserTypeElementType USER_TYPE = new JetUserTypeElementType("USER_TYPE"); + + JetNameReferenceExpressionElementType REFERENCE_EXPRESSION = new JetNameReferenceExpressionElementType("REFERENCE_EXPRESSION"); } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/impl/PsiJetNameReferenceExpressionStubImpl.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/impl/PsiJetNameReferenceExpressionStubImpl.java new file mode 100644 index 00000000000..8e161df8743 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/impl/PsiJetNameReferenceExpressionStubImpl.java @@ -0,0 +1,41 @@ +/* + * Copyright 2010-2014 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.jet.lang.psi.stubs.impl; + +import com.intellij.psi.stubs.StubBase; +import com.intellij.psi.stubs.StubElement; +import com.intellij.util.io.StringRef; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.psi.JetNameReferenceExpression; +import org.jetbrains.jet.lang.psi.stubs.PsiJetNameReferenceExpressionStub; +import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes; + +public class PsiJetNameReferenceExpressionStubImpl extends StubBase implements PsiJetNameReferenceExpressionStub { + @NotNull + private final StringRef referencedName; + + public PsiJetNameReferenceExpressionStubImpl(StubElement parent, @NotNull StringRef referencedName) { + super(parent, JetStubElementTypes.REFERENCE_EXPRESSION); + this.referencedName = referencedName; + } + + @NotNull + @Override + public String getReferencedName() { + return referencedName.getString(); + } +}