Search in sources :

Example 1 with InsertEdit

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);
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) InsertEdit(org.eclipse.text.edits.InsertEdit) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) CoreException(org.eclipse.core.runtime.CoreException) TextEdit(org.eclipse.text.edits.TextEdit) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with InsertEdit

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);
}
Also used : TextEditCopier(org.eclipse.text.edits.TextEditCopier) InsertEdit(org.eclipse.text.edits.InsertEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) Test(org.junit.Test)

Example 3 with InsertEdit

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");
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) UndoEdit(org.eclipse.text.edits.UndoEdit) Test(org.junit.Test)

Example 4 with InsertEdit

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");
}
Also used : MoveTargetEdit(org.eclipse.text.edits.MoveTargetEdit) InsertEdit(org.eclipse.text.edits.InsertEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) MoveSourceEdit(org.eclipse.text.edits.MoveSourceEdit) RangeMarker(org.eclipse.text.edits.RangeMarker) DeleteEdit(org.eclipse.text.edits.DeleteEdit) UndoEdit(org.eclipse.text.edits.UndoEdit) Test(org.junit.Test)

Example 5 with InsertEdit

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");
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) MoveTargetEdit(org.eclipse.text.edits.MoveTargetEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) MoveSourceEdit(org.eclipse.text.edits.MoveSourceEdit) RangeMarker(org.eclipse.text.edits.RangeMarker) DeleteEdit(org.eclipse.text.edits.DeleteEdit) UndoEdit(org.eclipse.text.edits.UndoEdit) Test(org.junit.Test)

Aggregations

InsertEdit (org.eclipse.text.edits.InsertEdit)65 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)33 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)24 TextEdit (org.eclipse.text.edits.TextEdit)22 DeleteEdit (org.eclipse.text.edits.DeleteEdit)20 Test (org.junit.Test)18 UndoEdit (org.eclipse.text.edits.UndoEdit)13 BadLocationException (org.eclipse.jface.text.BadLocationException)12 ArrayList (java.util.ArrayList)9 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)8 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)8 Location (org.eclipse.titan.designer.AST.Location)8 Module (org.eclipse.titan.designer.AST.Module)7 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)7 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)5 CoreException (org.eclipse.core.runtime.CoreException)5 IFile (org.eclipse.core.resources.IFile)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 IDocument (org.eclipse.jface.text.IDocument)4 IRegion (org.eclipse.jface.text.IRegion)4