/// <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));
            }
        }
Ejemplo n.º 2
0
        /// <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;
        }