forked from PowerShell/PSReadLine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViRegister.cs
318 lines (270 loc) · 10.6 KB
/
ViRegister.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System;
using System.Text;
namespace Microsoft.PowerShell
{
public partial class PSConsoleReadLine
{
/// <summary>
/// Represents a clipboard that holds a piece of text.
/// </summary>
internal interface IClipboard
{
/// <summary>
/// Retrieves the text stored in the clipboard.
/// </summary>
string GetText();
/// <summary>
/// Stores some text in the clipboard.
/// </summary>
/// <param name="text"></param>
void SetText(string text);
}
/// <summary>
/// Represents an in-memory clipboard.
/// </summary>
internal sealed class InMemoryClipboard : IClipboard
{
private string _text;
public string GetText()
=> _text ?? "";
public void SetText(string text)
=> _text = text;
}
/// <summary>
/// Represents a clipboard that does not store any text.
/// </summary>
internal sealed class NoOpClipboard : IClipboard
{
public string GetText() => "";
public void SetText(string text) { }
}
/// <summary>
/// Represents the system clipboard.
/// </summary>
internal sealed class SystemClipboard : IClipboard
{
public string GetText()
=> Internal.Clipboard.GetText();
public void SetText(string text)
=> Internal.Clipboard.SetText(text);
}
/// <summary>
/// Represents a named register.
/// </summary>
internal sealed class ViRegister
{
private readonly PSConsoleReadLine _singleton;
private readonly IClipboard _clipboard;
/// Initialize a new instance of the <see cref="ViRegister" /> class.
/// </summary>
/// <param name="singleton">The <see cref="PSConsoleReadLine" /> object.
/// Used to hook into the undo / redo subsystem as part of
/// pasting the contents of the register into a buffer.
/// </param>
/// <param name="clipboard">The clipboard to store text to and retrieve text from</param>
public ViRegister(PSConsoleReadLine singleton, IClipboard clipboard)
{
_clipboard = clipboard;
_singleton = singleton;
}
/// Initialize a new instance of the <see cref="ViRegister" /> class.
/// </summary>
/// <param name="singleton">The <see cref="PSConsoleReadLine" /> object.
/// Used to hook into the undo / redo subsystem as part of
/// pasting the contents of the register into a buffer.
/// </param>
public ViRegister(PSConsoleReadLine singleton)
: this(singleton, new InMemoryClipboard())
{
}
/// <summary>
/// Returns whether this register is empty.
/// </summary>
public bool IsEmpty
=> String.IsNullOrEmpty(_clipboard.GetText());
/// <summary>
/// Returns whether this register contains
/// linewise yanked text.
/// </summary>
public bool HasLinewiseText { get; private set; }
/// <summary>
/// Gets the raw text contained in the register
/// </summary>
public string RawText
=> _clipboard.GetText();
/// <summary>
/// Records the entire buffer in the register.
/// </summary>
/// <param name="buffer"></param>
public void Record(StringBuilder buffer)
=> Record(buffer, 0, buffer.Length);
/// <summary>
/// Records a piece of text in the register.
/// </summary>
/// <param name="buffer"></param>
/// <param name="offset"></param>
/// <param name="count"></param>
public void Record(StringBuilder buffer, int offset, int count)
{
System.Diagnostics.Debug.Assert(
offset >= 0 &&
(buffer.Length == 0 || offset < buffer.Length)
);
System.Diagnostics.Debug.Assert(offset + count <= buffer.Length);
HasLinewiseText = false;
_clipboard.SetText(buffer.ToString(offset, count));
}
/// <summary>
/// Records a block of lines in the register.
/// </summary>
/// <param name="text"></param>
public void LinewiseRecord(string text)
{
HasLinewiseText = true;
_clipboard.SetText(text);
}
public int PasteAfter(StringBuilder buffer, int position)
{
if (IsEmpty)
{
return position;
}
var yanked = _clipboard.GetText();
if (HasLinewiseText)
{
var text = yanked;
if (text[0] != '\n')
{
text = '\n' + text;
}
// paste text after the next line
var pastePosition = -1;
for (var index = position; index < buffer.Length; index++)
{
if (buffer[index] == '\n')
{
pastePosition = index;
break;
}
}
if (pastePosition == -1)
{
pastePosition = buffer.Length;
}
InsertAt(buffer, text, pastePosition, position);
return pastePosition + 1;
}
else
{
if (position < buffer.Length)
{
position += 1;
}
InsertAt(buffer, yanked, position, position);
position += yanked.Length - 1;
return position;
}
}
public int PasteBefore(StringBuilder buffer, int position)
{
var yanked = _clipboard.GetText();
if (HasLinewiseText)
{
// currently, in Vi Edit Mode, the cursor may be positioned
// exactly one character past the end of the buffer.
// we adjust the current position to prevent a crash
position = Math.Max(0, Math.Min(position, buffer.Length - 1));
var text = yanked;
if (text[0] == '\n')
{
text = text.Remove(0, 1);
}
if (text[text.Length - 1] != '\n')
{
text += '\n';
}
// paste text before the current line
var previousLinePos = -1;
if (buffer.Length > 0)
{
for (var index = position; index >= 0; index--)
{
if (buffer[index] == '\n')
{
previousLinePos = index + 1;
break;
}
}
}
if (previousLinePos == -1)
{
previousLinePos = 0;
}
InsertBefore(buffer, text, previousLinePos, position);
return previousLinePos;
}
else
{
InsertAt(buffer, yanked, position, position);
return position + yanked.Length - 1;
}
}
private void InsertBefore(StringBuilder buffer, string text, int pastePosition, int position)
{
RecordPaste(text, pastePosition, position);
buffer.Insert(pastePosition, text);
}
private void InsertAt(StringBuilder buffer, string text, int pastePosition, int position)
{
RecordPaste(text, pastePosition, position);
// Use Append if possible because Insert at end makes StringBuilder quite slow.
if (pastePosition == buffer.Length)
{
buffer.Append(text);
}
else
{
buffer.Insert(pastePosition, text);
}
}
/// <summary>
/// Called to record the paste operation in the undo subsystem.
/// </summary>
/// <param name="text">
/// The text being pasted.
/// </param>
/// <param name="position">
/// The position in the buffer at
/// which the pasted text will be inserted.
/// </param>
/// <param name="anchor">
/// The recorded position in the buffer
/// from which the paste operation originates.
/// This is usually the same as Position, but
/// not always. For instance, when pasting a
/// linewise selection before the current line,
/// the Anchor is the cursor position, whereas
/// the Position is the beginning of the previous line.
/// </param>
private void RecordPaste(string text, int position, int anchor)
{
if (_singleton != null)
{
var editItem = EditItemInsertLines.Create(
text,
position,
anchor
);
_singleton.SaveEditItem(editItem);
}
}
#if DEBUG
public override string ToString()
{
var text = _clipboard.GetText().Replace("\n", "\\n");
return (HasLinewiseText ? "line: " : "") + "\"" + text + "\"";
}
#endif
}
}
}