Skip to content

Commit d7b9f82

Browse files
authored
[vi-mode] Supports the text-object command diw (#2059)
1 parent 4d78ce1 commit d7b9f82

13 files changed

+709
-26
lines changed

PSReadLine/Cmdlets.cs

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

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

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

PSReadLine/KeyBindings.vi.cs

+8
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

@@ -238,6 +240,7 @@ private void SetDefaultViBindings()
238240
{ Keys.ucG, MakeKeyHandler( DeleteEndOfBuffer, "DeleteEndOfBuffer") },
239241
{ Keys.ucE, MakeKeyHandler( ViDeleteEndOfGlob, "ViDeleteEndOfGlob") },
240242
{ Keys.H, MakeKeyHandler( BackwardDeleteChar, "BackwardDeleteChar") },
243+
{ Keys.I, MakeKeyHandler( ViChordDeleteTextObject, "ChordViTextObject") },
241244
{ Keys.J, MakeKeyHandler( DeleteNextLines, "DeleteNextLines") },
242245
{ Keys.K, MakeKeyHandler( DeletePreviousLines, "DeletePreviousLines") },
243246
{ Keys.L, MakeKeyHandler( DeleteChar, "DeleteChar") },
@@ -296,6 +299,11 @@ private void SetDefaultViBindings()
296299
{ Keys.Percent, MakeKeyHandler( ViYankPercent, "ViYankPercent") },
297300
};
298301

302+
_viChordTextObjectsTable = new Dictionary<PSKeyInfo, KeyHandler>
303+
{
304+
{ Keys.W, MakeKeyHandler(ViHandleTextObject, "WordTextObject")},
305+
};
306+
299307
_viChordDGTable = new Dictionary<PSKeyInfo, KeyHandler>
300308
{
301309
{ Keys.G, MakeKeyHandler( DeleteRelativeLines, "DeleteRelativeLines") },

PSReadLine/Position.cs

+2-11
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,14 @@ private static int GetFirstNonBlankOfLogicalLinePos(int current)
102102
var beginningOfLine = GetBeginningOfLinePos(current);
103103

104104
var newCurrent = beginningOfLine;
105+
var buffer = _singleton._buffer;
105106

106-
while (newCurrent < _singleton._buffer.Length && IsVisibleBlank(newCurrent))
107+
while (newCurrent < buffer.Length && buffer.IsVisibleBlank(newCurrent))
107108
{
108109
newCurrent++;
109110
}
110111

111112
return newCurrent;
112113
}
113-
114-
private static bool IsVisibleBlank(int newCurrent)
115-
{
116-
var c = _singleton._buffer[newCurrent];
117-
118-
// [:blank:] of vim's pattern matching behavior
119-
// defines blanks as SPACE and TAB characters.
120-
121-
return c == ' ' || c == '\t';
122-
}
123114
}
124115
}

PSReadLine/Prediction.Views.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1513,12 +1513,12 @@ internal int FindForwardSuggestionWordPoint(int currentIndex, string wordDelimit
15131513
}
15141514

15151515
int i = currentIndex;
1516-
if (!_singleton.InWord(_suggestionText[i], wordDelimiters))
1516+
if (!Character.IsInWord(_suggestionText[i], wordDelimiters))
15171517
{
15181518
// Scan to end of current non-word region
15191519
while (++i < _suggestionText.Length)
15201520
{
1521-
if (_singleton.InWord(_suggestionText[i], wordDelimiters))
1521+
if (Character.IsInWord(_suggestionText[i], wordDelimiters))
15221522
{
15231523
break;
15241524
}
@@ -1529,7 +1529,7 @@ internal int FindForwardSuggestionWordPoint(int currentIndex, string wordDelimit
15291529
{
15301530
while (++i < _suggestionText.Length)
15311531
{
1532-
if (!_singleton.InWord(_suggestionText[i], wordDelimiters))
1532+
if (!Character.IsInWord(_suggestionText[i], wordDelimiters))
15331533
{
15341534
if (_suggestionText[i] == ' ')
15351535
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System.Text;
2+
3+
namespace Microsoft.PowerShell
4+
{
5+
internal static class StringBuilderCharacterExtensions
6+
{
7+
/// <summary>
8+
/// Returns true if the character at the specified position is a visible whitespace character.
9+
/// A blank character is defined as a SPACE or a TAB.
10+
/// </summary>
11+
/// <param name="buffer"></param>
12+
/// <param name="i"></param>
13+
/// <returns></returns>
14+
public static bool IsVisibleBlank(this StringBuilder buffer, int i)
15+
{
16+
var c = buffer[i];
17+
18+
// [:blank:] of vim's pattern matching behavior
19+
// defines blanks as SPACE and TAB characters.
20+
21+
return c == ' ' || c == '\t';
22+
}
23+
24+
/// <summary>
25+
/// Returns true if the character at the specified position is
26+
/// not present in a list of word-delimiter characters.
27+
/// </summary>
28+
/// <param name="buffer"></param>
29+
/// <param name="i"></param>
30+
/// <param name="wordDelimiters"></param>
31+
/// <returns></returns>
32+
public static bool InWord(this StringBuilder buffer, int i, string wordDelimiters)
33+
{
34+
return Character.IsInWord(buffer[i], wordDelimiters);
35+
}
36+
37+
/// <summary>
38+
/// Returns true if the character at the specified position is
39+
/// at the end of the buffer
40+
/// </summary>
41+
/// <param name="buffer"></param>
42+
/// <param name="i"></param>
43+
/// <returns></returns>
44+
public static bool IsAtEndOfBuffer(this StringBuilder buffer, int i)
45+
{
46+
return i >= (buffer.Length - 1);
47+
}
48+
49+
/// <summary>
50+
/// Returns true if the character at the specified position is
51+
/// a unicode whitespace character.
52+
/// </summary>
53+
/// <param name="buffer"></param>
54+
/// <param name="i"></param>
55+
/// <returns></returns>
56+
public static bool IsWhiteSpace(this StringBuilder buffer, int i)
57+
{
58+
// Treat just beyond the end of buffer as whitespace because
59+
// it looks like whitespace to the user even though they haven't
60+
// entered a character yet.
61+
return i >= buffer.Length || char.IsWhiteSpace(buffer[i]);
62+
}
63+
}
64+
65+
public static class Character
66+
{
67+
/// <summary>
68+
/// Returns true if the character not present in a list of word-delimiter characters.
69+
/// </summary>
70+
/// <param name="c"></param>
71+
/// <param name="wordDelimiters"></param>
72+
/// <returns></returns>
73+
public static bool IsInWord(char c, string wordDelimiters)
74+
{
75+
return !char.IsWhiteSpace(c) && wordDelimiters.IndexOf(c) < 0;
76+
}
77+
}
78+
}

PSReadLine/StringBuilderExtensions.cs PSReadLine/StringBuilderLinewiseExtensions.cs

+20
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,26 @@ internal static Range GetRange(this StringBuilder buffer, int lineIndex, int lin
7272
endPosition - startPosition + 1
7373
);
7474
}
75+
76+
/// <summary>
77+
/// Returns true if the specified position is on an empty logical line.
78+
/// </summary>
79+
/// <param name="buffer"></param>
80+
/// <param name="cursor"></param>
81+
/// <returns></returns>
82+
public static bool IsLogigalLineEmpty(this StringBuilder buffer, int cursor)
83+
{
84+
// the cursor is on a logical line considered empty if...
85+
return
86+
// the entire buffer is empty (by definition),
87+
buffer.Length == 0 ||
88+
// or the cursor sits at the start of the empty last line,
89+
// meaning that it is past the end of the buffer and the
90+
// last character in the buffer is a newline character,
91+
(cursor == buffer.Length && buffer[cursor - 1] == '\n') ||
92+
// or if the cursor is on a newline character.
93+
(cursor > 0 && buffer[cursor] == '\n');
94+
}
7595
}
7696

7797
internal static class StringBuilderPredictionExtensions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace Microsoft.PowerShell
5+
{
6+
internal static class StringBuilderTextObjectExtensions
7+
{
8+
private const string WhiteSpace = " \n\t";
9+
10+
/// <summary>
11+
/// Returns the position of the beginning of the current word as delimited by white space and delimiters
12+
/// This method differs from <see cref="ViFindPreviousWordPoint(string)"/>:
13+
/// - When the cursor location is on the first character of a word, <see cref="ViFindPreviousWordPoint(string)"/>
14+
/// returns the position of the previous word, whereas this method returns the cursor location.
15+
/// - When the cursor location is in a word, both methods return the same result.
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+
var i = Math.Min(position, buffer.Length - 1);
23+
24+
// If starting on a word consider a text object as a sequence of characters excluding the delimiters,
25+
// otherwise, consider a word as a sequence of delimiters.
26+
var delimiters = wordDelimiters;
27+
var isInWord = buffer.InWord(i, wordDelimiters);
28+
29+
if (isInWord)
30+
{
31+
// For the purpose of this method, whitespace character is considered a delimiter.
32+
delimiters += WhiteSpace;
33+
}
34+
else
35+
{
36+
char c = buffer[i];
37+
if ((wordDelimiters + '\n').IndexOf(c) == -1 && char.IsWhiteSpace(c))
38+
{
39+
// Current position points to a whitespace that is not a newline.
40+
delimiters = WhiteSpace;
41+
}
42+
else
43+
{
44+
delimiters += '\n';
45+
}
46+
}
47+
48+
var isTextObjectChar = isInWord
49+
? (Func<char, bool>)(c => delimiters.IndexOf(c) == -1)
50+
: c => delimiters.IndexOf(c) != -1;
51+
52+
var beginning = i;
53+
while (i >= 0 && isTextObjectChar(buffer[i]))
54+
{
55+
beginning = i--;
56+
}
57+
58+
return beginning;
59+
}
60+
61+
/// <summary>
62+
/// Finds the position of the beginning of the next word object starting from the specified position.
63+
/// If positioned on the last word in the buffer, returns buffer length + 1.
64+
/// This method supports VI "iw" text-object.
65+
/// iw: "inner word", select words. White space between words is counted too.
66+
/// </summary>
67+
public static int ViFindBeginningOfNextWordObjectBoundary(this StringBuilder buffer, int position, string wordDelimiters)
68+
{
69+
// Cursor may be past the end of the buffer when calling this method
70+
// this may happen if the cursor is at the beginning of a new line.
71+
var i = Math.Min(position, buffer.Length - 1);
72+
73+
// Always skip the first newline character.
74+
if (buffer[i] == '\n' && i < buffer.Length - 1)
75+
{
76+
++i;
77+
}
78+
79+
// If starting on a word consider a text object as a sequence of characters excluding the delimiters,
80+
// otherwise, consider a word as a sequence of delimiters.
81+
var delimiters = wordDelimiters;
82+
var isInWord = buffer.InWord(i, wordDelimiters);
83+
84+
if (isInWord)
85+
{
86+
delimiters += WhiteSpace;
87+
}
88+
else if (char.IsWhiteSpace(buffer[i]))
89+
{
90+
delimiters = " \t";
91+
}
92+
93+
var isTextObjectChar = isInWord
94+
? (Func<char, bool>)(c => delimiters.IndexOf(c) == -1)
95+
: c => delimiters.IndexOf(c) != -1;
96+
97+
// Try to skip a second newline characters to replicate vim behaviour.
98+
if (buffer[i] == '\n' && i < buffer.Length - 1)
99+
{
100+
++i;
101+
}
102+
103+
// Skip to next non-word characters.
104+
while (i < buffer.Length && isTextObjectChar(buffer[i]))
105+
{
106+
++i;
107+
}
108+
109+
// Make sure end includes the starting position.
110+
return Math.Max(i, position);
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)