Search in sources :

Example 16 with TextEdit

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

the class TextChange method createTextEditProcessor.

private TextEditProcessor createTextEditProcessor(IDocument document, int flags, boolean preview) {
    if (fEdit == null)
        return new TextEditProcessor(document, new MultiTextEdit(0, 0), flags);
    List excludes = new ArrayList(0);
    TextEditBasedChangeGroup[] groups = getChangeGroups();
    for (int index = 0; index < groups.length; index++) {
        TextEditBasedChangeGroup edit = groups[index];
        if (!edit.isEnabled()) {
            excludes.addAll(Arrays.asList(edit.getTextEditGroup().getTextEdits()));
        }
    }
    if (preview) {
        fCopier = new TextEditCopier(fEdit);
        TextEdit copiedEdit = fCopier.perform();
        boolean keep = getKeepPreviewEdits();
        if (keep)
            flags = flags | TextEdit.UPDATE_REGIONS;
        LocalTextEditProcessor result = new LocalTextEditProcessor(document, copiedEdit, flags);
        result.setExcludes(mapEdits((TextEdit[]) excludes.toArray(new TextEdit[excludes.size()]), fCopier));
        if (!keep)
            fCopier = null;
        return result;
    } else {
        LocalTextEditProcessor result = new LocalTextEditProcessor(document, fEdit, flags | TextEdit.UPDATE_REGIONS);
        result.setExcludes((TextEdit[]) excludes.toArray(new TextEdit[excludes.size()]));
        return result;
    }
}
Also used : TextEditCopier(org.eclipse.text.edits.TextEditCopier) TextEditProcessor(org.eclipse.text.edits.TextEditProcessor) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 17 with TextEdit

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

the class CompilationUnitChangeNode method coveredBy.

private boolean coveredBy(TextEditBasedChangeGroup group, IRegion sourceRegion) {
    int sLength = sourceRegion.getLength();
    if (sLength == 0)
        return false;
    int sOffset = sourceRegion.getOffset();
    int sEnd = sOffset + sLength - 1;
    TextEdit[] edits = group.getTextEdits();
    for (int i = 0; i < edits.length; i++) {
        TextEdit edit = edits[i];
        if (edit.isDeleted())
            return false;
        int rOffset = edit.getOffset();
        int rLength = edit.getLength();
        int rEnd = rOffset + rLength - 1;
        if (rLength == 0) {
            if (!(sOffset < rOffset && rOffset <= sEnd))
                return false;
        } else {
            if (!(sOffset <= rOffset && rEnd <= sEnd))
                return false;
        }
    }
    return true;
}
Also used : TextEdit(org.eclipse.text.edits.TextEdit)

Example 18 with TextEdit

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

the class TextChangeCompatibility method addTextEdit.

public static void addTextEdit(TextChange change, String name, TextEdit edit) 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.addTextEditGroup(new TextEditGroup(name, edit));
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) TextEditGroup(org.eclipse.text.edits.TextEditGroup) CategorizedTextEditGroup(org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 19 with TextEdit

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

the class SurroundWithTryCatchRefactoring method createChange.

/* non Java-doc
	 * @see IRefactoring#createChange(IProgressMonitor)
	 */
@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
    //$NON-NLS-1$
    final String NN = "";
    if (pm == null)
        pm = new NullProgressMonitor();
    pm.beginTask(NN, 2);
    try {
        final CompilationUnitChange result = new CompilationUnitChange(getName(), fCUnit);
        if (fLeaveDirty)
            result.setSaveMode(TextFileChange.LEAVE_DIRTY);
        MultiTextEdit root = new MultiTextEdit();
        result.setEdit(root);
        fRewriter = ASTRewrite.create(fAnalyzer.getEnclosingBodyDeclaration().getAST());
        fRewriter.setTargetSourceRangeComputer(new SelectionAwareSourceRangeComputer(fAnalyzer.getSelectedNodes(), fCUnit.getBuffer(), fSelection.getOffset(), fSelection.getLength()));
        fImportRewrite = StubUtility.createImportRewrite(fRootNode, true);
        fLinkedProposalModel = new LinkedProposalModel();
        fScope = CodeScopeBuilder.perform(fAnalyzer.getEnclosingBodyDeclaration(), fSelection).findScope(fSelection.getOffset(), fSelection.getLength());
        fScope.setCursor(fSelection.getOffset());
        fSelectedNodes = fAnalyzer.getSelectedNodes();
        createTryCatchStatement(fCUnit.getBuffer(), fCUnit.findRecommendedLineSeparator());
        if (fImportRewrite.hasRecordedChanges()) {
            TextEdit edit = fImportRewrite.rewriteImports(null);
            root.addChild(edit);
            result.addTextEditGroup(new TextEditGroup(NN, new TextEdit[] { edit }));
        }
        TextEdit change = fRewriter.rewriteAST();
        root.addChild(change);
        result.addTextEditGroup(new TextEditGroup(NN, new TextEdit[] { change }));
        return result;
    } finally {
        pm.done();
    }
}
Also used : SelectionAwareSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.SelectionAwareSourceRangeComputer) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) LinkedProposalModel(org.eclipse.jdt.internal.corext.fix.LinkedProposalModel) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) TextEditGroup(org.eclipse.text.edits.TextEditGroup) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 20 with TextEdit

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

the class TextEditUtil method flatten.

private static void flatten(TextEdit edit, MultiTextEdit result) {
    if (!edit.hasChildren()) {
        result.addChild(edit);
    } else {
        TextEdit[] children = edit.getChildren();
        for (int i = 0; i < children.length; i++) {
            TextEdit child = children[i];
            child.getParent().removeChild(0);
            flatten(child, result);
        }
    }
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit)

Aggregations

TextEdit (org.eclipse.text.edits.TextEdit)190 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)113 Test (org.junit.Test)49 IDocument (org.eclipse.jface.text.IDocument)48 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)48 BadLocationException (org.eclipse.jface.text.BadLocationException)34 Document (org.eclipse.jface.text.Document)33 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)32 UndoEdit (org.eclipse.text.edits.UndoEdit)26 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)23 InsertEdit (org.eclipse.text.edits.InsertEdit)22 CoreException (org.eclipse.core.runtime.CoreException)20 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)19 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)19 DeleteEdit (org.eclipse.text.edits.DeleteEdit)17 MoveSourceEdit (org.eclipse.text.edits.MoveSourceEdit)16 ArrayList (java.util.ArrayList)15 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)15 MoveTargetEdit (org.eclipse.text.edits.MoveTargetEdit)15 TextEditGroup (org.eclipse.text.edits.TextEditGroup)15