/** * Applies all given changes to the given document. This method assumes that 'changes' contains only changes * intended for the given document; the actual URI stored in the changes is ignored. */ public void applyAllInSameDocument(Collection<? extends IAtomicChange> changes, IDocument document) throws BadLocationException { DocumentRewriteSession rewriteSession = null; try { // prepare if (document instanceof IDocumentExtension4) { rewriteSession = ((IDocumentExtension4) document).startRewriteSession( DocumentRewriteSessionType.UNRESTRICTED); } // perform replacements for (IAtomicChange currRepl : changes) { currRepl.apply(document); } } finally { // cleanup if (rewriteSession != null) ((IDocumentExtension4) document).stopRewriteSession(rewriteSession); } }
/** * Sets text to a document * @param document * @param message * @param append * @throws BadLocationException * Has to be run from non-UI thread */ public static synchronized void setDocumentText(final Document document, final String message, final boolean append) { UIHelper.runUIAsync(new Runnable() { public void run() { try { DocumentRewriteSession rewriteSession; if (append && !isDefaultLabel(document)) { rewriteSession = document.startRewriteSession(DocumentRewriteSessionType.SEQUENTIAL); // append to existing document (0 length is valid and means message is going to be appended) document.replace(document.getLength(), 0, message + ((message.endsWith(CR)) ? EMPTY : CR)); } else { rewriteSession = document.startRewriteSession(DocumentRewriteSessionType.STRICTLY_SEQUENTIAL); // replace of complete document document.replace(0, document.getLength(), message + ((message.endsWith(CR)) ? EMPTY : CR)); } document.stopRewriteSession(rewriteSession); } catch (BadLocationException ignored) { } } }); }
/** * Executes the text edits on the given document. Subclasses that override this method should call * <code>super.performEdits(document)</code>. * * @param document the document * @return an object representing the undo of the executed edits * @exception MalformedTreeException is thrown if the edit tree isn't in a valid state. This * exception is thrown before any edit is executed. So the document is still in its original * state. * @exception BadLocationException is thrown if one of the edits in the tree can't be executed. * The state of the document is undefined if this exception is thrown. * @since 3.5 */ protected UndoEdit performEdits(IDocument document) throws BadLocationException, MalformedTreeException { DocumentRewriteSession session = null; try { if (document instanceof IDocumentExtension4) { session = ((IDocumentExtension4) document) .startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED); } LinkedModeModel.closeAllModels(document); TextEditProcessor processor = createTextEditProcessor(document, TextEdit.CREATE_UNDO, false); return processor.performEdits(); } finally { if (session != null) { ((IDocumentExtension4) document).stopRewriteSession(session); } } }
protected UndoEdit performEdits(IDocument document) throws BadLocationException, MalformedTreeException { DocumentRewriteSession session= null; try { if (document instanceof IDocumentExtension4) { session= ((IDocumentExtension4)document).startRewriteSession( DocumentRewriteSessionType.UNRESTRICTED); } return undoEdit.apply(document); } finally { if (session != null) { ((IDocumentExtension4)document).stopRewriteSession(session); } } }
public DocumentRewriteSession startRewriteSession(DocumentRewriteSessionType sessionType) { Object lockObject = getLockObject(); if (lockObject == null) { return super.startRewriteSession(sessionType); } synchronized (lockObject) { return super.startRewriteSession(sessionType); } }
public void stopRewriteSession(DocumentRewriteSession session) { Object lockObject = getLockObject(); if (lockObject == null) { super.stopRewriteSession(session); return; } synchronized (lockObject) { super.stopRewriteSession(session); } }
private void performChangesInSynchronizationContext( final IDocument document, final LinkedList undoList, final boolean preview) throws BadLocationException { DocumentRewriteSession session = null; try { if (document instanceof IDocumentExtension4) session = ((IDocumentExtension4) document) .startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED); for (final Iterator iterator = fChanges.iterator(); iterator.hasNext(); ) { final ComposableBufferChange change = (ComposableBufferChange) iterator.next(); final UndoEdit edit = createTextEditProcessor( change, document, undoList != null ? TextEdit.CREATE_UNDO : TextEdit.NONE, preview) .performEdits(); if (undoList != null) undoList.addFirst(edit); } } finally { if (session != null) ((IDocumentExtension4) document).stopRewriteSession(session); } }
/** * Stop a rewrite session */ public static void endWrite(IDocument doc, DocumentRewriteSession session) { if (doc instanceof IDocumentExtension4 && session != null) { IDocumentExtension4 d = (IDocumentExtension4) doc; d.stopRewriteSession(session); } }
/** * Starts a rewrite session (keep things in a single undo/redo) */ public static DocumentRewriteSession startWrite(IDocument doc) { if (doc instanceof IDocumentExtension4) { IDocumentExtension4 d = (IDocumentExtension4) doc; return d.startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED); } return null; }
/** * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) */ @Override public void run(IAction action) { try { if (!canModifyEditor()) { return; } PyEdit pyEdit = getPyEdit(); PySelection ps = PySelectionFromEditor.createPySelectionFromEditor(pyEdit); final IDocument doc = ps.getDoc(); if (ps.getStartLineIndex() == ps.getEndLineIndex()) { organizeImports(pyEdit, doc, null, ps); } else { DocumentRewriteSession session = TextSelectionUtils.startWrite(doc); try { ps.performSimpleSort(doc, ps.getStartLineIndex(), ps.getEndLineIndex()); } finally { TextSelectionUtils.endWrite(doc, session); } } } catch (Exception e) { Log.log(e); beep(e); } }
public void startRewriteSession(DocumentRewriteSession session) throws IllegalStateException { if (fActiveRewriteSession != null) throw new IllegalStateException(); fActiveRewriteSession= session; }
/** * @since 2.2 */ public void startRewriteSession(DocumentRewriteSession session) throws IllegalStateException { if (fActiveRewriteSession != null) throw new IllegalStateException(); fActiveRewriteSession = session; }
public void startRewriteSession(DocumentRewriteSession session) throws IllegalStateException { if (fActiveRewriteSession != null) throw new IllegalStateException(); fActiveRewriteSession = session; }
@Override public DocumentRewriteSession startRewriteSession(DocumentRewriteSessionType sessionType) throws IllegalStateException { throw new RuntimeException("not implemented"); }
@Override public void stopRewriteSession(DocumentRewriteSession session) { throw new RuntimeException("not implemented"); }
@Override public DocumentRewriteSession getActiveRewriteSession() { throw new RuntimeException("not implemented"); }
/** * {@inheritDoc} * <p> * May be extended by subclasses. * </p> */ public void stopRewriteSession(DocumentRewriteSession session) { if (fActiveRewriteSession == session) flushRewriteSession(); }
/** * {@inheritDoc} * <p> * May be extended by subclasses. * </p> */ public DocumentRewriteSession getActiveRewriteSession() { return fActiveRewriteSession; }
/** * {@inheritDoc} * <p> * May be extended by subclasses. * </p> * * @since 2.2 */ public void stopRewriteSession(DocumentRewriteSession session) { if (fActiveRewriteSession == session) flushRewriteSession(); }
/** * {@inheritDoc} * <p> * May be extended by subclasses. * </p> * * @since 2.2 */ public DocumentRewriteSession getActiveRewriteSession() { return fActiveRewriteSession; }
/** * {@inheritDoc} * * <p>May be extended by subclasses. */ public void stopRewriteSession(DocumentRewriteSession session) { if (fActiveRewriteSession == session) flushRewriteSession(); }
/** * {@inheritDoc} * * <p>May be extended by subclasses. */ public DocumentRewriteSession getActiveRewriteSession() { return fActiveRewriteSession; }