/// <summary> /// Move the caret and restore the selection as part of the Redo operation. /// </summary> protected virtual void DoMoveCaretAndSelect(ITextView view, IMapEditToData map) { SnapshotPoint newCaret = new SnapshotPoint(view.TextSnapshot, BeforeTextBufferChangeUndoPrimitive.MapToEdit(map, _newCaretIndex)); view.Caret.MoveTo(newCaret, (PositionAffinity)_newCaretAffinityByte); view.Selection.Clear(); }
/// <summary> /// Constructs a BeforeTextBufferChangeUndoPrimitive. /// </summary> /// <param name="textView"> /// The text view that was responsible for causing this change. /// </param> /// <param name="undoHistory"> /// The <see cref="ITextUndoHistory" /> this primitive will be added to. /// </param> /// <exception cref="ArgumentNullException"><paramref name="textView"/> is null.</exception> /// <exception cref="ArgumentNullException"><paramref name="undoHistory"/> is null.</exception> public static BeforeTextBufferChangeUndoPrimitive Create(ITextView textView, ITextUndoHistory undoHistory) { if (textView == null) { throw new ArgumentNullException("textView"); } if (undoHistory == null) { throw new ArgumentNullException("undoHistory"); } // Store the ITextView for these changes in the ITextUndoHistory properties so we can retrieve it later. if (!undoHistory.Properties.ContainsProperty(typeof(ITextView))) { undoHistory.Properties[typeof(ITextView)] = textView; } CaretPosition caret = textView.Caret.Position; IMapEditToData map = BeforeTextBufferChangeUndoPrimitive.GetMap(textView); int oldCaretIndex = BeforeTextBufferChangeUndoPrimitive.MapToData(map, caret.BufferPosition); int oldCaretVirtualSpaces = caret.VirtualBufferPosition.VirtualSpaces; VirtualSnapshotPoint anchor = textView.Selection.AnchorPoint; int oldSelectionAnchorIndex = BeforeTextBufferChangeUndoPrimitive.MapToData(map, anchor.Position); int oldSelectionAnchorVirtualSpaces = anchor.VirtualSpaces; VirtualSnapshotPoint active = textView.Selection.ActivePoint; int oldSelectionActiveIndex = BeforeTextBufferChangeUndoPrimitive.MapToData(map, active.Position); int oldSelectionActiveVirtualSpaces = active.VirtualSpaces; TextSelectionMode oldSelectionMode = textView.Selection.Mode; if (oldCaretVirtualSpaces != 0 || oldSelectionAnchorIndex != oldCaretIndex || oldSelectionAnchorVirtualSpaces != 0 || oldSelectionActiveIndex != oldCaretIndex || oldSelectionActiveVirtualSpaces != 0 || oldSelectionMode != TextSelectionMode.Stream) { return(new GeneralBeforeTextBufferChangeUndoPrimitive (undoHistory, oldCaretIndex, caret.Affinity, oldCaretVirtualSpaces, oldSelectionAnchorIndex, oldSelectionAnchorVirtualSpaces, oldSelectionActiveIndex, oldSelectionActiveVirtualSpaces, oldSelectionMode)); } else { return(new BeforeTextBufferChangeUndoPrimitive(undoHistory, oldCaretIndex, caret.Affinity)); } }
/// <summary> /// Move the caret and restore the selection as part of the Redo operation. /// </summary> protected override void DoMoveCaretAndSelect(ITextView view, IMapEditToData map) { SnapshotPoint newCaret = new SnapshotPoint(view.TextSnapshot, BeforeTextBufferChangeUndoPrimitive.MapToEdit(map, _newCaretIndex)); SnapshotPoint newAnchor = new SnapshotPoint(view.TextSnapshot, BeforeTextBufferChangeUndoPrimitive.MapToEdit(map, _newSelectionAnchorIndex)); SnapshotPoint newActive = new SnapshotPoint(view.TextSnapshot, BeforeTextBufferChangeUndoPrimitive.MapToEdit(map, _newSelectionActiveIndex)); view.Caret.MoveTo(new VirtualSnapshotPoint(newCaret, _newCaretVirtualSpaces), (PositionAffinity)_newCaretAffinityByte); view.Selection.Mode = _newSelectionMode; var virtualAnchor = new VirtualSnapshotPoint(newAnchor, _newSelectionAnchorVirtualSpaces); var virtualActive = new VirtualSnapshotPoint(newActive, _newSelectionActiveVirtualSpaces); // Buffer may have been changed by one of the listeners on the caret move event. virtualAnchor = virtualAnchor.TranslateTo(view.TextSnapshot); virtualActive = virtualActive.TranslateTo(view.TextSnapshot); view.Selection.Select(virtualAnchor, virtualActive); }
/// <summary> /// Do the action. /// </summary> /// <exception cref="InvalidOperationException">Operation cannot be redone.</exception> public override void Do() { // Validate, we shouldn't be allowed to undo if (!CanRedo) { throw new InvalidOperationException(Strings.CannotRedo); } // Set the new caret position and active selection var view = this.GetTextView(); Debug.Assert(view == null || !view.IsClosed, "Attempt to undo/redo on a closed view? This shouldn't happen."); if (view != null && !view.IsClosed) { DoMoveCaretAndSelect(view, BeforeTextBufferChangeUndoPrimitive.GetMap(view)); view.Caret.EnsureVisible(); } _canUndo = true; }
/// <summary> /// Undo the action. /// </summary> /// <exception cref="InvalidOperationException">Operation cannot be undone.</exception> public override void Undo() { // Validate that we can undo this change if (!CanUndo) { throw new InvalidOperationException(Strings.CannotUndo); } // Restore the old caret position and active selection var view = this.GetTextView(); Debug.Assert(view == null || !view.IsClosed, "Attempt to undo/redo on a closed view? This shouldn't happen."); if (view != null && !view.IsClosed) { UndoMoveCaretAndSelect(view, BeforeTextBufferChangeUndoPrimitive.GetMap(view)); view.Caret.EnsureVisible(); } _canUndo = false; }