forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWords.cs
More file actions
204 lines (187 loc) · 5.78 KB
/
Words.cs
File metadata and controls
204 lines (187 loc) · 5.78 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System.Collections.Generic;
using System.Management.Automation.Language;
namespace Microsoft.PowerShell
{
public partial class PSConsoleReadLine
{
private enum FindTokenMode
{
CurrentOrNext,
Next,
Previous,
}
static bool OffsetWithinToken(int offset, Token token)
{
return offset < token.Extent.EndOffset && offset >= token.Extent.StartOffset;
}
private Token FindNestedToken(int offset, IList<Token> tokens, FindTokenMode mode)
{
Token token = null;
bool foundNestedToken = false;
int i;
for (i = tokens.Count - 1; i >= 0; i--)
{
if (OffsetWithinToken(offset, tokens[i]))
{
token = tokens[i];
var strToken = token as StringExpandableToken;
if (strToken != null && strToken.NestedTokens != null)
{
var nestedToken = FindNestedToken(offset, strToken.NestedTokens, mode);
if (nestedToken != null)
{
token = nestedToken;
foundNestedToken = true;
}
}
break;
}
if (offset >= tokens[i].Extent.EndOffset)
{
break;
}
}
switch (mode)
{
case FindTokenMode.CurrentOrNext:
if (token == null && (i + 1) < tokens.Count)
{
token = tokens[i + 1];
}
break;
case FindTokenMode.Next:
if (!foundNestedToken)
{
// If there is no next token, return null (happens with nested
// tokens where there is no EOF/EOS token).
token = ((i + 1) < tokens.Count) ? tokens[i + 1] : null;
}
break;
case FindTokenMode.Previous:
if (token == null)
{
if (i >= 0)
{
token = tokens[i];
}
}
else if (offset == token.Extent.StartOffset)
{
token = i > 0 ? tokens[i - 1] : null;
}
break;
}
return token;
}
private Token FindToken(int current, FindTokenMode mode)
{
MaybeParseInput();
return FindNestedToken(current, _tokens, mode);
}
private bool InWord(int index, string wordDelimiters)
{
char c = _buffer[index];
return !char.IsWhiteSpace(c) && wordDelimiters.IndexOf(c) < 0;
}
/// <summary>
/// Find the end of the current/next word as defined by wordDelimiters and whitespace.
/// </summary>
private int FindForwardWordPoint(string wordDelimiters)
{
int i = _current;
if (i == _buffer.Length)
{
return i;
}
if (!InWord(i, wordDelimiters))
{
// Scan to end of current non-word region
while (i < _buffer.Length)
{
if (InWord(i, wordDelimiters))
{
break;
}
i += 1;
}
}
while (i < _buffer.Length)
{
if (!InWord(i, wordDelimiters))
{
break;
}
i += 1;
}
return i;
}
/// <summary>
/// Find the start of the next word.
/// </summary>
private int FindNextWordPoint(string wordDelimiters)
{
int i = _singleton._current;
if (i == _singleton._buffer.Length)
{
return i;
}
if (InWord(i, wordDelimiters))
{
// Scan to end of current word region
while (i < _singleton._buffer.Length)
{
if (!InWord(i, wordDelimiters))
{
break;
}
i += 1;
}
}
while (i < _singleton._buffer.Length)
{
if (InWord(i, wordDelimiters))
{
break;
}
i += 1;
}
return i;
}
/// <summary>
/// Find the beginning of the previous word.
/// </summary>
private int FindBackwardWordPoint(string wordDelimiters)
{
int i = _current - 1;
if (i < 0)
{
return 0;
}
if (!InWord(i, wordDelimiters))
{
// Scan backwards until we are at the end of the previous word.
while (i > 0)
{
if (InWord(i, wordDelimiters))
{
break;
}
i -= 1;
}
}
while (i > 0)
{
if (!InWord(i, wordDelimiters))
{
i += 1;
break;
}
i -= 1;
}
return i;
}
}
}