Fix AcceptAndGetNext for Forward/ReverseSearchHistory by sharpchen · Pull Request #5210 · PowerShell/PSReadLine · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions PSReadLine/History.cs
27 changes: 24 additions & 3 deletions PSReadLine/ReadLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public partial class PSConsoleReadLine : IPSConsoleReadLineMockableMethods
private bool _statusIsErrorMessage;
private string _statusLinePrompt;
private string _acceptedCommandLine;
// edits of current loaded line, this reference gets updated in UpdateFromHistory
private List<EditItem> _edits;
private int _editGroupStart;
private int _undoEditIndex;
Expand Down Expand Up @@ -549,6 +550,8 @@ private string InputLoop()
var visualSelectionCommandCount = _visualSelectionCommandCount;
var moveToLineCommandCount = _moveToLineCommandCount;
var moveToEndOfLineCommandCount = _moveToEndOfLineCommandCount;
var currentHistoryIndex = _currentHistoryIndex;
var prevLine = _buffer.ToString();

// We attempt to handle window resizing only once per a keybinding processing, because we assume the
// window resizing cannot and shouldn't happen within the processing of a given keybinding.
Expand Down Expand Up @@ -610,16 +613,34 @@ private string InputLoop()
{
_recallHistoryCommandCount = 0;
}
// if the latest input didn't trigger a SaveCurrentLine call
if (anyHistoryCommandCount == _anyHistoryCommandCount)
{
if (_anyHistoryCommandCount > 0)
{
ClearSavedCurrentLine();
_hashedHistory = null;
_currentHistoryIndex = _history.Count;
// _savedCurrentLine should be consumed only if current line has the same content
if (_savedCurrentLine.CommandLine == _buffer.ToString())
{
ClearSavedCurrentLine();
_hashedHistory = null;
_currentHistoryIndex = _history.Count;
}
}
// this might not be desired sometimes as non-editing inputs e.g. moving cursor also trigger this
_anyHistoryCommandCount = 0;
}
// if any edit happened on same history
if (currentHistoryIndex == _currentHistoryIndex && prevLine != _buffer.ToString())
{
// once edited, we should reset the _savedCurrentLine
ClearSavedCurrentLine();
// if the current line is recalled and edited,
// it become effectively a new command, we should reset _currentHistoryIndex
if (_currentHistoryIndex != _history.Count)
{
_currentHistoryIndex = _history.Count;
}
}
if (visualSelectionCommandCount == _visualSelectionCommandCount && _visualSelectionCommandCount > 0)
{
_visualSelectionCommandCount = 0;
Expand Down
40 changes: 40 additions & 0 deletions test/BasicEditingTest.cs