"Remove unnecessary parentheses" intention - smaller range & refactoring

This commit is contained in:
Valentin Kipyatkov
2015-04-28 16:45:02 +03:00
parent 444da656e6
commit c6b18efb15
6 changed files with 13 additions and 17 deletions
@@ -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...
@@ -24,8 +24,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully
public class MoveLambdaInsideParenthesesIntention : JetSelfTargetingIntention<JetFunctionLiteralArgument>(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) {
@@ -28,7 +28,7 @@ public class MoveLambdaOutsideParenthesesIntention : JetSelfTargetingIntention<J
val functionLiteral = getFunctionLiteral(expression) ?: return false
if (caretOffset < argument.asElement().getTextRange().getStartOffset()) return false
val bodyRange = functionLiteral.getBodyExpression()?.getTextRange() ?: return true
return caretOffset <= bodyRange.getStartOffset() || caretOffset >= bodyRange.getEndOffset()
return !bodyRange.containsInside(caretOffset)
}
private fun getFunctionLiteral(expression: JetExpression?): JetFunctionLiteral? {
@@ -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<JetParenthesizedExpression>(
"remove.unnecessary.parentheses", javaClass()
) {
override fun isApplicableTo(element: JetParenthesizedExpression): Boolean = JetPsiUtil.areParenthesesUseless(element)
public class RemoveUnnecessaryParenthesesIntention : JetSelfTargetingIntention<JetParenthesizedExpression>(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()!!)
}
}
@@ -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()
@@ -2,5 +2,5 @@ class C() {
fun get(i: Int) = i + 1
// KT-4513: Unnecessary parenthesis around 'this'
val foo = (th<caret>is)[41]
val foo = (this<caret>)[41]
}