Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct cursor jumping from line 2 to line 1 due to line 1 being empty. #1108

Merged
merged 4 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 1 addition & 19 deletions PSReadLine/BasicEditing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,25 +473,7 @@ public static void AddLine(ConsoleKeyInfo? key = null, object arg = null)
public static void InsertLineAbove(ConsoleKeyInfo? key = null, object arg = null)
{
// Move the current position to the beginning of the current line and only the current line.
if (_singleton.LineIsMultiLine())
{
int i = Math.Max(0, _singleton._current - 1);
for (; i > 0; i--)
{
if (_singleton._buffer[i] == '\n')
{
i += 1;
break;
}
}

_singleton._current = i;
}
else
{
_singleton._current = 0;
}

_singleton._current = GetBeginningOfLinePos(_singleton._current);
Insert('\n');
PreviousLine();
}
Expand Down
6 changes: 3 additions & 3 deletions PSReadLine/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ private static int GetBeginningOfLinePos(int current)

if (_singleton.LineIsMultiLine())
{
int i = Math.Max(0, current - 1);
for (; i > 0; i--)
int i = Math.Max(0, current);
while (i > 0)
{
if (_singleton._buffer[i] == '\n')
if (_singleton._buffer[--i] == '\n')
{
i += 1;
break;
Expand Down
6 changes: 6 additions & 0 deletions test/BasicEditingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ public void InsertLineAbove()
_.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
_.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
"9ABC"));

// Test case - create leading blank line, cursor to stay on same line
Test("\n\n1234", Keys("1234",
_.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(0,0)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
_.Ctrl_Enter, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1))));
}

[SkippableFact]
Expand Down
6 changes: 6 additions & 0 deletions test/MovementTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ public void MultilineCursorMovement()
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(5 + continutationPromptLength, 4)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(6 + continutationPromptLength, 5)),

// Using the input previously entered, check for correct cursor movements when first line is blank
_.Home, _.Home, CheckThat(() => AssertCursorLeftTopIs(0, 0)),
_.DownArrow, CheckThat(() => AssertCursorLeftTopIs(8 + continutationPromptLength, 1)),
_.Home, CheckThat(() => AssertCursorLeftTopIs(continutationPromptLength, 1)),
_.Home, CheckThat(() => AssertCursorLeftTopIs(0,0)),

// Clear the input, we were just testing movement
_.Escape
));
Expand Down