Skip to content

Commit 175d42e

Browse files
msftrncsdaxian-dbw
authored andcommitted
Correct cursor jumping from line 2 to line 1 due to line 1 being empty. (#1108)
- Reduce code duplication by reusing GetBeginningOfLinePos in InsertLineAbove. - Correct newline search in GetBeginningOfLinePos (HOME) and InsertLineAbove to not ignore first character in the buffer which might be a new line. - Manifests as cursor jumps to line 1, because line 1 is blank, when triggered with cursor anywhere on line 2.
1 parent e17624e commit 175d42e

File tree

4 files changed

+16
-22
lines changed

4 files changed

+16
-22
lines changed

PSReadLine/BasicEditing.cs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -473,25 +473,7 @@ public static void AddLine(ConsoleKeyInfo? key = null, object arg = null)
473473
public static void InsertLineAbove(ConsoleKeyInfo? key = null, object arg = null)
474474
{
475475
// Move the current position to the beginning of the current line and only the current line.
476-
if (_singleton.LineIsMultiLine())
477-
{
478-
int i = Math.Max(0, _singleton._current - 1);
479-
for (; i > 0; i--)
480-
{
481-
if (_singleton._buffer[i] == '\n')
482-
{
483-
i += 1;
484-
break;
485-
}
486-
}
487-
488-
_singleton._current = i;
489-
}
490-
else
491-
{
492-
_singleton._current = 0;
493-
}
494-
476+
_singleton._current = GetBeginningOfLinePos(_singleton._current);
495477
Insert('\n');
496478
PreviousLine();
497479
}

PSReadLine/Position.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ private static int GetBeginningOfLinePos(int current)
1515

1616
if (_singleton.LineIsMultiLine())
1717
{
18-
int i = Math.Max(0, current - 1);
19-
for (; i > 0; i--)
18+
int i = Math.Max(0, current);
19+
while (i > 0)
2020
{
21-
if (_singleton._buffer[i] == '\n')
21+
if (_singleton._buffer[--i] == '\n')
2222
{
2323
i += 1;
2424
break;

test/BasicEditingTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ public void InsertLineAbove()
292292
_.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
293293
_.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
294294
"9ABC"));
295+
296+
// Test case - create leading blank line, cursor to stay on same line
297+
Test("\n\n1234", Keys("1234",
298+
_.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(0,0)),
299+
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
300+
_.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1))));
295301
}
296302

297303
[SkippableFact]

test/MovementTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ public void MultilineCursorMovement()
173173
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(5 + continutationPromptLength, 4)),
174174
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(6 + continutationPromptLength, 5)),
175175

176+
// Using the input previously entered, check for correct cursor movements when first line is blank
177+
_.Home, _.Home, CheckThat(() => AssertCursorLeftTopIs(0, 0)),
178+
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(8 + continutationPromptLength, 1)),
179+
_.Home, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
180+
_.Home, CheckThat(() => AssertCursorLeftTopIs(0,0)),
181+
176182
// Clear the input, we were just testing movement
177183
_.Escape
178184
));

0 commit comments

Comments
 (0)