Fix AcceptAndGetNext for Forward/ReverseSearchHistory - #5210
Draft
sharpchen (sharpchen) wants to merge 1 commit into
Draft
sharpchen (sharpchen) wants to merge 1 commit into
sharpchen (sharpchen) wants to merge 1 commit into
Conversation
sharpchen (sharpchen)
marked this pull request as draft
September 17, 2026 14:28
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Summary
Fixes #514
The Context
To get
AcceptAndGetNextwork,_currentHistoryIndexis required to be set before line accepted.PSReadLine/PSReadLine/BasicEditing.cs
Line 562 in 2984546
For
Previous/NextHistory,_currentHistoryIndexis set byHistoryRecall:PSReadLine/PSReadLine/History.cs
Line 936 in 2984546
For
Forward/ReverseSearchHistory,_currentHistoryIndexis set byUpdateHistoryDuringInteractiveSearch:PSReadLine/PSReadLine/History.cs
Line 1138 in 2984546
Current Behaviour
AcceptAndGetNextduring the interactive search works as expected.AcceptAndGetNextas expected.esc, any edit based on the match doesn't reset_currentHistoryIndex, soAcceptAndGetNextwould still proceed from the index of search result.The Problem
For Behaviour 2, I located the problem come from:
PSReadLine/PSReadLine/ReadLine.cs
Lines 615 to 620 in 2984546
The problem is
InteractiveHistorySearchcallsSaveCurrentLine()which increments_anyHistoryCommandCount, triggering the reset of_currentHistoryIndexin next input loop, even though the new input didn't edit the current line.The Solution
_currentHistoryIndexis expected to be reset here only if_savedCurrentLineis identical to the current_buffer.So the fix simple, just add a guard on it.
if (_anyHistoryCommandCount > 0) { - ClearSavedCurrentLine(); - _hashedHistory = null; - _currentHistoryIndex = _history.Count; + if (_savedCurrentLine.CommandLine == _buffer.ToString()) { + ClearSavedCurrentLine(); + _hashedHistory = null; + _currentHistoryIndex = _history.Count; + } } _anyHistoryCommandCount = 0;Note
I really doubt this piece of code should be executed after line accepted instead of on any input?
What is the point of trying clearing
_savedCurrentLineon every key press?BTW the name
_anyHistoryCommandCountis really confusing, please consider refactoring it to a better name or add comment for why it exists.PR Checklist
Microsoft Reviewers: Open in CodeFlow