Search in sources :

Example 31 with MultiTextEdit

use of org.eclipse.text.edits.MultiTextEdit in project eclipse.platform.text by eclipse.

the class LinkedPositionGroup method handleEvent.

/**
 * Creates an edition of a document change that will forward any
 * modification in one position to all linked siblings. The return value is
 * a map from <code>IDocument</code> to <code>TextEdit</code>.
 *
 * @param event the document event to check
 * @return a map of edits, grouped by edited document, or <code>null</code>
 *         if there are no edits
 */
Map<IDocument, TextEdit> handleEvent(DocumentEvent event) {
    if (fLastPosition != null) {
        Map<IDocument, List<ReplaceEdit>> map = new HashMap<>();
        int relativeOffset = event.getOffset() - fLastRegion.getOffset();
        if (relativeOffset < 0) {
            relativeOffset = 0;
        }
        int eventEnd = event.getOffset() + event.getLength();
        int lastEnd = fLastRegion.getOffset() + fLastRegion.getLength();
        int length;
        if (eventEnd > lastEnd)
            length = lastEnd - relativeOffset - fLastRegion.getOffset();
        else
            length = eventEnd - relativeOffset - fLastRegion.getOffset();
        String text = event.getText();
        if (text == null)
            // $NON-NLS-1$
            text = "";
        for (LinkedPosition p : fPositions) {
            if (p == fLastPosition || p.isDeleted())
                // don't re-update the origin of the change
                continue;
            List<ReplaceEdit> edits = map.get(p.getDocument());
            if (edits == null) {
                edits = new ArrayList<>();
                map.put(p.getDocument(), edits);
            }
            if (fMustEnforceEqualContents) {
                try {
                    edits.add(new ReplaceEdit(p.getOffset(), p.getLength(), fLastPosition.getContent()));
                } catch (BadLocationException e) {
                    // should not happen
                    throw new RuntimeException(e);
                }
            } else {
                edits.add(new ReplaceEdit(p.getOffset() + relativeOffset, length, text));
            }
        }
        fMustEnforceEqualContents = false;
        try {
            Map<IDocument, TextEdit> result = new HashMap<>();
            for (IDocument d : map.keySet()) {
                TextEdit edit = new MultiTextEdit(0, d.getLength());
                edit.addChildren(map.get(d).toArray(new TextEdit[0]));
                result.put(d, edit);
            }
            return result;
        } catch (MalformedTreeException x) {
            // manager
            return null;
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 32 with MultiTextEdit

use of org.eclipse.text.edits.MultiTextEdit in project AutoRefactor by JnRouvignac.

the class ASTCommentRewriter method overlaps.

private boolean overlaps(TextEdit edits, TextEdit edit2) {
    final SourceLocation range = toSourceLoc(edit2);
    final AtomicBoolean overlaps = new AtomicBoolean();
    edits.accept(new TextEditVisitor() {

        @Override
        public boolean visit(MultiTextEdit edit) {
            // move on there is nothing to check here
            return VISIT_SUBTREE;
        }

        @Override
        public boolean visitNode(TextEdit edit) {
            if (!overlaps.get()) {
                overlaps.set(range.overlapsWith(toSourceLoc(edit)));
            }
            return !overlaps.get();
        }
    });
    return overlaps.get();
}
Also used : SourceLocation(org.autorefactor.refactoring.SourceLocation) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TextEditVisitor(org.eclipse.text.edits.TextEditVisitor) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 33 with MultiTextEdit

use of org.eclipse.text.edits.MultiTextEdit in project che by eclipse.

the class UnimplementedCodeFix method createAddUnimplementedMethodsFix.

public static IProposableFix createAddUnimplementedMethodsFix(final CompilationUnit root, IProblemLocation problem) {
    ASTNode typeNode = getSelectedTypeNode(root, problem);
    if (typeNode == null)
        return null;
    if (isTypeBindingNull(typeNode))
        return null;
    AddUnimplementedMethodsOperation operation = new AddUnimplementedMethodsOperation(typeNode);
    if (operation.getMethodsToImplement().length > 0) {
        return new UnimplementedCodeFix(CorrectionMessages.UnimplementedMethodsCorrectionProposal_description, root, new CompilationUnitRewriteOperation[] { operation });
    } else {
        return new IProposableFix() {

            public CompilationUnitChange createChange(IProgressMonitor progressMonitor) throws CoreException {
                CompilationUnitChange change = new CompilationUnitChange(CorrectionMessages.UnimplementedMethodsCorrectionProposal_description, (ICompilationUnit) root.getJavaElement()) {

                    @Override
                    public Change perform(IProgressMonitor pm) throws CoreException {
                        //TODO
                        return new NullChange();
                    }
                };
                change.setEdit(new MultiTextEdit());
                return change;
            }

            public String getAdditionalProposalInfo() {
                return new String();
            }

            public String getDisplayString() {
                return CorrectionMessages.UnimplementedMethodsCorrectionProposal_description;
            }

            public IStatus getStatus() {
                return new Status(IStatus.ERROR, JavaPlugin.ID_PLUGIN, CorrectionMessages.UnimplementedCodeFix_DependenciesStatusMessage);
            }
        };
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) NullChange(org.eclipse.ltk.core.refactoring.NullChange) ASTNode(org.eclipse.jdt.core.dom.ASTNode) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 34 with MultiTextEdit

use of org.eclipse.text.edits.MultiTextEdit in project che by eclipse.

the class TextChangeCompatibility method addTextEdit.

public static void addTextEdit(TextChange change, String name, TextEdit edit, GroupCategorySet groupCategories) throws MalformedTreeException {
    Assert.isNotNull(change);
    Assert.isNotNull(name);
    Assert.isNotNull(edit);
    TextEdit root = change.getEdit();
    if (root == null) {
        root = new MultiTextEdit();
        change.setEdit(root);
    }
    insert(root, edit);
    change.addTextEditChangeGroup(new TextEditChangeGroup(change, new CategorizedTextEditGroup(name, edit, groupCategories)));
}
Also used : TextEditChangeGroup(org.eclipse.ltk.core.refactoring.TextEditChangeGroup) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) CategorizedTextEditGroup(org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup)

Example 35 with MultiTextEdit

use of org.eclipse.text.edits.MultiTextEdit in project che by eclipse.

the class RenameLocalVariableProcessor method createEdits.

private void createEdits() {
    TextEdit declarationEdit = createRenameEdit(fTempDeclarationNode.getName().getStartPosition());
    TextEdit[] allRenameEdits = getAllRenameEdits(declarationEdit);
    TextEdit[] allUnparentedRenameEdits = new TextEdit[allRenameEdits.length];
    TextEdit unparentedDeclarationEdit = null;
    fChange = new CompilationUnitChange(RefactoringCoreMessages.RenameTempRefactoring_rename, fCu);
    MultiTextEdit rootEdit = new MultiTextEdit();
    fChange.setEdit(rootEdit);
    fChange.setKeepPreviewEdits(true);
    for (int i = 0; i < allRenameEdits.length; i++) {
        if (fIsComposite) {
            // Add a copy of the text edit (text edit may only have one
            // parent) to keep problem reporting code clean
            TextChangeCompatibility.addTextEdit(fChangeManager.get(fCu), RefactoringCoreMessages.RenameTempRefactoring_changeName, allRenameEdits[i].copy(), fCategorySet);
            // Add a separate copy for problem reporting
            allUnparentedRenameEdits[i] = allRenameEdits[i].copy();
            if (allRenameEdits[i].equals(declarationEdit))
                unparentedDeclarationEdit = allUnparentedRenameEdits[i];
        }
        rootEdit.addChild(allRenameEdits[i]);
        fChange.addTextEditGroup(new TextEditGroup(RefactoringCoreMessages.RenameTempRefactoring_changeName, allRenameEdits[i]));
    }
    // store information for analysis
    if (fIsComposite) {
        fLocalAnalyzePackage = new RenameAnalyzeUtil.LocalAnalyzePackage(unparentedDeclarationEdit, allUnparentedRenameEdits);
    } else
        fLocalAnalyzePackage = new RenameAnalyzeUtil.LocalAnalyzePackage(declarationEdit, allRenameEdits);
}
Also used : LocalAnalyzePackage(org.eclipse.jdt.internal.corext.refactoring.rename.RenameAnalyzeUtil.LocalAnalyzePackage) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) LocalAnalyzePackage(org.eclipse.jdt.internal.corext.refactoring.rename.RenameAnalyzeUtil.LocalAnalyzePackage) TextEditGroup(org.eclipse.text.edits.TextEditGroup) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Aggregations

MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)51 TextEdit (org.eclipse.text.edits.TextEdit)33 Test (org.junit.Test)18 IDocument (org.eclipse.jface.text.IDocument)13 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)13 Document (org.eclipse.jface.text.Document)11 TextEditGroup (org.eclipse.text.edits.TextEditGroup)10 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)9 DeleteEdit (org.eclipse.text.edits.DeleteEdit)7 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 ArrayList (java.util.ArrayList)5 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)5 HashSet (java.util.HashSet)4 List (java.util.List)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 TextChange (org.eclipse.ltk.core.refactoring.TextChange)4 CopySourceEdit (org.eclipse.text.edits.CopySourceEdit)4 TextEditCopier (org.eclipse.text.edits.TextEditCopier)4