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

TrackingPoint.cs « TextModel « Impl « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 02750f38dbfbc40b77cc1c4520b21268d1817aa2 (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
//
//  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;

    /// <summary>
    /// Base class for a tracking position in a particular <see cref="ITextBuffer"/>.
    /// </summary>
    internal abstract partial class TrackingPoint : ITrackingPoint
    {
        #region State and Construction
        protected readonly PointTrackingMode trackingMode;

        protected TrackingPoint(ITextVersion version, int position, PointTrackingMode trackingMode)
        {
            if (version == null)
            {
                throw new ArgumentNullException("version");
            }
            if (position < 0 | position > version.Length)
            {
                throw new ArgumentOutOfRangeException("position");
            }
            if (trackingMode < PointTrackingMode.Positive || trackingMode > PointTrackingMode.Negative)
            {
                throw new ArgumentOutOfRangeException("trackingMode");
            }

            this.trackingMode = trackingMode;
        }
        #endregion

        #region ITrackingPoint members
        public abstract ITextBuffer TextBuffer { get; }

        public PointTrackingMode TrackingMode
        {
            get { return this.trackingMode; }
        }

        public abstract TrackingFidelityMode TrackingFidelity { get; }

        public int GetPosition(ITextVersion version)
        {
            if (version == null)
            {
                throw new ArgumentNullException("version");
            }
            if (version.TextBuffer != this.TextBuffer)
            {
                throw new ArgumentException(Strings.InvalidVersion);
            }
            return TrackPosition(version);
        }

        public int GetPosition(ITextSnapshot snapshot)
        {
            if (snapshot == null)
            {
                throw new ArgumentNullException("snapshot");
            }
            if (snapshot.TextBuffer != this.TextBuffer)
            {
                throw new ArgumentException(Strings.InvalidSnapshot);
            }
            return TrackPosition(snapshot.Version);
        }

        public SnapshotPoint GetPoint(ITextSnapshot snapshot)
        {
            return new SnapshotPoint(snapshot, GetPosition(snapshot));
        }

        public char GetCharacter(ITextSnapshot snapshot)
        {
            return GetPoint(snapshot).GetChar();
        }
        #endregion

        protected abstract int TrackPosition(ITextVersion targetVersion);

        #region Diagnostic Support
        protected static string PointTrackingModeToString(PointTrackingMode trackingMode)
        {
            return trackingMode == PointTrackingMode.Positive ? "→" : "←";
        }

        protected static string ToString(ITextVersion version, int position, PointTrackingMode trackingMode)
        {
            return string.Format(System.Globalization.CultureInfo.CurrentCulture, "V{0} {2}@{1}",
                                 version.VersionNumber, position, PointTrackingModeToString(trackingMode));
        }
        #endregion
    }
}