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

BaseProjectionBuffer.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: 5dbdfc3c6b5bfae9a959dd7685b50a5e612e5064 (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
//
//  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.Diagnostics;
    using System.Text;

    using Microsoft.VisualStudio.Text.Implementation;
    using Microsoft.VisualStudio.Utilities;
    using Microsoft.VisualStudio.Text.Differencing;
    using Microsoft.VisualStudio.Text.Utilities;
    using System.Collections.ObjectModel;

    internal abstract class BaseProjectionBuffer : BaseBuffer, IProjectionBufferBase
    {
        #region State and construction
        protected internal readonly IProjectionEditResolver resolver;
        protected bool editApplicationInProgress;
        protected List<TextContentChangedEventArgs> pendingContentChangedEventArgs = new List<TextContentChangedEventArgs>();

        protected BaseProjectionBuffer(IProjectionEditResolver resolver, IContentType contentType, ITextDifferencingService textDifferencingService, GuardedOperations guardedOperations)
            : base(contentType, 0, textDifferencingService, guardedOperations)
        {
            this.resolver = resolver;   // null is OK
        }
        #endregion

        #region Source buffer event handling
        internal abstract void OnSourceTextChanged(object sender, TextContentChangedEventArgs e);

        internal virtual void OnSourceBufferReadOnlyRegionsChanged(object sender, SnapshotSpanEventArgs e)
        {
            NormalizedSpanCollection mappedAffectedSpans = new NormalizedSpanCollection(this.CurrentBaseSnapshot.MapFromSourceSnapshot(e.Span));

            if (mappedAffectedSpans.Count > 0)
            {
                ITextEventRaiser raiser = 
                    new ReadOnlyRegionsChangedEventRaiser(new SnapshotSpan(this.currentSnapshot, 
                                                                           Span.FromBounds(mappedAffectedSpans[0].Start, 
                                                                                           mappedAffectedSpans[mappedAffectedSpans.Count - 1].End)));

                this.group.BeginEdit();
                this.group.EnqueueEvents(raiser, this);
                this.group.FinishEdit();
            }
        }

        internal void OnSourceBufferContentTypeChanged(object sender, ContentTypeChangedEventArgs e)
        {
            TextContentChangedEventArgs args = new TextContentChangedEventArgs(e.Before, e.After, EditOptions.None, e.EditTag);
            this.pendingContentChangedEventArgs.Add(args);
            this.group.ScheduleIndependentEdit(this);
        }
        #endregion

        #region Editing Shortcuts
        public new IProjectionSnapshot Insert(int position, string text)
        {
            return (IProjectionSnapshot)base.Insert(position, text);
        }

        public new IProjectionSnapshot Delete(Span deleteSpan)
        {
            return (IProjectionSnapshot)base.Delete(deleteSpan);
        }

        public new IProjectionSnapshot Replace(Span replaceSpan, string replaceWith)
        {
            return (IProjectionSnapshot)base.Replace(replaceSpan, replaceWith);
        }
        #endregion

        #region Read Only Region support
        protected internal override bool IsReadOnlyImplementation(int position, bool isEdit)
        {
            if (this.CurrentBaseSnapshot.SpanCount == 0)
            {
                throw new InvalidOperationException();
            }
            if (base.IsReadOnlyImplementation(position, isEdit))
            {
                return true;
            }
            return AreBaseBuffersReadOnly(position, isEdit);
        }

        private bool AreBaseBuffersReadOnly(int position, bool isEdit)
        {
            // a position on a seam between two or more source spans is readonly only
            // if that position is readonly in all of the source buffers.
            ReadOnlyCollection<SnapshotPoint> snapPoints = this.CurrentBaseSnapshot.MapInsertionPointToSourceSnapshots(position, null);
            foreach (SnapshotPoint snapPoint in snapPoints)
            {
                BaseBuffer baseBuffer = (BaseBuffer)snapPoint.Snapshot.TextBuffer;
                if (!baseBuffer.IsReadOnlyImplementation(snapPoint.Position, isEdit))
                {
                    return false;
                }
            }
            return true;
        }

        protected internal override bool IsReadOnlyImplementation(Span span, bool isEdit)
        {
            if (this.CurrentSnapshot.SpanCount == 0)
            {
                throw new InvalidOperationException();
            }
            if (base.IsReadOnlyImplementation(span, isEdit))
            {
                return true;
            }
            return AreBaseBuffersReadOnly(span, isEdit);
        }

        public bool AreBaseBuffersReadOnly(Span span, bool isEdit)
        {
            if (span.Length == 0)
            {
                // treat like an insertion!
                return AreBaseBuffersReadOnly(span.Start, isEdit);
            }
            else
            {
                IList<SnapshotSpan> snapSpans = this.CurrentBaseSnapshot.MapToSourceSnapshots(span);
                foreach (SnapshotSpan snapSpan in snapSpans)
                {
                    BaseBuffer baseBuffer = (BaseBuffer)snapSpan.Snapshot.TextBuffer;
                    if (baseBuffer.IsReadOnlyImplementation(snapSpan, isEdit))
                    {
                        return true;
                    }
                }
                return false;
            }
        }

        protected internal override NormalizedSpanCollection GetReadOnlyExtentsImplementation(Span span)
        {
            // TODO: make something other than dead slow

            FrugalList<Span> result = new FrugalList<Span>(base.GetReadOnlyExtentsImplementation(span));

            IList<SnapshotSpan> restrictionSpans = this.CurrentBaseSnapshot.MapToSourceSnapshotsForRead(span);
            foreach (SnapshotSpan restrictionSpan in restrictionSpans)
            {
                SnapshotSpan? overlapSpan = (restrictionSpan.Span == span) ? restrictionSpan : restrictionSpan.Overlap(span);
                if (overlapSpan.HasValue)
                {
                    BaseBuffer baseBuffer = (BaseBuffer)restrictionSpan.Snapshot.TextBuffer;
                    NormalizedSpanCollection sourceExtents = baseBuffer.GetReadOnlyExtents(overlapSpan.Value);
                    foreach (Span sourceExtent in sourceExtents)
                    {
                        result.AddRange(this.CurrentBaseSnapshot.MapFromSourceSnapshot(new SnapshotSpan(restrictionSpan.Snapshot, sourceExtent)));
                    }
                }
            }

            return new NormalizedSpanCollection(result);
        }
        #endregion

        #region Abstract members
        protected abstract BaseProjectionSnapshot CurrentBaseSnapshot { get; }

        public override abstract ITextEdit CreateEdit(EditOptions options, int? reiteratedVersionNumber, object editTag);

        protected internal override abstract ISubordinateTextEdit CreateSubordinateEdit(EditOptions options, int? reiteratedVersionNumber, object editTag);

        protected override abstract BaseSnapshot TakeSnapshot();

        public new abstract IProjectionSnapshot CurrentSnapshot { get; }

        public abstract IList<ITextBuffer> SourceBuffers { get; }

        public abstract BaseBuffer.ITextEventRaiser PropagateSourceChanges(EditOptions options, object editTag);
        #endregion

        #region Debug support
        [Conditional("_DEBUG")]
        protected void DumpPendingChanges(List<Tuple<ITextBuffer, List<TextChange>>> pendingSourceChanges)
        {
            if (BufferGroup.Tracing)
            {
                StringBuilder sb = new StringBuilder(string.Format(System.Globalization.CultureInfo.CurrentCulture, "Pending Changes in {0}\r\n", TextUtilities.GetTag(this)));
                foreach (var p in pendingSourceChanges)
                {
                    sb.AppendLine(TextUtilities.GetTag(p.Item1));
                    for (int c = 0; c < p.Item2.Count - 1; ++c)    // don't display sentinel
                    {
                        sb.AppendLine(p.Item2[c].ToString());
                    }
                }
                sb.AppendLine("");
                Debug.Write(sb.ToString());
            }
        }

        [Conditional("_DEBUG")]
        protected void DumpPendingContentChangedEventArgs()
        {
            if (BufferGroup.Tracing)
            {
                StringBuilder sb = new StringBuilder(string.Format(System.Globalization.CultureInfo.CurrentCulture, "Pending Change Events in {0}\r\n", TextUtilities.GetTag(this)));
                foreach (var args in this.pendingContentChangedEventArgs)
                {
                    sb.Append(TextUtilities.GetTag(args.Before.TextBuffer));
                    sb.Append(" V");
                    sb.AppendLine(args.After.Version.VersionNumber.ToString());
                    foreach (var change in args.Changes)
                    {
                        sb.AppendLine(change.ToString());
                    }
                }
                sb.AppendLine("");
                Debug.Write(sb.ToString());
            }
        }
        #endregion
    }
}