Skip to content

Improve RecomputeInitialCoords to be more robust and handle a couple special cases #3074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 4, 2022
Merged
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
14 changes: 8 additions & 6 deletions PSReadLine/BasicEditing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,6 @@ private bool AcceptLineImpl(bool validate)
_emphasisStart = -1;
_emphasisLength = 0;

var insertionPoint = _current;
// Make sure cursor is at the end before writing the line
_current = _buffer.Length;

if (renderNeeded)
{
ForceRender();
Expand All @@ -281,6 +277,7 @@ private bool AcceptLineImpl(bool validate)
// can report an error as it normally does.
if (validate && !_statusIsErrorMessage)
{
var insertionPoint = _current;
var errorMessage = Validate(_ast);
if (!string.IsNullOrWhiteSpace(errorMessage))
{
Expand Down Expand Up @@ -308,8 +305,13 @@ private bool AcceptLineImpl(bool validate)
ClearStatusMessage(render: true);
}

// Let public API set cursor to end of line incase end of line is end of buffer
SetCursorPosition(_current);
// Make sure cursor is at the end before writing the line.
if (_current != _buffer.Length)
{
// Let public API set cursor to end of line incase end of line is end of buffer.
_current = _buffer.Length;
SetCursorPosition(_current);
}

if (_prediction.ActiveView is PredictionListView listView)
{
Expand Down
11 changes: 11 additions & 0 deletions PSReadLine/PSReadLineResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions PSReadLine/PSReadLineResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -855,4 +855,7 @@ Or not saving history with:
<data name="SelectCommandArgumentDescription" xml:space="preserve">
<value>Make visual selection of the command arguments.</value>
</data>
<data name="FailedToConvertPointToRenderDataOffset" xml:space="preserve">
<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>
</data>
</root>
13 changes: 9 additions & 4 deletions PSReadLine/ReadLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,10 @@ private string InputLoop()
var moveToLineCommandCount = _moveToLineCommandCount;
var moveToEndOfLineCommandCount = _moveToEndOfLineCommandCount;

// 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.
_handlePotentialResizing = true;

var key = ReadKey();
ProcessOneKey(key, _dispatchTable, ignoreIfNoAction: false, arg: null);
if (_inputAccepted)
Expand Down Expand Up @@ -709,10 +713,6 @@ private void Initialize(Runspace runspace, EngineIntrinsics engineIntrinsics)
_delayedOneTimeInitCompleted = true;
}

_previousRender = _initialPrevRender;
_previousRender.bufferWidth = _console.BufferWidth;
_previousRender.bufferHeight = _console.BufferHeight;
_previousRender.errorPrompt = false;
_buffer.Clear();
_edits = new List<EditItem>();
_undoEditIndex = 0;
Expand All @@ -728,6 +728,9 @@ private void Initialize(Runspace runspace, EngineIntrinsics engineIntrinsics)
_initialY = _console.CursorTop;
_initialForeground = _console.ForegroundColor;
_initialBackground = _console.BackgroundColor;
_previousRender = _initialPrevRender;
_previousRender.UpdateConsoleInfo(_console);
_previousRender.initialY = _initialY;
_statusIsErrorMessage = false;

_initialOutputEncoding = _console.OutputEncoding;
Expand Down Expand Up @@ -1033,6 +1036,8 @@ public static void InvokePrompt(ConsoleKeyInfo? key = null, object arg = null)
_singleton._initialX = console.CursorLeft;
_singleton._initialY = console.CursorTop;
_singleton._previousRender = _initialPrevRender;
_singleton._previousRender.UpdateConsoleInfo(console);
_singleton._previousRender.initialY = _singleton._initialY;

_singleton.Render();
console.CursorVisible = true;
Expand Down
6 changes: 3 additions & 3 deletions PSReadLine/Render.Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ private static string Spaces(int cnt)
: new string(' ', cnt);
}

private static int LengthInBufferCells(string str)
internal static int LengthInBufferCells(string str)
{
return LengthInBufferCells(str, 0, str.Length);
}

private static int LengthInBufferCells(string str, int start, int end)
internal static int LengthInBufferCells(string str, int start, int end)
{
var sum = 0;
for (var i = start; i < end; i++)
Expand All @@ -70,7 +70,7 @@ private static int LengthInBufferCells(string str, int start, int end)
return sum;
}

private static int LengthInBufferCells(char c)
internal static int LengthInBufferCells(char c)
{
if (c < 256)
{
Expand Down
Loading