mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-21 05:51:25 +07:00
IDEA-127512 Ctrl+Shift+Enter produces invalid code for catch statements
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
|
||||
class Foo {
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
} catch (Ex<caret>ception e)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
|
||||
class Foo {
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
} catch (Exception e)
|
||||
{
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user