quick fixes refactoring (disuse of CodeEditUtil)
This commit is contained in:
@@ -80,21 +80,9 @@ public class ChangeVariableMutabilityFix implements IntentionAction {
|
|||||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||||
JetProperty property = getCorrespondingProperty(editor, (JetFile)file);
|
JetProperty property = getCorrespondingProperty(editor, (JetFile)file);
|
||||||
assert property != null && !property.isVar();
|
assert property != null && !property.isVar();
|
||||||
JetProperty newElement = (JetProperty) property.copy();
|
|
||||||
if (newElement.isVar()) {
|
|
||||||
PsiElement varElement = newElement.getNode().findChildByType(JetTokens.VAR_KEYWORD).getPsi();
|
|
||||||
|
|
||||||
JetProperty valProperty = JetPsiFactory.createProperty(project, "x", "Any", false);
|
JetProperty newElement = JetPsiFactory.createProperty(project, property.getText().replaceFirst(
|
||||||
PsiElement valElement = valProperty.getNode().findChildByType(JetTokens.VAL_KEYWORD).getPsi();
|
property.isVar() ? "var" : "val", property.isVar() ? "val" : "var"));
|
||||||
CodeEditUtil.replaceChild(newElement.getNode(), varElement.getNode(), valElement.getNode());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
PsiElement valElement = newElement.getNode().findChildByType(JetTokens.VAL_KEYWORD).getPsi();
|
|
||||||
|
|
||||||
JetProperty varProperty = JetPsiFactory.createProperty(project, "x", "Any", true);
|
|
||||||
PsiElement varElement = varProperty.getNode().findChildByType(JetTokens.VAR_KEYWORD).getPsi();
|
|
||||||
CodeEditUtil.replaceChild(newElement.getNode(), valElement.getNode(), varElement.getNode());
|
|
||||||
}
|
|
||||||
property.replace(newElement);
|
property.replace(newElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-10
@@ -10,16 +10,17 @@ import org.jetbrains.jet.lang.diagnostics.DiagnosticWithPsiElement;
|
|||||||
import org.jetbrains.jet.lang.psi.JetBinaryExpressionWithTypeRHS;
|
import org.jetbrains.jet.lang.psi.JetBinaryExpressionWithTypeRHS;
|
||||||
import org.jetbrains.jet.lang.psi.JetExpression;
|
import org.jetbrains.jet.lang.psi.JetExpression;
|
||||||
import org.jetbrains.jet.lang.psi.JetPsiFactory;
|
import org.jetbrains.jet.lang.psi.JetPsiFactory;
|
||||||
|
import org.jetbrains.jet.lang.psi.JetTypeReference;
|
||||||
import org.jetbrains.jet.plugin.JetBundle;
|
import org.jetbrains.jet.plugin.JetBundle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author svtk
|
* @author svtk
|
||||||
*/
|
*/
|
||||||
public abstract class ReplaceOperationInBinaryExpressionFix<T extends JetExpression> extends JetIntentionAction<T> {
|
public abstract class ReplaceOperationInBinaryExpressionFix<T extends JetExpression> extends JetIntentionAction<T> {
|
||||||
private final String expressionWithNecessaryOperation;
|
private final String operation;
|
||||||
public ReplaceOperationInBinaryExpressionFix(@NotNull T element, String expressionWithNecessaryOperation) {
|
public ReplaceOperationInBinaryExpressionFix(@NotNull T element, String operation) {
|
||||||
super(element);
|
super(element);
|
||||||
this.expressionWithNecessaryOperation = expressionWithNecessaryOperation;
|
this.operation = operation;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -31,12 +32,12 @@ public abstract class ReplaceOperationInBinaryExpressionFix<T extends JetExpress
|
|||||||
@Override
|
@Override
|
||||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||||
if (element instanceof JetBinaryExpressionWithTypeRHS) {
|
if (element instanceof JetBinaryExpressionWithTypeRHS) {
|
||||||
JetBinaryExpressionWithTypeRHS expression = (JetBinaryExpressionWithTypeRHS) JetPsiFactory.createExpression(project, expressionWithNecessaryOperation);
|
JetExpression left = ((JetBinaryExpressionWithTypeRHS) element).getLeft();
|
||||||
|
JetTypeReference right = ((JetBinaryExpressionWithTypeRHS) element).getRight();
|
||||||
JetBinaryExpressionWithTypeRHS newElement = (JetBinaryExpressionWithTypeRHS) element.copy();
|
if (right != null) {
|
||||||
CodeEditUtil.replaceChild(newElement.getNode(), newElement.getOperationSign().getNode(), expression.getOperationSign().getNode());
|
JetExpression expression = JetPsiFactory.createExpression(project, left.getText() + operation + right.getText());
|
||||||
|
element.replace(expression);
|
||||||
element.replace(newElement);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,7 +46,7 @@ public abstract class ReplaceOperationInBinaryExpressionFix<T extends JetExpress
|
|||||||
@Override
|
@Override
|
||||||
public JetIntentionAction<JetBinaryExpressionWithTypeRHS> createAction(DiagnosticWithPsiElement diagnostic) {
|
public JetIntentionAction<JetBinaryExpressionWithTypeRHS> createAction(DiagnosticWithPsiElement diagnostic) {
|
||||||
assert diagnostic.getPsiElement() instanceof JetBinaryExpressionWithTypeRHS;
|
assert diagnostic.getPsiElement() instanceof JetBinaryExpressionWithTypeRHS;
|
||||||
return new ReplaceOperationInBinaryExpressionFix<JetBinaryExpressionWithTypeRHS>((JetBinaryExpressionWithTypeRHS) diagnostic.getPsiElement(), "2 : Int") {
|
return new ReplaceOperationInBinaryExpressionFix<JetBinaryExpressionWithTypeRHS>((JetBinaryExpressionWithTypeRHS) diagnostic.getPsiElement(), " : ") {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String getText() {
|
public String getText() {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
// "Make variable mutable" "true"
|
// "Make variable mutable" "true"
|
||||||
class A() {
|
class A() {
|
||||||
var a: Int = 0
|
var a: Int = 0
|
||||||
<caret>set(v: Int) {}
|
set(v: Int) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user