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

TextImageLine.cs « Model « TextData « Def « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b7e95c145d6d48ca49bdc04b86b05ad4ac8e41fe (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
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//  Licensed under the MIT License. See License.txt in the project root for license information.
//
namespace Microsoft.VisualStudio.Text
{
    using System;

    /// <summary>
    /// Immutable information about a line of text from an <see cref="ITextImage"/>.
    /// </summary>
    public struct TextImageLine : IEquatable<TextImageLine>
    {
        public readonly static TextImageLine Invalid = new TextImageLine();

        public TextImageLine(ITextImage image, int lineNumber, Span extent, int lineBreakLength)
        {
            if (image == null)
                throw new ArgumentNullException(nameof(image));

            if ((lineNumber < 0) || (lineNumber >= image.LineCount))
                throw new ArgumentOutOfRangeException(nameof(lineNumber));

            if (extent.End > image.Length)
                throw new ArgumentOutOfRangeException(nameof(extent));

            if ((lineBreakLength < 0) || (lineBreakLength > 2) || (extent.End + lineBreakLength > image.Length))
                throw new ArgumentOutOfRangeException(nameof(lineBreakLength));

            this.Image = image;
            this.LineNumber = lineNumber;
            this.Extent = extent;
            this.LineBreakLength = lineBreakLength;
        }

        /// <summary>
        /// The <see cref="ITextImage"/> in which the line appears.
        /// </summary>
        public readonly ITextImage Image;

        /// <summary>
        /// The extent of the line, excluding any line break characters.
        /// </summary>
        public readonly Span Extent;

        /// <summary>
        /// The extent of the line, including any line break characters.
        /// </summary>
        public Span ExtentIncludingLineBreak { get { return new Span(this.Extent.Start, this.LengthIncludingLineBreak); } }

        /// <summary>
        /// The 0-origin line number of the line.
        /// </summary>
        public readonly int LineNumber;

        /// <summary>
        /// The position of the first character in the line.
        /// </summary>
        public int Start { get { return this.Extent.Start; } }

        /// <summary>
        /// Length of the line, excluding any line break characters.
        /// </summary>
        public int Length { get { return this.Extent.Length; } }

        /// <summary>
        /// Length of the line, including any line break characters.
        /// </summary>
        public int LengthIncludingLineBreak { get { return this.Extent.Length + this.LineBreakLength; } }

        /// <summary>
        /// The position of the first character past the end of the line, excluding any
        /// line break characters (thus will address a line break character, except 
        /// for the last line in the buffer, in which case it addresses a
        /// position past the end of the buffer).
        /// </summary>
        public int End { get { return this.Extent.End; } }

        /// <summary>
        /// The position of the first character past the end of the line, including any
        /// line break characters (thus will address the first character in 
        /// the succeeding line, unless this is the last line, in which case it addresses a
        /// position past the end of the buffer).
        /// </summary>
        public int EndIncludingLineBreak { get { return this.Extent.End + this.LineBreakLength; } }

        /// <summary>
        /// Length of line break characters (always falls in the range [0..2]).
        /// </summary>
        public readonly int LineBreakLength;

        /// <summary>
        /// The text of the line, excluding any line break characters.
        /// </summary>
        public string GetText() { return this.Image.GetText(this.Extent); }

        /// <summary>
        /// The text of the line, including any line break characters.
        /// </summary>
        public string GetTextIncludingLineBreak() { return this.Image.GetText(this.ExtentIncludingLineBreak); }

        /// <summary>
        /// The string consisting of the line break characters (if any) at the
        /// end of the line. Has zero length for the last line in the buffer.
        /// </summary>
        public string GetLineBreakText() { return this.Image.GetText(new Span(this.Extent.End, this.LineBreakLength)); }

        public override int GetHashCode()
        {
            return (this.Image != null) ? (this.LineNumber ^ this.Image.GetHashCode()) : 0;
        }

        public override bool Equals(object obj)
        {
            if (obj is TextImageLine)
            {
                var other = (TextImageLine)obj;
                return this.Equals(other);
            }

            return false;
        }

        public bool Equals(TextImageLine other)
        {
            return (other.Image == this.Image) && (other.LineNumber == this.LineNumber);
        }

        public static bool operator ==(TextImageLine left, TextImageLine right)
        {
            return left.Equals(right);
        }

        public static bool operator !=(TextImageLine left, TextImageLine right)
        {
            return !left.Equals(right);
        }

        public override string ToString()
        {
            return (this.Image == null)
                   ? nameof(Invalid)
                   : string.Format(System.Globalization.CultureInfo.CurrentCulture, "v{0}[{1}, {2}+{3}]",
                                   this.Image.Version?.VersionNumber, this.Extent.Start, this.Extent.End, this.LineBreakLength);
        }
    }
}