From fffcd492d5563abd375fa71543c94e4046739044 Mon Sep 17 00:00:00 2001 From: Andrey Starovoyt Date: Wed, 16 Apr 2014 14:43:34 +0400 Subject: [PATCH] Add "try" live postfix template --- .../TryStatementPostfixTemplate.java | 66 +++++++++++++++++++ .../postfix/templates/try/multiStatement.java | 7 ++ .../templates/try/multiStatement_after.java | 11 ++++ .../postfix/templates/try/notStatement.java | 3 + .../templates/try/notStatement_after.java | 3 + .../postfix/templates/try/simple.java | 5 ++ .../postfix/templates/try/simple_after.java | 9 +++ .../postfix/templates/try/statement.java | 5 ++ .../templates/try/statement_after.java | 9 +++ .../templates/TryPostfixTemplateTest.java | 44 +++++++++++++ resources/src/META-INF/PostfixTemplates.xml | 1 + 11 files changed, 163 insertions(+) create mode 100644 java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/TryStatementPostfixTemplate.java create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/try/multiStatement.java create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/try/multiStatement_after.java create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/try/notStatement.java create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/try/notStatement_after.java create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/try/simple.java create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/try/simple_after.java create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/try/statement.java create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/try/statement_after.java create mode 100644 java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/TryPostfixTemplateTest.java 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 new file mode 100644 index 000000000000..f14754f30270 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/TryStatementPostfixTemplate.java @@ -0,0 +1,66 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.template.postfix.templates; + +import com.intellij.codeInsight.generation.surroundWith.JavaWithTryCatchSurrounder; +import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +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; + +public class TryStatementPostfixTemplate extends PostfixTemplate { + + protected TryStatementPostfixTemplate() { + super("try", "Insert statement in try-catch block", "try { exp } catch (Exception e) { e.printStackTrace(); }"); + } + + @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); + assert statement != null; + + PsiFile file = statement.getContainingFile(); + Project project = context.getProject(); + + JavaWithTryCatchSurrounder surrounder = new JavaWithTryCatchSurrounder(); + TextRange range = surrounder.surroundElements(project, editor, new PsiElement[]{statement}); + + if (range == null) { + PostfixTemplatesUtils.showErrorHint(project, editor); + return; + } + + PsiElement element = file.findElementAt(range.getStartOffset()); + PsiTryStatement tryStatement = PsiTreeUtil.getParentOfType(element, PsiTryStatement.class); + assert tryStatement != null; + PsiCodeBlock block = tryStatement.getTryBlock(); + assert block != null; + PsiStatement statementInTry = ArrayUtil.getFirstElement(block.getStatements()); + if (null != statementInTry) { + editor.getCaretModel().moveToOffset(statementInTry.getTextRange().getEndOffset()); + } + } +} 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 new file mode 100644 index 000000000000..6b6447294b3f --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/multiStatement.java @@ -0,0 +1,7 @@ +public class Foo { + void m() { + somevalue.try + int i=0; + + } +} \ 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 new file mode 100644 index 000000000000..cf2395d563c8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/multiStatement_after.java @@ -0,0 +1,11 @@ +public class Foo { + void m() { + try { + somevalue + } catch (Exception e) { + e.printStackTrace(); + } + int i=0; + + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/notStatement.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/notStatement.java new file mode 100644 index 000000000000..bec0827189df --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/notStatement.java @@ -0,0 +1,3 @@ +public class Foo.try { + +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/try/notStatement_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/try/notStatement_after.java new file mode 100644 index 000000000000..59c9b045004a --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/notStatement_after.java @@ -0,0 +1,3 @@ +public class Foo.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 new file mode 100644 index 000000000000..d77e002333b8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/simple.java @@ -0,0 +1,5 @@ +public class Foo { + void m() { + somevalue.try + } +} \ 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 new file mode 100644 index 000000000000..bf0347afd14c --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/simple_after.java @@ -0,0 +1,9 @@ +public class Foo { + void m() { + try { + somevalue + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ 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 new file mode 100644 index 000000000000..e68cb699ddb3 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/statement.java @@ -0,0 +1,5 @@ +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 new file mode 100644 index 000000000000..41f8cc1c0885 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/try/statement_after.java @@ -0,0 +1,9 @@ +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 new file mode 100644 index 000000000000..7183883cbe3f --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/TryPostfixTemplateTest.java @@ -0,0 +1,44 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.template.postfix.templates; + +import org.jetbrains.annotations.NotNull; + + +public class TryPostfixTemplateTest extends PostfixTemplateTestCase { + + @NotNull + @Override + protected String getSuffix() { + return "try"; + } + + public void testSimple() { + doTest(); + } + + public void testStatement() { + doTest(); + } + + public void testMultiStatement() { + doTest(); + } + + public void testNotStatement() { + doTest(); + } +} diff --git a/resources/src/META-INF/PostfixTemplates.xml b/resources/src/META-INF/PostfixTemplates.xml index 91505d6149d1..af5d57ecef32 100644 --- a/resources/src/META-INF/PostfixTemplates.xml +++ b/resources/src/META-INF/PostfixTemplates.xml @@ -25,6 +25,7 @@ +