mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
reduce scope of try postfix template (2) after review IDEA-132878
This commit is contained in:
+21
-17
@@ -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<PsiElement> HAS_TYPE = new Condition<PsiElement>() {
|
||||
@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();
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
public class Foo {
|
||||
void m() {
|
||||
new Object().try<caret>
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
public class Foo {
|
||||
void m() {
|
||||
try {
|
||||
new Object()
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
public class Foo {
|
||||
void m() {
|
||||
Object obj = new Object().try<caret>
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
public class Foo {
|
||||
void m() {
|
||||
try {
|
||||
Object obj = new Object()<caret>
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
public class Foo {
|
||||
void m() {
|
||||
doAct() + "aaa".try<caret>
|
||||
}
|
||||
|
||||
String doAct() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
public class Foo {
|
||||
void m() {
|
||||
try {
|
||||
doAct() + "aaa"
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
String doAct() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import java.lang.Exception;
|
||||
|
||||
public class Foo {
|
||||
void m() {
|
||||
methodCall(.try<caret>
|
||||
}
|
||||
|
||||
void methodCall(String s) {
|
||||
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import java.lang.Exception;
|
||||
|
||||
public class Foo {
|
||||
void m() {
|
||||
methodCall(.try <caret>
|
||||
}
|
||||
|
||||
void methodCall(String s) {
|
||||
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
import java.io.IOException;
|
||||
|
||||
public class Foo {
|
||||
void m() {
|
||||
doAct().try<caret>
|
||||
}
|
||||
|
||||
void doAct() throws IOException {}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
import java.io.IOException;
|
||||
|
||||
public class Foo {
|
||||
void m() {
|
||||
try {
|
||||
doAct()<caret>
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
void doAct() throws IOException {}
|
||||
}
|
||||
+20
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user