IDEA-127512 Ctrl+Shift+Enter produces invalid code for catch statements

This commit is contained in:
peter
2014-07-28 10:11:37 +02:00
parent 4d8d3b0721
commit a24e62f28d
5 changed files with 53 additions and 8 deletions

View File

@@ -36,7 +36,7 @@ public class MissingCatchBodyFixer implements Fixer {
final Document doc = editor.getDocument();
PsiCodeBlock body = catchSection.getCatchBlock();
if (body != null && startLine(doc, body) == startLine(doc, catchSection)) return;
if (body != null && body.getLBrace() != null && body.getRBrace() != null) return;
final PsiJavaToken rParenth = catchSection.getRParenth();
if (rParenth == null) return;

View File

@@ -83,6 +83,22 @@ public class PlainEnterProcessor implements EnterProcessor {
@Nullable
private static PsiCodeBlock getControlStatementBlock(int caret, PsiElement element) {
if (element instanceof PsiTryStatement) {
PsiCodeBlock tryBlock = ((PsiTryStatement)element).getTryBlock();
if (tryBlock != null && caret < tryBlock.getTextRange().getEndOffset()) return tryBlock;
for (PsiCodeBlock catchBlock : ((PsiTryStatement)element).getCatchBlocks()) {
if (catchBlock != null && caret < catchBlock.getTextRange().getEndOffset()) return catchBlock;
}
return ((PsiTryStatement)element).getFinallyBlock();
}
if (element instanceof PsiMethod) {
PsiCodeBlock methodBody = ((PsiMethod)element).getBody();
if (methodBody != null) return methodBody;
}
PsiStatement body = null;
if (element instanceof PsiIfStatement) {
body = ((PsiIfStatement)element).getThenBranch();
@@ -102,10 +118,6 @@ public class PlainEnterProcessor implements EnterProcessor {
else if (element instanceof PsiDoWhileStatement) {
body = ((PsiDoWhileStatement)element).getBody();
}
else if (element instanceof PsiMethod) {
PsiCodeBlock methodBody = ((PsiMethod)element).getBody();
if (methodBody != null) return methodBody;
}
return body instanceof PsiBlockStatement ? ((PsiBlockStatement)body).getCodeBlock() : null;
}

View File

@@ -0,0 +1,12 @@
class Foo {
{
try
{
} catch (Ex<caret>ception e)
{
}
}
}

View File

@@ -0,0 +1,12 @@
class Foo {
{
try
{
} catch (Exception e)
{
<caret>
}
}
}

View File

@@ -17,8 +17,6 @@ package com.intellij.codeInsight;
import com.intellij.JavaTestUtil;
import com.intellij.openapi.actionSystem.IdeActions;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
@@ -54,6 +52,18 @@ public class CompleteStatementTest extends EditorActionTestCase {
public void testCompleteCatchLParen() throws Exception { doTest(); }
public void testAlreadyCompleteCatch() throws Exception {
CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
int old = settings.BRACE_STYLE;
settings.BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;
try {
doTest();
}
finally {
settings.BRACE_STYLE = old;
}
}
public void testCompleteCatchWithExpression() throws Exception { doTest(); }
public void testCompleteCatchBody() throws Exception { doTest(); }
@@ -172,7 +182,6 @@ public class CompleteStatementTest extends EditorActionTestCase {
public void testSCR36110() throws Exception {
JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
LanguageLevel old = LanguageLevelProjectExtension.getInstance(facade.getProject()).getLanguageLevel();
doTest();
}