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

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

namespace Microsoft.VisualStudio.Text.Implementation
{
    public static class LineBreakManager
    {
        public readonly static ILineBreaks Empty = new ShortLineBreaksEditor(0);

        public static ILineBreaksEditor CreateLineBreakEditor(int maxLength, int initialCapacity = 0)
        {
            return (maxLength < short.MaxValue)
                   ? (ILineBreaksEditor)(new ShortLineBreaksEditor(initialCapacity))
                   : (ILineBreaksEditor)(new IntLineBreaksEditor(initialCapacity));
        }

        public static ILineBreaks CreateLineBreaks(string source)
        {
            ILineBreaksEditor lineBreaks = null;

            int index = 0;
            while (index < source.Length)
            {
                int breakLength = TextUtilities.LengthOfLineBreak(source, index, source.Length);
                if (breakLength == 0)
                {
                    ++index;
                }
                else
                {
                    if (lineBreaks == null)
                        lineBreaks = LineBreakManager.CreateLineBreakEditor(source.Length);

                    lineBreaks.Add(index, breakLength);
                    index += breakLength;
                }
            }

            return lineBreaks ?? Empty;
        }

        private class ShortLineBreaksEditor : ILineBreaksEditor
        {
            private const ushort MaskForPosition = 0x7fff;
            private const ushort MaskForLength = 0x8000;

            private readonly static List<ushort> Empty = new List<ushort>(0);
            private List<ushort> _lineBreaks = Empty;

            public ShortLineBreaksEditor(int initialCapacity)
            {
                if (initialCapacity > 0)
                    _lineBreaks = new List<ushort>(initialCapacity);
            }

            public int Length => _lineBreaks.Count;

            public int LengthOfLineBreak(int index)
            {
                return ((_lineBreaks[index] & MaskForLength) != 0 ? 2 : 1);
            }

            public int StartOfLineBreak(int index)
            {
                return (int)(_lineBreaks[index] & MaskForPosition);
            }
            public int EndOfLineBreak(int index)
            {
                int lineBreak = _lineBreaks[index];
                return (lineBreak & MaskForPosition) + 
                       (((lineBreak & MaskForLength) != 0) ? 2 : 1);
            }

            public void Add(int start, int length)
            {
                if ((start < 0) || (start > short.MaxValue))
                    throw new ArgumentOutOfRangeException(nameof(start));
                if ((length < 1) || (length > 2))
                    throw new ArgumentOutOfRangeException(nameof(length));

                if (_lineBreaks == Empty)
                    _lineBreaks = new List<ushort>();

                if (length == 1)
                    _lineBreaks.Add((ushort)start);
                else if (length == 2)
                    _lineBreaks.Add((ushort)(start | MaskForLength));
            }
        }

        private class IntLineBreaksEditor : ILineBreaksEditor
        {
            private const uint MaskForPosition = 0x7fffffff;
            private const uint MaskForLength = 0x80000000;

            private readonly static List<uint> Empty = new List<uint>(0);
            private List<uint> _lineBreaks = Empty;

            public IntLineBreaksEditor(int initialCapacity)
            {
                if (initialCapacity > 0)
                    _lineBreaks = new List<uint>(initialCapacity);
            }

            public int Length => _lineBreaks.Count;

            public int LengthOfLineBreak(int index)
            {
                return (_lineBreaks[index] & MaskForLength) != 0 ? 2 : 1;
            }

            public int StartOfLineBreak(int index)
            {
                return (int)(_lineBreaks[index] & MaskForPosition);
            }

            public int EndOfLineBreak(int index)
            {
                uint lineBreak = _lineBreaks[index];
                return (int)((lineBreak & MaskForPosition) +
                             (((lineBreak & MaskForLength) != 0) ? 2 : 1));
            }

            public void Add(int start, int length)
            {
                if ((start < 0) || (start > int.MaxValue))
                    throw new ArgumentOutOfRangeException(nameof(start));
                if ((length < 1) || (length > 2))
                    throw new ArgumentOutOfRangeException(nameof(length));

                if (_lineBreaks == Empty)
                    _lineBreaks = new List<uint>();

                if (length == 1)
                    _lineBreaks.Add((uint)start);
                else if (length == 2)
                    _lineBreaks.Add((uint)(start | MaskForLength));
            }
        }
    }
}