-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEditor.Selection.cs
More file actions
313 lines (254 loc) · 9.96 KB
/
Copy pathEditor.Selection.cs
File metadata and controls
313 lines (254 loc) · 9.96 KB
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
using Terminal.Gui.Document;
namespace Terminal.Gui.Views;
/// <summary>
/// Selection state and operations. Internal model is two anchors: <c>_selectionAnchor</c> is
/// where Shift was first held; the caret anchor is the live end. Selection is the segment
/// between the two; when they coincide (or the selection anchor is null), there is no selection.
/// </summary>
public partial class Editor
{
private TextAnchor? _selectionAnchor;
/// <summary>True when there is a non-empty selection.</summary>
public bool HasSelection => _selectionAnchor is { IsDeleted: false } a && a.Offset != CaretOffset;
/// <summary>Start offset of the selection (inclusive). Equals <see cref="CaretOffset" /> when no selection.</summary>
public int SelectionStart => HasSelection ? Math.Min (SelectionAnchorOffset, CaretOffset) : CaretOffset;
/// <summary>End offset of the selection (exclusive). Equals <see cref="CaretOffset" /> when no selection.</summary>
public int SelectionEnd => HasSelection ? Math.Max (SelectionAnchorOffset, CaretOffset) : CaretOffset;
/// <summary>Length of the selection in characters; 0 when no selection.</summary>
public int SelectionLength => SelectionEnd - SelectionStart;
/// <summary>The selection as a <see cref="TextSegment" />, or <see langword="null" /> if no selection.</summary>
public TextSegment? Selection =>
!HasSelection ? null : new TextSegment { StartOffset = SelectionStart, Length = SelectionLength };
/// <summary>The selected document text, or an empty string when there is no selection.</summary>
public string SelectedText => HasSelection ? _document!.GetText (SelectionStart, SelectionLength) : string.Empty;
private int SelectionAnchorOffset => _selectionAnchor is { IsDeleted: false } anchor ? anchor.Offset : CaretOffset;
/// <summary>Raised whenever the selection range changes (created, extended, cleared).</summary>
public event EventHandler? SelectionChanged;
/// <summary>Clears the selection without moving the caret.</summary>
public void ClearSelection ()
{
if (_selectionAnchor is null)
{
return;
}
(int start, int end) before = SelectionTuple ();
_selectionAnchor = null;
RaiseSelectionChangedIfMoved (before);
SetNeedsDraw ();
}
/// <summary>Selects the entire document. Caret moves to <c>TextLength</c>.</summary>
public void SelectAll ()
{
SelectRange (0, _document!.TextLength);
}
/// <summary>Selects a document range and moves the caret to the range end.</summary>
public void SelectRange (int startOffset, int length)
{
if (_document is null)
{
return;
}
var start = Math.Clamp (startOffset, 0, _document.TextLength);
var end = (int)Math.Clamp (start + Math.Max (0L, length), 0L, _document.TextLength);
(int start, int end) before = SelectionTuple ();
_selectionAnchor = start == end ? null : CreateSelectionAnchor (start);
CaretOffset = end;
RefreshSelectionAnchorMovement ();
RaiseSelectionChangedIfMoved (before);
SetNeedsDraw ();
}
/// <summary>
/// Replaces the current selection with <paramref name="replacement" />. If there is no selection, this is a no-op.
/// The caret lands at <c>SelectionStart + replacement.Length</c>.
/// </summary>
public void ReplaceSelection (string replacement)
{
if (ReadOnly || !HasSelection)
{
return;
}
var start = SelectionStart;
var len = SelectionLength;
(int start, int end) before = SelectionTuple ();
_selectionAnchor = null;
_document!.Replace (start, len, replacement);
SetCaretOffset (start + replacement.Length, true);
RaiseSelectionChangedIfMoved (before);
}
/// <summary>
/// Begins (if needed) or extends the selection by setting the anchor to the current caret and then moving the
/// caret <paramref name="delta" /> characters horizontally.
/// </summary>
private void ExtendCaretBy (int delta)
{
ExtendCaretTo (CaretOffset + delta);
}
private void ExtendCaretVertically (int delta)
{
(int start, int end) before = SelectionTupleWithEnsuredAnchor ();
MoveCaretVertically (delta);
RaiseSelectionChangedIfMoved (before);
SetNeedsDraw ();
}
private void ExtendCaretTo (int newCaret)
{
(int start, int end) before = SelectionTupleWithEnsuredAnchor ();
CaretOffset = newCaret;
RaiseSelectionChangedIfMoved (before);
SetNeedsDraw ();
}
private void EnsureSelectionAnchor ()
{
_selectionAnchor ??= CreateSelectionAnchor (CaretOffset);
RefreshSelectionAnchorMovement ();
}
/// <summary>
/// Returns the current effective selection range as a (start, end) tuple. Callers compare a
/// before-snapshot to the post-mutation tuple via <see cref="RaiseSelectionChangedIfMoved" /> to
/// suppress no-op <see cref="SelectionChanged" /> firings.
/// </summary>
private (int start, int end) SelectionTuple ()
{
return (SelectionStart, SelectionEnd);
}
/// <summary>
/// Snapshot variant that primes the selection anchor first — used by extend-style operations
/// where setting the anchor is part of the operation but should not by itself count as a
/// selection-range change.
/// </summary>
private (int start, int end) SelectionTupleWithEnsuredAnchor ()
{
EnsureSelectionAnchor ();
return SelectionTuple ();
}
private void RaiseSelectionChangedIfMoved ((int start, int end) before)
{
if (before == SelectionTuple ())
{
return;
}
SelectionChanged?.Invoke (this, EventArgs.Empty);
}
/// <summary>
/// Movement helper that respects an existing selection: plain (non-extending) cursor keys clear the selection
/// and snap to the appropriate end; otherwise the caret moves by <paramref name="delta" />.
/// </summary>
private void MoveCaretByCollapsingSelection (int delta)
{
if (HasSelection)
{
var target = delta < 0 ? SelectionStart : SelectionEnd;
ClearSelection ();
CaretOffset = target;
return;
}
CaretOffset = CaretOffset + delta;
}
/// <summary>
/// Vertical movement helper: clears any selection then runs the standard line-up/line-down with sticky-column.
/// </summary>
private void MoveCaretVerticallyCollapsingSelection (int delta)
{
ClearSelection ();
MoveCaretVertically (delta);
}
private void SelectWordAtOffset (int offset)
{
if (_document is null)
{
return;
}
var originalOffset = Math.Clamp (offset, 0, _document.TextLength);
var wordOffset = originalOffset;
if (wordOffset == _document.TextLength || !IsIdentifierWordCharAt (wordOffset))
{
if (wordOffset == 0 || !IsIdentifierWordCharAt (wordOffset - 1))
{
ClearSelection ();
CaretOffset = originalOffset;
return;
}
wordOffset--;
}
var start = wordOffset;
while (start > 0 && IsIdentifierWordCharAt (start - 1))
{
start--;
}
var end = wordOffset + 1;
while (end < _document.TextLength && IsIdentifierWordCharAt (end))
{
end++;
}
SelectRange (start, end - start);
}
private void SelectLineAtOffset (int offset)
{
if (_document is null)
{
return;
}
DocumentLine line = _document.GetLineByOffset (Math.Clamp (offset, 0, _document.TextLength));
SelectRange (line.Offset, line.TotalLength);
}
internal void SelectLineAtViewRow (int row)
{
if (_document is null || _document.LineCount == 0)
{
return;
}
var lineNumber = ViewRowToLineNumber (row);
SelectLines (lineNumber, lineNumber);
}
internal void SelectLines (int anchorLineNumber, int activeLineNumber)
{
if (_document is null || _document.LineCount == 0)
{
return;
}
var startLineNumber = Math.Clamp (Math.Min (anchorLineNumber, activeLineNumber), 1, _document.LineCount);
var endLineNumber = Math.Clamp (Math.Max (anchorLineNumber, activeLineNumber), 1, _document.LineCount);
DocumentLine startLine = _document.GetLineByNumber (startLineNumber);
DocumentLine endLine = _document.GetLineByNumber (endLineNumber);
SelectRange (startLine.Offset, endLine.Offset + endLine.TotalLength - startLine.Offset);
}
internal int ViewRowToLineNumber (int row)
{
if (_document is null || _document.LineCount == 0)
{
return 1;
}
List<int> visibleLines = GetVisibleLineNumbers ();
var visibleIndex = Viewport.Y + row;
if (visibleIndex < 0 || visibleLines.Count == 0)
{
return 1;
}
if (visibleIndex >= visibleLines.Count)
{
return visibleLines[^1];
}
return visibleLines[visibleIndex];
}
private bool IsIdentifierWordCharAt (int offset)
{
var ch = _document!.GetCharAt (offset);
return char.IsLetterOrDigit (ch) || ch == '_';
}
private TextAnchor CreateSelectionAnchor (int offset)
{
TextAnchor anchor = _document!.CreateAnchor (offset);
anchor.SurviveDeletion = true;
return anchor;
}
private void RefreshSelectionAnchorMovement ()
{
if (_selectionAnchor is not { IsDeleted: false } anchor)
{
return;
}
anchor.MovementType = anchor.Offset <= CaretOffset
? AnchorMovementType.AfterInsertion
: AnchorMovementType.BeforeInsertion;
}
}