Skip to content

Commit 958ce3a

Browse files
authored
Fix a few VI edit mode issues (#1262)
1 parent 4fe166e commit 958ce3a

File tree

7 files changed

+44
-13
lines changed

7 files changed

+44
-13
lines changed

PSReadLine/BasicEditing.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,6 @@ public static void BackwardDeleteChar(ConsoleKeyInfo? key = null, object arg = n
164164

165165
private void DeleteCharImpl(int qty, bool orExit)
166166
{
167-
qty = Math.Min(qty, _singleton._buffer.Length + 1 + ViEndOfLineFactor - _singleton._current);
168-
169167
if (_visualSelectionCommandCount > 0)
170168
{
171169
GetRegion(out var start, out var length);
@@ -177,6 +175,8 @@ private void DeleteCharImpl(int qty, bool orExit)
177175
{
178176
if (_current < _buffer.Length)
179177
{
178+
qty = Math.Min(qty, _singleton._buffer.Length - _singleton._current);
179+
180180
SaveEditItem(EditItemDelete.Create(_buffer.ToString(_current, qty), _current, DeleteChar, qty));
181181
SaveToClipboard(_current, qty);
182182
_buffer.Remove(_current, qty);

PSReadLine/Movement.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@ private static void ViOffsetCursorPosition(int count)
114114
_singleton.MoveCursor(newCurrent);
115115
}
116116
}
117-
else
117+
else if (_singleton._current < _singleton._buffer.Length)
118118
{
119-
var end = GetEndOfLogicalLinePos(_singleton._current);
119+
// when in the VI command mode, 'end' is the position of the last character;
120+
// when in the VI insert mode, 'end' is 1 char beyond the last character.
121+
var end = GetEndOfLogicalLinePos(_singleton._current) + 1 + ViEndOfLineFactor;
120122
var newCurrent = Math.Min(end, _singleton._current + count);
121123
if (_singleton._current != newCurrent)
122124
{

PSReadLine/Position.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private static int GetEndOfLogicalLinePos(int current)
8080
{
8181
var newCurrent = current;
8282

83-
for (var position = newCurrent; position < _singleton._buffer.Length; position++)
83+
for (var position = current; position < _singleton._buffer.Length; position++)
8484
{
8585
if (_singleton._buffer[position] == '\n')
8686
{

PSReadLine/ReadLine.vi.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -589,14 +589,8 @@ public static void ViInsertWithDelete(ConsoleKeyInfo? key = null, object arg = n
589589
{
590590
_singleton._groupUndoHelper.StartGroup(ViInsertWithDelete, arg);
591591

592-
var isEOL = _singleton._current == _singleton._buffer.Length + ViEndOfLineFactor;
593-
594-
DeleteChar(key, arg);
595-
if(isEOL)
596-
{
597-
_singleton._current++;
598-
}
599592
ViInsertMode(key, arg);
593+
DeleteChar(key, arg);
600594
}
601595

602596
/// <summary>

PSReadLine/Replace.vi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ private static void ViReplaceEndOfGlob(ConsoleKeyInfo? key, object arg)
213213
private static void ReplaceChar(ConsoleKeyInfo? key, object arg)
214214
{
215215
_singleton._groupUndoHelper.StartGroup(ReplaceChar, arg);
216-
DeleteChar(key, arg);
217216
ViInsertMode(key, arg);
217+
DeleteChar(key, arg);
218218
}
219219

220220
/// <summary>

test/BasicEditingTest.VI.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,13 @@ public void ViDelete()
255255
{
256256
TestSetup(KeyMode.Vi);
257257

258+
Test("bc", Keys(
259+
"a", _.Escape,
260+
CheckThat(() => AssertLineIs("a")),
261+
CheckThat(() => AssertCursorLeftIs(0)),
262+
"s", CheckThat(() => AssertLineIs("")),
263+
"bc"));
264+
258265
Test("", Keys(
259266
"0123456789", _.Escape, CheckThat(() => AssertCursorLeftIs(9)),
260267
"x", CheckThat(() => AssertLineIs("012345678")), CheckThat(() => AssertCursorLeftIs(8)),
@@ -881,5 +888,18 @@ public void ViComplete()
881888
_.Escape, "Csness"
882889
));
883890
}
891+
892+
[SkippableFact]
893+
public void ViInsertModeMoveCursor()
894+
{
895+
TestSetup(KeyMode.Vi);
896+
897+
Test("abc", Keys(
898+
"ab", CheckThat(() => AssertCursorLeftIs(2)),
899+
_.LeftArrow, CheckThat(() => AssertCursorLeftIs(1)),
900+
_.RightArrow, CheckThat(() => AssertCursorLeftIs(2)),
901+
_.RightArrow, // 'RightArrow' again does nothing, but doesn't crash
902+
"c"));
903+
}
884904
}
885905
}

test/BasicEditingTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,21 @@ public void DeleteChar()
158158
Test("ac", Keys("abc", _.Home, _.RightArrow, _.Delete));
159159
}
160160

161+
[SkippableFact]
162+
public void DeleteCharAfterDigitArgument()
163+
{
164+
TestSetup(KeyMode.Cmd);
165+
166+
Test("abc", Keys(
167+
"ab", _.LeftArrow, _.Alt_2, _.Delete,
168+
CheckThat(() => AssertLineIs("a")),
169+
CheckThat(() => AssertCursorLeftIs(1)),
170+
_.Delete, // 'Delete' again does nothing, but doesn't crash
171+
CheckThat(() => AssertLineIs("a")),
172+
CheckThat(() => AssertCursorLeftIs(1)),
173+
"bc"));
174+
}
175+
161176
[SkippableFact]
162177
public void DeleteCharOrExit()
163178
{

0 commit comments

Comments
 (0)