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

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

namespace Microsoft.VisualStudio.Text.Editor
{
    public static class TextViewExtensions2
    {
        public static bool TryMoveCaretToAndEnsureVisible(
            this ITextView textView,
            SnapshotPoint point,
            IOutliningManagerService outliningManagerService = null,
            EnsureSpanVisibleOptions ensureSpanVisibleOptions = EnsureSpanVisibleOptions.None)
        {
            return textView.TryMoveCaretToAndEnsureVisible(
                new VirtualSnapshotPoint(point),
                outliningManagerService,
                ensureSpanVisibleOptions);
        }

        public static bool TryMoveCaretToAndEnsureVisible(
            this ITextView textView,
            VirtualSnapshotPoint point,
            IOutliningManagerService outliningManagerService = null,
            EnsureSpanVisibleOptions ensureSpanVisibleOptions = EnsureSpanVisibleOptions.None)
        {
            if (textView.IsClosed)
            {
                return false;
            }

            var pointInView = textView.GetPositionInView(point.Position);
            if (!pointInView.HasValue)
            {
                return false;
            }

            // If we were given an outlining service, we need to expand any outlines first, or else
            // the Caret.MoveTo won't land in the correct location if our target is inside a
            // collapsed outline.
            if (outliningManagerService != null)
            {
                var outliningManager = outliningManagerService.GetOutliningManager(textView);
                if (outliningManager != null)
                {
                    outliningManager.ExpandAll(new SnapshotSpan(pointInView.Value, length: 0), match: _ => true);
                }
            }

            var newPosition = textView.Caret.MoveTo(new VirtualSnapshotPoint(pointInView.Value, point.VirtualSpaces));

            // We use the caret's position in the view's current snapshot here in case something
            // changed text in response to a caret move (e.g. line commit)
            var spanInView = new SnapshotSpan(newPosition.BufferPosition, 0);
            textView.ViewScroller.EnsureSpanVisible(spanInView, ensureSpanVisibleOptions);

            return true;
        }

        public static SnapshotPoint GetSnapshotPoint(this ITextView textView, int lineNumber, int columnNumber = 0)
        {
            return textView.TextSnapshot.TryGetSnapshotPoint(lineNumber, columnNumber) ?? default;
        }

        public static void SelectSpan(
            this ITextView textView,
            SnapshotSpan span,
            IOutliningManagerService outliningManagerService = null,
            EnsureSpanVisibleOptions ensureSpanVisibleOptions = EnsureSpanVisibleOptions.None)
        {
            textView.TryMoveCaretToAndEnsureVisible(span.Start, outliningManagerService, ensureSpanVisibleOptions);
            textView.Selection.Select(span, isReversed: false);
        }

        public static SnapshotPoint GetPosition(this ITextView textView, int position)
        {
            var snapshot = textView.TextSnapshot;
            if (position < 0 || position > snapshot.Length)
            {
                return default;
            }

            return new SnapshotPoint(textView.TextSnapshot, position);
        }

        public static void NavigateToLineAndColumn(this ITextView textView, int lineNumber, int columnNumber = 0)
        {
            var point = textView.TextSnapshot.TryGetSnapshotPoint(lineNumber, columnNumber) ?? default;
            if (point != default)
            {
                textView.TryMoveCaretToAndEnsureVisible(point);
            }
        }

        public static SnapshotPoint? GetPositionInView(this ITextView textView, SnapshotPoint point)
        {
            return textView.BufferGraph.MapUpToSnapshot(
                point,
                PointTrackingMode.Positive,
                PositionAffinity.Successor,
                textView.TextSnapshot);
        }

        public static SnapshotPoint? GetPositionInSubjectBuffer(this ITextView textView, SnapshotPoint point)
        {
            var dataBufferPosition = textView.BufferGraph.MapDownToFirstMatch(
                point,
                PointTrackingMode.Positive,
                t => !(t.TextBuffer is IProjectionBufferBase),
                PositionAffinity.Successor);
            return dataBufferPosition;
        }

        public static ITextBuffer GetSubjectBufferFromPosition(this ITextView textView, SnapshotPoint point)
        {
            var position = GetPositionInSubjectBuffer(textView, point);
            if (position.HasValue && position.Value.Snapshot.TextBuffer is ITextBuffer buffer)
            {
                return buffer;
            }

            return null;
        }

        public static ITextBuffer GetSubjectBufferFromCaret(this ITextView view)
            => view.GetSubjectBufferFromPosition(view.Caret.Position.BufferPosition);
    }
}