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

DefaultBufferPrimitive.cs « EditorPrimitives « Impl « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9e383d9befdbbc0715c2774f2498fed96e28e5d3 (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
//
//  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.EditorPrimitives.Implementation
{
    using System;
    using System.Collections.Generic;

    using Microsoft.VisualStudio.Text;
    using Microsoft.VisualStudio.Text.Editor;

    internal sealed class DefaultBufferPrimitive : TextBuffer
    {
        #region Private members
        private ITextBuffer _textBuffer;
        private IBufferPrimitivesFactoryService _bufferPrimitivesFactory;
        #endregion

        public DefaultBufferPrimitive(ITextBuffer textBuffer, IBufferPrimitivesFactoryService bufferPrimitivesFactory)
        {
            _textBuffer = textBuffer;
            _bufferPrimitivesFactory = bufferPrimitivesFactory;
        }

        public override TextPoint GetTextPoint(int position)
        {
            if ((position < 0) || (position > _textBuffer.CurrentSnapshot.Length))
            {
                throw new ArgumentOutOfRangeException(nameof(position));
            }
            return _bufferPrimitivesFactory.CreateTextPoint(this, position);
        }

        public override TextPoint GetTextPoint(int line, int column)
        {
            if ((line < 0) || (line > _textBuffer.CurrentSnapshot.LineCount))
            {
                throw new ArgumentOutOfRangeException(nameof(line));
            }

            ITextSnapshotLine snapshotLine = _textBuffer.CurrentSnapshot.GetLineFromLineNumber(line);

            if ((column < 0) || (column > snapshotLine.Length))
            {
                throw new ArgumentOutOfRangeException(nameof(column));
            }
            return _bufferPrimitivesFactory.CreateTextPoint(this, snapshotLine.Start + column);
        }

        public override TextRange GetLine(int line)
        {
            if ((line < 0) || (line > _textBuffer.CurrentSnapshot.LineCount))
            {
                throw new ArgumentOutOfRangeException(nameof(line));
            }

            ITextSnapshotLine snapshotLine = _textBuffer.CurrentSnapshot.GetLineFromLineNumber(line);

            return GetTextRange(snapshotLine.Extent.Start, snapshotLine.Extent.End);            
        }

        public override TextRange GetTextRange(TextPoint startPoint, TextPoint endPoint)
        {
            if (startPoint == null)
            {
                throw new ArgumentNullException(nameof(startPoint));
            }
            if (endPoint == null)
            {
                throw new ArgumentNullException(nameof(endPoint));
            }

            if (!object.ReferenceEquals(startPoint.TextBuffer, this))
            {
                throw new ArgumentException(Strings.TextPointFromWrongBuffer);
            }

            if (!object.ReferenceEquals(endPoint.TextBuffer, this))
            {
                throw new ArgumentException(Strings.TextPointFromWrongBuffer);
            }

            return _bufferPrimitivesFactory.CreateTextRange(this, startPoint, endPoint);
        }

        public override TextRange GetTextRange(int startPosition, int endPosition)
        {
            if ((startPosition < 0) || (startPosition > _textBuffer.CurrentSnapshot.Length))
            {
                throw new ArgumentOutOfRangeException(nameof(startPosition));
            }

            if ((endPosition < 0) || (endPosition > _textBuffer.CurrentSnapshot.Length))
            {
                throw new ArgumentOutOfRangeException(nameof(endPosition));
            }
            
            TextPoint startPoint = GetTextPoint(startPosition);
            TextPoint endPoint = GetTextPoint(endPosition);

            return _bufferPrimitivesFactory.CreateTextRange(this, startPoint, endPoint);
        }

        public override ITextBuffer AdvancedTextBuffer
        {
            get { return _textBuffer; }
        }

        public override TextPoint GetStartPoint()
        {
            return GetTextPoint(0);
        }

        public override TextPoint GetEndPoint()
        {
            return GetTextPoint(_textBuffer.CurrentSnapshot.Length);
        }

        public override IEnumerable<TextRange> Lines
        {
            get 
            {
                foreach (ITextSnapshotLine line in _textBuffer.CurrentSnapshot.Lines)
                {
                    yield return GetTextRange(line.Start, line.End);
                }
            }
        }
    }
}