Skip to content

Commit ef66547

Browse files
committed
Fix exception in SwapCharacters with short line
Fix #599
1 parent 6de89cc commit ef66547

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

PSReadLine/ReadLine.vi.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -642,15 +642,16 @@ public static void InvertCase(ConsoleKeyInfo? key = null, object arg = null)
642642
public static void SwapCharacters(ConsoleKeyInfo? key = null, object arg = null)
643643
{
644644
// if in vi command mode, the cursor can't go as far
645-
int cursorRightLimit = _singleton._buffer.Length + ViEndOfLineFactor;
646-
if (_singleton._current <= 0 || _singleton._current > cursorRightLimit)
645+
var bufferLength = _singleton._buffer.Length;
646+
int cursorRightLimit = bufferLength + ViEndOfLineFactor;
647+
if (_singleton._current <= 0 || bufferLength < 2 || _singleton._current > cursorRightLimit)
647648
{
648649
Ding();
649650
return;
650651
}
651652

652653
int cursor = _singleton._current;
653-
if (cursor == _singleton._buffer.Length)
654+
if (cursor == bufferLength)
654655
--cursor; // if at end of line, swap previous two chars
655656

656657
char current = _singleton._buffer[cursor];

UnitTestPSReadLine/BasicEditingTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ public void TestSwapCharacters()
167167
{
168168
TestSetup(KeyMode.Emacs);
169169

170+
TestMustDing("", Keys(_.CtrlT));
171+
TestMustDing("a", Keys("a", _.CtrlT));
172+
TestMustDing("abc", Keys("abc", _.Home, _.CtrlT));
173+
170174
Test("abc", Keys(
171175
"abc", CheckThat(() => AssertLineIs("abc")),
172176
_.CtrlT, CheckThat(() => AssertLineIs("acb")),

0 commit comments

Comments
 (0)