Search in sources :

Example 31 with UndoEdit

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

the class MultiStateTextFileChange method perform.

/*
	 * @see org.eclipse.ltk.core.refactoring.Change#perform(org.eclipse.core.runtime.IProgressMonitor)
	 */
public final Change perform(final IProgressMonitor monitor) throws CoreException {
    //$NON-NLS-1$
    monitor.beginTask("", 3);
    IDocument document = null;
    try {
        document = acquireDocument(new SubProgressMonitor(monitor, 1));
        final LinkedList undoList = new LinkedList();
        performChanges(document, undoList, false);
        if (needsSaving())
            fBuffer.commit(new SubProgressMonitor(monitor, 1), false);
        return new MultiStateUndoChange(getName(), fFile, (UndoEdit[]) undoList.toArray(new UndoEdit[undoList.size()]), fContentStamp, fSaveMode);
    } catch (BadLocationException exception) {
        throw Changes.asCoreException(exception);
    } finally {
        if (document != null) {
            releaseDocument(document, new SubProgressMonitor(monitor, 1));
        }
        monitor.done();
    }
}
Also used : MultiStateUndoChange(org.eclipse.ltk.internal.core.refactoring.MultiStateUndoChange) UndoEdit(org.eclipse.text.edits.UndoEdit) IDocument(org.eclipse.jface.text.IDocument) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) LinkedList(java.util.LinkedList) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 32 with UndoEdit

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

the class UndoDocumentChange method performEdits.

private UndoEdit performEdits() throws BadLocationException, MalformedTreeException {
    ITextFileBufferManager fileBufferManager = FileBuffers.getTextFileBufferManager();
    ITextFileBuffer fileBuffer = fileBufferManager.getTextFileBuffer(fDocument);
    if (fileBuffer == null || !fileBuffer.isSynchronizationContextRequested()) {
        return fUndo.apply(fDocument, TextEdit.CREATE_UNDO);
    }
    /** The lock for waiting for computation in the UI thread to complete. */
    final Lock completionLock = new Lock();
    final UndoEdit[] result = new UndoEdit[1];
    final BadLocationException[] exception = new BadLocationException[1];
    Runnable runnable = new Runnable() {

        public void run() {
            synchronized (completionLock) {
                try {
                    result[0] = fUndo.apply(fDocument, TextEdit.CREATE_UNDO);
                } catch (BadLocationException e) {
                    exception[0] = e;
                } finally {
                    completionLock.fDone = true;
                    completionLock.notifyAll();
                }
            }
        }
    };
    synchronized (completionLock) {
        fileBufferManager.execute(runnable);
        while (!completionLock.fDone) {
            try {
                completionLock.wait(500);
            } catch (InterruptedException x) {
            }
        }
    }
    if (exception[0] != null) {
        throw exception[0];
    }
    return result[0];
}
Also used : ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) UndoEdit(org.eclipse.text.edits.UndoEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 33 with UndoEdit

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

the class TextEditTests method testInsertReplace1.

@Test
public void testInsertReplace1() throws Exception {
    TextEdit e1 = new ReplaceEdit(2, 1, "y");
    TextEdit e2 = new InsertEdit(2, "xx");
    fRoot.addChild(e1);
    fRoot.addChild(e2);
    UndoEdit undo = fRoot.apply(fDocument);
    assertEquals(fRoot, 2, 3);
    assertEquals(e1, 4, 1);
    assertEquals(e2, 2, 2);
    Assert.assertEquals("Buffer content", "01xxy3456789", fDocument.get());
    doUndoRedo(undo, "01xxy3456789");
}
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 34 with UndoEdit

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

the class TextEditTests method testNestedCopySourceWithInsert.

@Test
public void testNestedCopySourceWithInsert() throws Exception {
    CopySourceEdit s1 = new CopySourceEdit(1, 5);
    CopySourceEdit s2 = new CopySourceEdit(2, 3);
    CopySourceEdit s3 = new CopySourceEdit(3, 1);
    InsertEdit i1 = new InsertEdit(4, "x");
    s1.addChild(s2);
    s2.addChild(s3);
    s3.addChild(i1);
    CopyTargetEdit t1 = new CopyTargetEdit(9, s1);
    CopyTargetEdit t2 = new CopyTargetEdit(8, s2);
    CopyTargetEdit t3 = new CopyTargetEdit(7, s3);
    fRoot.addChild(s1);
    fRoot.addChild(t1);
    fRoot.addChild(t2);
    fRoot.addChild(t3);
    UndoEdit undo = fRoot.apply(fDocument);
    assertEquals(s1, 1, 6);
    assertEquals(s2, 2, 4);
    assertEquals(s3, 3, 2);
    assertEquals(i1, 4, 1);
    assertEquals(t1, 16, 6);
    assertEquals(t2, 11, 4);
    assertEquals(t3, 8, 2);
    String result = "0123x4563x723x48123x459";
    Assert.assertEquals("Buffer content", result, fDocument.get());
    doUndoRedo(undo, result);
}
Also used : CopySourceEdit(org.eclipse.text.edits.CopySourceEdit) InsertEdit(org.eclipse.text.edits.InsertEdit) CopyTargetEdit(org.eclipse.text.edits.CopyTargetEdit) UndoEdit(org.eclipse.text.edits.UndoEdit) Test(org.junit.Test)

Example 35 with UndoEdit

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

the class TextEditTests method testMove5.

@Test
public void testMove5() throws Exception {
    // Move onto itself
    MoveSourceEdit s1 = new MoveSourceEdit(2, 1);
    MoveTargetEdit t1 = new MoveTargetEdit(3, s1);
    TextEdit e2 = new ReplaceEdit(2, 1, "x");
    s1.addChild(e2);
    fRoot.addChild(s1);
    fRoot.addChild(t1);
    UndoEdit undo = fRoot.apply(fDocument);
    assertEquals(s1, 2, 0);
    assertEquals(t1, 2, 1);
    assertEquals(e2, 2, 1);
    Assert.assertEquals("Buffer content", "01x3456789", fDocument.get());
    doUndoRedo(undo, "01x3456789");
}
Also used : MoveTargetEdit(org.eclipse.text.edits.MoveTargetEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) MoveSourceEdit(org.eclipse.text.edits.MoveSourceEdit) UndoEdit(org.eclipse.text.edits.UndoEdit) Test(org.junit.Test)

Aggregations

UndoEdit (org.eclipse.text.edits.UndoEdit)53 Test (org.junit.Test)40 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)25 TextEdit (org.eclipse.text.edits.TextEdit)25 MoveSourceEdit (org.eclipse.text.edits.MoveSourceEdit)23 MoveTargetEdit (org.eclipse.text.edits.MoveTargetEdit)23 InsertEdit (org.eclipse.text.edits.InsertEdit)12 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)12 BadLocationException (org.eclipse.jface.text.BadLocationException)10 DeleteEdit (org.eclipse.text.edits.DeleteEdit)9 RangeMarker (org.eclipse.text.edits.RangeMarker)8 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)6 CopySourceEdit (org.eclipse.text.edits.CopySourceEdit)6 CopyTargetEdit (org.eclipse.text.edits.CopyTargetEdit)6 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)5 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)4 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)4 IDocument (org.eclipse.jface.text.IDocument)4 Lock (org.eclipse.ltk.internal.core.refactoring.Lock)3 LinkedList (java.util.LinkedList)2