diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties index 2e6c3bb457c..44cdc4aa6ea 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties @@ -285,8 +285,6 @@ change.function.signature.action.multiple=Change function signature... change.function.signature.family=Change function signature change.function.signature.chooser.title=Choose signature change.function.signature.action=Change function signature -remove.unnecessary.parentheses=Remove unnecessary parentheses -remove.unnecessary.parentheses.family=Remove Unnecessary Parentheses add.name.to.argument.family=Add Name to Argument add.name.to.argument.single=Add name to argument\: ''{0}'' add.name.to.argument.multiple=Add name to argument... diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/MoveLambdaInsideParenthesesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/MoveLambdaInsideParenthesesIntention.kt index 898389ccc2a..00b8c125132 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/MoveLambdaInsideParenthesesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/MoveLambdaInsideParenthesesIntention.kt @@ -24,8 +24,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully public class MoveLambdaInsideParenthesesIntention : JetSelfTargetingIntention(javaClass(), "Move lambda argument into parentheses") { override fun isApplicableTo(element: JetFunctionLiteralArgument, caretOffset: Int): Boolean { val body = element.getFunctionLiteral().getBodyExpression() ?: return true - val bodyRange = body.getTextRange() - return caretOffset <= bodyRange.getStartOffset() || caretOffset >= bodyRange.getEndOffset() + return !body.getTextRange().containsInside(caretOffset) } override fun applyTo(element: JetFunctionLiteralArgument, editor: Editor) { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/MoveLambdaOutsideParenthesesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/MoveLambdaOutsideParenthesesIntention.kt index 7098911e171..dcb6e9d5189 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/MoveLambdaOutsideParenthesesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/MoveLambdaOutsideParenthesesIntention.kt @@ -28,7 +28,7 @@ public class MoveLambdaOutsideParenthesesIntention : JetSelfTargetingIntention= bodyRange.getEndOffset() + return !bodyRange.containsInside(caretOffset) } private fun getFunctionLiteral(expression: JetExpression?): JetFunctionLiteral? { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveUnnecessaryParenthesesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveUnnecessaryParenthesesIntention.kt index 84e1db9d7d3..aa10c22ff98 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveUnnecessaryParenthesesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveUnnecessaryParenthesesIntention.kt @@ -20,15 +20,14 @@ import com.intellij.openapi.editor.Editor import org.jetbrains.kotlin.psi.JetParenthesizedExpression import org.jetbrains.kotlin.psi.JetPsiUtil -public class RemoveUnnecessaryParenthesesIntention : JetSelfTargetingOffsetIndependentIntention( - "remove.unnecessary.parentheses", javaClass() -) { - override fun isApplicableTo(element: JetParenthesizedExpression): Boolean = JetPsiUtil.areParenthesesUseless(element) +public class RemoveUnnecessaryParenthesesIntention : JetSelfTargetingIntention(javaClass(), "Remove unnecessary parentheses") { + override fun isApplicableTo(element: JetParenthesizedExpression, caretOffset: Int): Boolean { + val expression = element.getExpression() ?: return false + if (!JetPsiUtil.areParenthesesUseless(element)) return false + return !expression.getTextRange().containsInside(caretOffset) + } override fun applyTo(element: JetParenthesizedExpression, editor: Editor) { - with (element.getExpression()) { - assert (this != null, "parenthesizedExpression.getExpression() == null despite @IfNotParsed annotation") - element.replace(this!!) - } + element.replace(element.getExpression()!!) } } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt index d16c1a74f16..d598677dc6d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt @@ -16,17 +16,15 @@ package org.jetbrains.kotlin.idea.intentions +import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.JetNodeTypes import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers import org.jetbrains.kotlin.idea.util.ShortenReferences -import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.psi.psiUtil.parents import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.types.JetType -import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull fun specifyTypeExplicitly(declaration: JetNamedFunction, typeText: String) { specifyTypeExplicitly(declaration, JetPsiFactory(declaration).createType(typeText)) @@ -71,3 +69,5 @@ fun JetContainerNode.description(): String? { } return null } + +fun TextRange.containsInside(offset: Int) = getStartOffset() < offset && offset < getEndOffset() \ No newline at end of file diff --git a/idea/testData/intentions/removeUnnecessaryParentheses/unnecessaryParentheses8.kt b/idea/testData/intentions/removeUnnecessaryParentheses/unnecessaryParentheses8.kt index e4ecc0bcfe9..9a57e7ef813 100644 --- a/idea/testData/intentions/removeUnnecessaryParentheses/unnecessaryParentheses8.kt +++ b/idea/testData/intentions/removeUnnecessaryParentheses/unnecessaryParentheses8.kt @@ -2,5 +2,5 @@ class C() { fun get(i: Int) = i + 1 // KT-4513: Unnecessary parenthesis around 'this' - val foo = (this)[41] + val foo = (this)[41] }