Skip to content

Commit cd420fb

Browse files
committed
Clear the saved current line in a more general way
1 parent 1798e15 commit cd420fb

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

PSReadLine/BasicEditing.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@ public static void RevertLine(ConsoleKeyInfo? key = null, object arg = null)
7474
_singleton._edits[_singleton._undoEditIndex - 1].Undo();
7575
_singleton._undoEditIndex--;
7676
}
77-
78-
// It's possible we just finished a history search, which saved the line when the search was triggered
79-
// so that another search with the reversed order can go back to the same line.
80-
// Now that we are reverting the real current line, the saved current line should be cleared since we
81-
// will no longer go back to it.
82-
_singleton.ClearSavedCurrentLine();
8377
_singleton.Render();
8478
}
8579

PSReadLine/History.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public class HistoryItem
9595
private int _getNextHistoryIndex;
9696
private int _searchHistoryCommandCount;
9797
private int _recallHistoryCommandCount;
98+
private int _allHistoryCommandCount;
9899
private string _searchHistoryPrefix;
99100
// When cycling through history, the current line (not yet added to history)
100101
// is saved here so it can be restored.
@@ -510,6 +511,7 @@ private void SaveCurrentLine()
510511
// to check if we need to load history from another sessions now.
511512
MaybeReadHistoryFile();
512513

514+
_allHistoryCommandCount += 1;
513515
if (_savedCurrentLine.CommandLine == null)
514516
{
515517
_savedCurrentLine.CommandLine = _buffer.ToString();
@@ -686,6 +688,12 @@ public static void BeginningOfHistory(ConsoleKeyInfo? key = null, object arg = n
686688
/// Move to the last item (the current input) in the history.
687689
/// </summary>
688690
public static void EndOfHistory(ConsoleKeyInfo? key = null, object arg = null)
691+
{
692+
_singleton.SaveCurrentLine();
693+
GoToEndOfHistory();
694+
}
695+
696+
private static void GoToEndOfHistory()
689697
{
690698
_singleton._currentHistoryIndex = _singleton._history.Count;
691699
_singleton.UpdateFromHistory(HistoryMoveCursor.ToEnd);
@@ -844,7 +852,7 @@ private void InteractiveHistorySearchLoop(int direction)
844852
else if (function == Abort)
845853
{
846854
// Abort search
847-
EndOfHistory();
855+
GoToEndOfHistory();
848856
break;
849857
}
850858
else

PSReadLine/ReadLine.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ private string InputLoop()
474474
var tabCommandCount = _tabCommandCount;
475475
var searchHistoryCommandCount = _searchHistoryCommandCount;
476476
var recallHistoryCommandCount = _recallHistoryCommandCount;
477+
var allHistoryCommandCount = _allHistoryCommandCount;
477478
var yankLastArgCommandCount = _yankLastArgCommandCount;
478479
var visualSelectionCommandCount = _visualSelectionCommandCount;
479480
var moveToLineCommandCount = _moveToLineCommandCount;
@@ -515,18 +516,22 @@ private string InputLoop()
515516
_emphasisStart = -1;
516517
_emphasisLength = 0;
517518
Render();
518-
_currentHistoryIndex = _history.Count;
519519
}
520520
_searchHistoryCommandCount = 0;
521521
_searchHistoryPrefix = null;
522522
}
523523
if (recallHistoryCommandCount == _recallHistoryCommandCount)
524524
{
525-
if (_recallHistoryCommandCount > 0)
525+
_recallHistoryCommandCount = 0;
526+
}
527+
if (allHistoryCommandCount == _allHistoryCommandCount)
528+
{
529+
if (_allHistoryCommandCount > 0)
526530
{
531+
ClearSavedCurrentLine();
527532
_currentHistoryIndex = _history.Count;
528533
}
529-
_recallHistoryCommandCount = 0;
534+
_allHistoryCommandCount = 0;
530535
}
531536
if (searchHistoryCommandCount == _searchHistoryCommandCount &&
532537
recallHistoryCommandCount == _recallHistoryCommandCount)

test/HistoryTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,37 @@ public void HistorySearchCurrentLine()
428428
Test("", Keys("ec", _.UpArrow, _.DownArrow, _.Escape, "echo ", _.UpArrow, _.DownArrow, _.Escape));
429429
}
430430

431+
[SkippableFact]
432+
public void HistorySavedCurrentLine()
433+
{
434+
TestSetup(KeyMode.Cmd,
435+
new KeyHandler("F3", PSConsoleReadLine.BeginningOfHistory),
436+
new KeyHandler("Shift+F3", PSConsoleReadLine.EndOfHistory));
437+
438+
SetHistory("echo foo", "echo bar");
439+
Test("echo bar", Keys("ec", _.UpArrow));
440+
Test("echo foo", Keys("ec", _.UpArrow, _.F3));
441+
Test("echo bar", Keys("ec", _.UpArrow, _.F3, _.DownArrow));
442+
Test("ec", Keys("ec", _.UpArrow, _.F3, _.Shift_F3));
443+
Test("ec", Keys("ec", _.UpArrow, _.F3, _.DownArrow, _.DownArrow));
444+
445+
Test("echo foo", Keys("e", _.UpArrow, _.UpArrow));
446+
Test("e", Keys("e", _.UpArrow, _.UpArrow, _.Shift_F3));
447+
448+
Test("echo bar", Keys("ech", _.F8));
449+
Test("echo foo", Keys("ech", _.F8, _.F3));
450+
Test("echo bar", Keys("ech", _.F8, _.F3, _.DownArrow));
451+
Test("ech", Keys("ech", _.F8, _.F3, _.DownArrow, _.DownArrow));
452+
Test("ech", Keys("ech", _.F8, _.F8, _.Shift_F3));
453+
454+
SetHistory("echo foo", "echo f");
455+
Test("echo f", Keys("ec", _.UpArrow));
456+
Test("echo foo", Keys("ec", _.UpArrow, _.F8));
457+
Test("echo f", Keys("ec", _.UpArrow, _.F8, _.Shift_F8));
458+
Test("ec", Keys("ec", _.UpArrow, _.F8, _.DownArrow, _.DownArrow));
459+
Test("ec", Keys("ec", _.UpArrow, _.F8, _.Shift_F8, _.Shift_F3));
460+
}
461+
431462
[SkippableFact]
432463
public void SearchHistory()
433464
{

0 commit comments

Comments
 (0)