Skip to content

Commit 51be412

Browse files
committed
Add 'AcceptNextSuggestionWord'
1 parent 31b14de commit 51be412

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

PSReadLine/Movement.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,12 @@ public static void ForwardWord(ConsoleKeyInfo? key = null, object arg = null)
318318
return;
319319
}
320320

321+
if (_singleton._current == _singleton._buffer.Length && numericArg > 0)
322+
{
323+
AcceptNextSuggestionWord(numericArg);
324+
return;
325+
}
326+
321327
if (numericArg < 0)
322328
{
323329
BackwardWord(key, -numericArg);

PSReadLine/PublicAPI.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,5 +246,22 @@ public static void AcceptSuggestion()
246246
Replace(0, _singleton._buffer.Length, _singleton._suggestionText);
247247
}
248248
}
249+
250+
/// <summary>
251+
/// Accept the current or the next suggestion text word.
252+
/// </summary>
253+
public static void AcceptNextSuggestionWord(int numericArg)
254+
{
255+
if (_singleton._suggestionText != null)
256+
{
257+
int index = _singleton._buffer.Length;
258+
while (numericArg-- > 0 && index < _singleton._suggestionText.Length)
259+
{
260+
index = _singleton.FindForwardSuggestionWordPoint(index, _singleton.Options.WordDelimiters);
261+
}
262+
263+
Replace(0, _singleton._buffer.Length, _singleton._suggestionText.Substring(0, index));
264+
}
265+
}
249266
}
250267
}

PSReadLine/Words.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,50 @@ private bool InWord(int index, string wordDelimiters)
9494
return !char.IsWhiteSpace(c) && wordDelimiters.IndexOf(c) < 0;
9595
}
9696

97+
private bool InWord(string text, int index, string wordDelimiters)
98+
{
99+
char c = text[index];
100+
return !char.IsWhiteSpace(c) && wordDelimiters.IndexOf(c) < 0;
101+
}
102+
103+
private int FindForwardSuggestionWordPoint(int currentIndex, string wordDelimiters)
104+
{
105+
System.Diagnostics.Debug.Assert(
106+
_suggestionText != null && _suggestionText.Length > _buffer.Length,
107+
"Caller needs to make srue the suggestion text exist.");
108+
109+
if (currentIndex >= _suggestionText.Length)
110+
{
111+
return _suggestionText.Length;
112+
}
113+
114+
int i = currentIndex;
115+
if (!InWord(_suggestionText, i, wordDelimiters))
116+
{
117+
// Scan to end of current non-word region
118+
while (++i < _suggestionText.Length)
119+
{
120+
if (InWord(_suggestionText, i, wordDelimiters))
121+
{
122+
break;
123+
}
124+
}
125+
}
126+
127+
if (i < _suggestionText.Length)
128+
{
129+
while (++i < _suggestionText.Length)
130+
{
131+
if (!InWord(_suggestionText, i, wordDelimiters))
132+
{
133+
break;
134+
}
135+
}
136+
}
137+
138+
return i;
139+
}
140+
97141
/// <summary>
98142
/// Find the end of the current/next word as defined by wordDelimiters and whitespace.
99143
/// </summary>

0 commit comments

Comments
 (0)