KT-4980 'Remove braces' intention throws NPE for a for-loop with variable declaration

#KT-4980 fixed
This commit is contained in:
Evgeny Gerashchenko
2014-07-08 11:40:11 +04:00
parent 86d2565700
commit 55ed0471b6
4 changed files with 19 additions and 2 deletions
@@ -28,6 +28,9 @@ import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.PsiComment
import org.jetbrains.jet.lang.psi.JetExpression
import org.jetbrains.jet.plugin.intentions.declarations.JetDeclarationJoinLinesHandler
import org.jetbrains.jet.lang.psi.JetProperty
public class RemoveBracesIntention : JetSelfTargetingIntention<JetExpressionImpl>("remove.braces", javaClass()) {
override fun isApplicableTo(element: JetExpressionImpl): Boolean {
@@ -56,10 +59,11 @@ public class RemoveBracesIntention : JetSelfTargetingIntention<JetExpressionImpl
handleComments(element, jetBlockElement)
val newElement = jetBlockElement.replace(JetPsiFactory.createExpression(element.getProject(), firstStatement.getText()))
val project = element.getProject()
val newElement = jetBlockElement.replace(firstStatement.copy())
if (expressionKind == ExpressionKind.DOWHILE) {
newElement.getParent()!!.addAfter(JetPsiFactory.createNewLine(element.getProject()), newElement)
newElement.getParent()!!.addAfter(JetPsiFactory.createNewLine(project), newElement)
}
}
@@ -0,0 +1,5 @@
fun foo(a: List<Int>) {
for<caret> (x in a) {
val y = x
}
}
@@ -0,0 +1,3 @@
fun foo(a: List<Int>) {
for<caret> (x in a) val y = x
}
@@ -3883,6 +3883,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/intentions/removeBraces"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("forWithLocalVariable.kt")
public void testForWithLocalVariable() throws Exception {
doTest("idea/testData/intentions/removeBraces/forWithLocalVariable.kt");
}
@TestMetadata("removeBracesForDoWhile.kt")
public void testRemoveBracesForDoWhile() throws Exception {
doTest("idea/testData/intentions/removeBraces/removeBracesForDoWhile.kt");