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

UndoTransactionImpl.cs « StandaloneUndo « Impl « Text « Editor « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7bc7db8bf3639f3d3ea77722931d08c1ce71195c (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
//
//  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;

namespace Microsoft.VisualStudio.Text.Operations.Standalone
{
    internal class UndoTransactionImpl : ITextUndoTransaction
    {
        #region Private Fields

        private UndoHistoryImpl history;
        private readonly UndoTransactionImpl parent;

        private string description;
        private UndoTransactionState state;
        private List<ITextUndoPrimitive> primitives;
        private IMergeTextUndoTransactionPolicy mergePolicy;
        internal bool _isDisposed = false;

        #endregion

        public UndoTransactionImpl(ITextUndoHistory history, ITextUndoTransaction parent, string description)
        {
            if (history == null)
            {
                throw new ArgumentNullException(nameof(history));
            }

            if (string.IsNullOrEmpty(description))
            {
                throw new ArgumentNullException(nameof(description));
            }

            this.history = history as UndoHistoryImpl;

            if (this.history == null)
            {
                throw new ArgumentException("Strings.InvalidHistoryInTransaction");
            }

            this.parent = parent as UndoTransactionImpl;

            if (this.parent == null && parent != null)
            {
                throw new ArgumentException("Strings.InvalidParentInTransaction");
            }

            this.description = description;

            this.state = UndoTransactionState.Open;
            this.primitives = new List<ITextUndoPrimitive>();
            this.mergePolicy = NullMergeUndoTransactionPolicy.Instance;
            this.IsReadOnly = true;
        }

        /// <summary>
        /// This is how you turn transaction into "Invalid" state. Use it to indicate that this transaction is retired forever,
        /// such as when clearing transactions from the redo stack.
        /// </summary>
        internal void Invalidate()
        {
            this.state = UndoTransactionState.Invalid;
        }

        internal bool IsInvalid
        {
            get { return this.state == UndoTransactionState.Invalid; }
        }

        /// <summary>
        /// Used by UndoHistoryImpl.cs to allow UndoPrimitives to be modified during merging.
        /// </summary>
        internal bool IsReadOnly { get; set; }

        /// <summary>
        /// Description is the [localized] string that describes the transaction to a user.
        /// </summary>
        public string Description
        {
            get { return this.description; }
            set { this.description = value; }
        }

        /// <summary>
        /// State is the UndoTransactionState for the UndoTransaction, as described in that type.
        /// </summary>
        public UndoTransactionState State
        {
            get { return this.state; }
        }

        /// <summary>
        /// History is a reference to the UndoHistory that contains this transaction.
        /// </summary>
        public ITextUndoHistory History
        {
            get { return this.history; }
        }

        /// <summary>
        /// UndoPrimitives allows access to the list of primitives in this transaction container, but should only be called
        /// after the transaction has been completed. 
        /// </summary>
        public IList<ITextUndoPrimitive> UndoPrimitives
        {
            get 
            {
                if (this.IsReadOnly)
                    return this.primitives.AsReadOnly();
                else
                    return this.primitives;
            }
        }

        /// <summary>
        /// Complete marks the transaction finished and eligible for Undo.
        /// </summary>
        public void Complete()
        {
            if (this.State != UndoTransactionState.Open)
            {
                throw new InvalidOperationException("Strings.CompleteCalledOnTransationThatIsNotOpened");
            }

            this.state = UndoTransactionState.Completed;
            
            // now we need to pump these primitives into the parent, if the parent exists.
            FlattenPrimitivesToParent();
        }

        /// <summary>
        /// This is called by the transaction when it is complete. It results in the parent getting
        /// all of this transaction's undo history, so that transactions are not really recursive (they
        /// exist for rollback).
        /// </summary>
        public void FlattenPrimitivesToParent()
        {
            if (this.parent != null)
            {
                // first, copy up each primitive. 
                this.parent.CopyPrimitivesFrom(this);

                // once all the primitives are in the parent, just clear them so
                // no one has a chance to tweak them here, or do/undo us.
                this.primitives.Clear();
            }
        }

        /// <summary>
        /// Copies all of the primitives from the given transaction, and appends them to the UndoPrimitives list.
        /// </summary>
        /// <param name="transaction">The UndoTransactionImpl to copy from.</param>
        public void CopyPrimitivesFrom(UndoTransactionImpl transaction)
        {
            foreach (ITextUndoPrimitive p in transaction.UndoPrimitives)
            {
                this.AddUndo(p);
            }
        }

        /// <summary>
        /// Cancel marks an Open transaction Canceled, and Undoes and clears any primitives that have been added.
        /// </summary>
        public void Cancel()
        {
            if (this.State != UndoTransactionState.Open)
            {
                throw new InvalidOperationException("Strings.CancelCalledOnTransationThatIsNotOpened");
            }

            for (int i = primitives.Count - 1; i >= 0; --i)
            {
                primitives[i].Undo();
            }

            this.primitives.Clear();
            this.state = UndoTransactionState.Canceled;
        }

        /// <summary>
        /// AddUndo adds a new primitive to the end of the list when the transaction is Open.
        /// </summary>
        /// <param name="undo"></param>
        public void AddUndo(ITextUndoPrimitive undo)
        {
            if (State != UndoTransactionState.Open)
            {
                throw new InvalidOperationException("Strings.AddUndoCalledOnTransationThatIsNotOpened");
            }

            this.primitives.Add(undo);
            undo.Parent = this;

            MergeMostRecentUndoPrimitive();
        }

        /// <summary>
        /// This is called by AddUndo, so that primitives are always in a fully merged state as we go.
        /// </summary>
        protected void MergeMostRecentUndoPrimitive()
        {
            // no merging unless there are at least two items
            if (primitives.Count < 2)
            {
                return;
            }

            ITextUndoPrimitive top = primitives[primitives.Count - 1];

            ITextUndoPrimitive victim = null;
            int victimIndex = -1;

            for (int i = primitives.Count - 2; i >= 0; --i)
            {
                if (top.GetType() == primitives[i].GetType() && top.CanMerge(primitives[i]))
                {
                    victim = primitives[i];
                    victimIndex = i;
                    break;
                }
            }

            if (victim != null)
            {
                ITextUndoPrimitive newPrimitive = top.Merge(victim);
                primitives.RemoveRange(primitives.Count - 1, 1);
                primitives.RemoveRange(victimIndex, 1);
                primitives.Add(newPrimitive);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public ITextUndoTransaction Parent
        {
            get { return parent; }
        }

        /// <summary>
        /// This is true iff every contained primitive is CanRedo and we are in an Undone state.
        /// </summary>
        public bool CanRedo
        {
            get
            {
                if (this.state == UndoTransactionState.Invalid)
                {
                    return true;
                }

                if (this.State != UndoTransactionState.Undone)
                {
                    return false;
                }

                foreach (ITextUndoPrimitive primitive in UndoPrimitives)
                {
                    if (!primitive.CanRedo)
                    {
                        return false;
                    }
                }

                return true;                
            }
        }

        /// <summary>
        /// This is true iff every contained primitive is CanUndo and we are in a Completed state.
        /// </summary>
        public bool CanUndo
        {
            get 
            {
                if (this.state == UndoTransactionState.Invalid)
                {
                    return true;
                }

                if (this.State != UndoTransactionState.Completed)
                {
                    return false;
                }

                foreach (ITextUndoPrimitive primitive in UndoPrimitives)
                {
                    if (!primitive.CanUndo)
                    {
                        return false;
                    }
                }

                return true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void Do()
        {
            if (this.state == UndoTransactionState.Invalid)
            {
                return;
            }

            if (!CanRedo)
            {
                throw new InvalidOperationException("Strings.DoCalledButCanRedoFalse");
            }

            this.state = UndoTransactionState.Redoing;

            for (int i = 0; i < primitives.Count; ++i)
            {
                primitives[i].Do();
            }

            this.state = UndoTransactionState.Completed;
        }

        /// <summary>
        /// This defers to the linked transaction if there is one.
        /// </summary>
        public void Undo()
        {
            if (this.state == UndoTransactionState.Invalid)
            {
                return;
            }

            if (!CanUndo)
            {
                throw new InvalidOperationException("Strings.UndoCalledButCanUndoFalse");
            }

            this.state = UndoTransactionState.Undoing;

            for (int i = primitives.Count - 1; i >= 0; --i)
            {
                primitives[i].Undo();
            }

            this.state = UndoTransactionState.Undone;
        }

        /// <summary>
        /// 
        /// </summary>
        public IMergeTextUndoTransactionPolicy MergePolicy
        {
            get { return this.mergePolicy; }
            set 
            {
                if (value == null)
                {
                    throw new ArgumentNullException(nameof(value));
                }

                this.mergePolicy = value; 
            }
        }

#pragma warning disable CA1063 // Implement IDisposable Correctly
                              /// <summary>
                              /// Closes a transaction and disposes it.
                              /// </summary>
        public void Dispose()
#pragma warning restore CA1063 // Implement IDisposable Correctly
        {
            if (!_isDisposed)
            {
                _isDisposed = true;

                GC.SuppressFinalize(this);
                switch (this.State)
                {
                    case UndoTransactionState.Open:
                        Cancel();
                        break;

                    case UndoTransactionState.Canceled:
                    case UndoTransactionState.Completed:
                        break;

                    case UndoTransactionState.Redoing:
                    case UndoTransactionState.Undoing:
                    case UndoTransactionState.Undone:
                        throw new InvalidOperationException("Strings.ClosingAnOpenTransactionThatAppearsToBeUndoneOrUndoing");
                }

                this.history.EndTransaction(this);
            }
        }

    }
}