Rewrite JetSimpleNameExpression and inheritors to Kotlin

This commit is contained in:
Pavel V. Talanov
2014-03-26 17:29:55 +04:00
parent c2f28a16b4
commit 6413691c33
5 changed files with 119 additions and 151 deletions
@@ -14,13 +14,8 @@
* limitations under the License.
*/
package org.jetbrains.jet.lang.psi;
package org.jetbrains.jet.lang.psi
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
import com.intellij.lang.ASTNode
public class JetNameReferenceExpression extends JetSimpleNameExpression {
public JetNameReferenceExpression(@NotNull ASTNode node) {
super(node);
}
}
public class JetLabelReferenceExpression(node: ASTNode) : JetSimpleNameExpression(node)
@@ -14,13 +14,8 @@
* limitations under the License.
*/
package org.jetbrains.jet.lang.psi;
package org.jetbrains.jet.lang.psi
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
import com.intellij.lang.ASTNode
public class JetLabelReferenceExpression extends JetSimpleNameExpression {
public JetLabelReferenceExpression(@NotNull ASTNode node) {
super(node);
}
}
public class JetNameReferenceExpression(node: ASTNode) : JetSimpleNameExpression(node)
@@ -14,13 +14,8 @@
* limitations under the License.
*/
package org.jetbrains.jet.lang.psi;
package org.jetbrains.jet.lang.psi
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
import com.intellij.lang.ASTNode
public class JetOperationReferenceExpression extends JetSimpleNameExpression {
public JetOperationReferenceExpression(@NotNull ASTNode node) {
super(node);
}
}
public class JetOperationReferenceExpression(node: ASTNode) : JetSimpleNameExpression(node)
@@ -1,127 +0,0 @@
/*
* Copyright 2010-2013 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.parsing.JetExpressionParsing;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
import org.jetbrains.jet.lexer.JetTokens;
import static org.jetbrains.jet.lexer.JetTokens.*;
public abstract class JetSimpleNameExpression extends JetExpressionImpl implements JetReferenceExpression {
public static final TokenSet REFERENCE_TOKENS = TokenSet.create(LABEL_IDENTIFIER, IDENTIFIER, FIELD_IDENTIFIER, THIS_KEYWORD, SUPER_KEYWORD);
public JetSimpleNameExpression(@NotNull ASTNode node) {
super(node);
}
@Nullable
public JetExpression getReceiverExpression() {
PsiElement parent = getParent();
if (parent instanceof JetQualifiedExpression && !isImportDirectiveExpression()) {
JetExpression receiverExpression = ((JetQualifiedExpression) parent).getReceiverExpression();
// Name expression can't be receiver for itself
if (receiverExpression != this) {
return receiverExpression;
}
}
else if (parent instanceof JetCallExpression) {
//This is in case `a().b()`
JetCallExpression callExpression = (JetCallExpression) parent;
parent = callExpression.getParent();
if (parent instanceof JetQualifiedExpression) {
JetQualifiedExpression qualifiedExpression = (JetQualifiedExpression) parent;
JetExpression parentsReceiver = qualifiedExpression.getReceiverExpression();
if (parentsReceiver != callExpression) {
return parentsReceiver;
}
}
}
else if (parent instanceof JetBinaryExpression && ((JetBinaryExpression) parent).getOperationReference() == this) {
JetBinaryExpression expr = (JetBinaryExpression) parent;
//noinspection SuspiciousMethodCalls
return OperatorConventions.IN_OPERATIONS.contains(expr.getOperationToken())
? expr.getRight()
: expr.getLeft();
}
else if (parent instanceof JetUnaryExpression && ((JetUnaryExpression) parent).getOperationReference() == this) {
return ((JetUnaryExpression) parent).getBaseExpression();
}
else if (parent instanceof JetUserType) {
JetUserType qualifier = ((JetUserType) parent).getQualifier();
if (qualifier != null) {
return qualifier.getReferenceExpression();
}
}
return null;
}
public boolean isImportDirectiveExpression() {
PsiElement parent = getParent();
if (parent == null) return false;
else return parent instanceof JetImportDirective ||
parent.getParent() instanceof JetImportDirective;
}
@NotNull
public String getReferencedName() {
String text = getReferencedNameElement().getNode().getText();
return JetPsiUtil.unquoteIdentifierOrFieldReference(text);
}
@NotNull
public Name getReferencedNameAsName() {
String name = getReferencedName();
return Name.identifierNoValidate(name);
}
@NotNull
public PsiElement getReferencedNameElement() {
PsiElement element = findChildByType(REFERENCE_TOKENS);
if (element == null) {
element = findChildByType(JetExpressionParsing.ALL_OPERATIONS);
}
if (element != null) {
return element;
}
return this;
}
@Nullable
public PsiElement getIdentifier() {
return findChildByType(JetTokens.IDENTIFIER);
}
@Nullable @IfNotParsed
public IElementType getReferencedNameElementType() {
return getReferencedNameElement().getNode().getElementType();
}
@Override
public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
return visitor.visitSimpleNameExpression(this, data);
}
}
@@ -0,0 +1,110 @@
/*
* 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.Nullable
import org.jetbrains.jet.lang.parsing.JetExpressionParsing
import org.jetbrains.jet.lang.resolve.name.Name
import org.jetbrains.jet.lexer.JetTokens
import org.jetbrains.jet.lexer.JetTokens.*
import org.jetbrains.jet.lang.types.expressions.OperatorConventions
public abstract class JetSimpleNameExpression(node: ASTNode) : JetExpressionImpl(node), JetReferenceExpression {
public fun getReceiverExpression(): JetExpression? {
val parent = getParent()
when {
parent is JetQualifiedExpression && !isImportDirectiveExpression() -> {
val receiverExpression = parent.getReceiverExpression()
// Name expression can't be receiver for itself
if (receiverExpression != this) {
return receiverExpression
}
}
parent is JetCallExpression -> {
//This is in case `a().b()`
val callExpression = (parent as JetCallExpression)
val grandParent = callExpression.getParent()
if (grandParent is JetQualifiedExpression) {
val parentsReceiver = grandParent.getReceiverExpression()
if (parentsReceiver != callExpression) {
return parentsReceiver
}
}
}
parent is JetBinaryExpression && parent.getOperationReference() == this -> {
return if (parent.getOperationToken() in OperatorConventions.IN_OPERATIONS) parent.getRight() else parent.getLeft()
}
parent is JetUnaryExpression && parent.getOperationReference() == this -> {
return parent.getBaseExpression()!!
}
parent is JetUserType -> {
val qualifier = parent.getQualifier()
if (qualifier != null) {
return qualifier.getReferenceExpression()!!
}
}
}
return null
}
public fun isImportDirectiveExpression(): Boolean {
val parent = getParent()
if (parent == null) {
return false
}
else {
return parent is JetImportDirective || parent.getParent() is JetImportDirective
}
}
public fun getReferencedName(): String {
val text = getReferencedNameElement().getNode()!!.getText()
return JetPsiUtil.unquoteIdentifierOrFieldReference(text)
}
public fun getReferencedNameAsName(): Name {
val name = getReferencedName()
return Name.identifierNoValidate(name)
}
public fun getReferencedNameElement(): PsiElement {
return findChildByType(REFERENCE_TOKENS) ?:
findChildByType(JetExpressionParsing.ALL_OPERATIONS) ?:
this
}
public fun getIdentifier(): PsiElement? {
return findChildByType(JetTokens.IDENTIFIER)
}
public fun getReferencedNameElementType(): IElementType {
return getReferencedNameElement().getNode()!!.getElementType()!!
}
override fun <R, D> accept(visitor: JetVisitor<R, D>, data: D?): R {
return visitor.visitSimpleNameExpression(this, data) as R
}
class object {
public val REFERENCE_TOKENS: TokenSet = TokenSet.create(LABEL_IDENTIFIER, IDENTIFIER, FIELD_IDENTIFIER, THIS_KEYWORD, SUPER_KEYWORD)
}
}