Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UndoHistoryImpl.cs « StandaloneUndo « Impl « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9f5bac5aff1c3e31e7cf5d11a6d59c51a71ebfde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//  Licensed under the MIT License. See License.txt in the project root for license information.
//
// This file contain implementations details that are subject to change without notice.
// Use at your own risk.
//
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Utilities;

namespace Microsoft.VisualStudio.Text.Operations.Standalone
{
    internal class UndoHistoryImpl : ITextUndoHistory
    {
        public event EventHandler<TextUndoRedoEventArgs> UndoRedoHappened;
        public event EventHandler<TextUndoTransactionCompletedEventArgs> UndoTransactionCompleted;

        #region Private Fields

        private UndoTransactionImpl currentTransaction;
        private Stack<ITextUndoTransaction> undoStack;
        private Stack<ITextUndoTransaction> redoStack;
        private DelegatedUndoPrimitiveImpl activeUndoOperationPrimitive;
        private TextUndoHistoryState state;
        private PropertyCollection properties;

        #endregion

        internal UndoHistoryRegistryImpl UndoHistoryRegistry;

        public UndoHistoryImpl(UndoHistoryRegistryImpl undoHistoryRegistry)
        {
            this.currentTransaction = null;
            this.UndoHistoryRegistry = undoHistoryRegistry;
            this.undoStack = new Stack<ITextUndoTransaction>();
            this.redoStack = new Stack<ITextUndoTransaction>();
            this.activeUndoOperationPrimitive = null;
            this.state = TextUndoHistoryState.Idle;
        }

        /// <summary>
        /// The full undo stack for this history. Does not include any currently opened or redo transactions.
        /// </summary>
        public IEnumerable<ITextUndoTransaction> UndoStack
        {
            get { return this.undoStack; }
        }

        /// <summary>
        /// The full redo stack for this history. Does not include any currently opened or undo transactions.
        /// </summary>
        public IEnumerable<ITextUndoTransaction> RedoStack
        {
            get { return this.redoStack; }
        }

        /// <summary>
        /// It returns most recently pushed (topmost) item of the <see cref="ITextUndoHistory.UndoStack"/> or if the stack is
        /// empty it returns null.
        /// </summary>
        public ITextUndoTransaction LastUndoTransaction
        {
            get 
            {
                if (this.undoStack.Count != 0)
                {
                    return this.undoStack.Peek();
                }

                return null;
            }
        }

        /// <summary>
        /// It returns most recently pushed (topmost) item of the <see cref="ITextUndoHistory.RedoStack"/> or if the stack is
        /// empty it returns null.
        /// </summary>
        public ITextUndoTransaction LastRedoTransaction
        {
            get 
            {
                if (this.redoStack.Count != 0)
                {
                    return this.redoStack.Peek();
                }

                return null;
            }
        }

        /// <summary>
        /// Whether a single undo is permissible (corresponds to the most recent visible undo UndoTransaction's CanUndo).        
        /// </summary>
        /// <remarks>
        /// If there are hidden transactions on top of the visible transaction, this property returns true only they are 
        /// undoable as well.
        /// </remarks>
        public bool CanUndo
        {
            get 
            {
                if (this.undoStack.Count > 0)
                {
                    return this.undoStack.Peek().CanUndo;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// Whether a single redo is permissible (corresponds to the most recent visible redo UndoTransaction's CanRedo).
        /// </summary>
        /// <remarks>
        /// If there are hidden transactions on top of the visible transaction, this property returns true only they are 
        /// redoable as well.
        /// </remarks>
        public bool CanRedo
        {
            get
            {
                if (this.redoStack.Count > 0)
                {
                    return this.redoStack.Peek().CanRedo;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// The most recent visible undo UndoTransactions's Description.
        /// </summary>
        public string UndoDescription
        {
            get 
            {
                if (this.undoStack.Count > 0)
                {
                    return this.undoStack.Peek().Description;
                }
                else
                {
                    return "Strings.HistoryCantUndo";
                }
            }
        }

        /// <summary>
        /// The most recent visible redo UndoTransaction's Description.
        /// </summary>
        public string RedoDescription
        {
            get 
            {
                if (this.undoStack.Count > 0)
                {
                    return this.redoStack.Peek().Description;
                }
                else
                {
                    return "Strings.HistoryCantRedo";
                }
            }
        }

        /// <summary>
        /// The current UndoTransaction in progress.
        /// </summary>
        public ITextUndoTransaction CurrentTransaction
        {
            get { return this.currentTransaction; }
        }

        /// <summary>
        /// 
        /// </summary>
        public TextUndoHistoryState State
        {
            get { return this.state; }
        }

        /// <summary>
        /// Creates a new transaction, nests it in the previously current transaction, and marks it current.
        /// If there is a redo stack, it gets cleared.
        /// UNDONE: should the redo-clearing happen now or when the new transaction is committed?
        /// </summary>
        /// <param name="description">A string description for the transaction.</param>
        /// <param name="isHidden">The new transaction.</param>
        /// <returns></returns>
        public ITextUndoTransaction CreateTransaction(string description)
        {
            if (String.IsNullOrEmpty(description))
            {
                throw new ArgumentNullException("description", String.Format(CultureInfo.CurrentUICulture, "Strings.ArgumentCannotBeNull", "CreateTransaction", "description"));
            }

            // If there is a pending transaction that has already been completed, we should not be permitted
            // to open a new transaction, since it cannot later be added to its parent.
            if ((this.currentTransaction != null) && (this.currentTransaction.State != UndoTransactionState.Open))
            {
                throw new InvalidOperationException("Strings.CannotCreateTransactionWhenCurrentTransactionNotOpen");
            }

            // new transactions that are visible should clear the redo stack.
            if (this.currentTransaction == null)
            {
                foreach (UndoTransactionImpl redoTransaction in this.redoStack)
                {
                    redoTransaction.Invalidate();
                }

                this.redoStack.Clear();
            }

            UndoTransactionImpl newTransaction = new UndoTransactionImpl(this, this.currentTransaction, description);

            this.currentTransaction = newTransaction;

            return this.currentTransaction;
        }

        /// <summary>
        /// Performs requested amount of undo operation and places the transactions on the redo stack.
        /// UNDONE: What if there is a currently opened transaction?
        /// </summary>
        /// <param name="count">The number of undo operations to perform. At the end of the operation, requested number of visible
        /// transactions are undone. Hence actual number of transactions undone might be more than this number if there are some 
        /// hidden transactions adjacent to (on top of or at the bottom of) the visible ones.
        /// </param>        
        /// <remarks>
        /// After the last visible transaction is undone, hidden transactions left on top the stack are undone as well until a 
        /// visible or linked transaction is encountered or stack is emptied totally.
        /// </remarks>
        public void Undo(int count)
        {
            if (count <= 0)
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentUICulture, "Strings.RedoAndUndoAcceptOnlyPositiveCounts", "Undo", count), "count");
            }

            if (!IsThereEnoughVisibleTransactions(this.undoStack, count))
            {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture, "Strings.CannotUndoMoreTransactionsThanExist", "undo", count));
            }

            TextUndoHistoryState originalState = this.state;
            this.state = TextUndoHistoryState.Undoing;
            using (new AutoEnclose(delegate { this.state = originalState; }))
            {
                while (count > 0)
                {
                    if (!this.undoStack.Peek().CanUndo)
                    {
                        throw new InvalidOperationException("Strings.CannotUndoRequestedPrimitiveFromHistoryUndo");
                    }

                    ITextUndoTransaction ut = this.undoStack.Pop();
                    ut.Undo();
                    this.redoStack.Push(ut);

                    RaiseUndoRedoHappened(this.state, ut);

                    --count;
                }
            }
        }

        /// <summary>
        /// Performs an undo operation and places the primitives on the redo stack, up until (and 
        /// including) the transaction indicated. This is called by the linked undo transaction that
        /// is aware of the linking relationship between transactions, and it does not call back into
        /// the transactions' public Undo().
        /// </summary>
        /// <param name="transaction"></param>
        public void UndoInIsolation(UndoTransactionImpl transaction)
        {
            TextUndoHistoryState originalState = this.state;
            this.state = TextUndoHistoryState.Undoing;
            using (new AutoEnclose(delegate { this.state = originalState; }))
            {

                if (this.undoStack.Contains(transaction))
                {
                    UndoTransactionImpl undone = null;
                    while (undone != transaction)
                    {
                        UndoTransactionImpl ut = this.undoStack.Pop() as UndoTransactionImpl;
                        ut.Undo();
                        this.redoStack.Push(ut);

                        RaiseUndoRedoHappened(this.state, ut); 

                        undone = ut;
                    }
                }
            }
        }

        /// <summary>
        /// Performs requested amount of redo operation and places the transactions on the undo stack.
        /// UNDONE: What if there is a currently opened transaction?
        /// </summary>
        /// <param name="count">The number of redo operations to perform. At the end of the operation, requested number of visible
        /// transactions are redone. Hence actual number of transactions redone might be more than this number if there are some 
        /// hidden transactions adjacent to (on top of or at the bottom of) the visible ones.
        /// </param>        
        /// <remarks>
        /// After the last visible transaction is redone, hidden transactions left on top the stack are redone as well until a 
        /// visible or linked transaction is encountered or stack is emptied totally.
        /// </remarks>
        public void Redo(int count)
        {
            if (count <= 0)
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentUICulture, "Strings.RedoAndUndoAcceptOnlyPositiveCounts", "Redo", count), "count");
            }

            if (!IsThereEnoughVisibleTransactions(this.redoStack, count))
            {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture, "Strings.CannotUndoMoreTransactionsThanExist", "redo", count));
            }

            TextUndoHistoryState originalState = this.state;
            this.state = TextUndoHistoryState.Redoing;
            using (new AutoEnclose(delegate { this.state = originalState; }))
            {
                while (count > 0)
                {
                    if (!this.redoStack.Peek().CanRedo)
                    {
                        throw new InvalidOperationException("Strings.CannotRedoRequestedPrimitiveFromHistoryRedo");
                    }
                    ITextUndoTransaction ut = this.redoStack.Pop();
                    ut.Do();
                    this.undoStack.Push(ut);

                    RaiseUndoRedoHappened(this.state, ut);

                    --count;
                }
            }
        }

        /// <summary>
        /// Performs a redo operation and places the primitives on the redo stack, up until (and 
        /// including) the transaction indicated. This is called by the linked undo transaction that
        /// is aware of the linking relationship between transactions, and it does not call back into
        /// the transactions' public Redo().
        /// </summary>
        /// <param name="transaction"></param>
        public void RedoInIsolation(UndoTransactionImpl transaction)
        {
            TextUndoHistoryState originalState = this.state;
            this.state = TextUndoHistoryState.Redoing;
            using (new AutoEnclose(delegate { this.state = originalState; }))
            {
                if (this.redoStack.Contains(transaction))
                {
                    UndoTransactionImpl redone = null;
                    while (redone != transaction)
                    {
                        UndoTransactionImpl ut = this.redoStack.Pop() as UndoTransactionImpl;
                        ut.Do();
                        this.undoStack.Push(ut);

                        RaiseUndoRedoHappened(this.state, ut); 

                        redone = ut;
                    }
                }
            }
        }
        
        /// <summary>
        /// This method is called from the DelegatedUndoPrimitive just as it starts a do or undo, so that this
        /// history knows to forward any new UndoableOperations to the primitive. This and its pair EndForward... only manage
        /// the state of the activeUndoOperationPrimitive.
        /// </summary>
        /// <param name="primitive">The delegated primitive to be marked active</param>
        public void ForwardToUndoOperation(DelegatedUndoPrimitiveImpl primitive)
        {
            if (this.activeUndoOperationPrimitive != null)
            {
                throw new InvalidOperationException();
            }

            this.activeUndoOperationPrimitive = primitive;
        }

        /// <summary>
        /// This method ends the lifetime of the activeUndoOperationPrimitive and should be called after ForwardToUndoOperation.
        /// </summary>
        /// <param name="primitive">The previously active delegated primitive--used for sanity check.</param>
        public void EndForwardToUndoOperation(DelegatedUndoPrimitiveImpl primitive)
        {
            if (this.activeUndoOperationPrimitive != primitive)
            {
                throw new InvalidOperationException();
            }

            this.activeUndoOperationPrimitive = null;
        }

        /// <summary>
        /// This is how the transactions alert their containing history that they have finished
        /// (likely from the Dispose() method). 
        /// </summary>
        /// <param name="transaction">This is the transaction that's finishing. It should match the history's current transaction.
        /// If it does not match, then the current transaction will be discarded and an exception will be thrown.</param>
        public void EndTransaction(ITextUndoTransaction transaction)
        {
            if (this.currentTransaction != transaction)
            {
                this.currentTransaction = null;
                throw new InvalidOperationException("Strings.EndTransactionOutOfOrder");
            }

            // only add completed transactions to their parents (or the stack)
            if (this.currentTransaction.State == UndoTransactionState.Completed)
            {
                if (this.currentTransaction.Parent == null) // stack bottomed out!
                {
                    MergeOrPushToUndoStack(this.currentTransaction);
                }
            }
            this.currentTransaction = this.currentTransaction.Parent as UndoTransactionImpl;
        }

        /// <summary>
        /// This does two different things, depending on the MergeUndoTransactionPolicys in question.
        /// It either simply pushes the current transaction to the undo stack, OR it merges it with
        /// the most recent item in the stack.
        /// </summary>
        private void MergeOrPushToUndoStack(UndoTransactionImpl transaction)
        {
            ITextUndoTransaction transactionAdded;
            TextUndoTransactionCompletionResult transactionResult;

            UndoTransactionImpl utPrevious = this.undoStack.Count > 0 ? this.undoStack.Peek() as UndoTransactionImpl : null;
            if (utPrevious != null && ProceedWithMerge(transaction, utPrevious))
            {
                // Temporarily make utPrevious non-read-only, during merge.
                utPrevious.IsReadOnly = false;
                try
                {
                    transaction.MergePolicy.PerformTransactionMerge(utPrevious, transaction);
                }
                finally
                {
                    utPrevious.IsReadOnly = true;
                }

                // utPrevious is already on the undo stack, so we don't need to add it; but report
                // it as the added transaction in the UndoTransactionCompleted event.
                transactionAdded = utPrevious;
                transactionResult = TextUndoTransactionCompletionResult.TransactionMerged;
            }
            else
            {
                this.undoStack.Push(transaction);

                transactionAdded = transaction;
                transactionResult = TextUndoTransactionCompletionResult.TransactionAdded;
            }
            RaiseUndoTransactionCompleted(transactionAdded, transactionResult);            
        }

        public bool ValidTransactionForMarkers(ITextUndoTransaction transaction)
        {
            return transaction == null                     // you can put a marker on the null transaction
                || this.currentTransaction == transaction  // you can put a marker on the currently active transaction
                || (transaction.History == this && !(transaction.State == UndoTransactionState.Invalid));
                                                           // and you can put a marker on any transaction in this history.
        }

        public static bool IsThereEnoughVisibleTransactions(Stack<ITextUndoTransaction> stack, int visibleCount)
        {
            if (visibleCount <= 0)
            {
                return true;
            }

            foreach (ITextUndoTransaction transaction in stack)
            {
                visibleCount--;

                if (visibleCount <= 0)
                {
                    return true;
                }
            }

            return false;
        }

        private bool ProceedWithMerge(UndoTransactionImpl transaction1, UndoTransactionImpl transaction2)
        {
            UndoHistoryRegistryImpl registry = UndoHistoryRegistry;

            return transaction1.MergePolicy != null
                && transaction2.MergePolicy != null
                && transaction1.MergePolicy.TestCompatiblePolicy(transaction2.MergePolicy)
                && transaction1.MergePolicy.CanMerge(transaction1, transaction2);
        }        

        private void RaiseUndoRedoHappened(TextUndoHistoryState state, ITextUndoTransaction transaction)
        {
            EventHandler<TextUndoRedoEventArgs> undoRedoHappened = UndoRedoHappened;
            if (undoRedoHappened != null)
            {
                undoRedoHappened(this, new TextUndoRedoEventArgs(state, transaction));
            }
        }

        private void RaiseUndoTransactionCompleted(ITextUndoTransaction transaction, TextUndoTransactionCompletionResult result)
        {
            EventHandler<TextUndoTransactionCompletedEventArgs> undoTransactionAdded = UndoTransactionCompleted;
            if (undoTransactionAdded != null)
            {
                undoTransactionAdded(this, new TextUndoTransactionCompletedEventArgs(transaction, result));
            }
        }

        public PropertyCollection Properties
        {
            get 
            {
                if (this.properties == null)
                {
                    this.properties = new PropertyCollection();
                }
                return this.properties;
            }
        }
    }
}