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

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

    /// <summary>
    /// Base class for all varieties of Text Snapshots.
    /// </summary>
    internal abstract class BaseSnapshot : ITextSnapshot, ITextSnapshot2
    {
        #region State and Construction
        protected readonly ITextVersion2 version;
        private readonly IContentType contentType;

        internal readonly StringRebuilder Content;
        internal readonly ITextImage cachingContent;

        protected BaseSnapshot(ITextVersion2 version, StringRebuilder content)
        {
            this.version = version;
            this.Content = content;
            this.cachingContent = CachingTextImage.Create(this.Content, version.ImageVersion);

            // we must extract the content type here, because the content type of the text buffer may change later.
            this.contentType = version.TextBuffer.ContentType;
        }
        #endregion

        #region ITextSnapshot implementations

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

        public IContentType ContentType
        {
            get { return this.contentType; }
        }

        public ITextVersion Version
        {
            get { return this.version; }
        }

        public string GetText(int startIndex, int length)
        {
            return GetText(new Span(startIndex, length));
        }

        public string GetText()
        {
            return GetText(new Span(0, this.Length));
        }

        #region Point and Span factories
        public ITrackingPoint CreateTrackingPoint(int position, PointTrackingMode trackingMode)
        {
            return this.version.CreateTrackingPoint(position, trackingMode);
        }

        public ITrackingPoint CreateTrackingPoint(int position, PointTrackingMode trackingMode, TrackingFidelityMode trackingFidelity)
        {
            return this.version.CreateTrackingPoint(position, trackingMode, trackingFidelity);
        }

        public ITrackingSpan CreateTrackingSpan(int start, int length, SpanTrackingMode trackingMode)
        {
            return this.version.CreateTrackingSpan(start, length, trackingMode);
        }

        public ITrackingSpan CreateTrackingSpan(int start, int length, SpanTrackingMode trackingMode, TrackingFidelityMode trackingFidelity)
        {
            return this.version.CreateTrackingSpan(start, length, trackingMode, trackingFidelity);
        }

        public ITrackingSpan CreateTrackingSpan(Span span, SpanTrackingMode trackingMode)
        {
            return this.version.CreateTrackingSpan(span, trackingMode, TrackingFidelityMode.Forward);
        }

        public ITrackingSpan CreateTrackingSpan(Span span, SpanTrackingMode trackingMode, TrackingFidelityMode trackingFidelity)
        {
            return this.version.CreateTrackingSpan(span, trackingMode, trackingFidelity);
        }
        #endregion
        #endregion

        #region ITextSnapshot2 implementations
        public void SaveToFile(string filePath, bool replaceFile, Encoding encoding)
        {
            FileUtilities.SaveSnapshot(this, replaceFile ? FileMode.Create : FileMode.CreateNew, encoding, filePath);
        }
        #endregion

        #region ITextSnapshot abstract methods
        protected abstract ITextBuffer TextBufferHelper { get; }

        public int Length
        {
            get { return this.cachingContent.Length; }
        }

        public int LineCount
        {
            get { return this.cachingContent.LineCount; }
        }

        public string GetText(Span span)
        {
            return this.cachingContent.GetText(span);
        }

        public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
        {
            this.cachingContent.CopyTo(sourceIndex, destination, destinationIndex, count);
        }

        public char[] ToCharArray(int startIndex, int length)
        {
            return this.cachingContent.ToCharArray(startIndex, length);
        }

        public char this[int position]
        {
            get
            {
                return this.cachingContent[position];
            }
        }

        public ITextSnapshotLine GetLineFromLineNumber(int lineNumber)
        {
            TextImageLine lineSpan = this.cachingContent.GetLineFromLineNumber(lineNumber);

            return new TextSnapshotLine(this, lineSpan);
        }

        public ITextSnapshotLine GetLineFromPosition(int position)
        {
            int lineNumber = this.cachingContent.GetLineNumberFromPosition(position);
            return this.GetLineFromLineNumber(lineNumber);
        }

        public int GetLineNumberFromPosition(int position)
        {
            return this.cachingContent.GetLineNumberFromPosition(position);
        }

        public IEnumerable<ITextSnapshotLine> Lines
        {
            get
            {
                // this is a naive implementation
                int lineCount = this.cachingContent.LineCount;
                for (int line = 0; line < lineCount; ++line)
                {
                    yield return GetLineFromLineNumber(line);
                }
            }
        }

        public void Write(System.IO.TextWriter writer)
        {
            this.cachingContent.Write(writer, new Span(0, this.cachingContent.Length));
        }

        public void Write(System.IO.TextWriter writer, Span span)
        {
            this.cachingContent.Write(writer, span);
        }
        #endregion

        public ITextImage TextImage => this.cachingContent;

        public override string ToString()
        {
            return String.Format("version: {0} lines: {1} length: {2} \r\n content: {3}",
                Version.VersionNumber, LineCount, Length,
                Microsoft.VisualStudio.Text.Utilities.TextUtilities.Escape(this.GetText(0, Math.Min(40, this.Length))));
        }

#if _DEBUG
        internal string DebugOnly_AllText
        {
            get
            {
                return this.GetText(0, Math.Min(this.Length, 1024*1024));
            }
        }
#endif
    }
}