Skip to content

Commit f46f15d

Browse files
authored
Improve RecomputeInitialCoords to be more robust and handle a couple special cases (#3074)
1 parent 15850a4 commit f46f15d

11 files changed

+2009
-115
lines changed

PSReadLine/BasicEditing.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,6 @@ private bool AcceptLineImpl(bool validate)
267267
_emphasisStart = -1;
268268
_emphasisLength = 0;
269269

270-
var insertionPoint = _current;
271-
// Make sure cursor is at the end before writing the line
272-
_current = _buffer.Length;
273-
274270
if (renderNeeded)
275271
{
276272
ForceRender();
@@ -281,6 +277,7 @@ private bool AcceptLineImpl(bool validate)
281277
// can report an error as it normally does.
282278
if (validate && !_statusIsErrorMessage)
283279
{
280+
var insertionPoint = _current;
284281
var errorMessage = Validate(_ast);
285282
if (!string.IsNullOrWhiteSpace(errorMessage))
286283
{
@@ -308,8 +305,13 @@ private bool AcceptLineImpl(bool validate)
308305
ClearStatusMessage(render: true);
309306
}
310307

311-
// Let public API set cursor to end of line incase end of line is end of buffer
312-
SetCursorPosition(_current);
308+
// Make sure cursor is at the end before writing the line.
309+
if (_current != _buffer.Length)
310+
{
311+
// Let public API set cursor to end of line incase end of line is end of buffer.
312+
_current = _buffer.Length;
313+
SetCursorPosition(_current);
314+
}
313315

314316
if (_prediction.ActiveView is PredictionListView listView)
315317
{

PSReadLine/PSReadLineResources.Designer.cs

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PSReadLine/PSReadLineResources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,4 +855,7 @@ Or not saving history with:
855855
<data name="SelectCommandArgumentDescription" xml:space="preserve">
856856
<value>Make visual selection of the command arguments.</value>
857857
</data>
858+
<data name="FailedToConvertPointToRenderDataOffset" xml:space="preserve">
859+
<value>Cannot locate the offset in the rendered text that was pointed by the original cursor. Initial Coord: ({0}, {1}) Buffer: ({2}, {3}) Cursor: ({4}, {5})</value>
860+
</data>
858861
</root>

PSReadLine/ReadLine.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,10 @@ private string InputLoop()
509509
var moveToLineCommandCount = _moveToLineCommandCount;
510510
var moveToEndOfLineCommandCount = _moveToEndOfLineCommandCount;
511511

512+
// We attempt to handle window resizing only once per a keybinding processing, because we assume the
513+
// window resizing cannot and shouldn't happen within the processing of a given keybinding.
514+
_handlePotentialResizing = true;
515+
512516
var key = ReadKey();
513517
ProcessOneKey(key, _dispatchTable, ignoreIfNoAction: false, arg: null);
514518
if (_inputAccepted)
@@ -709,10 +713,6 @@ private void Initialize(Runspace runspace, EngineIntrinsics engineIntrinsics)
709713
_delayedOneTimeInitCompleted = true;
710714
}
711715

712-
_previousRender = _initialPrevRender;
713-
_previousRender.bufferWidth = _console.BufferWidth;
714-
_previousRender.bufferHeight = _console.BufferHeight;
715-
_previousRender.errorPrompt = false;
716716
_buffer.Clear();
717717
_edits = new List<EditItem>();
718718
_undoEditIndex = 0;
@@ -729,6 +729,9 @@ private void Initialize(Runspace runspace, EngineIntrinsics engineIntrinsics)
729729
_initialY = _console.CursorTop;
730730
_initialForeground = _console.ForegroundColor;
731731
_initialBackground = _console.BackgroundColor;
732+
_previousRender = _initialPrevRender;
733+
_previousRender.UpdateConsoleInfo(_console);
734+
_previousRender.initialY = _initialY;
732735
_statusIsErrorMessage = false;
733736

734737
_initialOutputEncoding = _console.OutputEncoding;
@@ -1034,6 +1037,8 @@ public static void InvokePrompt(ConsoleKeyInfo? key = null, object arg = null)
10341037
_singleton._initialX = console.CursorLeft;
10351038
_singleton._initialY = console.CursorTop;
10361039
_singleton._previousRender = _initialPrevRender;
1040+
_singleton._previousRender.UpdateConsoleInfo(console);
1041+
_singleton._previousRender.initialY = _singleton._initialY;
10371042

10381043
_singleton.Render();
10391044
console.CursorVisible = true;

PSReadLine/Render.Helper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ private static string Spaces(int cnt)
4545
: new string(' ', cnt);
4646
}
4747

48-
private static int LengthInBufferCells(string str)
48+
internal static int LengthInBufferCells(string str)
4949
{
5050
return LengthInBufferCells(str, 0, str.Length);
5151
}
5252

53-
private static int LengthInBufferCells(string str, int start, int end)
53+
internal static int LengthInBufferCells(string str, int start, int end)
5454
{
5555
var sum = 0;
5656
for (var i = start; i < end; i++)
@@ -70,7 +70,7 @@ private static int LengthInBufferCells(string str, int start, int end)
7070
return sum;
7171
}
7272

73-
private static int LengthInBufferCells(char c)
73+
internal static int LengthInBufferCells(char c)
7474
{
7575
if (c < 256)
7676
{

0 commit comments

Comments
 (0)