blob: dd505405a9e81578a7f07c5c9c453eb0c6bb2519 (
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
|
//
// 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 internal APIs that are subject to change without notice.
// Use at your own risk.
//
using System;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Editor.OptionsExtensionMethods;
namespace Microsoft.VisualStudio.Text
{
public static class ExtensionMethods
{
/// <summary>
/// Find the index of the next non-whitespace character in a line.
/// </summary>
/// <param name="line">The line to search.</param>
/// <param name="startIndex">The index at which to begin the search, relative to the start of the line.</param>
/// <returns>The index, relative to the start of the line, of the first non-whitespace character whose index
/// is <paramref name="startIndex"/> or greater, or -1 if there are not any non-whitespace characters at that index or greater.</returns>
/// <exception cref="ArgumentOutOfRangeException"> if <paramref name="startIndex"/> is negative.</exception>
public static int IndexOfNextNonWhiteSpaceCharacter(this ITextSnapshotLine line, int startIndex)
{
if (startIndex < 0)
{
throw new ArgumentOutOfRangeException("start");
}
// Take advantage of performant [] operators on the ITextSnapshot
ITextSnapshot snapshot = line.Snapshot;
int snapshotPos = line.Start.Position;
for (int i = startIndex; i < line.Length; i++)
{
if (!char.IsWhiteSpace(snapshot[snapshotPos + i]))
{
return i;
}
}
return -1;
}
/// <summary>
/// Find the index of the previous non-whitespace character in a line.
/// </summary>
/// <param name="line">The line to search.</param>
/// <param name="startIndex">The index at which to begin the search, relative to the start of the line.</param>
/// <returns>The index, relative to the start of the line, of the first non-whitespace character whose index
/// is <paramref name="startIndex"/> or greater, or -1 if there are not any non-whitespace characters at that index or greater.</returns>
/// <exception cref="ArgumentOutOfRangeException"> if <paramref name="startIndex"/> is negative.</exception>
public static int IndexOfPreviousNonWhiteSpaceCharacter(this ITextSnapshotLine line, int startIndex)
{
if (startIndex < 0)
{
throw new ArgumentOutOfRangeException(nameof(startIndex));
}
// Take advantage of performant [] operators on the ITextSnapshot
ITextSnapshot snapshot = line.Snapshot;
int lineStart = line.Start.Position;
for (int i = startIndex - 1; i >= 0; i--)
{
if (!char.IsWhiteSpace(snapshot[lineStart + i]))
{
return i;
}
}
return -1;
}
}
}
|