Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DefaultSelectionPrimitive.cs « EditorPrimitives « Impl « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d9ce5556a3c0004a0adbc5269b6428b03dd43d6c (plain)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//  Licensed under the MIT License. See License.txt in the project root for license information.
//
// This file contain implementations details that are subject to change without notice.
// Use at your own risk.
//
namespace Microsoft.VisualStudio.Text.EditorPrimitives.Implementation
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Diagnostics;

    using Microsoft.VisualStudio.Text;
    using Microsoft.VisualStudio.Text.Editor;
    using Microsoft.VisualStudio.Text.Formatting;
    using Microsoft.VisualStudio.Text.Operations;

    using Microsoft.VisualStudio.Text.Editor.OptionsExtensionMethods;
    
    internal sealed class DefaultSelectionPrimitive : Text.Editor.LegacySelection
    {
        private TextView _textView;
        private IEditorOptions _editorOptions;

        public DefaultSelectionPrimitive(TextView textView, IEditorOptions editorOptions)
        {
            _textView = textView;
            _editorOptions = editorOptions;
        }

        private ITextSelection TextSelection 
        {
            get { return _textView.AdvancedTextView.Selection; } 
        }

        private ITextCaret Caret
        {
            get { return TextView.AdvancedTextView.Caret; }
        }

        private DisplayTextRange TextRange
        {
            get
            {
                // TODO: What do we do in box mode?
                return TextView.GetTextRange(TextSelection.Start.Position, TextSelection.End.Position);
            }
        }

        public override void SelectRange(TextRange textRange)
        {
            this.SelectRange(textRange.GetStartPoint().CurrentPosition, textRange.GetEndPoint().CurrentPosition);
        }

        public override void SelectRange(TextPoint selectionStart, TextPoint selectionEnd)
        {
            this.SelectRange(selectionStart.CurrentPosition, selectionEnd.CurrentPosition);
        }

        public override void SelectAll()
        {
            // For select all, the selection always goes back to stream mode.
            this.AdvancedSelection.Mode = TextSelectionMode.Stream;
            this.SelectRange(0, TextView.AdvancedTextView.TextSnapshot.Length);
        }

        public override void ExtendSelection(TextPoint newEnd)
        {
            int selectionStart, selectionEnd = newEnd.CurrentPosition;

            // Now, figure out where the selection actually started
            if (IsEmpty)
                selectionStart = TextSelection.Start.Position;
            else
            {
                if (IsReversed)
                {
                    selectionStart = GetEndPoint().CurrentPosition;
                }
                else
                {
                    selectionStart = GetStartPoint().CurrentPosition;
                }
            }

            this.SelectRange(selectionStart, selectionEnd);
        }

        private void SelectRange(int selectionStart, int selectionEnd)
        {
            SnapshotPoint startPoint = new SnapshotPoint(TextView.AdvancedTextView.TextSnapshot, selectionStart);
            SnapshotPoint endPoint = new SnapshotPoint(TextView.AdvancedTextView.TextSnapshot, selectionEnd);

            TextSelection.Select(new VirtualSnapshotPoint(startPoint), new VirtualSnapshotPoint(endPoint));

            ITextViewLine textViewLine = TextView.AdvancedTextView.GetTextViewLineContainingBufferPosition(endPoint);
            PositionAffinity affinity = (textViewLine.IsLastTextViewLineForSnapshotLine || (endPoint != textViewLine.End)) ? PositionAffinity.Successor : PositionAffinity.Predecessor;

            Caret.MoveTo(endPoint, affinity);
            TextView.AdvancedTextView.ViewScroller.EnsureSpanVisible(TextSelection.StreamSelectionSpan.SnapshotSpan,
                                                                     (selectionStart <= selectionEnd) 
                                                                     ? EnsureSpanVisibleOptions.MinimumScroll
                                                                     : (EnsureSpanVisibleOptions.MinimumScroll | EnsureSpanVisibleOptions.ShowStart));
        }

        public override void Clear()
        {
            TextSelection.Clear();
        }

        public override ITextSelection AdvancedSelection
        {
            get { return TextSelection; }
        }

        public override bool IsEmpty
        {
            get { return TextSelection.IsEmpty; }
        }

        public override bool IsReversed
        {
            get { return TextSelection.IsReversed; }
            set 
            {
                if (value)
                {
                    SelectRange(TextRange.GetEndPoint().CurrentPosition, TextRange.GetStartPoint().CurrentPosition);
                }
                else
                {
                    SelectRange(TextRange.GetStartPoint().CurrentPosition, TextRange.GetEndPoint().CurrentPosition);
                }
            }
        }

        public override TextView TextView
        {
            get { return _textView; }
        }

        public override DisplayTextPoint GetDisplayStartPoint()
        {
            return TextRange.GetDisplayStartPoint();
        }

        public override DisplayTextPoint GetDisplayEndPoint()
        {
            return TextRange.GetDisplayEndPoint();
        }

        public override VisibilityState Visibility
        {
            get { return TextRange.Visibility; }
        }

        protected override DisplayTextRange CloneDisplayTextRangeInternal()
        {
            return TextRange.Clone();
        }

        protected override IEnumerator<DisplayTextPoint> GetDisplayPointEnumeratorInternal()
        {
            return TextRange.GetEnumerator();
        }

        public override TextPoint GetStartPoint()
        {
            return TextRange.GetStartPoint();
        }

        public override TextPoint GetEndPoint()
        {
            return TextRange.GetEndPoint();
        }

        public override TextBuffer TextBuffer
        {
            get { return TextView.TextBuffer; }
        }

        public override SnapshotSpan AdvancedTextRange
        {
            get { return TextRange.AdvancedTextRange; }
        }

        public override bool MakeUppercase()
        {
            // NOTE: We store this as a *Span* because we don't want it to track
            Span selectionSpan = TextRange.AdvancedTextRange;
            bool isReversed = IsReversed;
            if (!TextRange.MakeUppercase())
                return false;
            TextSelection.Select(new SnapshotSpan(_textView.AdvancedTextView.TextSnapshot, selectionSpan), isReversed);
            return true;
        }

        public override bool MakeLowercase()
        {
            // NOTE: We store this as a *Span* because we don't want it to track
            Span selectionSpan = TextRange.AdvancedTextRange;
            bool isReversed = IsReversed;
            if (!TextRange.MakeLowercase())
                return false;
            TextSelection.Select(new SnapshotSpan(_textView.AdvancedTextView.TextSnapshot, selectionSpan), isReversed);
            return true;
        }

        public override bool Capitalize()
        {
            // NOTE: We store this as a *Span* because we don't want it to track
            Span selectionSpan = TextRange.AdvancedTextRange;
            bool isReversed = IsReversed;
            bool isEmpty = IsEmpty;
            if (!TextRange.Capitalize())
                return false;

            if (!isEmpty)
            {
                TextSelection.Select(new SnapshotSpan(_textView.AdvancedTextView.TextSnapshot, selectionSpan), isReversed);
            }

            return true;
        }

        public override bool ToggleCase()
        {
            // NOTE: We store this as a *Span* because we don't want it to track
            Span selectionSpan = TextRange.AdvancedTextRange;
            bool isReversed = IsReversed;
            bool isEmpty = IsEmpty;
            if (!TextRange.ToggleCase())
                return false;

            if (!isEmpty)
            {
                TextSelection.Select(new SnapshotSpan(_textView.AdvancedTextView.TextSnapshot, selectionSpan), isReversed);
            }

            return true;
        }

        public override bool Delete()
        {
            foreach (var span in TextSelection.SelectedSpans)
            {
                DisplayTextRange selectedRange = TextView.GetTextRange(span.Start, span.End);
                if (!selectedRange.Delete())
                    return false;
            }

            return true;
        }

        public override bool Indent()
        {
            bool singleLineSelection = (GetStartPoint().LineNumber == GetEndPoint().LineNumber);
            bool entireLastLineSelected 
                = (GetStartPoint().CurrentPosition != GetEndPoint().CurrentPosition &&
                   GetStartPoint().CurrentPosition == TextBuffer.GetEndPoint().StartOfLine &&
                   GetEndPoint().CurrentPosition == TextBuffer.GetEndPoint().EndOfLine);
            
            if (singleLineSelection && !entireLastLineSelected)
            {
                TextPoint endPoint = GetEndPoint();
                if (!Delete())
                    return false;
                if (!endPoint.InsertIndent())
                    return false;
                TextView.AdvancedTextView.Caret.MoveTo(endPoint.AdvancedTextPoint);
            }
            else // indent the selected lines
            {
                VirtualSnapshotPoint oldStartPoint = TextSelection.Start;
                VirtualSnapshotPoint oldEndPoint = TextSelection.End;
                bool isReversed = TextSelection.IsReversed;

                ITextSnapshotLine startLine = AdvancedTextRange.Snapshot.GetLineFromPosition(oldStartPoint.Position);
                ITextSnapshotLine endLine = AdvancedTextRange.Snapshot.GetLineFromPosition(oldEndPoint.Position);

                // If the selection span initially starts at the whitespace at the beginning of the line in the startLine or 
                // ends at the whitespace at the beginning of the line in the endLine, restore selection and caret position,
                // *unless* the selection was in box mode.
                bool startAtStartLineWhitespace = oldStartPoint.Position <= _textView.GetTextPoint(startLine.Start).GetFirstNonWhiteSpaceCharacterOnLine().CurrentPosition;
                bool endAtEndLineWhitespace = oldEndPoint.Position < _textView.GetTextPoint(endLine.Start).GetFirstNonWhiteSpaceCharacterOnLine().CurrentPosition;
                bool isBoxSelection = AdvancedSelection.Mode == TextSelectionMode.Box;

                if (isBoxSelection)
                {
                    if (!this.BoxIndent())
                        return false;
                }
                else
                {
                    if (!TextRange.Indent())
                        return false;
                }

                // Computing the new selection and caret position
                VirtualSnapshotPoint newStartPoint = TextSelection.Start;
                VirtualSnapshotPoint newEndPoint = TextSelection.End;

                if (!isBoxSelection && (startAtStartLineWhitespace || endAtEndLineWhitespace))
                {
                    // After indent selection span should start at the start of startLine and end at the start of endLine
                    if (startAtStartLineWhitespace)
                    {
                        newStartPoint = new VirtualSnapshotPoint(AdvancedTextRange.Snapshot, oldStartPoint.Position.Position);
                    }

                    if (endAtEndLineWhitespace && oldEndPoint.Position.Position != endLine.Start && endLine.Length != 0)
                    {
                        int insertedTextSize = _editorOptions.IsConvertTabsToSpacesEnabled() ? _editorOptions.GetTabSize() : 1;
                        newEndPoint = new VirtualSnapshotPoint(AdvancedTextRange.Snapshot, newEndPoint.Position.Position - insertedTextSize);
                    }

                    if (!isReversed)
                        TextSelection.Select(newStartPoint, newEndPoint);
                    else
                        TextSelection.Select(newEndPoint, newStartPoint);

                    TextView.AdvancedTextView.Caret.MoveTo(TextSelection.ActivePoint, PositionAffinity.Successor);
                }
            }
            TextView.AdvancedTextView.Caret.EnsureVisible();
            return true;
        }

        /// <summary>
        /// Indent the given box selection
        /// </summary>
        /// <remarks>
        /// This is fairly close to the normal text range indenting logic, except that it also
        /// indents an empty selection at the endline, which the normal text range ignores.
        /// </remarks>
        private bool BoxIndent()
        {
            string textToInsert = _editorOptions.IsConvertTabsToSpacesEnabled() ? new string(' ', _editorOptions.GetTabSize()) : "\t";
            
            using (ITextEdit edit = TextBuffer.AdvancedTextBuffer.CreateEdit())
            {
                ITextSnapshot snapshot = TextBuffer.AdvancedTextBuffer.CurrentSnapshot;
                int startLineNumber = GetStartPoint().LineNumber;
                int endLineNumber = GetEndPoint().LineNumber;

                for (int i = startLineNumber; i <= endLineNumber; i++)
                {
                    ITextSnapshotLine line = snapshot.GetLineFromLineNumber(i);
                    if (line.Length > 0)
                    {
                        if (!edit.Insert(line.Start, textToInsert))
                            return false;
                    }
                }

                edit.Apply();

                if (edit.Canceled)
                    return false;
            }

            return true;
        }

        public override bool Unindent()
        {
            if (GetStartPoint().LineNumber != GetEndPoint().LineNumber &&
                AdvancedSelection.Mode == TextSelectionMode.Box)
            {
                if (!this.BoxUnindent())
                    return false;
            }
            else
            {
                if (!TextRange.Unindent())
                    return false;
            }

            TextView.AdvancedTextView.Caret.EnsureVisible();
            return true;
        }

        /// <summary>
        /// Unindent the given box selection
        /// </summary>
        /// <remarks>
        /// This is fairly close to the normal text range unindenting logic, except that it also
        /// unindents an empty selection at the endline, which the normal text range ignores.
        /// </remarks>
        private bool BoxUnindent()
        {
            using (ITextEdit edit = TextBuffer.AdvancedTextBuffer.CreateEdit())
            {
                ITextSnapshot snapshot = TextBuffer.AdvancedTextBuffer.CurrentSnapshot;
                int startLineNumber = GetStartPoint().LineNumber;
                int endLineNumber = GetEndPoint().LineNumber;

                for (int i = startLineNumber; i <= endLineNumber; i++)
                {
                    ITextSnapshotLine line = snapshot.GetLineFromLineNumber(i);
                    if (line.Length > 0)
                    {
                        if (snapshot[line.Start] == '\t')
                        {
                            if (!edit.Delete(new Span(line.Start, 1)))
                                return false;
                        }
                        else
                        {
                            int spacesToRemove = 0;
                            for (; (line.Start + spacesToRemove < snapshot.Length) && (spacesToRemove < _editorOptions.GetTabSize());
                                spacesToRemove++)
                            {
                                if (snapshot[line.Start + spacesToRemove] != ' ')
                                {
                                    break;
                                }
                            }

                            if (spacesToRemove > 0)
                            {
                                if (!edit.Delete(new Span(line.Start, spacesToRemove)))
                                    return false;
                            }
                        }
                    }
                }

                edit.Apply();

                if (edit.Canceled)
                    return false;
            }

            return true;
        }

        public override TextRange Find(string pattern)
        {
            return TextRange.Find(pattern);
        }

        public override TextRange Find(string pattern, FindOptions findOptions)
        {
            return TextRange.Find(pattern, findOptions);
        }

        public override Collection<TextRange> FindAll(string pattern)
        {
            return TextRange.FindAll(pattern);
        }

        public override Collection<TextRange> FindAll(string pattern, FindOptions findOptions)
        {
            return TextRange.FindAll(pattern, findOptions);
        }

        public override bool ReplaceText(string newText)
        {
            // We use *int* and *Span* here because we don't want them to track
            int startPosition = TextRange.GetDisplayStartPoint().CurrentPosition;
            Span newSelectionSpan = new Span(startPosition, newText.Length);
            bool isReversed = IsReversed;
            if (!TextRange.ReplaceText(newText))
                return false;
            TextSelection.Select(new SnapshotSpan(_textView.AdvancedTextView.TextSnapshot, newSelectionSpan), isReversed);
            return true;
        }

        public override string GetText()
        {
            return TextRange.GetText();
        }

        public override void SetStart(TextPoint startPoint)
        {
            this.SelectRange(startPoint.CurrentPosition, GetDisplayEndPoint().CurrentPosition);
        }

        public override void SetEnd(TextPoint endPoint)
        {
            this.ExtendSelection(endPoint);
        }

        public override void MoveTo(TextRange newRange)
        {
            this.SelectRange(newRange);
        }

        protected override IEnumerator<TextPoint> GetEnumeratorInternal()
        {
            return ((TextRange)TextRange).GetEnumerator();
        }
    }
}