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 71d181890383..8ed37ceb01a4 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,29 +16,37 @@ 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.util.ArrayUtil; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -public class TryStatementPostfixTemplate extends PostfixTemplate { +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; + } + }; protected TryStatementPostfixTemplate() { - super("try", "try { exp } catch(Exception e)"); + super("try", "try { exp } catch(Exception e)", JAVA_PSI_INFO, selectorTopmost(HAS_TYPE)); } - @Override - public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) { - return null != PsiTreeUtil.getNonStrictParentOfType(context, PsiStatement.class); - } @Override - public void expand(@NotNull PsiElement context, @NotNull Editor editor) { - PsiStatement statement = PsiTreeUtil.getNonStrictParentOfType(context, PsiStatement.class); + public void expandForChooseExpression(@NotNull PsiElement context, @NotNull Editor editor) { + PsiExpression expr = (PsiExpression)context; + PsiStatement statement = PsiTreeUtil.getParentOfType(expr, PsiStatement.class, false); assert statement != null; PsiFile file = statement.getContainingFile(); diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/multiStatement.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/multiStatement.java index 6b6447294b3f..560181d60d49 100644 --- a/java/java-tests/testData/codeInsight/template/postfix/templates/try/multiStatement.java +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/multiStatement.java @@ -1,7 +1,9 @@ public class Foo { void m() { - somevalue.try + doAct().try int i=0; } + + void doAct() {} } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/multiStatement_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/multiStatement_after.java index cf2395d563c8..9a4c8bc99ca9 100644 --- a/java/java-tests/testData/codeInsight/template/postfix/templates/try/multiStatement_after.java +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/multiStatement_after.java @@ -1,11 +1,13 @@ public class Foo { void m() { try { - somevalue + doAct() } catch (Exception e) { e.printStackTrace(); } int i=0; } + + void doAct() {} } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/notResolvedExpression.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/notResolvedExpression.java new file mode 100644 index 000000000000..48ff75501bd6 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/notResolvedExpression.java @@ -0,0 +1,5 @@ +public class Foo { + void m() { + aaaaaa.try + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/notResolvedExpression_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/notResolvedExpression_after.java new file mode 100644 index 000000000000..2cc82611a66e --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/notResolvedExpression_after.java @@ -0,0 +1,5 @@ +public class Foo { + void m() { + aaaaaa.try + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/simple.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/simple.java index d77e002333b8..8aed4ea474b0 100644 --- a/java/java-tests/testData/codeInsight/template/postfix/templates/try/simple.java +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/simple.java @@ -1,5 +1,7 @@ public class Foo { void m() { - somevalue.try + doAct().try } + + void doAct() {} } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/simple_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/simple_after.java index bf0347afd14c..e5b600e730c0 100644 --- a/java/java-tests/testData/codeInsight/template/postfix/templates/try/simple_after.java +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/simple_after.java @@ -1,9 +1,11 @@ public class Foo { void m() { try { - somevalue + doAct() } catch (Exception e) { e.printStackTrace(); } } + + void doAct() {} } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/statement.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/statement.java deleted file mode 100644 index e68cb699ddb3..000000000000 --- a/java/java-tests/testData/codeInsight/template/postfix/templates/try/statement.java +++ /dev/null @@ -1,5 +0,0 @@ -public class Foo { - void m() { - Object o = new Object().try - } -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/statement_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/statement_after.java deleted file mode 100644 index 41f8cc1c0885..000000000000 --- a/java/java-tests/testData/codeInsight/template/postfix/templates/try/statement_after.java +++ /dev/null @@ -1,9 +0,0 @@ -public class Foo { - void m() { - try { - Object o = new Object() - } catch (Exception e) { - e.printStackTrace(); - } - } -} \ 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 71a300449c78..e17d479f0fd5 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 @@ -28,10 +28,6 @@ public class TryPostfixTemplateTest extends PostfixTemplateTestCase { doTest(); } - public void testStatement() { - doTest(); - } - public void testMultiStatement() { doTest(); } @@ -39,4 +35,8 @@ public class TryPostfixTemplateTest extends PostfixTemplateTestCase { public void testNotStatement() { doTest(); } + + public void testNotResolvedExpression() { + doTest(); + } }