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

DefaultDisplayTextPointPrimitive.cs « EditorPrimitives « Impl « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8ca865a136020c6695347762eccfc59ec2bee2b0 (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
497
498
499
//
//  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;
    using System.Collections.ObjectModel;
    using System.Globalization;

    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;
    using System.Diagnostics;

    internal sealed class DefaultDisplayTextPointPrimitive : DisplayTextPoint
    {
        private TextView _textView;
        private TextPoint _bufferPoint;
        private IEditorOptions _editorOptions;

        internal DefaultDisplayTextPointPrimitive(TextView textView, int position, IEditorOptions editorOptions)
        {
            _textView = textView;
            _editorOptions = editorOptions;

            _bufferPoint = _textView.TextBuffer.GetStartPoint();

            // The position coming in will be from the view's snapshot which may not be the same snapshot as the buffer.
            // Force the position to track to the buffer's snapshot to prevent the position from being valid in the view's
            // snapshot but invalid in the buffers
            ITrackingPoint trackingPoint = _textView.AdvancedTextView.TextSnapshot.CreateTrackingPoint(position, PointTrackingMode.Positive);

            MoveTo(trackingPoint.GetPosition(_textView.TextBuffer.AdvancedTextBuffer.CurrentSnapshot));
        }

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

        public override ITextViewLine AdvancedTextViewLine
        {
            get 
            {
                return _textView.AdvancedTextView.GetTextViewLineContainingBufferPosition(this.AdvancedTextPoint);
            }
        }

        public override int DisplayColumn
        {
            get
            {
                ITextView textView = _textView.AdvancedTextView;
                SnapshotPoint position = this.AdvancedTextViewLine.Start;

                return PrimitivesUtilities.GetColumnOfPoint(
                    textView.TextSnapshot, 
                    AdvancedTextPoint, 
                    position, 
                    _editorOptions.GetTabSize(), 
                    (p) => (textView.GetTextElementSpan(p).End));
            }
        }

        public override bool IsVisible
        {
            get
            {
                // if no lines are being drawn then this point can't be visible
                return (this.AdvancedTextViewLine.VisibilityState == VisibilityState.FullyVisible);
            }
        }

        public override DisplayTextRange GetDisplayTextRange(DisplayTextPoint otherPoint)
        {
            if (!object.ReferenceEquals(this.TextBuffer, otherPoint.TextBuffer))
            {
                throw new ArgumentException("The other point must have the same TextBuffer as this one", nameof(otherPoint));
            }
            return TextView.GetTextRange(this, otherPoint);
        }

        public override DisplayTextRange GetDisplayTextRange(int otherPosition)
        {
            return TextView.GetTextRange(CurrentPosition, otherPosition);
        }

        protected override DisplayTextPoint CloneDisplayTextPointInternal()
        {
            return new DefaultDisplayTextPointPrimitive(_textView, CurrentPosition, _editorOptions);
        }

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

        public override int CurrentPosition
        {
            get { return _bufferPoint.CurrentPosition; }
        }

        public override int Column
        {
            get { return _bufferPoint.Column; }
        }

        public override bool DeleteNext()
        {
            if (_textView.AdvancedTextView.TextViewModel.IsPointInVisualBuffer(AdvancedTextPoint, PositionAffinity.Successor))
            {
                return PrimitivesUtilities.Delete(GetNextTextElementSpan());
            }
            else
            {
                return _bufferPoint.DeleteNext();
            }
        }

        public override bool DeletePrevious()
        {
            SnapshotSpan previousElementSpan = GetPreviousTextElementSpan();

            if ((previousElementSpan.Length > 0) &&
                (_textView.AdvancedTextView.TextViewModel.IsPointInVisualBuffer(AdvancedTextPoint, PositionAffinity.Successor)) &&
                (!_textView.AdvancedTextView.TextViewModel.IsPointInVisualBuffer(previousElementSpan.End - 1, PositionAffinity.Successor)))
            {
                // Since the previous character is not visible but the current one is, delete 
                // the entire previous text element span.
                return PrimitivesUtilities.Delete(previousElementSpan);
            }
            else
            {
                // Delegate to the buffer point's DeletePrevious implementation to handle deleting single
                // characters.  A single character should be deleted if this point and the previous one
                // are both visible or both not visible.
                return _bufferPoint.DeletePrevious();
            }
        }

        public override TextRange GetCurrentWord()
        {
            return _bufferPoint.GetCurrentWord();
        }

        public override TextRange GetNextWord()
        {
            return _bufferPoint.GetNextWord();
        }

        public override TextRange GetPreviousWord()
        {
            return _bufferPoint.GetPreviousWord();
        }

        public override TextRange GetTextRange(TextPoint otherPoint)
        {
            return _bufferPoint.GetTextRange(otherPoint);
        }

        public override TextRange GetTextRange(int otherPosition)
        {
            return _bufferPoint.GetTextRange(otherPosition);
        }

        public override bool InsertNewLine()
        {
            return _bufferPoint.InsertNewLine();
        }

        public override bool InsertIndent()
        {
            return _bufferPoint.InsertIndent();
        }

        public override bool InsertText(string text)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            return _bufferPoint.InsertText(text);
        }

        public override int LineNumber
        {
            get { return _bufferPoint.LineNumber; }
        }

        public override int StartOfLine
        {
            get { return _bufferPoint.StartOfLine; }
        }

        public override int EndOfLine
        {
            get { return _bufferPoint.EndOfLine; }
        }

        public override int StartOfViewLine
        {
            get
            {
                return this.AdvancedTextViewLine.Start;
            }
        }

        public override int EndOfViewLine
        {
            get
            {
                return this.AdvancedTextViewLine.End; 
            }
        }

        public override bool RemovePreviousIndent()
        {
            return _bufferPoint.RemovePreviousIndent();
        }

        public override bool TransposeCharacter()
        {
            SnapshotPoint insertionPoint = AdvancedTextPoint;

            ITextSnapshotLine line = _textView.AdvancedTextView.TextSnapshot.GetLineFromPosition(insertionPoint);

            string lineText = line.GetText();

            if (StringInfo.ParseCombiningCharacters(lineText).Length < 2)
            {
                return true;
            }

            SnapshotSpan textElementLeftSpan, textElementRightSpan;

            // We're at the start of a line
            if (insertionPoint == line.Start)
            {
                textElementLeftSpan = TextView.AdvancedTextView.GetTextElementSpan(insertionPoint);
                textElementRightSpan = TextView.AdvancedTextView.GetTextElementSpan(textElementLeftSpan.End);
            }
            // We're at the end of a line
            else if (insertionPoint == line.End)
            {
                textElementRightSpan = TextView.AdvancedTextView.GetTextElementSpan(insertionPoint - 1);
                textElementLeftSpan = TextView.AdvancedTextView.GetTextElementSpan(textElementRightSpan.Start - 1);
            }
            // We're at the middle of a line
            else
            {
                textElementRightSpan = TextView.AdvancedTextView.GetTextElementSpan(insertionPoint);
                textElementLeftSpan = TextView.AdvancedTextView.GetTextElementSpan(textElementRightSpan.Start - 1);
            }

            string transposedText = _textView.AdvancedTextView.TextSnapshot.GetText(textElementRightSpan)
                + _textView.AdvancedTextView.TextSnapshot.GetText(textElementLeftSpan);

            return PrimitivesUtilities.Replace(TextBuffer.AdvancedTextBuffer, new Span(textElementLeftSpan.Start, transposedText.Length), transposedText);
        }

        public override bool TransposeLine()
        {
            return _bufferPoint.TransposeLine();
        }

        public override bool TransposeLine(int lineNumber)
        {
            return _bufferPoint.TransposeLine(lineNumber);
        }

        public override SnapshotPoint AdvancedTextPoint
        {
            // TODO!: Should this not translate (and throw instead)?  Should it be positive or negative?
            // Use positive tracking to behave like the caret would.
            get 
            {
                Debug.Assert(_bufferPoint.AdvancedTextPoint.Snapshot == TextView.AdvancedTextView.TextSnapshot,
                             "WARNING: We are tracking a SnapshotPoint in a display primitive, which could have bad consequences.");
                             
                return _bufferPoint.AdvancedTextPoint.TranslateTo(TextView.AdvancedTextView.TextSnapshot,
                                                                  PointTrackingMode.Positive); 
            }
        }

        public override string GetNextCharacter()
        {
            if (CurrentPosition == _textView.AdvancedTextView.TextSnapshot.Length)
            {
                return string.Empty;
            }
            return _textView.AdvancedTextView.TextSnapshot.GetText(TextView.AdvancedTextView.GetTextElementSpan(AdvancedTextPoint));
        }

        public override string GetPreviousCharacter()
        {
            if (CurrentPosition == 0)
            {
                return string.Empty;
            }
            return _textView.AdvancedTextView.TextSnapshot.GetText(GetPreviousTextElementSpan());
        }

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

        public override TextRange Find(string pattern, TextPoint endPoint)
        {
            return _bufferPoint.Find(pattern, endPoint);
        }

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

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

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

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

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

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

        public override void MoveTo(int position)
        {
            SnapshotPoint point = new SnapshotPoint(TextView.AdvancedTextView.TextSnapshot, position);

            _bufferPoint.MoveTo(TextView.AdvancedTextView.GetTextElementSpan(point).Start);
        }

        public override void MoveToNextCharacter()
        {
            int currentPosition = this.CurrentPosition;

            MoveTo(GetNextTextElementSpan().End);

            // It's possible that the above code didn't actually move this point.  Dev11 #109752 shows how
            // this can happen, which is that GetNextTextElementSpan() above (which calls into WPF) may say
            // that the current character doesn't combine with anything, but the _bufferPoint (which calls into
            // framework methods that follow the unicode standard) will say that the character does combine.  In that case
            // the GetNextTextElementSpan().End isn't far enough away for this point to escape the combining character
            // sequence, and the point is left where it is, at the start of a base character.
            if (currentPosition == this.CurrentPosition &&
                currentPosition != this.AdvancedTextPoint.Snapshot.Length)
            {
                _bufferPoint.MoveToNextCharacter();

                Debug.Assert(currentPosition != this.CurrentPosition,
                             "DisplayTextPoint.MoveToNextCharacter was unable to successfully move forward. This may cause unexpected behavior or hangs.");
            }
        }

        public override void MoveToPreviousCharacter()
        {
            MoveTo(GetPreviousTextElementSpan().Start);
        }

        public override void MoveToLine(int lineNumber)
        {
            _bufferPoint.MoveToLine(lineNumber);
        }

        public override void MoveToEndOfLine()
        {
            _bufferPoint.MoveToEndOfLine();
        }

        public override void MoveToStartOfLine()
        {
            _bufferPoint.MoveToStartOfLine();
        }

        public override void MoveToEndOfViewLine()
        {
            MoveTo(this.EndOfViewLine);
        }

        public override void MoveToStartOfViewLine()
        {
            MoveTo(this.StartOfViewLine);
        }

        public override void MoveToEndOfDocument()
        {
            _bufferPoint.MoveToEndOfDocument();
        }

        public override void MoveToStartOfDocument()
        {
            _bufferPoint.MoveToStartOfDocument();
        }

        public override void MoveToBeginningOfNextLine()
        {
            _bufferPoint.MoveToBeginningOfNextLine();
        }

        public override void MoveToBeginningOfPreviousLine()
        {
            _bufferPoint.MoveToBeginningOfPreviousLine();
        }

        public override void MoveToBeginningOfNextViewLine()
        {
            this.MoveTo(this.AdvancedTextViewLine.EndIncludingLineBreak);
        }

        public override void MoveToBeginningOfPreviousViewLine()
        {
            ITextViewLine line = this.AdvancedTextViewLine;
            if (line.Start > 0)
                line = _textView.AdvancedTextView.GetTextViewLineContainingBufferPosition(line.Start - 1);

            this.MoveTo(line.Start);
        }

        public override void MoveToNextWord()
        {
            _bufferPoint.MoveToNextWord();

            // make sure the word structure navigator didn't return a position in the middle of a view element
            SnapshotPoint bufferPoint = _bufferPoint.AdvancedTextPoint;
            SnapshotSpan textElementSpan = _textView.AdvancedTextView.GetTextElementSpan(bufferPoint);
            if (bufferPoint > textElementSpan.Start)
                this.MoveTo(textElementSpan.End);
        }

        public override void MoveToPreviousWord()
        {
            _bufferPoint.MoveToPreviousWord();

            // make sure the word structure navigator didn't return a position in the middle of a view element
            this.MoveTo(_bufferPoint.AdvancedTextPoint);
        }

        public override DisplayTextPoint GetFirstNonWhiteSpaceCharacterOnViewLine()
        {
            ITextViewLine viewLine = _textView.AdvancedTextView.GetTextViewLineContainingBufferPosition(AdvancedTextPoint);
            ITextSnapshot snapshot = viewLine.Extent.Snapshot;

            int firstNonWhitespaceCharacter = viewLine.Start;
            while ((firstNonWhitespaceCharacter < viewLine.End) &&
                    char.IsWhiteSpace(snapshot[firstNonWhitespaceCharacter]))
            {
                firstNonWhitespaceCharacter++;
            }

            return new DefaultDisplayTextPointPrimitive(_textView, firstNonWhitespaceCharacter, _editorOptions);
        }

        public override TextPoint GetFirstNonWhiteSpaceCharacterOnLine()
        {
            return _bufferPoint.GetFirstNonWhiteSpaceCharacterOnLine();
        }

        #region Private helpers
        private SnapshotSpan GetNextTextElementSpan()
        {
            return TextView.AdvancedTextView.GetTextElementSpan(AdvancedTextPoint);
        }

        private SnapshotSpan GetPreviousTextElementSpan()
        {
            if (CurrentPosition == 0)
            {
                return new SnapshotSpan(AdvancedTextPoint, 0);
            }
            return TextView.AdvancedTextView.GetTextElementSpan(AdvancedTextPoint - 1);
        }
        #endregion
    }
}