Skip to content
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

Refactor usage of clipboard #2022

Merged
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
21 changes: 5 additions & 16 deletions PSReadLine/BasicEditing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,8 @@ private static void BackwardDeleteSubstring(int position, Action<ConsoleKeyInfo?
if (_singleton._current > position)
{
var count = _singleton._current - position;

_clipboard.Record(_singleton._buffer, position, count);
_singleton.SaveEditItem(EditItemDelete.Create(_clipboard, position, instigator));
_singleton._buffer.Remove(position, count);

_singleton.RemoveTextToViRegister(position, count, instigator);
_singleton._current = position;
_singleton.Render();
}
Expand All @@ -185,15 +183,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.RemoveTextToViRegister(startDeleteIndex, qty, BackwardDeleteChar, arg);
_singleton._current = startDeleteIndex;
_singleton.Render();
}
Expand All @@ -214,9 +205,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);
RemoveTextToViRegister(_current, qty, DeleteChar, qty);
if (_current >= _buffer.Length)
{
_current = Math.Max(0, _buffer.Length + ViEndOfLineFactor);
Expand Down
2 changes: 1 addition & 1 deletion PSReadLine/ReadLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ void ProcessOneKey(PSKeyInfo key, Dictionary<PSKeyInfo, KeyHandler> dispatchTabl
static PSConsoleReadLine()
{
_singleton = new PSConsoleReadLine();
_clipboard = new ViRegister(_singleton);
_viRegister = new ViRegister(_singleton);
}

private PSConsoleReadLine()
Expand Down
113 changes: 35 additions & 78 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.RemoveTextToViRegister(
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,12 @@ 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.RemoveTextToViRegister(
_singleton._current,
endPoint - _singleton._current,
instigator,
arg
));
_singleton._buffer.Remove(_singleton._current, endPoint - _singleton._current);
arg);

if (_singleton._current >= _singleton._buffer.Length)
{
_singleton._current = Math.Max(0, _singleton._buffer.Length - 1);
Expand All @@ -301,14 +296,12 @@ private static void DeleteBackwardToEndPoint(object arg, int endPoint, Action<Co
{
int deleteLength = _singleton._current - endPoint;

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

_singleton._current = endPoint;
_singleton.Render();
}
Expand All @@ -324,21 +317,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 +338,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 +354,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 @@ -724,10 +681,12 @@ public static void DeleteLineToFirstChar(ConsoleKeyInfo? key = null, object arg
{
var i = GetFirstNonBlankOfLogicalLinePos(_singleton._current);

_singleton.SaveToClipboard(i, _singleton._current - i);
_singleton.SaveEditItem(EditItemDelete.Create(_clipboard, i, DeleteLineToFirstChar));
_singleton.RemoveTextToViRegister(
i,
_singleton._current - i,
DeleteLineToFirstChar,
arg);

_singleton._buffer.Remove(i, _singleton._current - i);
_singleton._current = i;
_singleton.Render();
}
Expand Down Expand Up @@ -775,7 +734,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;
Expand Down Expand Up @@ -890,14 +849,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.RemoveTextToViRegister(
deletePoint,
_singleton._current - deletePoint,
BackwardDeleteWord,
arg
));
_singleton._buffer.Remove(deletePoint, _singleton._current - deletePoint);
arg);

_singleton._current = deletePoint;
_singleton.Render();
}
Expand All @@ -923,14 +880,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.RemoveTextToViRegister(
deletePoint,
_singleton._current - deletePoint,
BackwardDeleteWord,
arg
));
_singleton._buffer.Remove(deletePoint, _singleton._current - deletePoint);
arg);

_singleton._current = deletePoint;
_singleton.Render();
}
Expand Down Expand Up @@ -966,10 +921,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.RemoveTextToViRegister(
first,
length,
action);

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

Expand Down
6 changes: 0 additions & 6 deletions PSReadLine/ViRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 30 additions & 11 deletions PSReadLine/YankPaste.vi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Paste the clipboard after the cursor, moving the cursor to the end of the pasted text.
/// </summary>
public static void PasteAfter(ConsoleKeyInfo? key = null, object arg = null)
{
if (_clipboard.IsEmpty)
if (_viRegister.IsEmpty)
{
Ding();
return;
Expand All @@ -33,7 +33,7 @@ public static void PasteAfter(ConsoleKeyInfo? key = null, object arg = null)
/// </summary>
public static void PasteBefore(ConsoleKeyInfo? key = null, object arg = null)
{
if (_clipboard.IsEmpty)
if (_viRegister.IsEmpty)
{
Ding();
return;
Expand All @@ -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);
}

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

/// <summary>
/// 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.
/// </summary>
/// <param name="start"></param>
/// <param name="count"></param>
/// <param name="instigator"></param>
/// <param name="arg"></param>
private void RemoveTextToViRegister(int start, int count, Action<ConsoleKeyInfo?, object> instigator = null, object arg = null)
{
_singleton.SaveToClipboard(start, count);
_singleton.SaveEditItem(EditItemDelete.Create(
_viRegister.RawText,
start,
instigator,
arg));
_singleton._buffer.Remove(start, count);
}

/// <summary>
Expand Down Expand Up @@ -142,7 +161,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 +273,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 +288,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