Skip to content

Commit 694078e

Browse files
committed
Supports for 'diw' command
1 parent 2ba2b7c commit 694078e

6 files changed

+579
-1
lines changed

PSReadLine/Cmdlets.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public class PSConsoleReadLineOptions
141141
public const int DefaultCompletionQueryItems = 100;
142142

143143
// Default includes all characters PowerShell treats like a dash - em dash, en dash, horizontal bar
144-
public const string DefaultWordDelimiters = @";:,.[]{}()/\|^&*-=+'""" + "\u2013\u2014\u2015";
144+
public const string DefaultWordDelimiters = @";:,.[]{}()/\|!?^&*-=+'""" + "\u2013\u2014\u2015";
145145

146146
/// <summary>
147147
/// When ringing the bell, what should be done?

PSReadLine/KeyBindings.vi.cs

+9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ internal static ConsoleColor AlternateBackground(ConsoleColor bg)
4545
private static Dictionary<PSKeyInfo, KeyHandler> _viChordYTable;
4646
private static Dictionary<PSKeyInfo, KeyHandler> _viChordDGTable;
4747

48+
private static Dictionary<PSKeyInfo, KeyHandler> _viChordTextObjectsTable;
49+
4850
private static Dictionary<PSKeyInfo, Dictionary<PSKeyInfo, KeyHandler>> _viCmdChordTable;
4951
private static Dictionary<PSKeyInfo, Dictionary<PSKeyInfo, KeyHandler>> _viInsChordTable;
5052

@@ -231,6 +233,7 @@ private void SetDefaultViBindings()
231233
{ Keys.ucG, MakeKeyHandler( DeleteEndOfBuffer, "DeleteEndOfBuffer") },
232234
{ Keys.ucE, MakeKeyHandler( ViDeleteEndOfGlob, "ViDeleteEndOfGlob") },
233235
{ Keys.H, MakeKeyHandler( BackwardDeleteChar, "BackwardDeleteChar") },
236+
{ Keys.I, MakeKeyHandler( ViChordDeleteTextObject, "ChordViTextObject") },
234237
{ Keys.J, MakeKeyHandler( DeleteNextLines, "DeleteNextLines") },
235238
{ Keys.K, MakeKeyHandler( DeletePreviousLines, "DeletePreviousLines") },
236239
{ Keys.L, MakeKeyHandler( DeleteChar, "DeleteChar") },
@@ -255,6 +258,7 @@ private void SetDefaultViBindings()
255258
{ Keys.E, MakeKeyHandler( ViReplaceEndOfWord, "ViReplaceEndOfWord") },
256259
{ Keys.ucE, MakeKeyHandler( ViReplaceEndOfGlob, "ViReplaceEndOfGlob") },
257260
{ Keys.H, MakeKeyHandler( BackwardReplaceChar, "BackwardReplaceChar") },
261+
{ Keys.I, MakeKeyHandler( BackwardReplaceChar, "BackwardReplaceChar") },
258262
{ Keys.L, MakeKeyHandler( ReplaceChar, "ReplaceChar") },
259263
{ Keys.Space, MakeKeyHandler( ReplaceChar, "ReplaceChar") },
260264
{ Keys._0, MakeKeyHandler( ViBackwardReplaceLine, "ViBackwardReplaceLine") },
@@ -289,6 +293,11 @@ private void SetDefaultViBindings()
289293
{ Keys.Percent, MakeKeyHandler( ViYankPercent, "ViYankPercent") },
290294
};
291295

296+
_viChordTextObjectsTable = new Dictionary<PSKeyInfo, KeyHandler>
297+
{
298+
{ Keys.W, MakeKeyHandler(ViHandleTextObject, "WordTextObject")},
299+
};
300+
292301
_viChordDGTable = new Dictionary<PSKeyInfo, KeyHandler>
293302
{
294303
{ Keys.G, MakeKeyHandler( DeleteRelativeLines, "DeleteRelativeLines") },
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace Microsoft.PowerShell
5+
{
6+
internal static partial class StringBuilderTextObjectExtensions
7+
{
8+
/// <summary>
9+
/// Returns the position of the beginning of the current word as delimited by white space and delimiters
10+
/// This method differs from <see cref="ViFindPreviousWordPoint(string)" />:
11+
/// When the cursor location is on the first character of a word, <see cref="ViFindPreviousWordPoint(string)" />
12+
/// returns the position of the previous word, whereas this method returns the cursor location.
13+
///
14+
/// When the cursor location is in a word, both methods return the same result.
15+
///
16+
/// This method supports VI "iw" text object.
17+
/// </summary>
18+
public static int ViFindBeginningOfWordObjectBoundary(this StringBuilder buffer, int position, string wordDelimiters)
19+
{
20+
// cursor may be past the end of the buffer when calling this method
21+
// this may happen if the cursor is at the beginning of a new line
22+
23+
var i = Math.Min(position, buffer.Length - 1);
24+
25+
// if starting on a word consider a text object as a sequence of characters excluding the delimiters
26+
// otherwise, consider a word as a sequence of delimiters
27+
28+
var delimiters = wordDelimiters + '\n';
29+
if (buffer.InWord(i, wordDelimiters))
30+
{
31+
delimiters += " \t";
32+
}
33+
if (delimiters.IndexOf(buffer[i]) == -1 && buffer.IsWhiteSpace(i))
34+
{
35+
delimiters = " \t";
36+
}
37+
38+
var isTextObjectChar = buffer.InWord(i, wordDelimiters)
39+
? (Func<char, bool>)(c => delimiters.IndexOf(c) == -1)
40+
: c => delimiters.IndexOf(c) != -1
41+
;
42+
43+
var beginning = i;
44+
while (i >= 0 && isTextObjectChar(buffer[i]))
45+
{
46+
beginning = i--;
47+
}
48+
49+
return beginning;
50+
}
51+
52+
/// <summary>
53+
/// Finds the position of the beginning of the next word object starting from the specified position.
54+
/// If positioned on the last word in the buffer, returns buffer length + 1.
55+
/// This method supports VI "iw" text-object.
56+
/// iw: "inner word", select words. White space between words is counted too.
57+
/// </summary>
58+
public static int ViFindBeginningOfNextWordObjectBoundary(this StringBuilder buffer, int position, string wordDelimiters)
59+
{
60+
// cursor may be past the end of the buffer when calling this method
61+
// this may happen if the cursor is at the beginning of a new line
62+
63+
var i = Math.Min(position, buffer.Length - 1);
64+
65+
var startedAtBeginningOfLine =
66+
i == 0 ||
67+
buffer[i - 1] == '\n'
68+
;
69+
70+
// if starting on a word consider a text object as a sequence of characters excluding the delimiters
71+
// otherwise, consider a word as a sequence of delimiters
72+
73+
var delimiters = wordDelimiters;
74+
75+
if (buffer.InWord(i, wordDelimiters))
76+
{
77+
delimiters += " \t\n";
78+
}
79+
if (buffer.IsWhiteSpace(i))
80+
{
81+
delimiters = " \t";
82+
}
83+
84+
var isTextObjectChar = buffer.InWord(i, wordDelimiters)
85+
? (Func<char, bool>)(c => delimiters.IndexOf(c) == -1)
86+
: c => delimiters.IndexOf(c) != -1
87+
;
88+
89+
// if starting on a newline
90+
91+
if (buffer[i] == '\n' && i < buffer.Length - 1)
92+
{
93+
// try to skip two newline characters
94+
// to replicate vim behaviour
95+
96+
++i;
97+
98+
if (buffer[i] == '\n' && i < buffer.Length - 1)
99+
{
100+
++i;
101+
}
102+
}
103+
104+
// skip to next non word characters
105+
106+
while (i < buffer.Length && isTextObjectChar(buffer[i]))
107+
{
108+
++i;
109+
}
110+
111+
// when starting the search on a newline
112+
// skip one space character
113+
// replicate vim behaviour
114+
115+
if (startedAtBeginningOfLine && (i < buffer.Length && buffer.IsVisibleBlank(i)) && i < buffer.Length - 1)
116+
{
117+
++i;
118+
}
119+
120+
// make sure end includes the starting position
121+
122+
return Math.Max(i, position);
123+
}
124+
}
125+
}

PSReadLine/TextObjects.Vi.cs

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Microsoft.PowerShell
5+
{
6+
public partial class PSConsoleReadLine
7+
{
8+
internal enum TextObjectOperation
9+
{
10+
None,
11+
Change,
12+
Delete,
13+
}
14+
15+
internal enum TextObjectSpan
16+
{
17+
None,
18+
Around,
19+
Inner,
20+
}
21+
22+
private TextObjectOperation _textObjectOperation = TextObjectOperation.None;
23+
private TextObjectSpan _textObjectSpan = TextObjectSpan.None;
24+
25+
private readonly IDictionary<TextObjectOperation, IDictionary<TextObjectSpan, KeyHandler>> _textObjectHandlers
26+
= new Dictionary<TextObjectOperation, IDictionary<TextObjectSpan, KeyHandler>>
27+
{
28+
{
29+
TextObjectOperation.Delete,
30+
new Dictionary<TextObjectSpan, KeyHandler>
31+
{
32+
{TextObjectSpan.Inner, MakeKeyHandler(ViDeleteInnerWord, "ViDeleteInnerWord")}
33+
}
34+
}
35+
};
36+
37+
private void ViChordDeleteTextObject(ConsoleKeyInfo? key = null, object arg = null)
38+
{
39+
_textObjectOperation = TextObjectOperation.Delete;
40+
41+
if (!key.HasValue)
42+
{
43+
ResetTextObjectState();
44+
throw new ArgumentNullException(nameof(key));
45+
}
46+
47+
_textObjectSpan = GetRequestedTextObjectSpan(key.Value);
48+
49+
// handle text object
50+
51+
var textObjectKey = ReadKey();
52+
if (_viChordTextObjectsTable.TryGetValue(textObjectKey, out _))
53+
{
54+
_singleton.ProcessOneKey(textObjectKey, _viChordTextObjectsTable, ignoreIfNoAction: true, arg: arg);
55+
}
56+
else
57+
{
58+
ResetTextObjectState();
59+
Ding();
60+
}
61+
}
62+
63+
private TextObjectSpan GetRequestedTextObjectSpan(ConsoleKeyInfo key)
64+
{
65+
if (key.KeyChar == 'i')
66+
{
67+
return TextObjectSpan.Inner;
68+
}
69+
else if (key.KeyChar == 'a')
70+
{
71+
return TextObjectSpan.Around;
72+
}
73+
else
74+
{
75+
System.Diagnostics.Debug.Assert(false);
76+
throw new NotSupportedException();
77+
}
78+
}
79+
80+
private static void ViHandleTextObject(ConsoleKeyInfo? key = null, object arg = null)
81+
{
82+
if (
83+
!_singleton._textObjectHandlers.TryGetValue(_singleton._textObjectOperation, out var textObjectHandler) ||
84+
!textObjectHandler.TryGetValue(_singleton._textObjectSpan, out var handler)
85+
)
86+
{
87+
ResetTextObjectState();
88+
Ding();
89+
return;
90+
}
91+
92+
handler.Action(key, arg);
93+
}
94+
95+
private static void ResetTextObjectState()
96+
{
97+
_singleton._textObjectOperation = TextObjectOperation.None;
98+
_singleton._textObjectSpan = TextObjectSpan.None;
99+
}
100+
101+
private static void ViDeleteInnerWord(ConsoleKeyInfo? key = null, object arg = null)
102+
{
103+
var delimiters = _singleton.Options.WordDelimiters;
104+
105+
if (TryGetArgAsInt(arg, out var numericArg, 1))
106+
{
107+
if (_singleton._buffer.Length == 0)
108+
{
109+
if (numericArg > 1)
110+
{
111+
Ding();
112+
}
113+
return;
114+
}
115+
116+
// unless at the end of the buffer a single delete word should not delete backwards
117+
// so if the cursor is on an empty line, do nothing
118+
119+
if (
120+
numericArg == 1 &&
121+
_singleton._current < _singleton._buffer.Length &&
122+
_singleton._buffer.IsLogigalLineEmpty(_singleton._current)
123+
)
124+
{
125+
return;
126+
}
127+
128+
var start = _singleton._buffer.ViFindBeginningOfWordObjectBoundary(_singleton._current, delimiters);
129+
var end = _singleton._current;
130+
131+
// attempting to find a valid position for multiple words
132+
// if no valid position is found, this is a no-op
133+
134+
{
135+
while (numericArg-- > 0 && end < _singleton._buffer.Length)
136+
{
137+
end = _singleton._buffer.ViFindBeginningOfNextWordObjectBoundary(end, delimiters);
138+
}
139+
140+
// attempting to delete too many words should ding
141+
142+
if (numericArg > 0)
143+
{
144+
Ding();
145+
return;
146+
}
147+
}
148+
149+
_singleton.RemoveTextToClipboard(start, end - start);
150+
_singleton.AdjustCursorPosition(start);
151+
_singleton.Render();
152+
}
153+
}
154+
155+
/// <summary>
156+
/// Attempt to set the cursor at the specified position.
157+
/// </summary>
158+
/// <param name="position"></param>
159+
/// <returns></returns>
160+
private int AdjustCursorPosition(int position)
161+
{
162+
// this method might prove useful in a more general case
163+
164+
if (_buffer.Length == 0)
165+
{
166+
_current = 0;
167+
return 0;
168+
}
169+
170+
var maxPosition = _buffer[_buffer.Length - 1] == '\n'
171+
? _buffer.Length
172+
: _buffer.Length - 1
173+
;
174+
175+
var newCurrent = Math.Min(position, maxPosition);
176+
177+
var beginning = GetBeginningOfLinePos(newCurrent);
178+
179+
if (newCurrent < _buffer.Length && _buffer[newCurrent] == '\n' && (newCurrent + ViEndOfLineFactor > beginning))
180+
{
181+
newCurrent += ViEndOfLineFactor;
182+
}
183+
184+
_current = newCurrent;
185+
186+
return newCurrent;
187+
}
188+
}
189+
}

0 commit comments

Comments
 (0)