Skip to content

Refactor usage of clipboard #2022

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
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 4 additions & 15 deletions PSReadLine/BasicEditing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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);
Expand Down
110 changes: 34 additions & 76 deletions PSReadLine/ReadLine.vi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -282,14 +279,13 @@ public static void DeleteWord(ConsoleKeyInfo? key = null, object arg = null)

private static void DeleteToEndPoint(object arg, int endPoint, Action<ConsoleKeyInfo?, object> 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);
Expand All @@ -301,14 +297,12 @@ private static void DeleteBackwardToEndPoint(object arg, int endPoint, Action<Co
{
int deleteLength = _singleton._current - endPoint + 1;

_singleton.SaveToClipboard(endPoint, deleteLength);
_singleton.SaveEditItem(EditItemDelete.Create(
_clipboard,
_singleton.RemoveTextToClipboard(
endPoint,
deleteLength,
instigator,
arg
));
_singleton._buffer.Remove(endPoint, deleteLength);
arg);

_singleton._current = endPoint;
_singleton.Render();
}
Expand All @@ -324,21 +318,8 @@ public static void ViDeleteGlob(ConsoleKeyInfo? key = null, object arg = null)
{
endPoint = _singleton.ViFindNextGlob(endPoint);
}
int length = endPoint - _singleton._current;

_singleton.SaveToClipboard(_singleton._current, length);
_singleton.SaveEditItem(EditItemDelete.Create(
_clipboard,
_singleton._current,
ViDeleteGlob,
arg
));
_singleton._buffer.Remove(_singleton._current, length);
if (_singleton._current >= _singleton._buffer.Length)
{
_singleton._current = Math.Max(0, _singleton._buffer.Length - 1);
}
_singleton.Render();
DeleteToEndPoint(arg, endPoint, ViDeleteGlob);
}

/// <summary>
Expand All @@ -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);
}

/// <summary>
Expand All @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -973,10 +929,12 @@ private static void DeleteRange(int first, int last, Action<ConsoleKeyInfo?, obj
{
int length = last - first + 1;

_singleton.SaveToClipboard(first, length);
_singleton.SaveEditItem(EditItemDelete.Create(_clipboard, first, action));
_singleton.RemoveTextToClipboard(
first,
length,
action);

_singleton._current = first;
_singleton._buffer.Remove(first, length);
_singleton.Render();
}

Expand Down
28 changes: 24 additions & 4 deletions PSReadLine/YankPaste.vi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ private void SaveLinesToClipboard(int lineIndex, int lineCount)
_clipboard.LinewiseRecord(_buffer.ToString(range.Offset, range.Count));
}

/// <summary>
/// Removes a portion of text from the buffer
/// and saves it to the clipboard in order to support undo.
/// </summary>
/// <param name="start"></param>
/// <param name="count"></param>
/// <param name="instigator"></param>
/// <param name="arg"></param>
private void RemoveTextToClipboard(int start, int count, Action<ConsoleKeyInfo?, object> instigator = null, object arg = null)
{
_singleton.SaveToClipboard(start, count);
_singleton.SaveEditItem(EditItemDelete.Create(
_clipboard,
start,
instigator,
arg
));
_singleton._buffer.Remove(start, count);
}

/// <summary>
/// Yank the entire buffer.
/// </summary>
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down