forked from PowerShell/PSReadLine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringBuilderExtensions.cs
More file actions
84 lines (72 loc) · 2.49 KB
/
Copy pathStringBuilderExtensions.cs
File metadata and controls
84 lines (72 loc) · 2.49 KB
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
using System;
using System.Text;
namespace Microsoft.PowerShell
{
internal class Range
{
internal Range(int offset, int count)
{
Offset = offset;
Count = count;
}
internal int Offset { get; }
internal int Count { get; }
}
internal static class StringBuilderLinewiseExtensions
{
/// <summary>
/// Determines the offset and the length of the fragment
/// in the specified buffer that corresponds to a
/// given number of lines starting from the specified line index
/// </summary>
/// <param name="buffer" />
/// <param name="lineIndex" />
/// <param name="lineCount" />
internal static Range GetRange(this StringBuilder buffer, int lineIndex, int lineCount)
{
// this method considers lines by the first '\n' character from the previous line
// up until the last non new-line character of the current line.
//
// buffer: line 0\nline 1\nline 2[...]\nline n
// lines: 0....._1......_2......[...]_3......
var length = buffer.Length;
var startPosition = 0;
var startPositionIdentified = false;
var endPosition = length - 1;
var currentLine = 0;
if (lineIndex == 0)
{
startPosition = 0;
startPositionIdentified = true;
}
for (var position = 0; position < length; position++)
{
if (buffer[position] == '\n')
{
currentLine++;
if (!startPositionIdentified && currentLine == lineIndex)
{
startPosition = position;
startPositionIdentified = true;
}
if (currentLine == lineIndex + lineCount)
{
endPosition = position - 1;
break;
}
}
}
return new Range(
startPosition,
endPosition - startPosition + 1
);
}
}
internal static class StringBuilderPredictionExtensions
{
internal static StringBuilder EndColorSection(this StringBuilder buffer, string selectionHighlighting)
{
return buffer.Append(VTColorUtils.AnsiReset).Append(selectionHighlighting);
}
}
}