From 43c5582b7960c423ccd9c5b3548ae9a2b5ac6bf9 Mon Sep 17 00:00:00 2001 From: msftrncs Date: Mon, 28 Oct 2019 22:45:42 -0500 Subject: [PATCH 1/4] Reuse GetBeginningOfLinePos in InsertLineAbove Reduce code duplication by reusing GetBeginningOfLinePos in InsertLineAbove. --- PSReadLine/BasicEditing.cs | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/PSReadLine/BasicEditing.cs b/PSReadLine/BasicEditing.cs index 21a26a508..eaa83a0bb 100644 --- a/PSReadLine/BasicEditing.cs +++ b/PSReadLine/BasicEditing.cs @@ -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(); } From d6197e64589ca3e8b8799ee7052ef9a39241df0b Mon Sep 17 00:00:00 2001 From: msftrncs Date: Mon, 28 Oct 2019 22:35:12 -0500 Subject: [PATCH 2/4] Correct skipping of checking first character in buffer for new lines. 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. Fixes #1107. Fixes #1109. --- PSReadLine/Position.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/PSReadLine/Position.cs b/PSReadLine/Position.cs index 4beb03259..ececb92b2 100644 --- a/PSReadLine/Position.cs +++ b/PSReadLine/Position.cs @@ -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; From e02f4c97426eb1109f6afe9151f36f071e4ca496 Mon Sep 17 00:00:00 2001 From: msftrncs Date: Mon, 21 Oct 2019 23:15:53 -0500 Subject: [PATCH 3/4] Add test to check for HOME from line 2 when line 1 is blank. --- test/MovementTest.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/MovementTest.cs b/test/MovementTest.cs index 0f38d4694..48c73e0ad 100644 --- a/test/MovementTest.cs +++ b/test/MovementTest.cs @@ -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 )); From a47fddfc0f7bf36babbc5d7798e269209470caca Mon Sep 17 00:00:00 2001 From: msftrncs Date: Mon, 21 Oct 2019 22:41:55 -0500 Subject: [PATCH 4/4] Add test case for inserting leading blank lines. --- test/BasicEditingTest.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/BasicEditingTest.cs b/test/BasicEditingTest.cs index f802a9917..9ed7525fe 100644 --- a/test/BasicEditingTest.cs +++ b/test/BasicEditingTest.cs @@ -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]