1
+ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
2
+
3
+ using Microsoft . VisualStudio . Text ;
4
+ using Microsoft . VisualStudio . Text . Tagging ;
5
+ using Roslyn . Utilities ;
6
+
7
+ namespace Microsoft . CodeAnalysis . Text . Shared . Extensions
8
+ {
9
+ internal static partial class ITextSnapshotExtensions
10
+ {
11
+ public static SnapshotPoint GetPoint ( this ITextSnapshot snapshot , int position )
12
+ => new SnapshotPoint ( snapshot , position ) ;
13
+
14
+ public static SnapshotPoint ? TryGetPoint ( this ITextSnapshot snapshot , int lineNumber , int columnIndex )
15
+ {
16
+ var position = snapshot . TryGetPosition ( lineNumber , columnIndex ) ;
17
+ if ( position . HasValue ) {
18
+ return new SnapshotPoint ( snapshot , position . Value ) ;
19
+ } else {
20
+ return null ;
21
+ }
22
+ }
23
+
24
+ /*
25
+ /// <summary>
26
+ /// Convert a <see cref="LinePositionSpan"/> to <see cref="TextSpan"/>.
27
+ /// </summary>
28
+ public static TextSpan GetTextSpan (this ITextSnapshot snapshot, LinePositionSpan span)
29
+ {
30
+ return TextSpan.FromBounds (
31
+ GetPosition (snapshot, span.Start.Line, span.Start.Character),
32
+ GetPosition (snapshot, span.End.Line, span.End.Character));
33
+ }
34
+ */
35
+
36
+ public static int GetPosition ( this ITextSnapshot snapshot , int lineNumber , int columnIndex )
37
+ => TryGetPosition ( snapshot , lineNumber , columnIndex ) . Value ;
38
+
39
+ public static int ? TryGetPosition ( this ITextSnapshot snapshot , int lineNumber , int columnIndex )
40
+ {
41
+ if ( lineNumber < 0 || lineNumber >= snapshot . LineCount ) {
42
+ return null ;
43
+ }
44
+
45
+ var end = snapshot . GetLineFromLineNumber ( lineNumber ) . Start . Position + columnIndex ;
46
+ if ( end < 0 || end > snapshot . Length ) {
47
+ return null ;
48
+ }
49
+
50
+ return end ;
51
+ }
52
+
53
+ public static bool TryGetPosition ( this ITextSnapshot snapshot , int lineNumber , int columnIndex , out SnapshotPoint position )
54
+ {
55
+ var result = 0 ;
56
+ position = new SnapshotPoint ( ) ;
57
+
58
+ if ( lineNumber < 0 || lineNumber >= snapshot . LineCount ) {
59
+ return false ;
60
+ }
61
+
62
+ var line = snapshot . GetLineFromLineNumber ( lineNumber ) ;
63
+ if ( columnIndex < 0 || columnIndex >= line . Length ) {
64
+ return false ;
65
+ }
66
+
67
+ result = line . Start . Position + columnIndex ;
68
+ position = new SnapshotPoint ( snapshot , result ) ;
69
+ return true ;
70
+ }
71
+
72
+ public static SnapshotSpan GetSpan ( this ITextSnapshot snapshot , int start , int length )
73
+ => new SnapshotSpan ( snapshot , new Span ( start , length ) ) ;
74
+
75
+ public static SnapshotSpan GetSpanFromBounds ( this ITextSnapshot snapshot , int start , int end )
76
+ => new SnapshotSpan ( snapshot , Span . FromBounds ( start , end ) ) ;
77
+
78
+ public static SnapshotSpan GetSpan ( this ITextSnapshot snapshot , Span span )
79
+ => new SnapshotSpan ( snapshot , span ) ;
80
+
81
+ public static ITagSpan < TTag > GetTagSpan < TTag > ( this ITextSnapshot snapshot , Span span , TTag tag )
82
+ where TTag : ITag
83
+ {
84
+ return new TagSpan < TTag > ( new SnapshotSpan ( snapshot , span ) , tag ) ;
85
+ }
86
+
87
+ public static SnapshotSpan GetSpan ( this ITextSnapshot snapshot , int startLine , int startIndex , int endLine , int endIndex )
88
+ {
89
+ return TryGetSpan ( snapshot , startLine , startIndex , endLine , endIndex ) . Value ;
90
+ }
91
+
92
+ public static SnapshotSpan ? TryGetSpan ( this ITextSnapshot snapshot , int startLine , int startIndex , int endLine , int endIndex )
93
+ {
94
+ var startPosition = snapshot . TryGetPosition ( startLine , startIndex ) ;
95
+ var endPosition = snapshot . TryGetPosition ( endLine , endIndex ) ;
96
+ if ( startPosition == null || endPosition == null ) {
97
+ return null ;
98
+ }
99
+
100
+ return new SnapshotSpan ( snapshot , Span . FromBounds ( startPosition . Value , endPosition . Value ) ) ;
101
+ }
102
+
103
+ public static SnapshotSpan GetFullSpan ( this ITextSnapshot snapshot )
104
+ {
105
+ Contract . ThrowIfNull ( snapshot ) ;
106
+
107
+ return new SnapshotSpan ( snapshot , new Span ( 0 , snapshot . Length ) ) ;
108
+ }
109
+
110
+ public static NormalizedSnapshotSpanCollection GetSnapshotSpanCollection ( this ITextSnapshot snapshot )
111
+ {
112
+ Contract . ThrowIfNull ( snapshot ) ;
113
+
114
+ return new NormalizedSnapshotSpanCollection ( snapshot . GetFullSpan ( ) ) ;
115
+ }
116
+
117
+ public static void GetLineAndCharacter ( this ITextSnapshot snapshot , int position , out int lineNumber , out int characterIndex )
118
+ {
119
+ var line = snapshot . GetLineFromPosition ( position ) ;
120
+
121
+ lineNumber = line . LineNumber ;
122
+ characterIndex = position - line . Start . Position ;
123
+ }
124
+
125
+ /// <summary>
126
+ /// Returns the leading whitespace of the line located at the specified position in the given snapshot.
127
+ /// </summary>
128
+ public static string GetLeadingWhitespaceOfLineAtPosition ( this ITextSnapshot snapshot , int position )
129
+ {
130
+ Contract . ThrowIfNull ( snapshot ) ;
131
+
132
+ var line = snapshot . GetLineFromPosition ( position ) ;
133
+ var linePosition = line . GetFirstNonWhitespacePosition ( ) ;
134
+ if ( ! linePosition . HasValue ) {
135
+ return line . GetText ( ) ;
136
+ }
137
+
138
+ var lineText = line . GetText ( ) ;
139
+ return lineText . Substring ( 0 , linePosition . Value - line . Start ) ;
140
+ }
141
+
142
+ public static bool AreOnSameLine ( this ITextSnapshot snapshot , int x1 , int x2 )
143
+ => snapshot . GetLineNumberFromPosition ( x1 ) == snapshot . GetLineNumberFromPosition ( x2 ) ;
144
+ }
145
+ }
0 commit comments