use of org.eclipse.text.edits.InsertEdit in project che by eclipse.
the class LocalCorrectionsSubProcessor method getInvalidOperatorProposals.
public static void getInvalidOperatorProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
CompilationUnit root = context.getASTRoot();
AST ast = root.getAST();
ASTNode selectedNode = problem.getCoveringNode(root);
while (selectedNode instanceof ParenthesizedExpression) {
selectedNode = ((ParenthesizedExpression) selectedNode).getExpression();
}
if (selectedNode instanceof PrefixExpression) {
// !x instanceof X -> !(x instanceof X)
PrefixExpression expression = (PrefixExpression) selectedNode;
if (expression.getOperator() == PrefixExpression.Operator.NOT) {
ASTNode parent = expression.getParent();
String label = null;
switch(parent.getNodeType()) {
case ASTNode.INSTANCEOF_EXPRESSION:
label = CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_instanceof_description;
break;
case ASTNode.INFIX_EXPRESSION:
InfixExpression infixExpression = (InfixExpression) parent;
label = Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_description, infixExpression.getOperator().toString());
break;
}
if (label != null) {
ASTRewrite rewrite = ASTRewrite.create(ast);
rewrite.replace(selectedNode, rewrite.createMoveTarget(expression.getOperand()), null);
ParenthesizedExpression newParentExpr = ast.newParenthesizedExpression();
newParentExpr.setExpression((Expression) rewrite.createMoveTarget(parent));
PrefixExpression newPrefixExpr = ast.newPrefixExpression();
newPrefixExpr.setOperand(newParentExpr);
newPrefixExpr.setOperator(PrefixExpression.Operator.NOT);
rewrite.replace(parent, newPrefixExpr, null);
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVALID_OPERATOR, image);
proposals.add(proposal);
}
}
} else if (selectedNode instanceof InfixExpression && isBitOperation((((InfixExpression) selectedNode).getOperator()))) {
// a & b == c -> (a & b) == c
final CompareInBitWiseOpFinder opFinder = new CompareInBitWiseOpFinder(selectedNode);
if (opFinder.getCompareExpression() != null) {
// compare operation inside bit operations: set parents
String label = CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_bitop_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
CUCorrectionProposal proposal = new CUCorrectionProposal(label, context.getCompilationUnit(), IProposalRelevance.INVALID_OPERATOR, image) {
@Override
protected void addEdits(IDocument document, TextEdit edit) throws CoreException {
InfixExpression compareExpression = opFinder.getCompareExpression();
InfixExpression expression = opFinder.getParentInfixExpression();
ASTNode left = compareExpression.getLeftOperand();
if (expression.getStartPosition() < left.getStartPosition()) {
edit.addChild(new InsertEdit(expression.getStartPosition(), String.valueOf('(')));
edit.addChild(new InsertEdit(ASTNodes.getExclusiveEnd(left), String.valueOf(')')));
}
ASTNode rigth = compareExpression.getRightOperand();
int selEnd = ASTNodes.getExclusiveEnd(expression);
if (selEnd > ASTNodes.getExclusiveEnd(rigth)) {
edit.addChild(new InsertEdit(rigth.getStartPosition(), String.valueOf('(')));
edit.addChild(new InsertEdit(selEnd, String.valueOf(')')));
}
}
};
proposals.add(proposal);
}
}
}
use of org.eclipse.text.edits.InsertEdit in project eclipse.platform.text by eclipse.
the class TextEditTests method testCopy1.
@Test
public void testCopy1() throws Exception {
MultiTextEdit root = new MultiTextEdit();
TextEdit e1 = new InsertEdit(2, "yy");
TextEdit e2 = new ReplaceEdit(2, 3, "3456");
root.addChild(e1);
root.addChild(e2);
List<TextEdit> org = flatten(root);
TextEditCopier copier = new TextEditCopier(root);
List<TextEdit> copy = flatten(copier.perform());
compare(org, copy);
}
use of org.eclipse.text.edits.InsertEdit in project eclipse.platform.text by eclipse.
the class TextEditTests method testInsert1.
@Test
public void testInsert1() throws Exception {
// [][ ]
TextEdit e1 = new InsertEdit(2, "yy");
TextEdit e2 = new ReplaceEdit(2, 3, "3456");
fRoot.addChild(e1);
fRoot.addChild(e2);
UndoEdit undo = fRoot.apply(fDocument);
assertEquals(fRoot, 2, 6);
assertEquals(e1, 2, 2);
assertEquals(e2, 4, 4);
Assert.assertEquals("Buffer content", "01yy345656789", fDocument.get());
doUndoRedo(undo, "01yy345656789");
}
use of org.eclipse.text.edits.InsertEdit in project eclipse.platform.text by eclipse.
the class TextEditTests method testMoveUp.
@Test
public void testMoveUp() throws Exception {
MoveSourceEdit s1 = new MoveSourceEdit(7, 2);
MoveTargetEdit t1 = new MoveTargetEdit(2, s1);
TextEdit i1 = new InsertEdit(5, "x");
TextEdit d1 = new DeleteEdit(9, 1);
RangeMarker m1 = new RangeMarker(7, 2);
s1.addChild(m1);
fRoot.addChild(s1);
fRoot.addChild(i1);
fRoot.addChild(t1);
fRoot.addChild(d1);
UndoEdit undo = fRoot.apply(fDocument);
Assert.assertEquals("Buffer content", "0178234x56", fDocument.get());
assertEquals(s1, 10, 0);
assertEquals(i1, 7, 1);
assertEquals(t1, 2, 2);
assertEquals(m1, 2, 2);
assertEquals(d1, 10, 0);
doUndoRedo(undo, "0178234x56");
}
use of org.eclipse.text.edits.InsertEdit in project eclipse.platform.text by eclipse.
the class TextEditTests method testMoveDown.
@Test
public void testMoveDown() throws Exception {
MoveSourceEdit s1 = new MoveSourceEdit(2, 2);
TextEdit i1 = new InsertEdit(5, "x");
MoveTargetEdit t1 = new MoveTargetEdit(7, s1);
TextEdit d1 = new DeleteEdit(9, 1);
RangeMarker m1 = new RangeMarker(2, 2);
s1.addChild(m1);
fRoot.addChild(s1);
fRoot.addChild(i1);
fRoot.addChild(t1);
fRoot.addChild(d1);
UndoEdit undo = fRoot.apply(fDocument);
Assert.assertEquals("Buffer content", "014x562378", fDocument.get());
assertEquals(s1, 2, 0);
assertEquals(i1, 3, 1);
assertEquals(t1, 6, 2);
assertEquals(m1, 6, 2);
assertEquals(d1, 10, 0);
doUndoRedo(undo, "014x562378");
}
Aggregations