Search in sources :

Example 1 with UndoEdit

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

the class UndoDocumentChange method perform.

/**
	 * {@inheritDoc}
	 */
public Change perform(IProgressMonitor pm) throws CoreException {
    try {
        UndoEdit redo = performEdits();
        Change result = new UndoDocumentChange(getName(), fDocument, redo);
        return result;
    } catch (MalformedTreeException e) {
        throw Changes.asCoreException(e);
    } catch (BadLocationException e) {
        throw Changes.asCoreException(e);
    }
}
Also used : MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) Change(org.eclipse.ltk.core.refactoring.Change) UndoEdit(org.eclipse.text.edits.UndoEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with UndoEdit

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

the class MultiStateUndoChange method perform.

/**
	 * {@inheritDoc}
	 */
public Change perform(IProgressMonitor pm) throws CoreException {
    if (fValidationState == null || fValidationState.isValid(needsSaving(), false).hasFatalError())
        return new NullChange();
    if (pm == null)
        pm = new NullProgressMonitor();
    ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
    //$NON-NLS-1$
    pm.beginTask("", 2);
    ITextFileBuffer buffer = null;
    try {
        manager.connect(fFile.getFullPath(), LocationKind.IFILE, new SubProgressMonitor(pm, 1));
        buffer = manager.getTextFileBuffer(fFile.getFullPath(), LocationKind.IFILE);
        IDocument document = buffer.getDocument();
        ContentStamp currentStamp = ContentStamps.get(fFile, document);
        // perform the changes
        LinkedList list = new LinkedList();
        for (int index = 0; index < fUndos.length; index++) {
            UndoEdit edit = fUndos[index];
            UndoEdit redo = edit.apply(document, TextEdit.CREATE_UNDO);
            list.addFirst(redo);
        }
        // try to restore the document content stamp
        boolean success = ContentStamps.set(document, fContentStampToRestore);
        if (needsSaving()) {
            buffer.commit(pm, false);
            if (!success) {
                // We weren't able to restore document stamp.
                // Since we save restore the file stamp instead
                ContentStamps.set(fFile, fContentStampToRestore);
            }
        }
        return createUndoChange((UndoEdit[]) list.toArray(new UndoEdit[list.size()]), currentStamp);
    } catch (BadLocationException e) {
        throw Changes.asCoreException(e);
    } finally {
        if (buffer != null)
            manager.disconnect(fFile.getFullPath(), LocationKind.IFILE, new SubProgressMonitor(pm, 1));
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) NullChange(org.eclipse.ltk.core.refactoring.NullChange) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ContentStamp(org.eclipse.ltk.core.refactoring.ContentStamp) UndoEdit(org.eclipse.text.edits.UndoEdit) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IDocument(org.eclipse.jface.text.IDocument) LinkedList(java.util.LinkedList) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 3 with UndoEdit

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

the class TextFileChange method performEdits.

/*
	 * @see org.eclipse.ltk.core.refactoring.TextChange#performEdits(org.eclipse.jface.text.IDocument)
	 * @since 3.5
	 */
protected UndoEdit performEdits(final IDocument document) throws BadLocationException, MalformedTreeException {
    if (!fBuffer.isSynchronizationContextRequested()) {
        return super.performEdits(document);
    }
    ITextFileBufferManager fileBufferManager = FileBuffers.getTextFileBufferManager();
    /** 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] = TextFileChange.super.performEdits(document);
                } 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) UndoEdit(org.eclipse.text.edits.UndoEdit) BadLocationException(org.eclipse.jface.text.BadLocationException) Lock(org.eclipse.ltk.internal.core.refactoring.Lock)

Example 4 with UndoEdit

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

the class UndoTextFileChange method performEdits.

private UndoEdit performEdits(ITextFileBuffer buffer, final IDocument document, final boolean[] setContentStampSuccess) throws MalformedTreeException, BadLocationException, CoreException {
    if (!buffer.isSynchronizationContextRequested()) {
        return doPerformEdits(document, setContentStampSuccess);
    }
    ITextFileBufferManager fileBufferManager = FileBuffers.getTextFileBufferManager();
    /** 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[] badLocationException = new BadLocationException[1];
    final MalformedTreeException[] malformedTreeException = new MalformedTreeException[1];
    final CoreException[] coreException = new CoreException[1];
    Runnable runnable = new Runnable() {

        public void run() {
            synchronized (completionLock) {
                try {
                    result[0] = doPerformEdits(document, setContentStampSuccess);
                } catch (BadLocationException e) {
                    badLocationException[0] = e;
                } catch (MalformedTreeException e) {
                    malformedTreeException[0] = e;
                } catch (CoreException e) {
                    coreException[0] = e;
                } finally {
                    completionLock.fDone = true;
                    completionLock.notifyAll();
                }
            }
        }
    };
    synchronized (completionLock) {
        fileBufferManager.execute(runnable);
        while (!completionLock.fDone) {
            try {
                completionLock.wait(500);
            } catch (InterruptedException x) {
            }
        }
    }
    if (badLocationException[0] != null) {
        throw badLocationException[0];
    } else if (malformedTreeException[0] != null) {
        throw malformedTreeException[0];
    } else if (coreException[0] != null) {
        throw coreException[0];
    }
    return result[0];
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) UndoEdit(org.eclipse.text.edits.UndoEdit) BadLocationException(org.eclipse.jface.text.BadLocationException) Lock(org.eclipse.ltk.internal.core.refactoring.Lock)

Example 5 with UndoEdit

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

the class UndoTextFileChange method doPerformEdits.

private UndoEdit doPerformEdits(IDocument document, boolean[] setContentStampSuccess) throws MalformedTreeException, BadLocationException, CoreException {
    // perform the changes
    LinkedModeModel.closeAllModels(document);
    UndoEdit redo = fUndo.apply(document, TextEdit.CREATE_UNDO);
    // try to restore the document content stamp
    setContentStampSuccess[0] = ContentStamps.set(document, fContentStampToRestore);
    return redo;
}
Also used : UndoEdit(org.eclipse.text.edits.UndoEdit)

Aggregations

UndoEdit (org.eclipse.text.edits.UndoEdit)12 BadLocationException (org.eclipse.jface.text.BadLocationException)10 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)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 CoreException (org.eclipse.core.runtime.CoreException)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 Iterator (java.util.Iterator)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)1 DocumentRewriteSession (org.eclipse.jface.text.DocumentRewriteSession)1 IDocumentExtension4 (org.eclipse.jface.text.IDocumentExtension4)1 IRegion (org.eclipse.jface.text.IRegion)1 Change (org.eclipse.ltk.core.refactoring.Change)1 ContentStamp (org.eclipse.ltk.core.refactoring.ContentStamp)1 NullChange (org.eclipse.ltk.core.refactoring.NullChange)1