diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/TryStatementPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/TryStatementPostfixTemplate.java index 8ed37ceb01a4..0044c599a851 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/TryStatementPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/TryStatementPostfixTemplate.java @@ -16,37 +16,41 @@ package com.intellij.codeInsight.template.postfix.templates; import com.intellij.codeInsight.generation.surroundWith.JavaWithTryCatchSurrounder; +import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.TextRange; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.psi.util.PsiUtil; import com.intellij.util.ArrayUtil; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import static com.intellij.codeInsight.template.postfix.templates.PostfixTemplatesUtils.selectorTopmost; -import static com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils.JAVA_PSI_INFO; - -public class TryStatementPostfixTemplate extends PostfixTemplateWithExpressionSelector { - - public static Condition HAS_TYPE = new Condition() { - @Override - public boolean value(@Nullable PsiElement element) { - return element instanceof PsiExpression && ((PsiExpression)element).getType() != null; - } - }; +public class TryStatementPostfixTemplate extends PostfixTemplate { protected TryStatementPostfixTemplate() { - super("try", "try { exp } catch(Exception e)", JAVA_PSI_INFO, selectorTopmost(HAS_TYPE)); + super("try", "try { exp } catch(Exception e)"); } + @Override + public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) { + PsiStatement statementParent = PsiTreeUtil.getNonStrictParentOfType(context, PsiStatement.class); + if (statementParent == null || + newOffset != statementParent.getTextRange().getEndOffset()) return false; + + if (statementParent instanceof PsiDeclarationStatement) return true; + + if (statementParent instanceof PsiExpressionStatement) { + PsiExpression expression = ((PsiExpressionStatement)statementParent).getExpression(); + return null != expression.getType(); + } + + return false; + } @Override - public void expandForChooseExpression(@NotNull PsiElement context, @NotNull Editor editor) { - PsiExpression expr = (PsiExpression)context; - PsiStatement statement = PsiTreeUtil.getParentOfType(expr, PsiStatement.class, false); + public void expand(@NotNull PsiElement context, @NotNull Editor editor) { + PsiStatement statement = PsiTreeUtil.getNonStrictParentOfType(context, PsiStatement.class); assert statement != null; PsiFile file = statement.getContainingFile(); diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/constructorStatement.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/constructorStatement.java new file mode 100644 index 000000000000..05edd3342c64 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/constructorStatement.java @@ -0,0 +1,5 @@ +public class Foo { + void m() { + new Object().try + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/constructorStatement_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/constructorStatement_after.java new file mode 100644 index 000000000000..807cf33709ec --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/constructorStatement_after.java @@ -0,0 +1,9 @@ +public class Foo { + void m() { + try { + new Object() + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/declarationStatement.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/declarationStatement.java new file mode 100644 index 000000000000..ae6f5b02395d --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/declarationStatement.java @@ -0,0 +1,5 @@ +public class Foo { + void m() { + Object obj = new Object().try + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/declarationStatement_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/declarationStatement_after.java new file mode 100644 index 000000000000..c6c37844fc43 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/declarationStatement_after.java @@ -0,0 +1,9 @@ +public class Foo { + void m() { + try { + Object obj = new Object() + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/expressionInMethodBody.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/expressionInMethodBody.java new file mode 100644 index 000000000000..ab9cacffb71f --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/expressionInMethodBody.java @@ -0,0 +1,9 @@ +public class Foo { + void m() { + doAct() + "aaa".try + } + + String doAct() { + return null; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/expressionInMethodBody_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/expressionInMethodBody_after.java new file mode 100644 index 000000000000..ef09a53f103e --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/expressionInMethodBody_after.java @@ -0,0 +1,13 @@ +public class Foo { + void m() { + try { + doAct() + "aaa" + } catch (Exception e) { + e.printStackTrace(); + } + } + + String doAct() { + return null; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/incompleteStatement.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/incompleteStatement.java new file mode 100644 index 000000000000..b12d1eaeb327 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/incompleteStatement.java @@ -0,0 +1,11 @@ +import java.lang.Exception; + +public class Foo { + void m() { + methodCall(.try + } + + void methodCall(String s) { + + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/incompleteStatement_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/incompleteStatement_after.java new file mode 100644 index 000000000000..8e1763f0f221 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/incompleteStatement_after.java @@ -0,0 +1,11 @@ +import java.lang.Exception; + +public class Foo { + void m() { + methodCall(.try + } + + void methodCall(String s) { + + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/simpleWithThrowsCheckedException.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/simpleWithThrowsCheckedException.java new file mode 100644 index 000000000000..a28a0e66b63d --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/simpleWithThrowsCheckedException.java @@ -0,0 +1,9 @@ +import java.io.IOException; + +public class Foo { + void m() { + doAct().try + } + + void doAct() throws IOException {} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/simpleWithThrowsCheckedException_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/simpleWithThrowsCheckedException_after.java new file mode 100644 index 000000000000..5c893da1d0be --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/simpleWithThrowsCheckedException_after.java @@ -0,0 +1,13 @@ +import java.io.IOException; + +public class Foo { + void m() { + try { + doAct() + } catch (IOException e) { + e.printStackTrace(); + } + } + + void doAct() throws IOException {} +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/TryPostfixTemplateTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/TryPostfixTemplateTest.java index e17d479f0fd5..1b2e9d62c013 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/TryPostfixTemplateTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/TryPostfixTemplateTest.java @@ -39,4 +39,24 @@ public class TryPostfixTemplateTest extends PostfixTemplateTestCase { public void testNotResolvedExpression() { doTest(); } + + public void testDeclarationStatement() { + doTest(); + } + + public void testExpressionInMethodBody() { + doTest(); + } + + public void testSimpleWithThrowsCheckedException() { + doTest(); + } + + public void testIncompleteStatement() { + doTest(); + } + + public void testConstructorStatement() { + doTest(); + } }