diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt index 39d7650db7a..0e7fbb54ea6 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt @@ -109,7 +109,7 @@ public class ConvertToStringTemplateIntention : JetSelfTargetingOffsetIndependen } } - is JetSimpleNameExpression -> + is JetNameReferenceExpression -> "$" + (if (forceBraces) "{$expressionText}" else expressionText) null -> "" diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/IfNullToElvisIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/IfNullToElvisIntention.kt index 9ad9f06d699..a9685934079 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/IfNullToElvisIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/IfNullToElvisIntention.kt @@ -22,17 +22,17 @@ import com.intellij.psi.PsiComment import com.intellij.psi.PsiElement import com.intellij.psi.PsiRecursiveElementVisitor import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection import org.jetbrains.kotlin.idea.intentions.branchedTransformations.expressionComparedToNull -import org.jetbrains.kotlin.types.typeUtil.isNothing import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.endOffset -import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.psi.psiUtil.siblings import org.jetbrains.kotlin.psi.psiUtil.startOffset +import org.jetbrains.kotlin.types.typeUtil.isNothing import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull -import java.util.ArrayList +import java.util.* public class IfNullToElvisInspection : IntentionBasedInspection(IfNullToElvisIntention()) @@ -91,7 +91,7 @@ public class IfNullToElvisIntention : JetSelfTargetingRangeIntention() } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/MoveLambdaOutsideParenthesesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/MoveLambdaOutsideParenthesesIntention.kt index 47865612211..1b1f68a4bc4 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/MoveLambdaOutsideParenthesesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/MoveLambdaOutsideParenthesesIntention.kt @@ -21,11 +21,12 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.core.moveFunctionLiteralOutsideParentheses -import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.JetCallExpression +import org.jetbrains.kotlin.psi.JetNameReferenceExpression import org.jetbrains.kotlin.psi.psiUtil.containsInside import org.jetbrains.kotlin.psi.psiUtil.startOffset +import org.jetbrains.kotlin.psi.unpackFunctionLiteral import org.jetbrains.kotlin.resolve.BindingContext -import org.jetbrains.kotlin.resolve.calls.callUtil.getValueArgumentsInParentheses import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode public class MoveLambdaOutsideParenthesesIntention : JetSelfTargetingIntention(javaClass(), "Move lambda argument out of parentheses") { @@ -36,7 +37,7 @@ public class MoveLambdaOutsideParenthesesIntention : JetSelfTargetingIntention(javaClass(), "Remove curly braces") { override fun isApplicableTo(element: JetBlockStringTemplateEntry): Boolean { - if (element.getExpression() !is JetSimpleNameExpression) return false + if (element.getExpression() !is JetNameReferenceExpression) return false val nextSiblingText = element.getNextSibling()?.getText() return nextSiblingText == null || !pattern.matcher(nextSiblingText).matches() } @@ -39,7 +39,7 @@ public class RemoveCurlyBracesFromTemplateIntention : JetSelfTargetingOffsetInde } public fun applyTo(element: JetBlockStringTemplateEntry): JetStringTemplateEntryWithExpression { - val name = (element.getExpression() as JetSimpleNameExpression).getReferencedName() + val name = (element.getExpression() as JetNameReferenceExpression).getReferencedName() val newEntry = JetPsiFactory(element).createSimpleNameStringTemplateEntry(name) return element.replaced(newEntry) } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceExplicitFunctionLiteralParamWithItIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceExplicitFunctionLiteralParamWithItIntention.kt index 5fff4887cbc..ed3a5840496 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceExplicitFunctionLiteralParamWithItIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceExplicitFunctionLiteralParamWithItIntention.kt @@ -27,13 +27,13 @@ import com.intellij.usageView.UsageInfo import org.jetbrains.kotlin.descriptors.ParameterDescriptor import org.jetbrains.kotlin.descriptors.impl.AnonymousFunctionDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze -import org.jetbrains.kotlin.idea.references.JetReference import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.psi.JetFunctionLiteral -import org.jetbrains.kotlin.psi.JetSimpleNameExpression +import org.jetbrains.kotlin.psi.JetNameReferenceExpression import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.psi.psiUtil.getParentOfType import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode public class ReplaceExplicitFunctionLiteralParamWithItIntention() : PsiElementBaseIntentionAction() { override fun getFamilyName() = "Replace explicit lambda parameter with 'it'" @@ -56,9 +56,9 @@ public class ReplaceExplicitFunctionLiteralParamWithItIntention() : PsiElementBa } private fun targetFunctionLiteral(element: PsiElement, caretOffset: Int): JetFunctionLiteral? { - val expression = element.getParentOfType(true) + val expression = element.getParentOfType(true) if (expression != null) { - val target = expression.mainReference.resolveToDescriptors(expression.analyze()) + val target = expression.mainReference.resolveToDescriptors(expression.analyze(BodyResolveMode.PARTIAL)) .singleOrNull() as? ParameterDescriptor ?: return null val functionDescriptor = target.getContainingDeclaration() as? AnonymousFunctionDescriptor ?: return null return DescriptorToSourceUtils.descriptorToDeclaration(functionDescriptor) as? JetFunctionLiteral diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceItWithExplicitFunctionLiteralParamIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceItWithExplicitFunctionLiteralParamIntention.kt index 6f1384660c0..1496f16d016 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceItWithExplicitFunctionLiteralParamIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceItWithExplicitFunctionLiteralParamIntention.kt @@ -23,19 +23,16 @@ import com.intellij.refactoring.rename.inplace.VariableInplaceRenameHandler import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.references.JetReference import org.jetbrains.kotlin.idea.references.mainReference -import org.jetbrains.kotlin.psi.JetFunctionLiteral -import org.jetbrains.kotlin.psi.JetFunctionLiteralExpression -import org.jetbrains.kotlin.psi.JetPsiFactory -import org.jetbrains.kotlin.psi.JetSimpleNameExpression +import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils -public class ReplaceItWithExplicitFunctionLiteralParamIntention() : JetSelfTargetingOffsetIndependentIntention ( +public class ReplaceItWithExplicitFunctionLiteralParamIntention() : JetSelfTargetingOffsetIndependentIntention ( javaClass(), "Replace 'it' with explicit parameter" ), LowPriorityAction { - override fun isApplicableTo(element: JetSimpleNameExpression) + override fun isApplicableTo(element: JetNameReferenceExpression) = isAutoCreatedItUsage(element) - override fun applyTo(element: JetSimpleNameExpression, editor: Editor) { + override fun applyTo(element: JetNameReferenceExpression, editor: Editor) { val target = element.mainReference.resolveToDescriptors(element.analyze()).single() val functionLiteral = DescriptorToSourceUtils.descriptorToDeclaration(target.getContainingDeclaration()!!) as JetFunctionLiteral diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceWithOperatorAssignmentIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceWithOperatorAssignmentIntention.kt index a20ef0aa552..fd8883dc1e6 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceWithOperatorAssignmentIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceWithOperatorAssignmentIntention.kt @@ -25,8 +25,8 @@ import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection import org.jetbrains.kotlin.idea.util.psi.patternMatching.matches import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.psi.JetBinaryExpression +import org.jetbrains.kotlin.psi.JetNameReferenceExpression import org.jetbrains.kotlin.psi.JetPsiFactory -import org.jetbrains.kotlin.psi.JetSimpleNameExpression import org.jetbrains.kotlin.resolve.BindingContext public class ReplaceWithOperatorAssignmentInspection : IntentionBasedInspection(ReplaceWithOperatorAssignmentIntention()) @@ -35,14 +35,14 @@ public class ReplaceWithOperatorAssignmentIntention : JetSelfTargetingOffsetInde override fun isApplicableTo(element: JetBinaryExpression): Boolean { if (element.getOperationToken() != JetTokens.EQ) return false - val left = element.getLeft() as? JetSimpleNameExpression ?: return false + val left = element.getLeft() as? JetNameReferenceExpression ?: return false val right = element.getRight() as? JetBinaryExpression ?: return false if (right.getLeft() == null || right.getRight() == null) return false return checkExpressionRepeat(left, right) } - private fun checkExpressionRepeat(variableExpression: JetSimpleNameExpression, expression: JetBinaryExpression): Boolean { + private fun checkExpressionRepeat(variableExpression: JetNameReferenceExpression, expression: JetBinaryExpression): Boolean { val context = expression.analyze() val descriptor = context[BindingContext.REFERENCE_TARGET, expression.getOperationReference()]?.getContainingDeclaration() val isPrimitiveOperation = descriptor is ClassDescriptor && KotlinBuiltIns.isPrimitiveType(descriptor.getDefaultType()) @@ -81,14 +81,14 @@ public class ReplaceWithOperatorAssignmentIntention : JetSelfTargetingOffsetInde override fun applyTo(element: JetBinaryExpression, editor: Editor) { val replacement = buildOperatorAssignmentText( - element.getLeft() as JetSimpleNameExpression, + element.getLeft() as JetNameReferenceExpression, element.getRight() as JetBinaryExpression, "" ) element.replace(JetPsiFactory(element).createExpression(replacement)) } - tailrec private fun buildOperatorAssignmentText(variableExpression: JetSimpleNameExpression, expression: JetBinaryExpression, tail: String): String { + tailrec private fun buildOperatorAssignmentText(variableExpression: JetNameReferenceExpression, expression: JetBinaryExpression, tail: String): String { val operationText = expression.getOperationReference().getText() val variableName = variableExpression.getText() diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceWithOrdinaryAssignmentIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceWithOrdinaryAssignmentIntention.kt index d9997d43fed..ec4033e7943 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceWithOrdinaryAssignmentIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceWithOrdinaryAssignmentIntention.kt @@ -20,14 +20,14 @@ import com.intellij.codeInsight.intention.LowPriorityAction import com.intellij.openapi.editor.Editor import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.psi.JetBinaryExpression +import org.jetbrains.kotlin.psi.JetNameReferenceExpression import org.jetbrains.kotlin.psi.JetPsiFactory -import org.jetbrains.kotlin.psi.JetSimpleNameExpression import org.jetbrains.kotlin.psi.createExpressionByPattern public class ReplaceWithOrdinaryAssignmentIntention : JetSelfTargetingIntention(javaClass(), "Replace with ordinary assignment"), LowPriorityAction { override fun isApplicableTo(element: JetBinaryExpression, caretOffset: Int): Boolean { if (element.getOperationToken() !in JetTokens.AUGMENTED_ASSIGNMENTS) return false - if (element.getLeft() !is JetSimpleNameExpression) return false + if (element.getLeft() !is JetNameReferenceExpression) return false if (element.getRight() == null) return false return element.getOperationReference().getTextRange().containsOffset(caretOffset) } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/SplitIfIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/SplitIfIntention.kt index 86eaf715d40..037ca5254c0 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/SplitIfIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/SplitIfIntention.kt @@ -32,7 +32,7 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset public class SplitIfIntention : JetSelfTargetingIntention(javaClass(), "Split if into 2 if's") { override fun isApplicableTo(element: JetExpression, caretOffset: Int): Boolean { return when (element) { - is JetSimpleNameExpression -> isOperatorValid(element) + is JetOperationReferenceExpression -> isOperatorValid(element) is JetIfExpression -> getFirstValidOperator(element) != null && element.getIfKeyword().getTextRange().containsOffset(caretOffset) else -> false } @@ -41,7 +41,7 @@ public class SplitIfIntention : JetSelfTargetingIntention(javaCla override fun applyTo(element: JetExpression, editor: Editor) { val operator = when (element) { is JetIfExpression -> getFirstValidOperator(element)!! - else -> element as JetSimpleNameExpression + else -> element as JetOperationReferenceExpression } val ifExpression = operator.getNonStrictParentOfType() @@ -95,13 +95,13 @@ public class SplitIfIntention : JetSelfTargetingIntention(javaCla return expression } - private fun getFirstValidOperator(element: JetIfExpression): JetSimpleNameExpression? { + private fun getFirstValidOperator(element: JetIfExpression): JetOperationReferenceExpression? { val condition = element.getCondition() ?: return null - return PsiTreeUtil.findChildrenOfType(condition, javaClass()) + return PsiTreeUtil.findChildrenOfType(condition, javaClass()) .firstOrNull { isOperatorValid(it) } } - private fun isOperatorValid(element: JetSimpleNameExpression): Boolean { + private fun isOperatorValid(element: JetOperationReferenceExpression): Boolean { val operator = element.getReferencedNameElementType() if (operator != JetTokens.ANDAND && operator != JetTokens.OROR) return false diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ToInfixCallIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ToInfixCallIntention.kt index f4b68b2a46b..ae9acca35bb 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ToInfixCallIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ToInfixCallIntention.kt @@ -22,7 +22,7 @@ import org.jetbrains.kotlin.psi.* public class ToInfixCallIntention : JetSelfTargetingIntention(javaClass(), "Replace with infix function call") { override fun isApplicableTo(element: JetCallExpression, caretOffset: Int): Boolean { - val calleeExpr = element.getCalleeExpression() as? JetSimpleNameExpression ?: return false + val calleeExpr = element.getCalleeExpression() as? JetNameReferenceExpression ?: return false if (!calleeExpr.getTextRange().containsOffset(caretOffset)) return false val dotQualified = element.getParent() as? JetDotQualifiedExpression ?: return false diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/UsePropertyAccessSyntaxIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/UsePropertyAccessSyntaxIntention.kt index 2204b2ccf7c..840c31fdbc4 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/UsePropertyAccessSyntaxIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/UsePropertyAccessSyntaxIntention.kt @@ -78,7 +78,7 @@ class UsePropertyAccessSyntaxIntention : JetSelfTargetingOffsetIndependentIntent public fun detectPropertyNameToUse(callExpression: JetCallExpression): Name? { if (callExpression.getQualifiedExpressionForSelector()?.getReceiverExpression() is JetSuperExpression) return null // cannot call extensions on "super" - val callee = callExpression.getCalleeExpression() as? JetSimpleNameExpression ?: return null + val callee = callExpression.getCalleeExpression() as? JetNameReferenceExpression ?: return null val resolutionFacade = callExpression.getResolutionFacade() val bindingContext = resolutionFacade.analyze(callExpression, BodyResolveMode.PARTIAL) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt index d11dc7a996f..2250c66de45 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt @@ -30,6 +30,7 @@ import org.jetbrains.kotlin.psi.typeRefHelpers.setReceiverTypeReference import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.types.JetType fun JetCallableDeclaration.setType(type: JetType, shortenReferences: Boolean = true) { @@ -67,9 +68,9 @@ fun JetContainerNode.description(): String? { return null } -fun isAutoCreatedItUsage(expression: JetSimpleNameExpression): Boolean { +fun isAutoCreatedItUsage(expression: JetNameReferenceExpression): Boolean { if (expression.getReferencedName() != "it") return false - val context = expression.analyze() + val context = expression.analyze(BodyResolveMode.PARTIAL) val target = expression.mainReference.resolveToDescriptors(context).singleOrNull() as? ValueParameterDescriptor? ?: return false return context[BindingContext.AUTO_CREATED_IT, target]!! } @@ -101,7 +102,7 @@ val JetQualifiedExpression.callExpression: JetCallExpression? get() = getSelectorExpression() as? JetCallExpression val JetQualifiedExpression.calleeName: String? - get() = (callExpression?.getCalleeExpression() as? JetSimpleNameExpression)?.getText() + get() = (callExpression?.getCalleeExpression() as? JetNameReferenceExpression)?.getText() fun JetQualifiedExpression.toResolvedCall(): ResolvedCall? { val callExpression = callExpression ?: return null diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.kt index 2cdfc1d9f4c..2ae0b9e1756 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.kt @@ -26,7 +26,7 @@ object BranchedFoldingUtils { fun checkAssignment(expression: JetBinaryExpression): Boolean { if (expression.getOperationToken() !in JetTokens.ALL_ASSIGNMENTS) return false - val left = expression.getLeft() as? JetSimpleNameExpression ?: return false + val left = expression.getLeft() as? JetNameReferenceExpression ?: return false if (expression.getRight() == null) return false val parent = expression.getParent() diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt index 0a2b8ba4917..f8f8385e981 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt @@ -17,23 +17,22 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations import com.intellij.openapi.editor.Editor -import com.intellij.psi.PsiElement import com.intellij.psi.search.LocalSearchScope import com.intellij.psi.search.searches.ReferencesSearch import org.jetbrains.kotlin.JetNodeTypes import org.jetbrains.kotlin.descriptors.VariableDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.refactoring.inline.KotlinInlineValHandler import org.jetbrains.kotlin.idea.refactoring.introduce.introduceVariable.KotlinIntroduceVariableHandler +import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.idea.core.replaced -import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingContextUtils import org.jetbrains.kotlin.resolve.DescriptorUtils -import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsStatement import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode val NULL_PTR_EXCEPTION_FQ = "java.lang.NullPointerException" val KOTLIN_NULL_PTR_EXCEPTION_FQ = "kotlin.KotlinNullPointerException" @@ -68,8 +67,9 @@ fun JetThrowExpression.throwsNullPointerExceptionWithNoArguments(): Boolean { val thrownExpression = this.getThrownExpression() if (thrownExpression !is JetCallExpression) return false - val context = this.analyze() - val descriptor = context.get(BindingContext.REFERENCE_TARGET, thrownExpression.getCalleeExpression() as JetSimpleNameExpression) + val context = this.analyze(BodyResolveMode.PARTIAL) + val nameExpression = thrownExpression.calleeExpression as? JetNameReferenceExpression ?: return false + val descriptor = context[BindingContext.REFERENCE_TARGET, nameExpression] val declDescriptor = descriptor?.getContainingDeclaration() ?: return false val exceptionName = DescriptorUtils.getFqName(declDescriptor).asString() @@ -104,7 +104,7 @@ fun JetIfExpression.introduceValueForCondition(occurrenceInThenClause: JetExpres null) } -fun JetSimpleNameExpression.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor: Editor) { +fun JetNameReferenceExpression.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor: Editor) { val declaration = this.mainReference.resolve() as? JetProperty ?: return val enclosingElement = JetPsiUtil.getEnclosingElementForLocalDeclaration(declaration) @@ -120,15 +120,15 @@ fun JetSimpleNameExpression.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(edi } fun JetSafeQualifiedExpression.inlineReceiverIfApplicableWithPrompt(editor: Editor) { - (this.getReceiverExpression() as? JetSimpleNameExpression)?.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor) + (this.getReceiverExpression() as? JetNameReferenceExpression)?.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor) } fun JetBinaryExpression.inlineLeftSideIfApplicableWithPrompt(editor: Editor) { - (this.getLeft() as? JetSimpleNameExpression)?.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor) + (this.getLeft() as? JetNameReferenceExpression)?.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor) } fun JetPostfixExpression.inlineBaseExpressionIfApplicableWithPrompt(editor: Editor) { - (this.getBaseExpression() as? JetSimpleNameExpression)?.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor) + (this.getBaseExpression() as? JetNameReferenceExpression)?.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor) } fun JetExpression.isStableVariable(): Boolean { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/branchedTransformationUtils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/branchedTransformationUtils.kt index 4e8e6e54526..93aa5d2dd19 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/branchedTransformationUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/branchedTransformationUtils.kt @@ -16,10 +16,10 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations +import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.util.psi.patternMatching.matches import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.idea.core.replaced fun JetWhenCondition.toExpression(subject: JetExpression?): JetExpression { val factory = JetPsiFactory(this) @@ -58,7 +58,7 @@ public fun JetWhenExpression.getSubjectToIntroduce(): JetExpression? { for (condition in conditions) { if (condition !is JetWhenConditionWithExpression) return null - val candidate = condition.getExpression()?.getWhenConditionSubjectCandidate() as? JetSimpleNameExpression ?: return null + val candidate = condition.getExpression()?.getWhenConditionSubjectCandidate() as? JetNameReferenceExpression ?: return null if (lastCandidate == null) { lastCandidate = candidate @@ -82,7 +82,7 @@ private fun JetExpression?.getWhenConditionSubjectCandidate(): JetExpression? { val op = getOperationToken() when (op) { JetTokens.IN_KEYWORD, JetTokens.NOT_IN -> lhs - JetTokens.EQEQ -> lhs as? JetSimpleNameExpression ?: getRight() + JetTokens.EQEQ -> lhs as? JetNameReferenceExpression ?: getRight() else -> null } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/EliminateWhenSubjectIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/EliminateWhenSubjectIntention.kt index 8562bc284bf..73679f5d888 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/EliminateWhenSubjectIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/EliminateWhenSubjectIntention.kt @@ -20,15 +20,15 @@ import com.intellij.codeInsight.intention.LowPriorityAction import com.intellij.openapi.editor.Editor import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingIntention import org.jetbrains.kotlin.idea.intentions.branchedTransformations.toExpression +import org.jetbrains.kotlin.psi.JetNameReferenceExpression import org.jetbrains.kotlin.psi.JetPsiFactory -import org.jetbrains.kotlin.psi.JetSimpleNameExpression import org.jetbrains.kotlin.psi.JetWhenExpression import org.jetbrains.kotlin.psi.buildExpression import org.jetbrains.kotlin.psi.psiUtil.startOffset public class EliminateWhenSubjectIntention : JetSelfTargetingIntention(javaClass(), "Eliminate argument of 'when'"), LowPriorityAction { override fun isApplicableTo(element: JetWhenExpression, caretOffset: Int): Boolean { - if (element.getSubjectExpression() !is JetSimpleNameExpression) return false + if (element.getSubjectExpression() !is JetNameReferenceExpression) return false val lBrace = element.getOpenBrace() ?: return false return caretOffset <= lBrace.startOffset } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/FlattenWhenIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/FlattenWhenIntention.kt index 4366f5ec25f..5e950e4f4f0 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/FlattenWhenIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/FlattenWhenIntention.kt @@ -17,18 +17,18 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions import com.intellij.openapi.editor.Editor +import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingIntention import org.jetbrains.kotlin.idea.quickfix.moveCaret import org.jetbrains.kotlin.idea.util.psi.patternMatching.matches import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.endOffset -import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.psi.psiUtil.startOffset public class FlattenWhenIntention : JetSelfTargetingIntention(javaClass(), "Flatten 'when' expression") { override fun isApplicableTo(element: JetWhenExpression, caretOffset: Int): Boolean { val subject = element.getSubjectExpression() - if (subject != null && subject !is JetSimpleNameExpression) return false + if (subject != null && subject !is JetNameReferenceExpression) return false if (!JetPsiUtil.checkWhenExpressionHasSingleElse(element)) return false diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/FoldWhenToAssignmentIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/FoldWhenToAssignmentIntention.kt index cd4f009390a..15b5b8269aa 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/FoldWhenToAssignmentIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/FoldWhenToAssignmentIntention.kt @@ -21,7 +21,7 @@ import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedFoldingUtils import org.jetbrains.kotlin.psi.* -import java.util.ArrayList +import java.util.* public class FoldWhenToAssignmentIntention : JetSelfTargetingRangeIntention(javaClass(), "Replace 'when' expression with assignment") { override fun applicabilityRange(element: JetWhenExpression): TextRange? { @@ -53,7 +53,7 @@ public class FoldWhenToAssignmentIntention : JetSelfTargetingRangeIntention(), false) + val nameExpression = PsiTreeUtil.findElementOfClassAtOffset( + file, editor.getCaretModel().getOffset(), javaClass(), false) - return simpleNameExpression != null && isAutoCreatedItUsage(simpleNameExpression) + return nameExpression != null && isAutoCreatedItUsage(nameExpression) } override fun invoke(project: Project, editor: Editor, file: PsiFile, dataContext: DataContext?) {