Skip to content

Commit 9d5b41f

Browse files
authored
Refactor the usage of _clipboard (#2022)
1 parent 47fb957 commit 9d5b41f

File tree

5 files changed

+71
-112
lines changed

5 files changed

+71
-112
lines changed

PSReadLine/BasicEditing.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,8 @@ private static void BackwardDeleteSubstring(int position, Action<ConsoleKeyInfo?
157157
if (_singleton._current > position)
158158
{
159159
var count = _singleton._current - position;
160-
161-
_clipboard.Record(_singleton._buffer, position, count);
162-
_singleton.SaveEditItem(EditItemDelete.Create(_clipboard, position, instigator));
163-
_singleton._buffer.Remove(position, count);
160+
161+
_singleton.RemoveTextToViRegister(position, count, instigator);
164162
_singleton._current = position;
165163
_singleton.Render();
166164
}
@@ -185,15 +183,8 @@ public static void BackwardDeleteChar(ConsoleKeyInfo? key = null, object arg = n
185183
qty = Math.Min(qty, _singleton._current);
186184

187185
int startDeleteIndex = _singleton._current - qty;
188-
_singleton.SaveEditItem(
189-
EditItemDelete.Create(
190-
_singleton._buffer.ToString(startDeleteIndex, qty),
191-
startDeleteIndex,
192-
BackwardDeleteChar,
193-
arg)
194-
);
195-
_singleton.SaveToClipboard(startDeleteIndex, qty);
196-
_singleton._buffer.Remove(startDeleteIndex, qty);
186+
187+
_singleton.RemoveTextToViRegister(startDeleteIndex, qty, BackwardDeleteChar, arg);
197188
_singleton._current = startDeleteIndex;
198189
_singleton.Render();
199190
}
@@ -214,9 +205,7 @@ private void DeleteCharImpl(int qty, bool orExit)
214205
{
215206
qty = Math.Min(qty, _singleton._buffer.Length - _singleton._current);
216207

217-
SaveEditItem(EditItemDelete.Create(_buffer.ToString(_current, qty), _current, DeleteChar, qty));
218-
SaveToClipboard(_current, qty);
219-
_buffer.Remove(_current, qty);
208+
RemoveTextToViRegister(_current, qty, DeleteChar, qty);
220209
if (_current >= _buffer.Length)
221210
{
222211
_current = Math.Max(0, _buffer.Length + ViEndOfLineFactor);

PSReadLine/ReadLine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ void ProcessOneKey(PSKeyInfo key, Dictionary<PSKeyInfo, KeyHandler> dispatchTabl
630630
static PSConsoleReadLine()
631631
{
632632
_singleton = new PSConsoleReadLine();
633-
_clipboard = new ViRegister(_singleton);
633+
_viRegister = new ViRegister(_singleton);
634634
}
635635

636636
private PSConsoleReadLine()

PSReadLine/ReadLine.vi.cs

Lines changed: 35 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,11 @@ public static void DeleteToEnd(ConsoleKeyInfo? key = null, object arg = null)
237237
var length = endPosition - startPosition + 1;
238238
if (length > 0)
239239
{
240-
_clipboard.Record(_singleton._buffer, startPosition, length);
241-
_singleton.SaveEditItem(EditItemDelete.Create(
242-
_clipboard,
243-
_singleton._current,
240+
_singleton.RemoveTextToViRegister(
241+
startPosition,
242+
length,
244243
DeleteToEnd,
245-
arg));
246-
247-
_singleton._buffer.Remove(_singleton._current, length);
244+
arg);
248245

249246
// the cursor will go back one character, unless at the beginning of the line
250247
var endOfLineCursorPos = GetEndOfLogicalLinePos(_singleton._current) - 1;
@@ -282,14 +279,12 @@ public static void DeleteWord(ConsoleKeyInfo? key = null, object arg = null)
282279

283280
private static void DeleteToEndPoint(object arg, int endPoint, Action<ConsoleKeyInfo?, object> instigator)
284281
{
285-
_singleton.SaveToClipboard(_singleton._current, endPoint - _singleton._current);
286-
_singleton.SaveEditItem(EditItemDelete.Create(
287-
_clipboard,
282+
_singleton.RemoveTextToViRegister(
288283
_singleton._current,
284+
endPoint - _singleton._current,
289285
instigator,
290-
arg
291-
));
292-
_singleton._buffer.Remove(_singleton._current, endPoint - _singleton._current);
286+
arg);
287+
293288
if (_singleton._current >= _singleton._buffer.Length)
294289
{
295290
_singleton._current = Math.Max(0, _singleton._buffer.Length - 1);
@@ -301,14 +296,12 @@ private static void DeleteBackwardToEndPoint(object arg, int endPoint, Action<Co
301296
{
302297
int deleteLength = _singleton._current - endPoint;
303298

304-
_singleton.SaveToClipboard(endPoint, deleteLength);
305-
_singleton.SaveEditItem(EditItemDelete.Create(
306-
_clipboard,
299+
_singleton.RemoveTextToViRegister(
307300
endPoint,
301+
deleteLength,
308302
instigator,
309-
arg
310-
));
311-
_singleton._buffer.Remove(endPoint, deleteLength);
303+
arg);
304+
312305
_singleton._current = endPoint;
313306
_singleton.Render();
314307
}
@@ -324,21 +317,8 @@ public static void ViDeleteGlob(ConsoleKeyInfo? key = null, object arg = null)
324317
{
325318
endPoint = _singleton.ViFindNextGlob(endPoint);
326319
}
327-
int length = endPoint - _singleton._current;
328320

329-
_singleton.SaveToClipboard(_singleton._current, length);
330-
_singleton.SaveEditItem(EditItemDelete.Create(
331-
_clipboard,
332-
_singleton._current,
333-
ViDeleteGlob,
334-
arg
335-
));
336-
_singleton._buffer.Remove(_singleton._current, length);
337-
if (_singleton._current >= _singleton._buffer.Length)
338-
{
339-
_singleton._current = Math.Max(0, _singleton._buffer.Length - 1);
340-
}
341-
_singleton.Render();
321+
DeleteToEndPoint(arg, endPoint, ViDeleteGlob);
342322
}
343323

344324
/// <summary>
@@ -358,19 +338,8 @@ public static void DeleteEndOfWord(ConsoleKeyInfo? key = null, object arg = null
358338
Ding();
359339
return;
360340
}
361-
_singleton.SaveToClipboard(_singleton._current, 1 + endPoint - _singleton._current);
362-
_singleton.SaveEditItem(EditItemDelete.Create(
363-
_clipboard,
364-
_singleton._current,
365-
DeleteEndOfWord,
366-
arg
367-
));
368-
_singleton._buffer.Remove(_singleton._current, 1 + endPoint - _singleton._current);
369-
if (_singleton._current >= _singleton._buffer.Length)
370-
{
371-
_singleton._current = Math.Max(0, _singleton._buffer.Length - 1);
372-
}
373-
_singleton.Render();
341+
342+
DeleteToEndPoint(arg, 1 + endPoint, DeleteEndOfWord);
374343
}
375344

376345
/// <summary>
@@ -385,19 +354,7 @@ public static void ViDeleteEndOfGlob(ConsoleKeyInfo? key = null, object arg = nu
385354
endPoint = _singleton.ViFindGlobEnd(endPoint);
386355
}
387356

388-
_singleton.SaveToClipboard(_singleton._current, 1 + endPoint - _singleton._current);
389-
_singleton.SaveEditItem(EditItemDelete.Create(
390-
_clipboard,
391-
_singleton._current,
392-
ViDeleteEndOfGlob,
393-
arg
394-
));
395-
_singleton._buffer.Remove(_singleton._current, 1 + endPoint - _singleton._current);
396-
if (_singleton._current >= _singleton._buffer.Length)
397-
{
398-
_singleton._current = Math.Max(0, _singleton._buffer.Length - 1);
399-
}
400-
_singleton.Render();
357+
DeleteToEndPoint(arg, 1 + endPoint, ViDeleteEndOfGlob);
401358
}
402359

403360
/// <summary>
@@ -724,10 +681,12 @@ public static void DeleteLineToFirstChar(ConsoleKeyInfo? key = null, object arg
724681
{
725682
var i = GetFirstNonBlankOfLogicalLinePos(_singleton._current);
726683

727-
_singleton.SaveToClipboard(i, _singleton._current - i);
728-
_singleton.SaveEditItem(EditItemDelete.Create(_clipboard, i, DeleteLineToFirstChar));
684+
_singleton.RemoveTextToViRegister(
685+
i,
686+
_singleton._current - i,
687+
DeleteLineToFirstChar,
688+
arg);
729689

730-
_singleton._buffer.Remove(i, _singleton._current - i);
731690
_singleton._current = i;
732691
_singleton.Render();
733692
}
@@ -775,7 +734,7 @@ private static int DeleteLineImpl(int lineIndex, int lineCount)
775734

776735
var deleteText = _singleton._buffer.ToString(range.Offset, range.Count);
777736

778-
_clipboard.LinewiseRecord(deleteText);
737+
_viRegister.LinewiseRecord(deleteText);
779738

780739
var deletePosition = range.Offset;
781740
var anchor = _singleton._current;
@@ -890,14 +849,12 @@ public static void BackwardDeleteWord(ConsoleKeyInfo? key = null, object arg = n
890849
Ding();
891850
return;
892851
}
893-
_clipboard.Record(_singleton._buffer, deletePoint, _singleton._current - deletePoint);
894-
_singleton.SaveEditItem(EditItemDelete.Create(
895-
_clipboard,
852+
_singleton.RemoveTextToViRegister(
896853
deletePoint,
854+
_singleton._current - deletePoint,
897855
BackwardDeleteWord,
898-
arg
899-
));
900-
_singleton._buffer.Remove(deletePoint, _singleton._current - deletePoint);
856+
arg);
857+
901858
_singleton._current = deletePoint;
902859
_singleton.Render();
903860
}
@@ -923,14 +880,12 @@ public static void ViBackwardDeleteGlob(ConsoleKeyInfo? key = null, object arg =
923880
Ding();
924881
return;
925882
}
926-
_clipboard.Record(_singleton._buffer, deletePoint, _singleton._current - deletePoint);
927-
_singleton.SaveEditItem(EditItemDelete.Create(
928-
_clipboard,
883+
_singleton.RemoveTextToViRegister(
929884
deletePoint,
885+
_singleton._current - deletePoint,
930886
BackwardDeleteWord,
931-
arg
932-
));
933-
_singleton._buffer.Remove(deletePoint, _singleton._current - deletePoint);
887+
arg);
888+
934889
_singleton._current = deletePoint;
935890
_singleton.Render();
936891
}
@@ -966,10 +921,12 @@ private static void DeleteRange(int first, int last, Action<ConsoleKeyInfo?, obj
966921
{
967922
int length = last - first + 1;
968923

969-
_singleton.SaveToClipboard(first, length);
970-
_singleton.SaveEditItem(EditItemDelete.Create(_clipboard, first, action));
924+
_singleton.RemoveTextToViRegister(
925+
first,
926+
length,
927+
action);
928+
971929
_singleton._current = first;
972-
_singleton._buffer.Remove(first, length);
973930
_singleton.Render();
974931
}
975932

PSReadLine/ViRegister.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,6 @@ public void LinewiseRecord(string text)
8080
_text = text;
8181
}
8282

83-
// for compatibility reasons, as an interim solution
84-
public static implicit operator string(ViRegister register)
85-
{
86-
return register._text;
87-
}
88-
8983
public int PasteAfter(StringBuilder buffer, int position)
9084
{
9185
if (IsEmpty)

PSReadLine/YankPaste.vi.cs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ public partial class PSConsoleReadLine
1212
// *must* be initialized in the static ctor
1313
// because it depends on static member _singleton
1414
// being initialized first.
15-
private static readonly ViRegister _clipboard;
15+
private static readonly ViRegister _viRegister;
1616

1717
/// <summary>
1818
/// Paste the clipboard after the cursor, moving the cursor to the end of the pasted text.
1919
/// </summary>
2020
public static void PasteAfter(ConsoleKeyInfo? key = null, object arg = null)
2121
{
22-
if (_clipboard.IsEmpty)
22+
if (_viRegister.IsEmpty)
2323
{
2424
Ding();
2525
return;
@@ -33,7 +33,7 @@ public static void PasteAfter(ConsoleKeyInfo? key = null, object arg = null)
3333
/// </summary>
3434
public static void PasteBefore(ConsoleKeyInfo? key = null, object arg = null)
3535
{
36-
if (_clipboard.IsEmpty)
36+
if (_viRegister.IsEmpty)
3737
{
3838
Ding();
3939
return;
@@ -43,19 +43,19 @@ public static void PasteBefore(ConsoleKeyInfo? key = null, object arg = null)
4343

4444
private void PasteAfterImpl()
4545
{
46-
_current = _clipboard.PasteAfter(_buffer, _current);
46+
_current = _viRegister.PasteAfter(_buffer, _current);
4747
Render();
4848
}
4949

5050
private void PasteBeforeImpl()
5151
{
52-
_current = _clipboard.PasteBefore(_buffer, _current);
52+
_current = _viRegister.PasteBefore(_buffer, _current);
5353
Render();
5454
}
5555

5656
private void SaveToClipboard(int startIndex, int length)
5757
{
58-
_clipboard.Record(_buffer, startIndex, length);
58+
_viRegister.Record(_buffer, startIndex, length);
5959
}
6060

6161
/// <summary>
@@ -67,7 +67,26 @@ private void SaveToClipboard(int startIndex, int length)
6767
private void SaveLinesToClipboard(int lineIndex, int lineCount)
6868
{
6969
var range = _buffer.GetRange(lineIndex, lineCount);
70-
_clipboard.LinewiseRecord(_buffer.ToString(range.Offset, range.Count));
70+
_viRegister.LinewiseRecord(_buffer.ToString(range.Offset, range.Count));
71+
}
72+
73+
/// <summary>
74+
/// Remove a portion of text from the buffer, save it to the vi register
75+
/// and also save it to the edit list to support undo.
76+
/// </summary>
77+
/// <param name="start"></param>
78+
/// <param name="count"></param>
79+
/// <param name="instigator"></param>
80+
/// <param name="arg"></param>
81+
private void RemoveTextToViRegister(int start, int count, Action<ConsoleKeyInfo?, object> instigator = null, object arg = null)
82+
{
83+
_singleton.SaveToClipboard(start, count);
84+
_singleton.SaveEditItem(EditItemDelete.Create(
85+
_viRegister.RawText,
86+
start,
87+
instigator,
88+
arg));
89+
_singleton._buffer.Remove(start, count);
7190
}
7291

7392
/// <summary>
@@ -142,7 +161,7 @@ public static void ViYankToEndOfLine(ConsoleKeyInfo? key = null, object arg = nu
142161
var length = end - start + 1;
143162
if (length > 0)
144163
{
145-
_clipboard.Record(_singleton._buffer, start, length);
164+
_singleton.SaveToClipboard(start, length);
146165
}
147166
}
148167

@@ -254,8 +273,8 @@ public static void ViYankBeginningOfLine(ConsoleKeyInfo? key = null, object arg
254273
var start = GetBeginningOfLinePos(_singleton._current);
255274
var length = _singleton._current - start;
256275
if (length > 0)
257-
{
258-
_clipboard.Record(_singleton._buffer, start, length);
276+
{
277+
_singleton.SaveToClipboard(start, length);
259278
_singleton.MoveCursor(start);
260279
}
261280
}
@@ -269,7 +288,7 @@ public static void ViYankToFirstChar(ConsoleKeyInfo? key = null, object arg = nu
269288
var length = _singleton._current - start;
270289
if (length > 0)
271290
{
272-
_clipboard.Record(_singleton._buffer, start, length);
291+
_singleton.SaveToClipboard(start, length);
273292
_singleton.MoveCursor(start);
274293
}
275294
}

0 commit comments

Comments
 (0)