forked from PowerShell/PSReadLine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringBuilderTextObjectExtensions.cs
More file actions
113 lines (98 loc) · 4.39 KB
/
Copy pathStringBuilderTextObjectExtensions.cs
File metadata and controls
113 lines (98 loc) · 4.39 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
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
using System;
using System.Text;
namespace Microsoft.PowerShell
{
internal static class StringBuilderTextObjectExtensions
{
private const string WhiteSpace = " \n\t";
/// <summary>
/// Returns the position of the beginning of the current word as delimited by white space and delimiters
/// This method differs from <see cref="ViFindPreviousWordPoint(string)"/>:
/// - When the cursor location is on the first character of a word, <see cref="ViFindPreviousWordPoint(string)"/>
/// returns the position of the previous word, whereas this method returns the cursor location.
/// - When the cursor location is in a word, both methods return the same result.
/// This method supports VI "iw" text object.
/// </summary>
public static int ViFindBeginningOfWordObjectBoundary(this StringBuilder buffer, int position, string wordDelimiters)
{
// Cursor may be past the end of the buffer when calling this method
// this may happen if the cursor is at the beginning of a new line.
var i = Math.Min(position, buffer.Length - 1);
// If starting on a word consider a text object as a sequence of characters excluding the delimiters,
// otherwise, consider a word as a sequence of delimiters.
var delimiters = wordDelimiters;
var isInWord = buffer.InWord(i, wordDelimiters);
if (isInWord)
{
// For the purpose of this method, whitespace character is considered a delimiter.
delimiters += WhiteSpace;
}
else
{
char c = buffer[i];
if ((wordDelimiters + '\n').IndexOf(c) == -1 && char.IsWhiteSpace(c))
{
// Current position points to a whitespace that is not a newline.
delimiters = WhiteSpace;
}
else
{
delimiters += '\n';
}
}
var isTextObjectChar = isInWord
? (Func<char, bool>)(c => delimiters.IndexOf(c) == -1)
: c => delimiters.IndexOf(c) != -1;
var beginning = i;
while (i >= 0 && isTextObjectChar(buffer[i]))
{
beginning = i--;
}
return beginning;
}
/// <summary>
/// Finds the position of the beginning of the next word object starting from the specified position.
/// If positioned on the last word in the buffer, returns buffer length + 1.
/// This method supports VI "iw" text-object.
/// iw: "inner word", select words. White space between words is counted too.
/// </summary>
public static int ViFindBeginningOfNextWordObjectBoundary(this StringBuilder buffer, int position, string wordDelimiters)
{
// Cursor may be past the end of the buffer when calling this method
// this may happen if the cursor is at the beginning of a new line.
var i = Math.Min(position, buffer.Length - 1);
// Always skip the first newline character.
if (buffer[i] == '\n' && i < buffer.Length - 1)
{
++i;
}
// If starting on a word consider a text object as a sequence of characters excluding the delimiters,
// otherwise, consider a word as a sequence of delimiters.
var delimiters = wordDelimiters;
var isInWord = buffer.InWord(i, wordDelimiters);
if (isInWord)
{
delimiters += WhiteSpace;
}
else if (char.IsWhiteSpace(buffer[i]))
{
delimiters = " \t";
}
var isTextObjectChar = isInWord
? (Func<char, bool>)(c => delimiters.IndexOf(c) == -1)
: c => delimiters.IndexOf(c) != -1;
// Try to skip a second newline characters to replicate vim behaviour.
if (buffer[i] == '\n' && i < buffer.Length - 1)
{
++i;
}
// Skip to next non-word characters.
while (i < buffer.Length && isTextObjectChar(buffer[i]))
{
++i;
}
// Make sure end includes the starting position.
return Math.Max(i, position);
}
}
}