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

ProjectionSpanDiffer.cs « TextDataUtil « Util « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1a43cb94fe80cbad33e71e70e7c87db4d5ee2370 (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
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//  Licensed under the MIT License. See License.txt in the project root for license information.
//
namespace Microsoft.VisualStudio.Text.Utilities
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using Microsoft.VisualStudio.Text.Differencing;
    using Microsoft.VisualStudio.Text.Projection;

    internal class ProjectionSpanDiffer
    {
        private readonly IDifferenceService diffService;

        private readonly ReadOnlyCollection<SnapshotSpan> inputDeletedSnapSpans;
        private readonly ReadOnlyCollection<SnapshotSpan> inputInsertedSnapSpans;

        // exposed to unit tests
        internal List<SnapshotSpan>[] deletedSurrogates;
        internal List<SnapshotSpan>[] insertedSurrogates;

        public ProjectionSpanDiffer(IDifferenceService diffService,
                                    ReadOnlyCollection<SnapshotSpan> deletedSnapSpans,
                                    ReadOnlyCollection<SnapshotSpan> insertedSnapSpans)
        {
            this.diffService = diffService;
            this.inputDeletedSnapSpans = deletedSnapSpans;
            this.inputInsertedSnapSpans = insertedSnapSpans;
        }

        private IDifferenceCollection<SnapshotSpan> differences;

        public IDifferenceCollection<SnapshotSpan> GetDifferences()
        {
            if (this.differences == null)
            {
                DecomposeSpans();

                var deletedSpans = new List<SnapshotSpan>();
                var insertedSpans = new List<SnapshotSpan>();

                for (int s = 0; s < deletedSurrogates.Length; ++s)
                {
                    deletedSpans.AddRange(deletedSurrogates[s]);
                }

                for (int s = 0; s < insertedSurrogates.Length; ++s)
                {
                    insertedSpans.AddRange(insertedSurrogates[s]);
                }

                differences = this.diffService.DifferenceSequences(deletedSpans, insertedSpans);
            }

            return differences;
        }

        #region Internal (for testing) helpers
        internal void DecomposeSpans()
        {
            // Prepare spans for diffing. The basic idea is this: suppose we have input spans from some source snapshot as follows:
            //
            // deleted:  0..10
            // inserted: 0..3 7..10
            //
            // We would like to raise a text change event that indicates that the text from 3..7 was deleted, rather than
            // an event indicating that all the text from 0..10 was deleted and replaced. We could simply compute a string
            // difference of the before & after text, but there might be a lot of text (so that would be expensive), and we
            // also don't want to suppress eventing when identical text comes from different source buffers (which might have
            // different content types). So, this routine converts the input spans into a form suitable for diffing:
            //
            // deleted: 0..3 3..7 7..10
            // inserted 0..3 7..10
            //
            // then we compute the differences of the spans qua spans, which in this case will indicate that the span named "3..7"
            // was deleted, and that's what we use to generate text change events.

            // what to substitute for input spans during diffing
            this.deletedSurrogates = new List<SnapshotSpan>[this.inputDeletedSnapSpans.Count];
            this.insertedSurrogates = new List<SnapshotSpan>[this.inputInsertedSnapSpans.Count];

            // collect spans by text buffer

            Dictionary<ITextSnapshot, List<Thing>> buffer2DeletedThings = new Dictionary<ITextSnapshot, List<Thing>>();
            for (int ds = 0; ds < this.inputDeletedSnapSpans.Count; ++ds)
            {
                SnapshotSpan ss = this.inputDeletedSnapSpans[ds];
                List<Thing> things;
                if (!buffer2DeletedThings.TryGetValue(ss.Snapshot, out things))
                {
                    things = new List<Thing>();
                    buffer2DeletedThings.Add(ss.Snapshot, things);
                }
                things.Add(new Thing(ss.Span, ds));

                // unrelated
                deletedSurrogates[ds] = new List<SnapshotSpan>();
            }

            Dictionary<ITextSnapshot, List<Thing>> buffer2InsertedThings = new Dictionary<ITextSnapshot, List<Thing>>();
            for (int ns = 0; ns < this.inputInsertedSnapSpans.Count; ++ns)
            {
                SnapshotSpan ss = this.inputInsertedSnapSpans[ns];
                List<Thing> things;
                if (!buffer2InsertedThings.TryGetValue(ss.Snapshot, out things))
                {
                    things = new List<Thing>();
                    buffer2InsertedThings.Add(ss.Snapshot, things);
                }
                things.Add(new Thing(ss.Span, ns));

                // unrelated
                insertedSurrogates[ns] = new List<SnapshotSpan>();
            }

            foreach (KeyValuePair<ITextSnapshot, List<Thing>> pair in buffer2DeletedThings)
            {
                List<Thing> insertedThings;
                ITextSnapshot snapshot = pair.Key;
                if (buffer2InsertedThings.TryGetValue(snapshot, out insertedThings))
                {
                    List<Thing> deletedThings = pair.Value;
                    insertedThings.Sort(Comparison);
                    deletedThings.Sort(Comparison);

                    int i = 0;
                    int d = 0;
                    do
                    {
                        Span inserted = insertedThings[i].span;
                        Span deleted = deletedThings[d].span;
                        Span? overlap = inserted.Overlap(deleted);

                        if (overlap == null)
                        {
                            if (inserted.Start < deleted.Start)
                            {
                                i++;
                            }
                            else
                            {
                                d++;
                            }
                        }
                        else
                        {
                            NormalizedSpanCollection insertedResidue = NormalizedSpanCollection.Difference(new NormalizedSpanCollection(inserted),
                                                                                                           new NormalizedSpanCollection(overlap.Value));  // todo add overload to normalizedspancollection
                            if (insertedResidue.Count > 0)
                            {
                                int pos = insertedThings[i].position;
                                insertedThings.RemoveAt(i);
                                bool didOverlap = false;
                                int ir = 0;
                                while (ir < insertedResidue.Count)
                                {
                                    Span r = insertedResidue[ir];
                                    if (didOverlap || r.Start < overlap.Value.Start)
                                    {
                                        insertedThings.Insert(i++, new Thing(r, pos));
                                        ir++;
                                    }
                                    else
                                    {
                                        insertedThings.Insert(i++, new Thing(overlap.Value, pos));
                                        didOverlap = true;
                                    }
                                }
                                if (!didOverlap)
                                {
                                    insertedThings.Insert(i++, new Thing(overlap.Value, pos));
                                }
                                i--;
                            }

                            NormalizedSpanCollection deletedResidue = NormalizedSpanCollection.Difference(new NormalizedSpanCollection(deleted),
                                                                                                          new NormalizedSpanCollection(overlap.Value));
                            if (deletedResidue.Count > 0)
                            {
                                int pos = deletedThings[d].position;
                                deletedThings.RemoveAt(d);
                                bool didOverlap = false;
                                int dr = 0;
                                while (dr < deletedResidue.Count)
                                {
                                    Span r = deletedResidue[dr];
                                    if (didOverlap || r.Start < overlap.Value.Start)
                                    {
                                        deletedThings.Insert(d++, new Thing(r, pos));
                                        dr++;
                                    }
                                    else
                                    {
                                        deletedThings.Insert(d++, new Thing(overlap.Value, pos));
                                        didOverlap = true;
                                    }
                                }
                                if (!didOverlap)
                                {
                                    deletedThings.Insert(d++, new Thing(overlap.Value, pos));
                                }
                                d--;
                            }
                        }
                        if (inserted.End <= deleted.End)
                        {
                            i++;
                        }
                        if (deleted.End <= inserted.End)
                        {
                            d++;
                        }
                    } while (i < insertedThings.Count && d < deletedThings.Count);
                }
            }

            foreach (KeyValuePair<ITextSnapshot, List<Thing>> pair in buffer2DeletedThings)
            {
                foreach (Thing t in pair.Value)
                {
                    deletedSurrogates[t.position].Add(new SnapshotSpan(pair.Key, t.span));
                }
            }

            foreach (KeyValuePair<ITextSnapshot, List<Thing>> pair in buffer2InsertedThings)
            {
                foreach (Thing t in pair.Value)
                {
                    insertedSurrogates[t.position].Add(new SnapshotSpan(pair.Key, t.span));
                }
            }
        }

        #endregion

        #region Private helpers
        private class Thing
        {
            public Span span;
            public int position;
            public Thing(Span span, int position)
            {
                this.span = span;
                this.position = position;
            }
        }

        private static int Comparison(Thing left, Thing right)
        {
            return left.span.Start - right.span.Start;
        }
        #endregion
    }
}