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

ElisionBuffer.cs « Projection « TextModel « Impl « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f47748e9d736249e1e52fc1e7a564397b6172bd2 (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
//
//  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.
//
namespace Microsoft.VisualStudio.Text.Projection.Implementation
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Diagnostics;

    using Microsoft.VisualStudio.Text.Utilities;
    using Microsoft.VisualStudio.Text.Implementation;
    using Microsoft.VisualStudio.Text.Differencing;
    using Microsoft.VisualStudio.Utilities;

    internal sealed class ElisionBuffer : BaseProjectionBuffer, IElisionBuffer
    {
        #region ElisionEdit class
        private class ElisionEdit : Edit, ISubordinateTextEdit
        {
            private ElisionBuffer elisionBuffer;
            private bool subordinate;

            public ElisionEdit(ElisionBuffer elisionBuffer, ITextSnapshot originSnapshot, EditOptions options, int? reiteratedVersionNumber, object editTag)
                : base(elisionBuffer, originSnapshot, options, reiteratedVersionNumber, editTag)
            {
                this.elisionBuffer = elisionBuffer;
                this.subordinate = true;
            }

            public ITextBuffer TextBuffer
            {
                get { return this.elisionBuffer; }
            }

            // this is the master edit path -- initiated from outside
            protected override ITextSnapshot PerformApply()
            {
                CheckActive();
                this.applied = true;
                this.subordinate = false;

                ITextSnapshot result = this.baseBuffer.currentSnapshot;

                if (this.changes.Count > 0)
                {
                    this.elisionBuffer.group.PerformMasterEdit(this.elisionBuffer, this, this.options, this.editTag);

                    if (!this.Canceled)
                    {
                        result = this.baseBuffer.currentSnapshot;
                    }
                }
                else
                {
                    // vacuous edit
                    this.baseBuffer.editInProgress = false;
                }

                return result;
            }

            public void PreApply()
            {
                if (this.changes.Count > 0)
                {
                    this.elisionBuffer.ComputeSourceEdits(this.changes);
                }
            }

            public void FinalApply()
            {
                if (this.changes.Count > 0 || this.elisionBuffer.pendingContentChangedEventArgs.Count > 0)
                {
                    this.elisionBuffer.group.CancelIndependentEdit(this.elisionBuffer);   // just in case
                    TextContentChangedEventRaiser raiser = this.elisionBuffer.IncorporateChanges();
                    this.baseBuffer.group.EnqueueEvents(raiser, this.baseBuffer);
                    raiser.RaiseEvent(this.baseBuffer, true);
                }

                this.elisionBuffer.editInProgress = false;
                this.elisionBuffer.editApplicationInProgress = false;
                if (this.subordinate)
                {
                    this.elisionBuffer.group.FinishEdit();
                }
            }

            public override void CancelApplication()
            {
                if (!this.Canceled)
                {
                    base.CancelApplication();
                    this.elisionBuffer.editApplicationInProgress = false;
                    this.elisionBuffer.pendingContentChangedEventArgs.Clear();
                }
            }
        }
        #endregion

        #region Private members, Construction, and Disposal
        private ElisionBufferOptions elisionOptions;
        private ElisionMap content;
        private ElisionSnapshot currentElisionSnapshot;
        private readonly ITextBuffer sourceBuffer;
        private ITextSnapshot sourceSnapshot;
        private WeakEventHook eventHook;

        public ElisionBuffer(IProjectionEditResolver resolver, 
                             IContentType contentType, 
                             ITextBuffer sourceBuffer,
                             NormalizedSpanCollection exposedSpans,
                             ElisionBufferOptions options,
                             ITextDifferencingService textDifferencingService,
                             GuardedOperations guardedOperations)
            : base(resolver, contentType, textDifferencingService, guardedOperations)
        {
            Debug.Assert(sourceBuffer != null);
            this.sourceBuffer = sourceBuffer;
            this.sourceSnapshot = sourceBuffer.CurrentSnapshot;

            BaseBuffer baseSourceBuffer = (BaseBuffer)sourceBuffer;

            this.eventHook = new WeakEventHook(this, baseSourceBuffer);

            this.group = baseSourceBuffer.group;
            this.group.AddMember(this);

            this.content = new ElisionMap(this.sourceSnapshot, exposedSpans);

            StringRebuilder newBuilder = StringRebuilder.Empty;
            for (int i = 0; (i < exposedSpans.Count); ++i)
                newBuilder = newBuilder.Append(BufferFactoryService.StringRebuilderFromSnapshotAndSpan(this.sourceSnapshot, exposedSpans[i]));
            this.builder = newBuilder;

            this.elisionOptions = options;
            this.currentVersion.SetLength(content.Length);
            this.currentElisionSnapshot = new ElisionSnapshot(this, this.sourceSnapshot, base.currentVersion, this.builder, this.content, (options & ElisionBufferOptions.FillInMappingMode) != 0);
            this.currentSnapshot = this.currentElisionSnapshot;
        }
        #endregion

        #region Source Buffer
        public override IList<ITextBuffer> SourceBuffers 
        {
            get { return new FrugalList<ITextBuffer>() { this.sourceBuffer }; }
        }

        public ITextBuffer SourceBuffer 
        {
            get { return this.sourceBuffer; }
        }

        public ElisionBufferOptions Options
        {
            get { return this.elisionOptions; }
        }

        #endregion

        #region ElisionSourceSpansChangedEventRaiser Class
        private class ElisionSourceSpansChangedEventRaiser : ITextEventRaiser
        {
            private readonly ElisionSourceSpansChangedEventArgs args;

            public ElisionSourceSpansChangedEventRaiser(ElisionSourceSpansChangedEventArgs args)
            {
                this.args = args;
            }

            public void RaiseEvent(BaseBuffer baseBuffer, bool immediate)
            {
                ElisionBuffer elBuffer = (ElisionBuffer)baseBuffer;
                EventHandler<ElisionSourceSpansChangedEventArgs> spanHandlers = elBuffer.SourceSpansChanged;
                if (spanHandlers != null)
                {
                    spanHandlers(this, args);
                }

                // now raise the text content changed event
                baseBuffer.RawRaiseEvent(args, immediate);
            }

            public bool HasPostEvent
            {
                get { return false; }
            }
        }
        #endregion

        #region Span Editing
        private class SpanEdit : TextBufferBaseEdit
        {
            private readonly ElisionBuffer elBuffer;

            public SpanEdit(ElisionBuffer elBuffer): base(elBuffer)
            {
                this.elBuffer = elBuffer;
            }

            public IProjectionSnapshot Apply(NormalizedSpanCollection spansToElide, NormalizedSpanCollection spansToExpand)
            {
                this.applied = true;
                try
                {
                    if (spansToElide == null)
                    {
                        spansToElide = NormalizedSpanCollection.Empty;
                    }
                    if (spansToExpand == null)
                    {
                        spansToExpand = NormalizedSpanCollection.Empty;
                    }
                    if (spansToElide.Count > 0 || spansToExpand.Count > 0)
                    {
                        if ((spansToElide.Count > 0) && (spansToElide[spansToElide.Count - 1].End > this.elBuffer.sourceSnapshot.Length))
                        {
                            throw new ArgumentOutOfRangeException("spansToElide");
                        }
                        if ((spansToExpand.Count > 0) && (spansToExpand[spansToExpand.Count - 1].End > this.elBuffer.sourceSnapshot.Length))
                        {
                            throw new ArgumentOutOfRangeException("spansToExpand");
                        }
                        ElisionSourceSpansChangedEventArgs args = this.elBuffer.ApplySpanChanges(spansToElide, spansToExpand);
                        if (args != null)
                        {
                            ElisionSourceSpansChangedEventRaiser raiser = new ElisionSourceSpansChangedEventRaiser(args);
                            this.baseBuffer.group.EnqueueEvents(raiser, this.baseBuffer);
                            raiser.RaiseEvent(this.baseBuffer, true);
                        }
                        this.baseBuffer.editInProgress = false;
                    }
                    else
                    {
                        this.baseBuffer.editInProgress = false;
                    }
                }
                finally
                {
                    this.baseBuffer.group.FinishEdit();
                }
                return this.elBuffer.currentElisionSnapshot;
            }
        }

        public IProjectionSnapshot ElideSpans(NormalizedSpanCollection spansToElide)
        {
            if (spansToElide == null)
            {
                throw new ArgumentNullException("spansToElide");
            }
            return ModifySpans(spansToElide, null);
        }

        public IProjectionSnapshot ExpandSpans(NormalizedSpanCollection spansToExpand)
        {
            if (spansToExpand == null)
            {
                throw new ArgumentNullException("spansToExpand");
            }
            return ModifySpans(null, spansToExpand);
        }

        public IProjectionSnapshot ModifySpans(NormalizedSpanCollection spansToElide, NormalizedSpanCollection spansToExpand)
        {
            using (SpanEdit spedit = new SpanEdit(this))
            {
                return spedit.Apply(spansToElide, spansToExpand);
            }
        }
        #endregion

        public override ITextEdit CreateEdit(EditOptions options, int? reiteratedVersionNumber, object editTag)
        {
            return new ElisionEdit(this, this.currentElisionSnapshot, options, reiteratedVersionNumber, editTag);
        }

        protected internal override ISubordinateTextEdit CreateSubordinateEdit(EditOptions options, int? reiteratedVersionNumber, object editTag)
        {
            return new ElisionEdit(this, this.currentElisionSnapshot, options, reiteratedVersionNumber, editTag);
        }

        internal void ComputeSourceEdits(FrugalList<TextChange> changes)
        {
            ITextEdit xedit = this.group.GetEdit((BaseBuffer)this.sourceBuffer);
            foreach (TextChange change in changes)
            {
                if (change.OldLength > 0)
                {
                    IList<SnapshotSpan> sourceDeletionSpans = this.currentElisionSnapshot.MapToSourceSnapshots(new Span(change.OldPosition, change.OldLength));
                    foreach (SnapshotSpan sourceDeletionSpan in sourceDeletionSpans)
                    {
                        xedit.Delete(sourceDeletionSpan);
                    }
                }
                if (change.NewLength > 0)
                {
                    // change includes an insertion
                    ReadOnlyCollection<SnapshotPoint> sourceInsertionPoints = this.currentElisionSnapshot.MapInsertionPointToSourceSnapshots(change.OldPosition, null);

                    if (sourceInsertionPoints.Count == 1)
                    {
                        // the insertion point is unambiguous
                        xedit.Insert(sourceInsertionPoints[0].Position, change.NewText);
                    }
                    else
                    {
                        // the insertion is at the boundary of source spans
                        int[] insertionSizes = new int[sourceInsertionPoints.Count];

                        if (this.resolver != null)
                        {
                            this.resolver.FillInInsertionSizes(new SnapshotPoint(this.currentElisionSnapshot, change.OldPosition),
                                                               sourceInsertionPoints, change.NewText, insertionSizes);
                        }

                        // if resolver was not provided, we just use zeros for the insertion sizes, which will push the entire insertion 
                        // into the last slot.

                        int pos = 0;
                        for (int i = 0; i < insertionSizes.Length; ++i)
                        {
                            // contend with any old garbage that the client passed back.
                            int size = (i == insertionSizes.Length - 1)
                                            ? change.NewLength - pos
                                            : Math.Min(insertionSizes[i], change.NewLength - pos);
                            if (size > 0)
                            {
                                xedit.Insert(sourceInsertionPoints[i].Position, TextChange.ChangeNewSubstring(change, pos, size));
                                pos += size;
                                if (pos == change.NewLength)
                                {
                                    break;  // inserted text is used up, whether we've visited all of the insertionSizes or not
                                }
                            }
                        }
                    }
                }
            }
            this.editApplicationInProgress = true;
        }

        public override BaseBuffer.ITextEventRaiser PropagateSourceChanges(EditOptions options, object editTag)
        {
            TextContentChangedEventRaiser raiser = IncorporateChanges();
            raiser.RaiseEvent(this, true);
            return raiser;
        }

        #region ChangeApplication

        private ElisionSourceSpansChangedEventArgs ApplySpanChanges(NormalizedSpanCollection spansToElide, NormalizedSpanCollection spansToExpand)
        {
            ElisionSnapshot beforeSnapshot = this.currentElisionSnapshot;
            FrugalList<TextChange> textChanges;
            ElisionMap newContent = this.content.EditSpans(this.sourceSnapshot, spansToElide, spansToExpand, out textChanges);
            if (newContent != this.content)
            {
                this.content = newContent;
                INormalizedTextChangeCollection normalizedChanges = NormalizedTextChangeCollection.Create(textChanges);
                SetCurrentVersionAndSnapshot(normalizedChanges);
                return new ElisionSourceSpansChangedEventArgs(beforeSnapshot, this.currentElisionSnapshot,
                                                              spansToElide, spansToExpand, null);
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region Snapshots

        public override IProjectionSnapshot CurrentSnapshot
        {
            get { return this.currentElisionSnapshot; }
        }

        protected override BaseProjectionSnapshot CurrentBaseSnapshot
        {
            get { return this.currentElisionSnapshot; }
        }

        IElisionSnapshot IElisionBuffer.CurrentSnapshot
        {
            get { return this.currentElisionSnapshot; }
        }

        protected override StringRebuilder GetDoppelgangerBuilder()
        {
            // If there are no elisions, we can simply reference the source snapshot.
            var sourceSnapshot = this.sourceSnapshot;
            if ((this.currentVersion.Length == sourceSnapshot.Length) && (sourceSnapshot is BaseSnapshot))
            {
                return BufferFactoryService.StringRebuilderFromSnapshotAndSpan(sourceSnapshot, new Span(0, sourceSnapshot.Length));
            }

            return null;
        }

        protected override BaseSnapshot TakeSnapshot()
        {
            this.currentElisionSnapshot =
                new ElisionSnapshot(this, this.sourceSnapshot, this.currentVersion, this.builder, this.content,
                                    (this.elisionOptions & ElisionBufferOptions.FillInMappingMode) != 0);
            return this.currentElisionSnapshot;
        }
        #endregion

        private TextContentChangedEventRaiser IncorporateChanges()
        {
            Debug.Assert(this.sourceSnapshot == this.pendingContentChangedEventArgs[0].Before);
            FrugalList<TextChange> projectedChanges = new FrugalList<TextChange>();

            var args0 = this.pendingContentChangedEventArgs[0];
            INormalizedTextChangeCollection sourceChanges;

            // Separate the easy and common case:
            if (this.pendingContentChangedEventArgs.Count == 1)
            {
                sourceChanges = args0.Changes;
                this.sourceSnapshot = args0.After;
            }
            else
            {
                // there is more than one snapshot of the source buffer to deal with. Since the changes may be 
                // interleaved by position, we need to get a normalized list in sequence. First we denormalize the
                // changes so they are all relative to the same single starting snapshot, then we normalize them again into
                // a single list.

                // This relies crucially on the fact that we know something about the multiple snapshots: they were
                // induced by projection span adjustments, and the changes across them are independent. That is to say,
                // it is not the case that text inserted in one snapshot is deleted in a later snapshot in the series.

                DumpPendingContentChangedEventArgs();
                List<TextChange> denormalizedChanges = new List<TextChange>() { new TextChange(int.MaxValue, StringRebuilder.Empty, StringRebuilder.Empty, LineBreakBoundaryConditions.None) };
                for (int a = 0; a < this.pendingContentChangedEventArgs.Count; ++a)
                {
                    NormalizedTextChangeCollection.Denormalize(this.pendingContentChangedEventArgs[a].Changes, denormalizedChanges);
                }
                DumpPendingChanges(new List<Tuple<ITextBuffer, List<TextChange>>>() { new Tuple<ITextBuffer, List<TextChange>>(this.sourceBuffer, denormalizedChanges) } );
                FrugalList<TextChange> slicedChanges = new FrugalList<TextChange>();

                // remove the sentinel
                for (int d = 0; d < denormalizedChanges.Count - 1; ++d)
                {
                    slicedChanges.Add(denormalizedChanges[d]);
                }
                sourceChanges = NormalizedTextChangeCollection.Create(slicedChanges);
                this.sourceSnapshot = this.pendingContentChangedEventArgs[this.pendingContentChangedEventArgs.Count - 1].After;
            }

            if (sourceChanges.Count > 0)
            {
                this.content = this.content.IncorporateChanges(sourceChanges, projectedChanges, args0.Before, this.sourceSnapshot, this.currentElisionSnapshot);
            }

            this.pendingContentChangedEventArgs.Clear();
            ElisionSnapshot beforeSnapshot = this.currentElisionSnapshot;
            SetCurrentVersionAndSnapshot(NormalizedTextChangeCollection.Create(projectedChanges));
            this.editApplicationInProgress = false;
            return new TextContentChangedEventRaiser(beforeSnapshot, this.currentElisionSnapshot, args0.Options, args0.EditTag);
        }

        #region Event Handling
        internal override void OnSourceTextChanged(object sender, TextContentChangedEventArgs e)
        {
            this.pendingContentChangedEventArgs.Add(e);

            if (!this.editApplicationInProgress)
            {
                // We had better be a member of the same group as the buffer that we just heard from
                Debug.Assert(this.group.MembersContains(e.After.TextBuffer));
                // Let the buffer group decide when to issue events (this allows us to coalesce multiple snapshots
                // from the source buffer (this can happen if the source buffer is a projection buffer) into a single snapshot here.
                this.group.ScheduleIndependentEdit(this);
            }
        }
        #endregion

        #region Public Events
        public event EventHandler<ElisionSourceSpansChangedEventArgs> SourceSpansChanged;
        #endregion
    }
}