get rid of JavaSurroundersProxy

This commit is contained in:
Sergey Ignatov
2013-12-19 13:21:57 +04:00
parent 6ee5fbcfe5
commit 8f82a739c3
17 changed files with 67 additions and 98 deletions
@@ -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
@@ -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();
@@ -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");
}
}
@@ -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());
}
}
@@ -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);
}
}
@@ -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());
}
@@ -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<PsiExpression> 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));
@@ -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");
}
@@ -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<String, String> 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";
@@ -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
@@ -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());
}
@@ -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());
@@ -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<PsiExpression>() {
@Override
public boolean value(PsiExpression expression) {
return CommonUtils.isBoolean(expression.getType());
return PostfixTemplatesUtils.isBoolean(expression.getType());
}
};
}
@@ -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());
}
@@ -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");
}
}
@@ -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<Surrounder>() {
@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;
}
}
@@ -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;
}
}