mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
postfix template: reduce scope of 'try' IDEA-132878
This commit is contained in:
+17
-9
@@ -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<PsiElement> HAS_TYPE = new Condition<PsiElement>() {
|
||||
@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();
|
||||
|
||||
+3
-1
@@ -1,7 +1,9 @@
|
||||
public class Foo {
|
||||
void m() {
|
||||
somevalue.try<caret>
|
||||
doAct().try<caret>
|
||||
int i=0;
|
||||
|
||||
}
|
||||
|
||||
void doAct() {}
|
||||
}
|
||||
+3
-1
@@ -1,11 +1,13 @@
|
||||
public class Foo {
|
||||
void m() {
|
||||
try {
|
||||
somevalue<caret>
|
||||
doAct()
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
int i=0;
|
||||
|
||||
}
|
||||
|
||||
void doAct() {}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
public class Foo {
|
||||
void m() {
|
||||
aaaaaa.try<caret>
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
public class Foo {
|
||||
void m() {
|
||||
aaaaaa.try <caret>
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
public class Foo {
|
||||
void m() {
|
||||
somevalue.try<caret>
|
||||
doAct().try<caret>
|
||||
}
|
||||
|
||||
void doAct() {}
|
||||
}
|
||||
+3
-1
@@ -1,9 +1,11 @@
|
||||
public class Foo {
|
||||
void m() {
|
||||
try {
|
||||
somevalue<caret>
|
||||
doAct()
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
void doAct() {}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
public class Foo {
|
||||
void m() {
|
||||
Object o = new Object().try<caret>
|
||||
}
|
||||
}
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
public class Foo {
|
||||
void m() {
|
||||
try {
|
||||
Object o = new Object()<caret>
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user