Add base class for expression surrounders
This commit is contained in:
+34
@@ -0,0 +1,34 @@
|
||||
package org.jetbrains.jet.plugin.codeInsight.surroundWith.expression;
|
||||
|
||||
import com.intellij.lang.surroundWith.Surrounder;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.psi.JetExpression;
|
||||
|
||||
public abstract class KotlinExpressionSurrounder implements Surrounder {
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(@NotNull PsiElement[] elements) {
|
||||
if (elements.length != 1 || !(elements[0] instanceof JetExpression)) {
|
||||
return false;
|
||||
}
|
||||
return isApplicable((JetExpression) elements[0]);
|
||||
}
|
||||
|
||||
protected abstract boolean isApplicable(@NotNull JetExpression expression);
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public TextRange surroundElements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements) {
|
||||
assert elements.length == 1 : "KotlinExpressionSurrounder should be applicable only for 1 expression: " + elements.length;
|
||||
return surroundExpression(project, editor, (JetExpression) elements[0]);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected abstract TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull JetExpression expression);
|
||||
}
|
||||
+7
-13
@@ -19,18 +19,14 @@ import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
||||
import org.jetbrains.jet.plugin.project.WholeProjectAnalyzerFacade;
|
||||
|
||||
public class KotlinNotSurrounder implements Surrounder {
|
||||
public class KotlinNotSurrounder extends KotlinExpressionSurrounder {
|
||||
@Override
|
||||
public String getTemplateDescription() {
|
||||
return CodeInsightBundle.message("surround.with.not.template");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(@NotNull PsiElement[] elements) {
|
||||
if (elements.length != 1 || !(elements[0] instanceof JetExpression)) {
|
||||
return false;
|
||||
}
|
||||
JetExpression expression = (JetExpression) elements[0];
|
||||
public boolean isApplicable(@NotNull JetExpression expression) {
|
||||
ResolveSession resolveSession = WholeProjectAnalyzerFacade.getLazyResolveSessionForFile((JetFile) expression.getContainingFile());
|
||||
BindingContext expressionBindingContext = ResolveSessionUtils.resolveToExpression(resolveSession, expression);
|
||||
JetType type = expressionBindingContext.get(BindingContext.EXPRESSION_TYPE, expression);
|
||||
@@ -39,21 +35,19 @@ public class KotlinNotSurrounder implements Surrounder {
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public TextRange surroundElements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements) throws IncorrectOperationException {
|
||||
JetExpression expr = (JetExpression) elements[0];
|
||||
|
||||
public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull JetExpression expression) {
|
||||
JetPrefixExpression prefixExpr = (JetPrefixExpression)JetPsiFactory.createExpression(project, "!(a)");
|
||||
JetParenthesizedExpression parenthesizedExpression = (JetParenthesizedExpression) prefixExpr.getBaseExpression();
|
||||
assert parenthesizedExpression != null : "JetParenthesizedExpression should exists for " + prefixExpr.getText() + " expression";
|
||||
JetExpression expressionWithoutParentheses = parenthesizedExpression.getExpression();
|
||||
assert expressionWithoutParentheses != null : "JetExpression should exists for " + parenthesizedExpression.getText() + " expression";
|
||||
expressionWithoutParentheses.replace(expr);
|
||||
expressionWithoutParentheses.replace(expression);
|
||||
|
||||
expr = (JetExpression) expr.replace(prefixExpr);
|
||||
expression = (JetExpression) expression.replace(prefixExpr);
|
||||
|
||||
CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(expr);
|
||||
CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(expression);
|
||||
|
||||
int offset = expr.getTextRange().getEndOffset();
|
||||
int offset = expression.getTextRange().getEndOffset();
|
||||
return new TextRange(offset, offset);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user