From da9c4c7d9057190d41fe7c42f4155940d6cca539 Mon Sep 17 00:00:00 2001 From: Maxime Labelle Date: Sun, 6 Dec 2020 11:49:40 +0100 Subject: [PATCH 1/6] undelete --- PSReadLine/BasicEditing.cs | 19 ++----- PSReadLine/ReadLine.vi.cs | 110 ++++++++++++------------------------- PSReadLine/YankPaste.vi.cs | 28 ++++++++-- 3 files changed, 62 insertions(+), 95 deletions(-) diff --git a/PSReadLine/BasicEditing.cs b/PSReadLine/BasicEditing.cs index e7236a21..c0a3d181 100644 --- a/PSReadLine/BasicEditing.cs +++ b/PSReadLine/BasicEditing.cs @@ -122,9 +122,7 @@ public static void BackwardDeleteLine(ConsoleKeyInfo? key = null, object arg = n { if (_singleton._current > 0) { - _clipboard.Record(_singleton._buffer, 0, _singleton._current); - _singleton.SaveEditItem(EditItemDelete.Create(_clipboard, 0)); - _singleton._buffer.Remove(0, _singleton._current); + _singleton.RemoveTextToClipboard(0, _singleton._current); _singleton._current = 0; _singleton.Render(); } @@ -149,15 +147,8 @@ public static void BackwardDeleteChar(ConsoleKeyInfo? key = null, object arg = n qty = Math.Min(qty, _singleton._current); int startDeleteIndex = _singleton._current - qty; - _singleton.SaveEditItem( - EditItemDelete.Create( - _singleton._buffer.ToString(startDeleteIndex, qty), - startDeleteIndex, - BackwardDeleteChar, - arg) - ); - _singleton.SaveToClipboard(startDeleteIndex, qty); - _singleton._buffer.Remove(startDeleteIndex, qty); + + _singleton.RemoveTextToClipboard(startDeleteIndex, qty, BackwardDeleteChar, arg); _singleton._current = startDeleteIndex; _singleton.Render(); } @@ -178,9 +169,7 @@ private void DeleteCharImpl(int qty, bool orExit) { qty = Math.Min(qty, _singleton._buffer.Length - _singleton._current); - SaveEditItem(EditItemDelete.Create(_buffer.ToString(_current, qty), _current, DeleteChar, qty)); - SaveToClipboard(_current, qty); - _buffer.Remove(_current, qty); + RemoveTextToClipboard(_current, qty, DeleteChar); if (_current >= _buffer.Length) { _current = Math.Max(0, _buffer.Length + ViEndOfLineFactor); diff --git a/PSReadLine/ReadLine.vi.cs b/PSReadLine/ReadLine.vi.cs index fc3c20de..34650e96 100644 --- a/PSReadLine/ReadLine.vi.cs +++ b/PSReadLine/ReadLine.vi.cs @@ -237,14 +237,11 @@ public static void DeleteToEnd(ConsoleKeyInfo? key = null, object arg = null) var length = endPosition - startPosition + 1; if (length > 0) { - _clipboard.Record(_singleton._buffer, startPosition, length); - _singleton.SaveEditItem(EditItemDelete.Create( - _clipboard, - _singleton._current, + _singleton.RemoveTextToClipboard( + startPosition, + length, DeleteToEnd, - arg)); - - _singleton._buffer.Remove(_singleton._current, length); + arg); // the cursor will go back one character, unless at the beginning of the line var endOfLineCursorPos = GetEndOfLogicalLinePos(_singleton._current) - 1; @@ -282,14 +279,13 @@ public static void DeleteWord(ConsoleKeyInfo? key = null, object arg = null) private static void DeleteToEndPoint(object arg, int endPoint, Action instigator) { - _singleton.SaveToClipboard(_singleton._current, endPoint - _singleton._current); - _singleton.SaveEditItem(EditItemDelete.Create( - _clipboard, + _singleton.RemoveTextToClipboard( _singleton._current, + endPoint - _singleton._current, instigator, arg - )); - _singleton._buffer.Remove(_singleton._current, endPoint - _singleton._current); + ); + if (_singleton._current >= _singleton._buffer.Length) { _singleton._current = Math.Max(0, _singleton._buffer.Length - 1); @@ -301,14 +297,12 @@ private static void DeleteBackwardToEndPoint(object arg, int endPoint, Action= _singleton._buffer.Length) - { - _singleton._current = Math.Max(0, _singleton._buffer.Length - 1); - } - _singleton.Render(); + DeleteToEndPoint(arg, endPoint, ViDeleteGlob); } /// @@ -358,19 +339,8 @@ public static void DeleteEndOfWord(ConsoleKeyInfo? key = null, object arg = null Ding(); return; } - _singleton.SaveToClipboard(_singleton._current, 1 + endPoint - _singleton._current); - _singleton.SaveEditItem(EditItemDelete.Create( - _clipboard, - _singleton._current, - DeleteEndOfWord, - arg - )); - _singleton._buffer.Remove(_singleton._current, 1 + endPoint - _singleton._current); - if (_singleton._current >= _singleton._buffer.Length) - { - _singleton._current = Math.Max(0, _singleton._buffer.Length - 1); - } - _singleton.Render(); + + DeleteToEndPoint(arg, 1 + endPoint, DeleteEndOfWord); } /// @@ -385,19 +355,7 @@ public static void ViDeleteEndOfGlob(ConsoleKeyInfo? key = null, object arg = nu endPoint = _singleton.ViFindGlobEnd(endPoint); } - _singleton.SaveToClipboard(_singleton._current, 1 + endPoint - _singleton._current); - _singleton.SaveEditItem(EditItemDelete.Create( - _clipboard, - _singleton._current, - ViDeleteEndOfGlob, - arg - )); - _singleton._buffer.Remove(_singleton._current, 1 + endPoint - _singleton._current); - if (_singleton._current >= _singleton._buffer.Length) - { - _singleton._current = Math.Max(0, _singleton._buffer.Length - 1); - } - _singleton.Render(); + DeleteToEndPoint(arg, 1 + endPoint, ViDeleteEndOfGlob); } /// @@ -731,10 +689,12 @@ public static void DeleteLineToFirstChar(ConsoleKeyInfo? key = null, object arg } } - _singleton.SaveToClipboard(i, _singleton._current - i); - _singleton.SaveEditItem(EditItemDelete.Create(_clipboard, i, DeleteLineToFirstChar)); + _singleton.RemoveTextToClipboard( + i, + _singleton._current - i, + DeleteLineToFirstChar, + arg); - _singleton._buffer.Remove(i, _singleton._current - i); _singleton._current = i; _singleton.Render(); } @@ -897,14 +857,12 @@ public static void BackwardDeleteWord(ConsoleKeyInfo? key = null, object arg = n Ding(); return; } - _clipboard.Record(_singleton._buffer, deletePoint, _singleton._current - deletePoint); - _singleton.SaveEditItem(EditItemDelete.Create( - _clipboard, + _singleton.RemoveTextToClipboard( deletePoint, + _singleton._current - deletePoint, BackwardDeleteWord, - arg - )); - _singleton._buffer.Remove(deletePoint, _singleton._current - deletePoint); + arg); + _singleton._current = deletePoint; _singleton.Render(); } @@ -930,14 +888,12 @@ public static void ViBackwardDeleteGlob(ConsoleKeyInfo? key = null, object arg = Ding(); return; } - _clipboard.Record(_singleton._buffer, deletePoint, _singleton._current - deletePoint); - _singleton.SaveEditItem(EditItemDelete.Create( - _clipboard, + _singleton.RemoveTextToClipboard( deletePoint, + _singleton._current - deletePoint, BackwardDeleteWord, - arg - )); - _singleton._buffer.Remove(deletePoint, _singleton._current - deletePoint); + arg); + _singleton._current = deletePoint; _singleton.Render(); } @@ -973,10 +929,12 @@ private static void DeleteRange(int first, int last, Action + /// Removes a portion of text from the buffer + /// and saves it to the clipboard in order to support undo. + /// + /// + /// + /// + /// + private void RemoveTextToClipboard(int start, int count, Action instigator = null, object arg = null) + { + _singleton.SaveToClipboard(start, count); + _singleton.SaveEditItem(EditItemDelete.Create( + _clipboard, + start, + instigator, + arg + )); + _singleton._buffer.Remove(start, count); + } + /// /// Yank the entire buffer. /// @@ -142,7 +162,7 @@ public static void ViYankToEndOfLine(ConsoleKeyInfo? key = null, object arg = nu var length = end - start + 1; if (length > 0) { - _clipboard.Record(_singleton._buffer, start, length); + _singleton.SaveToClipboard(start, length); } } @@ -254,8 +274,8 @@ public static void ViYankBeginningOfLine(ConsoleKeyInfo? key = null, object arg var start = GetBeginningOfLinePos(_singleton._current); var length = _singleton._current - start; if (length > 0) - { - _clipboard.Record(_singleton._buffer, start, length); + { + _singleton.SaveToClipboard(start, length); _singleton.MoveCursor(start); } } @@ -269,7 +289,7 @@ public static void ViYankToFirstChar(ConsoleKeyInfo? key = null, object arg = nu var length = _singleton._current - start; if (length > 0) { - _clipboard.Record(_singleton._buffer, start, length); + _singleton.SaveToClipboard(start, length); _singleton.MoveCursor(start); } } From 2c276394feddc5d607d069de90a87ac8fc2ec956 Mon Sep 17 00:00:00 2001 From: springcomp Date: Tue, 19 Jan 2021 15:35:01 +0100 Subject: [PATCH 2/6] Addressed suggested code formatting. --- PSReadLine/ReadLine.vi.cs | 3 +-- PSReadLine/YankPaste.vi.cs | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/PSReadLine/ReadLine.vi.cs b/PSReadLine/ReadLine.vi.cs index 34650e96..4e9318ee 100644 --- a/PSReadLine/ReadLine.vi.cs +++ b/PSReadLine/ReadLine.vi.cs @@ -283,8 +283,7 @@ private static void DeleteToEndPoint(object arg, int endPoint, Action= _singleton._buffer.Length) { diff --git a/PSReadLine/YankPaste.vi.cs b/PSReadLine/YankPaste.vi.cs index abca3a1a..b7fde8d5 100644 --- a/PSReadLine/YankPaste.vi.cs +++ b/PSReadLine/YankPaste.vi.cs @@ -85,8 +85,7 @@ private void RemoveTextToClipboard(int start, int count, Action Date: Tue, 19 Jan 2021 15:40:09 +0100 Subject: [PATCH 3/6] Removed ViRegister implicit converter as it is no longer needed for compatibility reasons. --- PSReadLine/ViRegister.cs | 6 ------ PSReadLine/YankPaste.vi.cs | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/PSReadLine/ViRegister.cs b/PSReadLine/ViRegister.cs index 86c09b9d..48df9965 100644 --- a/PSReadLine/ViRegister.cs +++ b/PSReadLine/ViRegister.cs @@ -80,12 +80,6 @@ public void LinewiseRecord(string text) _text = text; } - // for compatibility reasons, as an interim solution - public static implicit operator string(ViRegister register) - { - return register._text; - } - public int PasteAfter(StringBuilder buffer, int position) { if (IsEmpty) diff --git a/PSReadLine/YankPaste.vi.cs b/PSReadLine/YankPaste.vi.cs index b7fde8d5..71879f68 100644 --- a/PSReadLine/YankPaste.vi.cs +++ b/PSReadLine/YankPaste.vi.cs @@ -82,7 +82,7 @@ private void RemoveTextToClipboard(int start, int count, Action Date: Tue, 19 Jan 2021 15:43:19 +0100 Subject: [PATCH 4/6] Renamed method to better reflect usage and scope. --- PSReadLine/BasicEditing.cs | 6 +++--- PSReadLine/ReadLine.vi.cs | 14 +++++++------- PSReadLine/YankPaste.vi.cs | 6 +++--- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/PSReadLine/BasicEditing.cs b/PSReadLine/BasicEditing.cs index c0a3d181..f65765c7 100644 --- a/PSReadLine/BasicEditing.cs +++ b/PSReadLine/BasicEditing.cs @@ -122,7 +122,7 @@ public static void BackwardDeleteLine(ConsoleKeyInfo? key = null, object arg = n { if (_singleton._current > 0) { - _singleton.RemoveTextToClipboard(0, _singleton._current); + _singleton.RemoveTextToViRegister(0, _singleton._current); _singleton._current = 0; _singleton.Render(); } @@ -148,7 +148,7 @@ public static void BackwardDeleteChar(ConsoleKeyInfo? key = null, object arg = n int startDeleteIndex = _singleton._current - qty; - _singleton.RemoveTextToClipboard(startDeleteIndex, qty, BackwardDeleteChar, arg); + _singleton.RemoveTextToViRegister(startDeleteIndex, qty, BackwardDeleteChar, arg); _singleton._current = startDeleteIndex; _singleton.Render(); } @@ -169,7 +169,7 @@ private void DeleteCharImpl(int qty, bool orExit) { qty = Math.Min(qty, _singleton._buffer.Length - _singleton._current); - RemoveTextToClipboard(_current, qty, DeleteChar); + RemoveTextToViRegister(_current, qty, DeleteChar); if (_current >= _buffer.Length) { _current = Math.Max(0, _buffer.Length + ViEndOfLineFactor); diff --git a/PSReadLine/ReadLine.vi.cs b/PSReadLine/ReadLine.vi.cs index 4e9318ee..d4dedfd7 100644 --- a/PSReadLine/ReadLine.vi.cs +++ b/PSReadLine/ReadLine.vi.cs @@ -237,7 +237,7 @@ public static void DeleteToEnd(ConsoleKeyInfo? key = null, object arg = null) var length = endPosition - startPosition + 1; if (length > 0) { - _singleton.RemoveTextToClipboard( + _singleton.RemoveTextToViRegister( startPosition, length, DeleteToEnd, @@ -279,7 +279,7 @@ public static void DeleteWord(ConsoleKeyInfo? key = null, object arg = null) private static void DeleteToEndPoint(object arg, int endPoint, Action instigator) { - _singleton.RemoveTextToClipboard( + _singleton.RemoveTextToViRegister( _singleton._current, endPoint - _singleton._current, instigator, @@ -296,7 +296,7 @@ private static void DeleteBackwardToEndPoint(object arg, int endPoint, Action - /// Removes a portion of text from the buffer - /// and saves it to the clipboard in order to support undo. + /// Remove a portion of text from the buffer, save it to the vi register + /// and also save it to the edit list to support undo. /// /// /// /// /// - private void RemoveTextToClipboard(int start, int count, Action instigator = null, object arg = null) + private void RemoveTextToViRegister(int start, int count, Action instigator = null, object arg = null) { _singleton.SaveToClipboard(start, count); _singleton.SaveEditItem(EditItemDelete.Create( From ff955fa305d3addf16d7b6ad14e978bcf73205c6 Mon Sep 17 00:00:00 2001 From: springcomp Date: Tue, 19 Jan 2021 15:45:13 +0100 Subject: [PATCH 5/6] Renamed _clipboard to better reflect its use limited to vi-mode. --- PSReadLine/ReadLine.cs | 2 +- PSReadLine/ReadLine.vi.cs | 2 +- PSReadLine/YankPaste.vi.cs | 16 ++++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/PSReadLine/ReadLine.cs b/PSReadLine/ReadLine.cs index 4ace2a84..4395a379 100644 --- a/PSReadLine/ReadLine.cs +++ b/PSReadLine/ReadLine.cs @@ -630,7 +630,7 @@ void ProcessOneKey(PSKeyInfo key, Dictionary dispatchTabl static PSConsoleReadLine() { _singleton = new PSConsoleReadLine(); - _clipboard = new ViRegister(_singleton); + _viRegister = new ViRegister(_singleton); } private PSConsoleReadLine() diff --git a/PSReadLine/ReadLine.vi.cs b/PSReadLine/ReadLine.vi.cs index d4dedfd7..c3a20de2 100644 --- a/PSReadLine/ReadLine.vi.cs +++ b/PSReadLine/ReadLine.vi.cs @@ -741,7 +741,7 @@ private static int DeleteLineImpl(int lineIndex, int lineCount) var deleteText = _singleton._buffer.ToString(range.Offset, range.Count); - _clipboard.LinewiseRecord(deleteText); + _viRegister.LinewiseRecord(deleteText); var deletePosition = range.Offset; var anchor = _singleton._current; diff --git a/PSReadLine/YankPaste.vi.cs b/PSReadLine/YankPaste.vi.cs index 14233736..f4e5be4d 100644 --- a/PSReadLine/YankPaste.vi.cs +++ b/PSReadLine/YankPaste.vi.cs @@ -12,14 +12,14 @@ public partial class PSConsoleReadLine // *must* be initialized in the static ctor // because it depends on static member _singleton // being initialized first. - private static readonly ViRegister _clipboard; + private static readonly ViRegister _viRegister; /// /// Paste the clipboard after the cursor, moving the cursor to the end of the pasted text. /// public static void PasteAfter(ConsoleKeyInfo? key = null, object arg = null) { - if (_clipboard.IsEmpty) + if (_viRegister.IsEmpty) { Ding(); return; @@ -33,7 +33,7 @@ public static void PasteAfter(ConsoleKeyInfo? key = null, object arg = null) /// public static void PasteBefore(ConsoleKeyInfo? key = null, object arg = null) { - if (_clipboard.IsEmpty) + if (_viRegister.IsEmpty) { Ding(); return; @@ -43,19 +43,19 @@ public static void PasteBefore(ConsoleKeyInfo? key = null, object arg = null) private void PasteAfterImpl() { - _current = _clipboard.PasteAfter(_buffer, _current); + _current = _viRegister.PasteAfter(_buffer, _current); Render(); } private void PasteBeforeImpl() { - _current = _clipboard.PasteBefore(_buffer, _current); + _current = _viRegister.PasteBefore(_buffer, _current); Render(); } private void SaveToClipboard(int startIndex, int length) { - _clipboard.Record(_buffer, startIndex, length); + _viRegister.Record(_buffer, startIndex, length); } /// @@ -67,7 +67,7 @@ private void SaveToClipboard(int startIndex, int length) private void SaveLinesToClipboard(int lineIndex, int lineCount) { var range = _buffer.GetRange(lineIndex, lineCount); - _clipboard.LinewiseRecord(_buffer.ToString(range.Offset, range.Count)); + _viRegister.LinewiseRecord(_buffer.ToString(range.Offset, range.Count)); } /// @@ -82,7 +82,7 @@ private void RemoveTextToViRegister(int start, int count, Action Date: Wed, 20 Jan 2021 08:01:19 +0100 Subject: [PATCH 6/6] Added missing arg to prevent potential regression. --- PSReadLine/BasicEditing.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PSReadLine/BasicEditing.cs b/PSReadLine/BasicEditing.cs index f65765c7..15e26801 100644 --- a/PSReadLine/BasicEditing.cs +++ b/PSReadLine/BasicEditing.cs @@ -169,7 +169,7 @@ private void DeleteCharImpl(int qty, bool orExit) { qty = Math.Min(qty, _singleton._buffer.Length - _singleton._current); - RemoveTextToViRegister(_current, qty, DeleteChar); + RemoveTextToViRegister(_current, qty, DeleteChar, qty); if (_current >= _buffer.Length) { _current = Math.Max(0, _buffer.Length + ViEndOfLineFactor);