Fixed EA-57220.

This commit is contained in:
Evgeny Gerashchenko
2014-10-10 23:17:42 +04:00
parent b653598df2
commit fbcfed6757
7 changed files with 35 additions and 2 deletions
@@ -818,7 +818,7 @@ public class JetPsiUtil {
}
@Nullable
public static JetElement getEnclosingElementForLocalDeclaration(@Nullable JetDeclaration declaration) {
public static JetElement getEnclosingElementForLocalDeclaration(@NotNull JetDeclaration declaration) {
if (declaration instanceof JetTypeParameter) {
declaration = PsiTreeUtil.getParentOfType(declaration, JetNamedDeclaration.class);
}
@@ -43,6 +43,7 @@ import org.jetbrains.jet.lang.psi.JetCallExpression
import org.jetbrains.jet.lang.resolve.DescriptorUtils
import org.jetbrains.jet.lang.psi.JetElement
import org.jetbrains.jet.lang.resolve.bindingContextUtil.isUsedAsStatement
import org.jetbrains.jet.lang.psi.JetProperty
val NULL_PTR_EXCEPTION = "NullPointerException"
val NULL_PTR_EXCEPTION_FQ = "java.lang.NullPointerException"
@@ -141,6 +142,8 @@ fun JetElement.replace(expressionAsString: String): PsiElement =
fun JetSimpleNameExpression.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor: Editor) {
val declaration = this.getReference()?.resolve() as JetDeclaration
if (declaration !is JetProperty) return
val enclosingElement = JetPsiUtil.getEnclosingElementForLocalDeclaration(declaration)
val isLocal = enclosingElement != null
if (!isLocal) return
@@ -80,7 +80,7 @@ public class IfThenToElvisIntention : JetSelfTargetingIntention<JetIfExpression>
assert(resultingExpression is JetBinaryExpression,
"Unexpected expression type: ${resultingExpression?.javaClass}, expected JetBinaryExpression, element = '${element.getText()}'")
val elvis= resultingExpression as JetBinaryExpression
val elvis = resultingExpression as JetBinaryExpression
elvis.inlineLeftSideIfApplicableWithPrompt(editor)
}
}
@@ -0,0 +1,8 @@
// "Replace 'if' expression with elvis expression" "true"
data class IntPair(val first: Int?, val second: Int?)
fun f(pair: IntPair): Int {
val (x, y) = pair
return if (x != <caret>null) x else 5
}
@@ -0,0 +1,8 @@
// "Replace 'if' expression with elvis expression" "true"
data class IntPair(val first: Int?, val second: Int?)
fun f(pair: IntPair): Int {
val (x, y) = pair
return x ?: 5
}
@@ -79,6 +79,14 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?:'.</problem_class>
<description>Replace 'if' expression with elvis expression</description>
</problem>
<problem>
<file>doesNotInlineVariableInMultiDeclaration.kt</file>
<line>7</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/src/doesNotInlineVariableInMultiDeclaration.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?:'.</problem_class>
<description>Replace 'if' expression with elvis expression</description>
</problem>
<problem>
<file>doesNotinlineValueIfUsedMoreThanOnce.kt</file>
<line>11</line>
@@ -1228,6 +1228,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("doesNotInlineVariableInMultiDeclaration.kt")
public void testDoesNotInlineVariableInMultiDeclaration() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/doesNotInlineVariableInMultiDeclaration.kt");
doTest(fileName);
}
@TestMetadata("doesNotinlineValueIfUsedMoreThanOnce.kt")
public void testDoesNotinlineValueIfUsedMoreThanOnce() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/doesNotinlineValueIfUsedMoreThanOnce.kt");