Search in sources :

Example 6 with IStructuredTextUndoManager

use of org.eclipse.wst.sse.core.internal.undo.IStructuredTextUndoManager in project webtools.sourceediting by eclipse.

the class StructuredTextViewer method endRecording.

private void endRecording(int cursorPosition, int selectionLength) {
    IDocument doc = getDocument();
    if (doc instanceof IStructuredDocument) {
        IStructuredDocument structuredDocument = (IStructuredDocument) doc;
        IStructuredTextUndoManager undoManager = structuredDocument.getUndoManager();
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=198617
        // undo after paste in document with folds - wrong behavior
        IRegion widgetSelection = new Region(cursorPosition, selectionLength);
        IRegion documentSelection = widgetRange2ModelRange(widgetSelection);
        if (documentSelection == null)
            documentSelection = widgetSelection;
        undoManager.endRecording(this, documentSelection.getOffset(), documentSelection.getLength());
    } else {
    // TODO: how to handle other document types?
    }
}
Also used : IStructuredTextUndoManager(org.eclipse.wst.sse.core.internal.undo.IStructuredTextUndoManager) IRegion(org.eclipse.jface.text.IRegion) Region(org.eclipse.jface.text.Region) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion)

Example 7 with IStructuredTextUndoManager

use of org.eclipse.wst.sse.core.internal.undo.IStructuredTextUndoManager in project liferay-ide by liferay.

the class KaleoEdits method performOnDOMDocument.

/**
 * performs an modifying operation on top the
 *
 * @param file
 * @param operation
 * @throws IOException
 * @throws CoreException
 */
public static void performOnDOMDocument(KaleoEdits.OperationTuple... fileOperations) throws CoreException, IOException {
    for (OperationTuple tuple : fileOperations) {
        IDOMModel domModel = null;
        /*
			 * TODO we might want to attempt iterating opened editors and
			 * somehow initialize those that were not yet initialized. Then we
			 * could avoid saving a file that is actually opened, but was never
			 * used so far (after restart)
			 */
        try {
            DocumentRewriteSession session = null;
            IStructuredTextUndoManager undo = null;
            if (tuple.isReadOnly()) {
                IModelManager modelManager = StructuredModelManager.getModelManager();
                domModel = (IDOMModel) modelManager.getExistingModelForRead(tuple.getDocument());
                if (domModel == null) {
                    domModel = (IDOMModel) modelManager.getModelForRead((IStructuredDocument) tuple.getDocument());
                }
            } else {
                if (tuple.getModel() != null) {
                    domModel = tuple.getModel();
                } else {
                    if (tuple.getFile() != null) {
                        domModel = (IDOMModel) StructuredModelManager.getModelManager().getModelForEdit(tuple.getFile());
                    } else {
                        IModelManager modelManager = StructuredModelManager.getModelManager();
                        domModel = (IDOMModel) modelManager.getExistingModelForEdit(tuple.getDocument());
                    }
                }
                // shall be ok here..let the model know we make changes
                domModel.aboutToChangeModel();
                undo = domModel.getStructuredDocument().getUndoManager();
                if (domModel.getStructuredDocument() instanceof IDocumentExtension4) {
                    IDocumentExtension4 ext4 = (IDocumentExtension4) domModel.getStructuredDocument();
                    session = ext4.startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED_SMALL);
                }
                undo.beginRecording(domModel);
            }
            try {
                tuple.getOperation().process(domModel.getDocument());
            } finally {
                if (!tuple.isReadOnly()) {
                    undo.endRecording(domModel);
                    if ((session != null) && domModel.getStructuredDocument() instanceof IDocumentExtension4) {
                        IDocumentExtension4 ext4 = (IDocumentExtension4) domModel.getStructuredDocument();
                        ext4.stopRewriteSession(session);
                    }
                    domModel.changedModel();
                }
            }
        } finally {
            if (domModel != null) {
                if (tuple.isReadOnly()) {
                    domModel.releaseFromRead();
                } else {
                    /*
						 * for ducuments saving shall generally only happen when
						 * the model is not held elsewhere (eg. in opened view)
						 * for files, save always
						 */
                    if (tuple.isForceSave() || (domModel.getReferenceCountForEdit() == 1)) {
                        domModel.save();
                    }
                    domModel.releaseFromEdit();
                }
            }
        }
    }
}
Also used : IStructuredTextUndoManager(org.eclipse.wst.sse.core.internal.undo.IStructuredTextUndoManager) DocumentRewriteSession(org.eclipse.jface.text.DocumentRewriteSession) IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager)

Aggregations

IStructuredTextUndoManager (org.eclipse.wst.sse.core.internal.undo.IStructuredTextUndoManager)7 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)3 IDocument (org.eclipse.jface.text.IDocument)2 IRegion (org.eclipse.jface.text.IRegion)2 Region (org.eclipse.jface.text.Region)2 Command (org.eclipse.emf.common.command.Command)1 IAction (org.eclipse.jface.action.IAction)1 DocumentRewriteSession (org.eclipse.jface.text.DocumentRewriteSession)1 IDocumentExtension4 (org.eclipse.jface.text.IDocumentExtension4)1 ITextViewer (org.eclipse.jface.text.ITextViewer)1 StyledText (org.eclipse.swt.custom.StyledText)1 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)1 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)1