Make JetSimpleNameExpression#getReceiverExpression extension function and move it to util class
This commit is contained in:
@@ -25,47 +25,9 @@ import org.jetbrains.jet.lang.parsing.JetExpressionParsing
|
|||||||
import org.jetbrains.jet.lang.resolve.name.Name
|
import org.jetbrains.jet.lang.resolve.name.Name
|
||||||
import org.jetbrains.jet.lexer.JetTokens
|
import org.jetbrains.jet.lexer.JetTokens
|
||||||
import org.jetbrains.jet.lexer.JetTokens.*
|
import org.jetbrains.jet.lexer.JetTokens.*
|
||||||
import org.jetbrains.jet.lang.types.expressions.OperatorConventions
|
|
||||||
|
|
||||||
public trait JetSimpleNameExpression : JetReferenceExpression {
|
public trait JetSimpleNameExpression : 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 {
|
public fun isImportDirectiveExpression(): Boolean {
|
||||||
val parent = getParent()
|
val parent = getParent()
|
||||||
if (parent == null) {
|
if (parent == null) {
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import com.intellij.psi.PsiPackage
|
|||||||
import com.intellij.psi.JavaDirectoryService
|
import com.intellij.psi.JavaDirectoryService
|
||||||
import com.intellij.psi.PsiDirectory
|
import com.intellij.psi.PsiDirectory
|
||||||
import org.jetbrains.jet.lang.psi.stubs.PsiJetClassOrObjectStub
|
import org.jetbrains.jet.lang.psi.stubs.PsiJetClassOrObjectStub
|
||||||
|
import org.jetbrains.jet.lang.types.expressions.OperatorConventions
|
||||||
|
|
||||||
public fun JetCallElement.getCallNameExpression(): JetSimpleNameExpression? {
|
public fun JetCallElement.getCallNameExpression(): JetSimpleNameExpression? {
|
||||||
val calleeExpression = getCalleeExpression()
|
val calleeExpression = getCalleeExpression()
|
||||||
@@ -264,4 +265,41 @@ public fun PsiElement.isInsideOf(elements: Iterable<PsiElement>): Boolean = elem
|
|||||||
public tailRecursive fun PsiElement.getOutermostParentContainedIn(container: PsiElement): PsiElement? {
|
public tailRecursive fun PsiElement.getOutermostParentContainedIn(container: PsiElement): PsiElement? {
|
||||||
val parent = getParent()
|
val parent = getParent()
|
||||||
return if (parent == container) this else parent?.getOutermostParentContainedIn(container)
|
return if (parent == container) this else parent?.getOutermostParentContainedIn(container)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public fun JetSimpleNameExpression.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
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import org.junit.Assert
|
|||||||
import org.jetbrains.jet.JetLiteFixture
|
import org.jetbrains.jet.JetLiteFixture
|
||||||
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment
|
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment
|
||||||
import org.jetbrains.jet.config.CompilerConfiguration
|
import org.jetbrains.jet.config.CompilerConfiguration
|
||||||
|
import org.jetbrains.jet.lang.psi.psiUtil.getReceiverExpression
|
||||||
|
|
||||||
public class JetSimpleNameExpressionTest() : JetLiteFixture() {
|
public class JetSimpleNameExpressionTest() : JetLiteFixture() {
|
||||||
public fun testGetReceiverExpressionIdentifier() {
|
public fun testGetReceiverExpressionIdentifier() {
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import org.jetbrains.jet.asJava.JavaElementFinder;
|
import org.jetbrains.jet.asJava.JavaElementFinder;
|
||||||
import org.jetbrains.jet.lang.descriptors.*;
|
import org.jetbrains.jet.lang.descriptors.*;
|
||||||
import org.jetbrains.jet.lang.psi.*;
|
import org.jetbrains.jet.lang.psi.*;
|
||||||
|
import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage;
|
||||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||||
import org.jetbrains.jet.lang.resolve.BindingTraceContext;
|
import org.jetbrains.jet.lang.resolve.BindingTraceContext;
|
||||||
import org.jetbrains.jet.lang.resolve.ImportPath;
|
import org.jetbrains.jet.lang.resolve.ImportPath;
|
||||||
@@ -289,7 +290,7 @@ public class JetShortNamesCache extends PsiShortNamesCache {
|
|||||||
@NotNull GlobalSearchScope searchScope
|
@NotNull GlobalSearchScope searchScope
|
||||||
) {
|
) {
|
||||||
BindingContext context = resolveSession.resolveToElement(expression);
|
BindingContext context = resolveSession.resolveToElement(expression);
|
||||||
JetExpression receiverExpression = expression.getReceiverExpression();
|
JetExpression receiverExpression = PsiUtilPackage.getReceiverExpression(expression);
|
||||||
if (receiverExpression == null) {
|
if (receiverExpression == null) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,6 +60,9 @@ import org.jetbrains.jet.lang.psi.JetTypeReference
|
|||||||
import com.intellij.util.containers.ContainerUtil
|
import com.intellij.util.containers.ContainerUtil
|
||||||
import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils
|
import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils
|
||||||
import org.jetbrains.jet.plugin.imports.*
|
import org.jetbrains.jet.plugin.imports.*
|
||||||
|
import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor
|
||||||
|
import org.jetbrains.jet.lang.psi.psiUtil.getReceiverExpression
|
||||||
|
import org.jetbrains.jet.utils.*
|
||||||
|
|
||||||
//NOTE: this class is based on CopyPasteReferenceProcessor and JavaCopyPasteReferenceProcessor
|
//NOTE: this class is based on CopyPasteReferenceProcessor and JavaCopyPasteReferenceProcessor
|
||||||
public class KotlinCopyPasteReferenceProcessor() : CopyPastePostProcessor<ReferenceTransferableData?> {
|
public class KotlinCopyPasteReferenceProcessor() : CopyPastePostProcessor<ReferenceTransferableData?> {
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
|||||||
import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor;
|
import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor;
|
||||||
import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor;
|
import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor;
|
||||||
import org.jetbrains.jet.lang.psi.*;
|
import org.jetbrains.jet.lang.psi.*;
|
||||||
|
import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage;
|
||||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||||
import org.jetbrains.jet.lang.resolve.calls.autocasts.AutoCastUtils;
|
import org.jetbrains.jet.lang.resolve.calls.autocasts.AutoCastUtils;
|
||||||
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
|
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
|
||||||
@@ -51,7 +52,7 @@ public final class TipsManager {
|
|||||||
@NotNull JetSimpleNameExpression expression,
|
@NotNull JetSimpleNameExpression expression,
|
||||||
@NotNull BindingContext context
|
@NotNull BindingContext context
|
||||||
) {
|
) {
|
||||||
JetExpression receiverExpression = expression.getReceiverExpression();
|
JetExpression receiverExpression = PsiUtilPackage.getReceiverExpression(expression);
|
||||||
PsiElement parent = expression.getParent();
|
PsiElement parent = expression.getParent();
|
||||||
boolean inPositionForCompletionWithReceiver = parent instanceof JetCallExpression || parent instanceof JetQualifiedExpression;
|
boolean inPositionForCompletionWithReceiver = parent instanceof JetCallExpression || parent instanceof JetQualifiedExpression;
|
||||||
if (receiverExpression != null && inPositionForCompletionWithReceiver) {
|
if (receiverExpression != null && inPositionForCompletionWithReceiver) {
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor
|
|||||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor
|
import org.jetbrains.jet.lang.descriptors.ClassDescriptor
|
||||||
import org.jetbrains.jet.lang.descriptors.CallableDescriptor
|
import org.jetbrains.jet.lang.descriptors.CallableDescriptor
|
||||||
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression
|
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression
|
||||||
|
import org.jetbrains.jet.lang.psi.psiUtil.getReceiverExpression
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import org.jetbrains.jet.lang.types.JetType
|
import org.jetbrains.jet.lang.types.JetType
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user