From 9a4c8fecea93d97c591c4eb81b6478e49e12b123 Mon Sep 17 00:00:00 2001 From: springcomp Date: Thu, 17 Dec 2020 10:24:48 +0100 Subject: [PATCH 1/4] Remove LineIsMultiline use multiline agnostic algs (#1125) --- PSReadLine/BasicEditing.cs | 64 ++++++++++++++++------------- PSReadLine/Movement.cs | 46 +++++++++------------ PSReadLine/Movement.vi.cs | 21 ++++------ PSReadLine/Movement.vi.multiline.cs | 7 ++-- 4 files changed, 66 insertions(+), 72 deletions(-) diff --git a/PSReadLine/BasicEditing.cs b/PSReadLine/BasicEditing.cs index 85b77bff0..a6d3fda1a 100644 --- a/PSReadLine/BasicEditing.cs +++ b/PSReadLine/BasicEditing.cs @@ -432,21 +432,35 @@ private bool UnresolvedCommandCouldSucceed(string commandName, Ast rootAst) { switch (commandName[0]) { - // The following are debugger commands that should be accepted if we're debugging - // because the console host will interpret these commands directly. - case 's': case 'v': case 'o': case 'c': case 'q': case 'k': case 'l': - case 'S': case 'V': case 'O': case 'C': case 'Q': case 'K': case 'L': - case '?': case 'h': case 'H': - // Ideally we would check $PSDebugContext, but it is set at function - // scope, and because we're in a module, we can't find that variable - // (arguably a PowerShell issue.) - // NestedPromptLevel is good enough though - it's rare to be in a nested. - var nestedPromptLevel = _engineIntrinsics.SessionState.PSVariable.GetValue("NestedPromptLevel"); - if (nestedPromptLevel is int) - { - return ((int)nestedPromptLevel) > 0; - } - break; + // The following are debugger commands that should be accepted if we're debugging + // because the console host will interpret these commands directly. + case 's': + case 'v': + case 'o': + case 'c': + case 'q': + case 'k': + case 'l': + case 'S': + case 'V': + case 'O': + case 'C': + case 'Q': + case 'K': + case 'L': + case '?': + case 'h': + case 'H': + // Ideally we would check $PSDebugContext, but it is set at function + // scope, and because we're in a module, we can't find that variable + // (arguably a PowerShell issue.) + // NestedPromptLevel is good enough though - it's rare to be in a nested. + var nestedPromptLevel = _engineIntrinsics.SessionState.PSVariable.GetValue("NestedPromptLevel"); + if (nestedPromptLevel is int) + { + return ((int)nestedPromptLevel) > 0; + } + break; } } @@ -522,25 +536,17 @@ public static void InsertLineAbove(ConsoleKeyInfo? key = null, object arg = null /// public static void InsertLineBelow(ConsoleKeyInfo? key = null, object arg = null) { - // Move the current position to the end of the current line and only the current line. - if (_singleton.LineIsMultiLine()) + int i = _singleton._current; + for (; i < _singleton._buffer.Length; i++) { - int i = _singleton._current; - for (; i < _singleton._buffer.Length; i++) + if (_singleton._buffer[i] == '\n') { - if (_singleton._buffer[i] == '\n') - { - break; - } + break; } - - _singleton._current = i; - } - else - { - _singleton._current = _singleton._buffer.Length; } + _singleton._current = i; + Insert('\n'); } } diff --git a/PSReadLine/Movement.cs b/PSReadLine/Movement.cs index cb27c1edd..074ae4b10 100644 --- a/PSReadLine/Movement.cs +++ b/PSReadLine/Movement.cs @@ -21,23 +21,16 @@ public partial class PSConsoleReadLine /// public static void EndOfLine(ConsoleKeyInfo? key = null, object arg = null) { - if (_singleton.LineIsMultiLine()) + int i = _singleton._current; + for (; i < _singleton._buffer.Length; i++) { - int i = _singleton._current; - for (; i < _singleton._buffer.Length; i++) + if (_singleton._buffer[i] == '\n') { - if (_singleton._buffer[i] == '\n') - { - break; - } + break; } - - _singleton.MoveCursor((i == _singleton._current) ? _singleton._buffer.Length : i); - } - else - { - _singleton.MoveCursor(_singleton._buffer.Length); } + + _singleton.MoveCursor((i == _singleton._current) ? _singleton._buffer.Length : i); } /// @@ -97,7 +90,7 @@ public static void ViForwardChar(ConsoleKeyInfo? key = null, object arg = null) } else { - ViOffsetCursorPosition(+ numericArg); + ViOffsetCursorPosition(+numericArg); } } } @@ -109,7 +102,7 @@ public static void ViBackwardChar(ConsoleKeyInfo? key = null, object arg = null) { if (TryGetArgAsInt(arg, out var numericArg, 1)) { - ViOffsetCursorPosition(- numericArg); + ViOffsetCursorPosition(-numericArg); } } @@ -225,7 +218,8 @@ private void MoveToLineImpl(int lineOffset) point = point ?? ConvertOffsetToPoint(_current); int newY = point.Value.Y + lineOffset; - Point newPoint = new Point() { + Point newPoint = new Point() + { X = _moveToLineDesiredColumn, Y = Math.Max(newY, _initialY) }; @@ -470,18 +464,18 @@ public static void GotoBrace(ConsoleKeyInfo? key = null, object arg = null) int direction; switch (token.Kind) { - case TokenKind.LParen: toMatch = TokenKind.RParen; direction = 1; break; - case TokenKind.LCurly: toMatch = TokenKind.RCurly; direction = 1; break; - case TokenKind.LBracket: toMatch = TokenKind.RBracket; direction = 1; break; + case TokenKind.LParen: toMatch = TokenKind.RParen; direction = 1; break; + case TokenKind.LCurly: toMatch = TokenKind.RCurly; direction = 1; break; + case TokenKind.LBracket: toMatch = TokenKind.RBracket; direction = 1; break; - case TokenKind.RParen: toMatch = TokenKind.LParen; direction = -1; break; - case TokenKind.RCurly: toMatch = TokenKind.LCurly; direction = -1; break; - case TokenKind.RBracket: toMatch = TokenKind.LBracket; direction = -1; break; + case TokenKind.RParen: toMatch = TokenKind.LParen; direction = -1; break; + case TokenKind.RCurly: toMatch = TokenKind.LCurly; direction = -1; break; + case TokenKind.RBracket: toMatch = TokenKind.LBracket; direction = -1; break; - default: - // Nothing to match (don't match inside strings/comments) - Ding(); - return; + default: + // Nothing to match (don't match inside strings/comments) + Ding(); + return; } var matchCount = 0; diff --git a/PSReadLine/Movement.vi.cs b/PSReadLine/Movement.vi.cs index 51d932124..d900233e6 100644 --- a/PSReadLine/Movement.vi.cs +++ b/PSReadLine/Movement.vi.cs @@ -159,21 +159,14 @@ public static void ViEndOfPreviousGlob(ConsoleKeyInfo? key = null, object arg = /// Move the cursor to the end of the current logical line. /// public static void MoveToEndOfLine(ConsoleKeyInfo? key = null, object arg = null) - { - if (_singleton.LineIsMultiLine()) - { - var eol = GetEndOfLogicalLinePos(_singleton._current); - if (eol != _singleton._current) - { - _singleton.MoveCursor(eol); - } - _singleton._moveToEndOfLineCommandCount++; - _singleton._moveToLineDesiredColumn = int.MaxValue; - } - else + { + var eol = GetEndOfLogicalLinePos(_singleton._current); + if (eol != _singleton._current) { - _singleton.MoveCursor(Math.Max(0, _singleton._buffer.Length + ViEndOfLineFactor)); - } + _singleton.MoveCursor(eol); + } + _singleton._moveToEndOfLineCommandCount++; + _singleton._moveToLineDesiredColumn = int.MaxValue; } /// diff --git a/PSReadLine/Movement.vi.multiline.cs b/PSReadLine/Movement.vi.multiline.cs index eda16b87d..8a097550c 100644 --- a/PSReadLine/Movement.vi.multiline.cs +++ b/PSReadLine/Movement.vi.multiline.cs @@ -12,7 +12,8 @@ public partial class PSConsoleReadLine /// public void MoveToFirstLine(ConsoleKeyInfo? key = null, object arg = null) { - if (!LineIsMultiLine()) + var count = GetLogicalLineCount(); + if (count == 1) { Ding(key, arg); return; @@ -38,13 +39,13 @@ public void MoveToFirstLine(ConsoleKeyInfo? key = null, object arg = null) /// public void MoveToLastLine(ConsoleKeyInfo? key = null, object arg = null) { - if (!LineIsMultiLine()) + var count = GetLogicalLineCount(); + if (count == 1) { Ding(key, arg); return; } - var count = GetLogicalLineCount(); var currentLine = GetLogicalLineNumber(); var pos = ConvertOffsetToPoint(_singleton._current); From 0f2a6fab9eaf507cd73f8d6a203ffcd70509e603 Mon Sep 17 00:00:00 2001 From: springcomp Date: Tue, 19 Jan 2021 13:53:14 +0100 Subject: [PATCH 2/4] Revert unwanted IDE automatic formatting --- PSReadLine/BasicEditing.cs | 44 +++++++++++++------------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/PSReadLine/BasicEditing.cs b/PSReadLine/BasicEditing.cs index a6d3fda1a..5e3feb031 100644 --- a/PSReadLine/BasicEditing.cs +++ b/PSReadLine/BasicEditing.cs @@ -432,35 +432,21 @@ private bool UnresolvedCommandCouldSucceed(string commandName, Ast rootAst) { switch (commandName[0]) { - // The following are debugger commands that should be accepted if we're debugging - // because the console host will interpret these commands directly. - case 's': - case 'v': - case 'o': - case 'c': - case 'q': - case 'k': - case 'l': - case 'S': - case 'V': - case 'O': - case 'C': - case 'Q': - case 'K': - case 'L': - case '?': - case 'h': - case 'H': - // Ideally we would check $PSDebugContext, but it is set at function - // scope, and because we're in a module, we can't find that variable - // (arguably a PowerShell issue.) - // NestedPromptLevel is good enough though - it's rare to be in a nested. - var nestedPromptLevel = _engineIntrinsics.SessionState.PSVariable.GetValue("NestedPromptLevel"); - if (nestedPromptLevel is int) - { - return ((int)nestedPromptLevel) > 0; - } - break; + // The following are debugger commands that should be accepted if we're debugging + // because the console host will interpret these commands directly. + case 's': case 'v': case 'o': case 'c': case 'q': case 'k': case 'l': + case 'S': case 'V': case 'O': case 'C': case 'Q': case 'K': case 'L': + case '?': case 'h': case 'H': + // Ideally we would check $PSDebugContext, but it is set at function + // scope, and because we're in a module, we can't find that variable + // (arguably a PowerShell issue.) + // NestedPromptLevel is good enough though - it's rare to be in a nested. + var nestedPromptLevel = _engineIntrinsics.SessionState.PSVariable.GetValue("NestedPromptLevel"); + if (nestedPromptLevel is int) + { + return ((int)nestedPromptLevel) > 0; + } + break; } } From 0409360818ba549d13d297a00529599483e9bd32 Mon Sep 17 00:00:00 2001 From: springcomp Date: Thu, 11 Feb 2021 22:10:46 +0100 Subject: [PATCH 3/4] Using less expensive method for better performance. --- PSReadLine/Movement.vi.multiline.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/PSReadLine/Movement.vi.multiline.cs b/PSReadLine/Movement.vi.multiline.cs index 8a097550c..9cf057c82 100644 --- a/PSReadLine/Movement.vi.multiline.cs +++ b/PSReadLine/Movement.vi.multiline.cs @@ -12,8 +12,7 @@ public partial class PSConsoleReadLine /// public void MoveToFirstLine(ConsoleKeyInfo? key = null, object arg = null) { - var count = GetLogicalLineCount(); - if (count == 1) + if (!LineIsMultiLine()) { Ding(key, arg); return; From 88f5ea6e0e1622171e1efab37af39383ee0762ac Mon Sep 17 00:00:00 2001 From: springcomp Date: Thu, 11 Feb 2021 22:10:46 +0100 Subject: [PATCH 4/4] Resetting _moveToEndOfLineCommandCount indicator. --- PSReadLine/ReadLine.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/PSReadLine/ReadLine.cs b/PSReadLine/ReadLine.cs index 4395a3791..62e7c2227 100644 --- a/PSReadLine/ReadLine.cs +++ b/PSReadLine/ReadLine.cs @@ -555,6 +555,7 @@ private string InputLoop() // nor a "move to line" command. In that case, the desired column // number will be computed from the current position on the logical line. + _moveToEndOfLineCommandCount = 0; _moveToLineDesiredColumn = -1; } }