diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithCastSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithCastSurrounder.java index 7bd6e45ba5b8..c2fe98a0e42f 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithCastSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithCastSurrounder.java @@ -33,7 +33,7 @@ import org.jetbrains.annotations.NonNls; import java.util.LinkedHashSet; import java.util.Set; -class JavaWithCastSurrounder extends JavaExpressionSurrounder { +public class JavaWithCastSurrounder extends JavaExpressionSurrounder { @NonNls private static final String TYPE_TEMPLATE_VARIABLE = "type"; @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfExpressionSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfExpressionSurrounder.java index bcfd4d04472e..b3ffb315b52d 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfExpressionSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfExpressionSurrounder.java @@ -27,7 +27,7 @@ import com.intellij.util.IncorrectOperationException; import com.intellij.psi.util.FileTypeUtils; import org.jetbrains.annotations.NonNls; -class JavaWithIfExpressionSurrounder extends JavaExpressionSurrounder{ +public class JavaWithIfExpressionSurrounder extends JavaExpressionSurrounder{ @Override public boolean isApplicable(PsiExpression expr) { PsiType type = expr.getType(); diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/AssertStatementPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/AssertStatementPostfixTemplate.java index 467082e9cedd..daa4da26a4f4 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/AssertStatementPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/AssertStatementPostfixTemplate.java @@ -1,6 +1,6 @@ package com.intellij.codeInsight.template.postfix.templates; -import com.intellij.codeInsight.template.postfix.util.CommonUtils; +import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils; import com.intellij.openapi.editor.Editor; import com.intellij.psi.PsiElement; import org.jetbrains.annotations.NotNull; @@ -12,6 +12,6 @@ public class AssertStatementPostfixTemplate extends BooleanPostfixTemplate { @Override public void expand(@NotNull PsiElement context, @NotNull Editor editor) { - CommonUtils.createSimpleStatement(context, editor, "assert"); + PostfixTemplatesUtils.createSimpleStatement(context, editor, "assert"); } } \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/BooleanPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/BooleanPostfixTemplate.java index 5839b72ef790..0f810fd90dc0 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/BooleanPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/BooleanPostfixTemplate.java @@ -1,6 +1,6 @@ package com.intellij.codeInsight.template.postfix.templates; -import com.intellij.codeInsight.template.postfix.util.CommonUtils; +import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils; import com.intellij.openapi.editor.Document; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiExpression; @@ -17,6 +17,6 @@ abstract public class BooleanPostfixTemplate extends PostfixTemplate { PsiExpression topmostExpression = getTopmostExpression(context); return topmostExpression != null && topmostExpression.getParent() instanceof PsiExpressionStatement && - CommonUtils.isBoolean(topmostExpression.getType()); + PostfixTemplatesUtils.isBoolean(topmostExpression.getType()); } } diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/CastExpressionPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/CastExpressionPostfixTemplate.java index 70416845541e..414b79ce0b20 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/CastExpressionPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/CastExpressionPostfixTemplate.java @@ -1,6 +1,7 @@ package com.intellij.codeInsight.template.postfix.templates; -import com.intellij.codeInsight.template.postfix.util.JavaSurroundersProxy; +import com.intellij.codeInsight.generation.surroundWith.JavaWithCastSurrounder; +import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils; import com.intellij.openapi.editor.Editor; import com.intellij.psi.PsiExpression; import org.jetbrains.annotations.NotNull; @@ -12,6 +13,6 @@ public class CastExpressionPostfixTemplate extends ExpressionPostfixTemplateWith @Override protected void doIt(@NotNull final Editor editor, @NotNull final PsiExpression expression) { - JavaSurroundersProxy.cast(expression.getProject(), editor, expression); + PostfixTemplatesUtils.apply(new JavaWithCastSurrounder(), expression.getProject(), editor, expression); } } \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ElseStatementPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ElseStatementPostfixTemplate.java index 7bc81a8351a7..37187455f40c 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ElseStatementPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ElseStatementPostfixTemplate.java @@ -1,7 +1,7 @@ package com.intellij.codeInsight.template.postfix.templates; import com.intellij.codeInsight.CodeInsightServicesUtil; -import com.intellij.codeInsight.template.postfix.util.JavaSurroundersProxy; +import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; @@ -18,7 +18,7 @@ public class ElseStatementPostfixTemplate extends BooleanPostfixTemplate { PsiExpression expression = getTopmostExpression(context); assert expression != null; PsiExpression invertedExpression = (PsiExpression)expression.replace(CodeInsightServicesUtil.invertCondition(expression)); - TextRange range = JavaSurroundersProxy.ifStatement(invertedExpression.getProject(), editor, invertedExpression); + TextRange range = PostfixTemplatesUtils.ifStatement(invertedExpression.getProject(), editor, invertedExpression); if (range != null) { editor.getCaretModel().moveToOffset(range.getStartOffset()); } diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ExpressionPostfixTemplateWithChooser.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ExpressionPostfixTemplateWithChooser.java index 996d7259704a..bd55ee8b3d96 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ExpressionPostfixTemplateWithChooser.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ExpressionPostfixTemplateWithChooser.java @@ -1,6 +1,6 @@ package com.intellij.codeInsight.template.postfix.templates; -import com.intellij.codeInsight.template.postfix.util.CommonUtils; +import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils; import com.intellij.codeInsight.unwrap.ScopeHighlighter; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.command.CommandProcessor; @@ -46,7 +46,7 @@ public abstract class ExpressionPostfixTemplateWithChooser extends PostfixTempla List expressions = getExpressions(context, editor, editor.getCaretModel().getOffset()); if (expressions.isEmpty()) { - CommonUtils.showErrorHint(context.getProject(), editor); + PostfixTemplatesUtils.showErrorHint(context.getProject(), editor); } else if (expressions.size() == 1) { doIt(editor, expressions.get(0)); diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForDescendingPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForDescendingPostfixTemplate.java index 81c9d7240fba..33888a5920e9 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForDescendingPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForDescendingPostfixTemplate.java @@ -1,6 +1,6 @@ package com.intellij.codeInsight.template.postfix.templates; -import com.intellij.codeInsight.template.postfix.util.CommonUtils; +import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils; import com.intellij.openapi.util.Pair; import com.intellij.psi.PsiExpression; import org.jetbrains.annotations.NotNull; @@ -20,7 +20,7 @@ public class ForDescendingPostfixTemplate extends ForIndexedPostfixTemplate { @NotNull @Override protected String getComparativeSign(@NotNull PsiExpression expr) { - return CommonUtils.isNumber(expr.getType()) ? ">" : ">="; + return PostfixTemplatesUtils.isNumber(expr.getType()) ? ">" : ">="; } @Nullable @@ -30,7 +30,7 @@ public class ForDescendingPostfixTemplate extends ForIndexedPostfixTemplate { if (bound == null) { return null; } - return CommonUtils.isNumber(expression.getType()) + return PostfixTemplatesUtils.isNumber(expression.getType()) ? Pair.create(bound, "0") : Pair.create(bound + " - 1", "0"); } diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForIndexedPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForIndexedPostfixTemplate.java index cb5bf140eee9..8aa38242e347 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForIndexedPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForIndexedPostfixTemplate.java @@ -4,7 +4,7 @@ import com.intellij.codeInsight.template.Template; import com.intellij.codeInsight.template.TemplateManager; import com.intellij.codeInsight.template.impl.MacroCallNode; import com.intellij.codeInsight.template.macro.SuggestVariableNameMacro; -import com.intellij.codeInsight.template.postfix.util.CommonUtils; +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; @@ -25,20 +25,21 @@ public abstract class ForIndexedPostfixTemplate extends PostfixTemplate { public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) { PsiExpression expr = getTopmostExpression(context); if (expr == null || !(expr.getParent() instanceof PsiExpressionStatement)) return false; - return CommonUtils.isNumber(expr.getType()) || CommonUtils.isArray(expr.getType()) || CommonUtils.isIterable(expr.getType()); + return PostfixTemplatesUtils.isNumber(expr.getType()) || PostfixTemplatesUtils.isArray(expr.getType()) || PostfixTemplatesUtils + .isIterable(expr.getType()); } @Override public void expand(@NotNull PsiElement context, @NotNull Editor editor) { PsiExpression expr = getTopmostExpression(context); if (expr == null) { - CommonUtils.showErrorHint(context.getProject(), editor); + PostfixTemplatesUtils.showErrorHint(context.getProject(), editor); return; } Pair bounds = calculateBounds(expr); if (bounds == null) { - CommonUtils.showErrorHint(context.getProject(), editor); + PostfixTemplatesUtils.showErrorHint(context.getProject(), editor); return; } Project project = context.getProject(); @@ -79,13 +80,13 @@ public abstract class ForIndexedPostfixTemplate extends PostfixTemplate { @Nullable protected static String getExpressionBound(@NotNull PsiExpression expr) { PsiType type = expr.getType(); - if (CommonUtils.isNumber(type)) { + if (PostfixTemplatesUtils.isNumber(type)) { return expr.getText(); } - else if (CommonUtils.isArray(type)) { + else if (PostfixTemplatesUtils.isArray(type)) { return expr.getText() + ".length"; } - else if (CommonUtils.isIterable(type)) { + else if (PostfixTemplatesUtils.isIterable(type)) { return expr.getText() + ".size()"; } return null; @@ -94,7 +95,7 @@ public abstract class ForIndexedPostfixTemplate extends PostfixTemplate { @NotNull private static String suggestIndexType(@NotNull PsiExpression expr) { PsiType type = expr.getType(); - if (CommonUtils.isNumber(type)) { + if (PostfixTemplatesUtils.isNumber(type)) { return type.getCanonicalText(); } return "int"; diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForeachPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForeachPostfixTemplate.java index 479fc51d3143..a82f74a9687f 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForeachPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForeachPostfixTemplate.java @@ -7,7 +7,7 @@ import com.intellij.codeInsight.template.impl.TextExpression; import com.intellij.codeInsight.template.impl.VariableNode; import com.intellij.codeInsight.template.macro.IterableComponentTypeMacro; import com.intellij.codeInsight.template.macro.SuggestVariableNameMacro; -import com.intellij.codeInsight.template.postfix.util.CommonUtils; +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; @@ -25,7 +25,7 @@ public class ForeachPostfixTemplate extends PostfixTemplate { public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) { PsiExpression expr = getTopmostExpression(context); if (expr == null || !(expr.getParent() instanceof PsiExpressionStatement)) return false; - return CommonUtils.isArray(expr.getType()) || CommonUtils.isIterable(expr.getType()); + return PostfixTemplatesUtils.isArray(expr.getType()) || PostfixTemplatesUtils.isIterable(expr.getType()); } @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/IfStatementPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/IfStatementPostfixTemplate.java index 5a74d939e367..9610ec57c23d 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/IfStatementPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/IfStatementPostfixTemplate.java @@ -1,6 +1,6 @@ package com.intellij.codeInsight.template.postfix.templates; -import com.intellij.codeInsight.template.postfix.util.JavaSurroundersProxy; +import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; @@ -16,7 +16,7 @@ public class IfStatementPostfixTemplate extends BooleanPostfixTemplate { public void expand(@NotNull PsiElement context, @NotNull final Editor editor) { PsiExpression expression = getTopmostExpression(context); assert expression != null; - TextRange range = JavaSurroundersProxy.ifStatement(expression.getProject(), editor, expression); + TextRange range = PostfixTemplatesUtils.ifStatement(expression.getProject(), editor, expression); if (range != null) { editor.getCaretModel().moveToOffset(range.getStartOffset()); } diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/InstanceofExpressionPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/InstanceofExpressionPostfixTemplate.java index 6884854ff909..412ca232eb5e 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/InstanceofExpressionPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/InstanceofExpressionPostfixTemplate.java @@ -5,7 +5,7 @@ import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.codeInsight.lookup.PsiTypeLookupItem; import com.intellij.codeInsight.template.*; import com.intellij.codeInsight.template.postfix.util.Aliases; -import com.intellij.codeInsight.template.postfix.util.CommonUtils; +import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.RangeMarker; @@ -53,7 +53,7 @@ public class InstanceofExpressionPostfixTemplate extends PostfixTemplate { else { RangeMarker rangeMarker = expr.getUserData(ElementToWorkOn.TEXT_RANGE); if (rangeMarker == null) { - CommonUtils.showErrorHint(project, editor); + PostfixTemplatesUtils.showErrorHint(project, editor); return; } range = new TextRange(rangeMarker.getStartOffset(), rangeMarker.getEndOffset()); diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NotExpressionPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NotExpressionPostfixTemplate.java index fe7618712782..85f3316014b0 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NotExpressionPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NotExpressionPostfixTemplate.java @@ -1,7 +1,7 @@ package com.intellij.codeInsight.template.postfix.templates; import com.intellij.codeInsight.CodeInsightServicesUtil; -import com.intellij.codeInsight.template.postfix.util.CommonUtils; +import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.util.Condition; import com.intellij.psi.PsiExpression; @@ -23,7 +23,7 @@ public class NotExpressionPostfixTemplate extends ExpressionPostfixTemplateWithC return new Condition() { @Override public boolean value(PsiExpression expression) { - return CommonUtils.isBoolean(expression.getType()); + return PostfixTemplatesUtils.isBoolean(expression.getType()); } }; } diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NullCheckPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NullCheckPostfixTemplate.java index 25a06faf3db6..f35eeb200316 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NullCheckPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NullCheckPostfixTemplate.java @@ -1,6 +1,6 @@ package com.intellij.codeInsight.template.postfix.templates; -import com.intellij.codeInsight.template.postfix.util.JavaSurroundersProxy; +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; @@ -38,7 +38,7 @@ public abstract class NullCheckPostfixTemplate extends PostfixTemplate { PsiElement replace = expr.replace(condition); assert replace instanceof PsiExpression; - TextRange range = JavaSurroundersProxy.ifStatement(project, editor, (PsiExpression)replace); + TextRange range = PostfixTemplatesUtils.ifStatement(project, editor, (PsiExpression)replace); if (range != null) { editor.getCaretModel().moveToOffset(range.getStartOffset()); } diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ThrowExceptionPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ThrowExceptionPostfixTemplate.java index 97f1f3c489ac..5af260b46be1 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ThrowExceptionPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ThrowExceptionPostfixTemplate.java @@ -1,6 +1,6 @@ package com.intellij.codeInsight.template.postfix.templates; -import com.intellij.codeInsight.template.postfix.util.CommonUtils; +import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.psi.PsiElement; @@ -22,6 +22,6 @@ public class ThrowExceptionPostfixTemplate extends PostfixTemplate { @Override public void expand(@NotNull PsiElement context, @NotNull Editor editor) { - CommonUtils.createSimpleStatement(context, editor, "throw"); + PostfixTemplatesUtils.createSimpleStatement(context, editor, "throw"); } } \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/JavaSurroundersProxy.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/JavaSurroundersProxy.java deleted file mode 100644 index 84ef4d2ea0b1..000000000000 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/JavaSurroundersProxy.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.intellij.codeInsight.template.postfix.util; - -import com.intellij.codeInsight.generation.surroundWith.JavaExpressionSurroundDescriptor; -import com.intellij.lang.surroundWith.Surrounder; -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.PsiElement; -import com.intellij.psi.PsiExpression; -import com.intellij.util.IncorrectOperationException; -import com.intellij.util.containers.ContainerUtil; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.Arrays; - -/** - * @author ignatov - */ -public class JavaSurroundersProxy { - private static final Surrounder[] SURROUNDERS = new JavaExpressionSurroundDescriptor().getSurrounders(); - - public static void cast(@NotNull Project project, @NotNull Editor editor, @NotNull PsiExpression expr) - throws IncorrectOperationException { - findAndApply("JavaWithCastSurrounder", project, editor, expr); - } - - @Nullable - public static TextRange ifStatement(@NotNull Project project, @NotNull Editor editor, @NotNull PsiExpression expr) - throws IncorrectOperationException { - return findAndApply("JavaWithIfExpressionSurrounder", project, editor, expr); - } - - @Nullable - private static TextRange findAndApply(@NotNull final String name, - @NotNull Project project, - @NotNull Editor editor, - @NotNull PsiExpression expr) { - Surrounder surrounder = ContainerUtil.find(SURROUNDERS, new Condition() { - @Override - public boolean value(Surrounder surrounder) { - return surrounder.getClass().getName().contains(name); - } - }); - - PsiElement[] elements = {expr}; - if (surrounder != null) { - if (surrounder.isApplicable(elements)) { - return surrounder.surroundElements(project, editor, elements); - } - else { - CommonUtils.showErrorHint(project, editor); - } - } - else { - throw new IncorrectOperationException("Can't find any applicable surrounder with elements: " + Arrays.toString(elements)); - } - return null; - } -} diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/CommonUtils.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/PostfixTemplatesUtils.java similarity index 67% rename from java/java-impl/src/com/intellij/codeInsight/template/postfix/util/CommonUtils.java rename to java/java-impl/src/com/intellij/codeInsight/template/postfix/util/PostfixTemplatesUtils.java index b0e0c81106e2..74eb999e8b58 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/CommonUtils.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/PostfixTemplatesUtils.java @@ -1,8 +1,11 @@ package com.intellij.codeInsight.template.postfix.util; +import com.intellij.codeInsight.generation.surroundWith.JavaExpressionSurrounder; +import com.intellij.codeInsight.generation.surroundWith.JavaWithIfExpressionSurrounder; import com.intellij.codeInsight.template.postfix.templates.PostfixTemplate; 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.InheritanceUtil; import com.intellij.refactoring.util.CommonRefactoringUtil; @@ -10,8 +13,8 @@ import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public abstract class CommonUtils { - private CommonUtils() { +public abstract class PostfixTemplatesUtils { + private PostfixTemplatesUtils() { } public static void showErrorHint(Project project, Editor editor) { @@ -55,5 +58,30 @@ public abstract class CommonUtils { PsiPrimitiveType unboxedType = PsiPrimitiveType.getUnboxedType(type); return PsiType.INT.equals(unboxedType) || PsiType.BYTE.equals(unboxedType) || PsiType.LONG.equals(unboxedType); } + + @Nullable + public static TextRange ifStatement(@NotNull Project project, @NotNull Editor editor, @NotNull PsiExpression expr) { + JavaExpressionSurrounder surrounder = new JavaWithIfExpressionSurrounder(); + PsiElement[] elements = {expr}; + if (surrounder.isApplicable(elements)) { + return surrounder.surroundElements(project, editor, elements); + } + else { + showErrorHint(project, editor); + } + return null; + } + + @Nullable + public static TextRange apply(@NotNull JavaExpressionSurrounder surrounder, @NotNull Project project, @NotNull Editor editor, @NotNull PsiExpression expr) { + PsiElement[] elements = {expr}; + if (surrounder.isApplicable(elements)) { + return surrounder.surroundElements(project, editor, elements); + } + else { + showErrorHint(project, editor); + } + return null; + } }