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

ElisionSnapshot.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: 8c70f2489723b80f525241deb5064cad748f3b5f (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
//
//  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.Implementation;
    using Microsoft.VisualStudio.Text.Utilities;
    using Strings = Microsoft.VisualStudio.Text.Implementation.Strings;

    internal class ElisionSnapshot : BaseProjectionSnapshot, IProjectionSnapshot, IElisionSnapshot
    {
        #region State and Construction
        private readonly ElisionBuffer elisionBuffer;
        private readonly ITextSnapshot sourceSnapshot;
        private readonly ReadOnlyCollection<ITextSnapshot> sourceSnapshots;
        private readonly ElisionMap content;
        private readonly bool fillInMappingMode;

        public ElisionSnapshot(ElisionBuffer elisionBuffer,
                               ITextSnapshot sourceSnapshot, 
                               ITextVersion2 version,
                               StringRebuilder builder,
                               ElisionMap content,
                               bool fillInMappingMode)
          : base(version, builder)
        {
            this.elisionBuffer = elisionBuffer;
            this.sourceSnapshot = sourceSnapshot;
            // The SourceSnapshots property is used heavily, so cache a handy copy.
            this.sourceSnapshots = new ReadOnlyCollection<ITextSnapshot>(new FrugalList<ITextSnapshot>() { sourceSnapshot });
            this.totalLength = content.Length;
            this.content = content;
            this.totalLineCount = content.LineCount;
            this.fillInMappingMode = fillInMappingMode;
            Debug.Assert(this.totalLength == version.Length,
                         string.Format(System.Globalization.CultureInfo.CurrentCulture,
                                       "Elision Snapshot Inconsistency. Content: {0}, Previous + delta: {1}", this.totalLength, version.Length));
            if (this.totalLength != version.Length)
            {
                throw new InvalidOperationException(Strings.InvalidLengthCalculation);
            }
        }
        #endregion

        #region Buffers and Spans
        public override IProjectionBufferBase TextBuffer
        {
            get { return this.elisionBuffer; }
        }

        IElisionBuffer IElisionSnapshot.TextBuffer
        {
            get { return this.elisionBuffer; }
        }

        protected override ITextBuffer TextBufferHelper
        {
            get { return this.elisionBuffer; }
        }

        public override int SpanCount
        {
            get { return this.content.SpanCount; }
        }

        public override ReadOnlyCollection<ITextSnapshot> SourceSnapshots
        {
            get { return this.sourceSnapshots; }
        }

        public ITextSnapshot SourceSnapshot
        {
            get { return this.sourceSnapshot; }
        }

        public SnapshotPoint MapFromSourceSnapshotToNearest(SnapshotPoint point)
        {
            return this.content.MapFromSourceSnapshotToNearest(this, point.Position);
        }

        public override ITextSnapshot GetMatchingSnapshot(ITextBuffer textBuffer)
        {
            if (textBuffer == null)
            {
                throw new ArgumentNullException(nameof(textBuffer));
            }
            return this.sourceSnapshot.TextBuffer == textBuffer ? this.sourceSnapshot : null;
        }

        public override ITextSnapshot GetMatchingSnapshotInClosure(ITextBuffer textBuffer)
        {
            if (textBuffer == null)
            {
                throw new ArgumentNullException(nameof(textBuffer));
            }

            if (this.sourceSnapshot.TextBuffer == textBuffer)
            {
                return this.sourceSnapshot;
            }

            IProjectionSnapshot2 projSnap = this.sourceSnapshot as IProjectionSnapshot2;
            if (projSnap != null)
            {
                return projSnap.GetMatchingSnapshotInClosure(textBuffer);
            }

            return null;
        }

        public override ITextSnapshot GetMatchingSnapshotInClosure(Predicate<ITextBuffer> match)
        {
            if (match == null)
            {
                throw new ArgumentNullException(nameof(match));
            }

            if (match(this.sourceSnapshot.TextBuffer))
            {
                return this.sourceSnapshot;
            }

            IProjectionSnapshot2 projSnap = this.sourceSnapshot as IProjectionSnapshot2;
            if (projSnap != null)
            {
                return projSnap.GetMatchingSnapshotInClosure(match);
            }

            return null;
        }

        public override ReadOnlyCollection<SnapshotSpan> GetSourceSpans(int startSpanIndex, int count)
        {
            if (startSpanIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(startSpanIndex));
            }
            if (count < 0 || startSpanIndex + count > SpanCount)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }
            return new ReadOnlyCollection<SnapshotSpan>(this.content.GetSourceSpans(this.sourceSnapshot, startSpanIndex, count));
        }

        public override ReadOnlyCollection<SnapshotSpan> GetSourceSpans()
        {
            return GetSourceSpans(0, this.content.SpanCount);
        }
        #endregion

        #region Mapping
        public override SnapshotPoint MapToSourceSnapshot(int position)
        {
            if (position < 0 || position > this.totalLength)
            {
                throw new ArgumentOutOfRangeException(nameof(position));
            }
            FrugalList<SnapshotPoint> points = this.content.MapInsertionPointToSourceSnapshots(this, position);
            if (points.Count == 1)
            {
                return points[0];
            }
            else if (this.elisionBuffer.resolver == null)
            {
                return points[points.Count - 1];
            }
            else
            {
                return points[this.elisionBuffer.resolver.GetTypicalInsertionPosition(new SnapshotPoint(this, position), new ReadOnlyCollection<SnapshotPoint>(points))];
            }
        }

        public override SnapshotPoint MapToSourceSnapshot(int position, PositionAffinity affinity)
        {
            if (position < 0 || position > this.totalLength)
            {
                throw new ArgumentOutOfRangeException(nameof(position));
            }
            if (affinity < PositionAffinity.Predecessor || affinity > PositionAffinity.Successor)
            {
                throw new ArgumentOutOfRangeException(nameof(affinity));
            }
            return this.content.MapToSourceSnapshot(this.sourceSnapshot, position, affinity);
        }

        public override SnapshotPoint? MapFromSourceSnapshot(SnapshotPoint point, PositionAffinity affinity)
        {
            if (point.Snapshot != this.sourceSnapshot)
            {
                throw new ArgumentException("The point does not belong to a source snapshot of the projection snapshot");
            }
            if (affinity < PositionAffinity.Predecessor || affinity > PositionAffinity.Successor)
            {
                throw new ArgumentOutOfRangeException(nameof(affinity));
            }
            return this.content.MapFromSourceSnapshot(this, point.Position);
        }

        private ReadOnlyCollection<SnapshotSpan> MapToSourceSnapshots(Span span, bool fillIn)
        {
            if (span.End > this.totalLength)
            {
                throw new ArgumentOutOfRangeException(nameof(span));
            }
            FrugalList<SnapshotSpan> result = new FrugalList<SnapshotSpan>();
            if (fillIn)
            {
                this.content.MapToSourceSnapshotsInFillInMode(this.sourceSnapshot, span, result);
            }
            else
            {
                this.content.MapToSourceSnapshots(this, span, result);
            }
            return new ReadOnlyCollection<SnapshotSpan>(result);
        }

        public override ReadOnlyCollection<SnapshotSpan> MapToSourceSnapshots(Span span)
        {
            return MapToSourceSnapshots(span, this.fillInMappingMode);
        }

        public override ReadOnlyCollection<SnapshotSpan> MapToSourceSnapshotsForRead(Span span)
        {
            return MapToSourceSnapshots(span, false);
        }

        public override ReadOnlyCollection<Span> MapFromSourceSnapshot(SnapshotSpan span)
        {
            if (span.Snapshot != this.sourceSnapshot)
            {
                throw new ArgumentException("The span does not belong to a source snapshot of the projection snapshot");
            }
            FrugalList<Span> result = new FrugalList<Span>();
            this.content.MapFromSourceSnapshot(span, result);
            return new ReadOnlyCollection<Span>(result);
        }

        internal override ReadOnlyCollection<SnapshotPoint> MapInsertionPointToSourceSnapshots(int position, ITextBuffer excludedBuffer)
        {
            return new ReadOnlyCollection<SnapshotPoint>(this.content.MapInsertionPointToSourceSnapshots(this, position));
        }

        internal override ReadOnlyCollection<SnapshotSpan> MapReplacementSpanToSourceSnapshots(Span replacementSpan, ITextBuffer excludedBuffer)
        {
            // this implementation won't return zero-length spans on the edges as it
            // should, but that's OK because it is not called in Beta1 (we never edit 
            // elision buffers directly). Third parties might do so, so we need a non-throwing
            // implementation here. Zero-length spans will be added for Beta2.
            return MapToSourceSnapshots(replacementSpan, false);
        }
        #endregion
    }
}