IDEA-178933 Complete current statement for if statement without opening bracket

This commit is contained in:
peter
2017-09-15 22:04:10 +02:00
parent 1c0b37c8cd
commit 8561e8cb19
4 changed files with 24 additions and 0 deletions
@@ -18,6 +18,7 @@ package com.intellij.codeInsight.editorActions.smartEnter;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
/**
@@ -53,5 +54,13 @@ public class IfConditionFixer implements Fixer {
doc.insertString(condition.getTextRange().getEndOffset(), ")");
}
}
else if (psiElement instanceof PsiExpression && psiElement.getParent() instanceof PsiExpressionStatement) {
PsiElement prevLeaf = PsiTreeUtil.prevVisibleLeaf(psiElement);
if (prevLeaf != null && prevLeaf.textMatches(PsiKeyword.IF)) {
Document doc = editor.getDocument();
doc.insertString(psiElement.getTextRange().getEndOffset(), ")");
doc.insertString(psiElement.getTextRange().getStartOffset(), "(");
}
}
}
}
@@ -0,0 +1,6 @@
class Foo {
public void foo() {
if 1 > 2<caret>
}
}
@@ -0,0 +1,8 @@
class Foo {
public void foo() {
if (1 > 2) {
<caret>
}
}
}
@@ -84,6 +84,7 @@ public class CompleteStatementTest extends EditorActionTestCase {
public void testForIncrementExpressionAndBody() { doTest(); }
public void testEmptyBeforeReturn() { doTest(); }
public void testIf() { doTest(); }
public void testIfWithoutParentheses() { doTest(); }
public void testBeforeStatement() { doTest(); }
public void testTry1() { doTest(); }
public void testInsideResourceVariable() { doTest(); }