Skip to content

Commit d648aed

Browse files
authored
Fix the exception when searching history with ctrl+r or ctrl+s (#1256)
1 parent 7e688f0 commit d648aed

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

PSReadLine/History.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,9 @@ private void HistorySearch(int direction)
657657

658658
if (newHistoryIndex >= 0 && newHistoryIndex <= _history.Count)
659659
{
660+
// Set '_current' back to where it was when starting the first search, because
661+
// it might be changed during the rendering of the last matching history command.
662+
_current = _emphasisLength;
660663
_currentHistoryIndex = newHistoryIndex;
661664
var moveCursor = InViCommandMode()
662665
? HistoryMoveCursor.ToBeginning

PSReadLine/Render.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -805,23 +805,29 @@ void UpdateColorsIfNecessary(string newColor)
805805
_initialY -= 1;
806806
point.Y -= 1;
807807
}
808-
else if (point.Y == -1)
808+
else if (point.Y < 0)
809809
{
810-
// This could only happen in two cases:
810+
// This could happen in at least 3 cases:
811811
//
812812
// 1. when you are adding characters to the first line in the buffer (top = 0) to make the logical line
813813
// wrap to one extra physical line. This would cause the buffer to scroll up and push the line being
814814
// edited up-off the buffer.
815-
// 2. when you are deleting characters backwards from the first line in the buffer without changing the
815+
// 2. when you are deleting characters (Backspace) from the first line in the buffer without changing the
816816
// number of physical lines (either editing the same logical line or causing the current logical line
817817
// to merge in the previous but still span to the current physical line). The cursor is supposed to
818818
// appear in the previous line (which is off the buffer).
819+
// 3. Both 'bck-i-search' and 'fwd-i-search' may find a history command with multi-line text, and the
820+
// matching string in the text, where the cursor is supposed to be moved to, will be scrolled up-off
821+
// the buffer after rendering.
819822
//
820-
// In these case, we move the cursor to the upper-left-most position of the window, where it's closest to
821-
// the previous editing position, and update '_current' appropriately.
823+
// In these case, we move the cursor to the left-most position of the first line, where it's closest to
824+
// the real position it should be in the ideal world.
822825

823-
_current += (bufferWidth - point.X);
826+
// First update '_current' to the index of the first character that appears on the line 0,
827+
// then we call 'ConvertOffsetToPoint' again to get the right cursor position to use.
824828
point.X = point.Y = 0;
829+
_current = ConvertLineAndColumnToOffset(point);
830+
point = ConvertOffsetToPoint(_current);
825831
}
826832

827833
_console.SetCursorPosition(point.X, point.Y);

0 commit comments

Comments
 (0)