From 345f9217ae5e8d22b0b6f56b7728f2f6da1ac991 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 5 May 2015 22:06:26 +0300 Subject: [PATCH] DoubleBangToIfThenIntention - smaller range + minor code improvements --- .../kotlin/idea/JetBundle.properties | 4 --- .../intentions/JetTypeLookupExpression.kt | 2 +- .../SpecifyTypeExplicitlyIntention.kt | 2 +- .../branchedTransformations/IfThenUtils.kt | 2 -- .../intentions/DoubleBangToIfThenIntention.kt | 33 +++++++++---------- 5 files changed, 17 insertions(+), 26 deletions(-) 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 c8ca9b4994f..3e4785b105c 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties @@ -241,10 +241,6 @@ unfold.return.to.when=Replace return with 'when' expression unfold.return.to.when.family=Replace Return with 'when' Expression unfold.call.to.when=Replace method call with 'when' expression unfold.call.to.when.family=Replace Method Call with 'when' Expression -double.bang.to.if.then.exception.text=Expression ''{0}'' must not be null -double.bang.to.if.then.choose.exception=Choose Exception Class -double.bang.to.if.then=Replace '!!' expression with 'if' expression -double.bang.to.if.then.family=Replace '!!' Expression with 'if' Expression if.then.to.double.bang=Replace 'if' expression with '!!' expression if.then.to.double.bang.replace.exception=Replace 'if' expression with '!!' expression (will remove Exception) if.then.to.double.bang.family=Replace 'if' Expression with '!!' Expression diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/JetTypeLookupExpression.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/JetTypeLookupExpression.kt index f256778460b..aba7589ae53 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/JetTypeLookupExpression.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/JetTypeLookupExpression.kt @@ -33,7 +33,7 @@ import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil public abstract class JetTypeLookupExpression( lookupItems: List, protected val defaultItem: T, - private val advertisementText: String? + private val advertisementText: String? = null ) : Expression() { protected abstract fun getLookupString(element: T): String diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt index 916b32390e5..28527cc55c6 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt @@ -84,7 +84,7 @@ public class SpecifyTypeExplicitlyIntention : JetSelfTargetingIntention() else arrayListOf(exprType) types.addAll(allSupertypes) - return object : JetTypeLookupExpression(types, types.first(), null) { + return object : JetTypeLookupExpression(types, types.first()) { override fun getLookupString(element: JetType) = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(element) override fun getResult(element: JetType) = IdeDescriptorRenderers.SOURCE_CODE.renderType(element) } 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 b7567318179..772454d359a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt @@ -32,9 +32,7 @@ import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsStatement import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.psi.* -val NULL_PTR_EXCEPTION = "NullPointerException" val NULL_PTR_EXCEPTION_FQ = "java.lang.NullPointerException" -val KOTLIN_NULL_PTR_EXCEPTION = "KotlinNullPointerException" val KOTLIN_NULL_PTR_EXCEPTION_FQ = "kotlin.KotlinNullPointerException" fun JetBinaryExpression.comparesNonNullToNull(): Boolean { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/DoubleBangToIfThenIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/DoubleBangToIfThenIntention.kt index 916e3040630..051ff9d64db 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/DoubleBangToIfThenIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/DoubleBangToIfThenIntention.kt @@ -19,12 +19,11 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions import com.intellij.codeInsight.template.Template import com.intellij.codeInsight.template.TemplateBuilderImpl import com.intellij.codeInsight.template.TemplateEditingAdapter -import com.intellij.codeInsight.template.impl.TemplateManagerImpl +import com.intellij.codeInsight.template.TemplateManager import com.intellij.openapi.editor.Editor import com.intellij.psi.PsiDocumentManager import org.apache.commons.lang.StringEscapeUtils.escapeJava -import org.jetbrains.kotlin.idea.JetBundle -import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention +import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingIntention import org.jetbrains.kotlin.idea.intentions.JetTypeLookupExpression import org.jetbrains.kotlin.idea.intentions.branchedTransformations.* import org.jetbrains.kotlin.lexer.JetTokens @@ -33,16 +32,16 @@ import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.psi.JetPsiUtil import org.jetbrains.kotlin.psi.JetThrowExpression -public class DoubleBangToIfThenIntention : JetSelfTargetingOffsetIndependentIntention("double.bang.to.if.then", javaClass()) { - - override fun isApplicableTo(element: JetPostfixExpression): Boolean = - element.getOperationToken() == JetTokens.EXCLEXCL +public class DoubleBangToIfThenIntention : JetSelfTargetingIntention(javaClass(), "Replace '!!' expression with 'if' expression") { + override fun isApplicableTo(element: JetPostfixExpression, caretOffset: Int): Boolean { + return element.getOperationToken() == JetTokens.EXCLEXCL && element.getOperationReference().getTextRange().containsOffset(caretOffset) + } override fun applyTo(element: JetPostfixExpression, editor: Editor) { - val base = checkNotNull(JetPsiUtil.deparenthesize(element.getBaseExpression()), "Base expression cannot be null") + val base = JetPsiUtil.safeDeparenthesize(element.getBaseExpression()) val expressionText = formatForUseInExceptionArgument(base.getText()!!) - val defaultException = JetPsiFactory(element).createExpression("throw $NULL_PTR_EXCEPTION()") + val defaultException = JetPsiFactory(element).createExpression("throw NullPointerException()") val isStatement = element.isStatement() val isStable = base.isStableVariable() @@ -55,19 +54,18 @@ public class DoubleBangToIfThenIntention : JetSelfTargetingOffsetIndependentInte val thrownExpression = ((if (isStatement) ifStatement.getThen() else ifStatement.getElse()) as JetThrowExpression).getThrownExpression()!! - val nullPtrExceptionText = "$NULL_PTR_EXCEPTION(\"%s\")".format(escapeJava(JetBundle.message("double.bang.to.if.then.exception.text", expressionText))) - val kotlinNullPtrExceptionText = "$KOTLIN_NULL_PTR_EXCEPTION()" + val message = escapeJava("Expression '$expressionText' must not be null") + val nullPtrExceptionText = "NullPointerException(\"$message\")" + val kotlinNullPtrExceptionText = "KotlinNullPointerException()" val exceptionLookupExpression = object: JetTypeLookupExpression(listOf(nullPtrExceptionText, kotlinNullPtrExceptionText), - nullPtrExceptionText, JetBundle.message("double.bang.to.if.then.choose.exception")) { - + nullPtrExceptionText) { override fun getLookupString(element: String) = element override fun getResult(element: String) = element } val project = element.getProject() - val manager = TemplateManagerImpl(project) val builder = TemplateBuilderImpl(thrownExpression) builder.replaceElement(thrownExpression, exceptionLookupExpression); @@ -75,7 +73,7 @@ public class DoubleBangToIfThenIntention : JetSelfTargetingOffsetIndependentInte PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument()); editor.getCaretModel().moveToOffset(thrownExpression.getNode()!!.getStartOffset()); - manager.startTemplate(editor, builder.buildInlineTemplate()!!, object: TemplateEditingAdapter() { + TemplateManager.getInstance(project).startTemplate(editor, builder.buildInlineTemplate(), object: TemplateEditingAdapter() { override fun templateFinished(template: Template?, brokenOff: Boolean) { if (!isStable && !isStatement) { ifStatement.introduceValueForCondition(ifStatement.getThen()!!, editor) @@ -84,10 +82,9 @@ public class DoubleBangToIfThenIntention : JetSelfTargetingOffsetIndependentInte }) } - fun formatForUseInExceptionArgument(expressionText: String): String { + private fun formatForUseInExceptionArgument(expressionText: String): String { val lines = expressionText.split('\n') - - return if (lines.size > 1) + return if (lines.size() > 1) lines.first().trim() + " ..." else expressionText.trim()