-
Notifications
You must be signed in to change notification settings - Fork 9
/
CBETagger.cs
714 lines (624 loc) · 27.2 KB
/
CBETagger.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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
// Thanks to https://github.com/jaredpar/ControlCharAdornmentSample/blob/master/CharDisplayTaggerSource.cs
using CodeBlockEndTag.Shell;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Operations;
using Microsoft.VisualStudio.Text.Tagging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
namespace CodeBlockEndTag
{
/// <summary>f
/// This tagger provides editor tags that are inserted into the TextView (IntraTextAdornmentTags)
/// The tags are added after each code block encapsulated by curly bracets: { ... }
/// The tags will show the code blocks condition, or whatever serves as header for the block
/// By clicking on a tag, the editor will jump to that code blocks header
/// </summary>
internal class CBETagger : ITagger<IntraTextAdornmentTag>, IDisposable
{
private static readonly ReadOnlyCollection<ITagSpan<IntraTextAdornmentTag>> EmptyTagColllection =
new(new List<ITagSpan<IntraTextAdornmentTag>>());
#region Properties & Fields
// EventHandler for ITagger<IntraTextAdornmentTag> tags changed event
private EventHandler<SnapshotSpanEventArgs> _changedEvent;
/// <summary>
/// Service by VisualStudio for fast navigation in structured texts
/// </summary>
private readonly ITextStructureNavigator _TextStructureNavigator;
/// <summary>
/// The TextView this tagger is assigned to
/// </summary>
private readonly IWpfTextView _TextView;
/// <summary>
/// This is a list of already created adornment tags used as cache
/// </summary>
private readonly List<CBAdornmentData> _adornmentCache = new();
/// <summary>
/// This is the visible span of the textview
/// </summary>
private Span? _VisibleSpan;
/// <summary>
/// Is set, when the instance is disposed
/// </summary>
private bool _Disposed;
#endregion
#region Ctor
/// <summary>
/// Creates a new instance of CBRTagger
/// </summary>
/// <param name="provider">the CBETaggerProvider that created the tagger</param>
/// <param name="textView">the WpfTextView this tagger is assigned to</param>
/// <param name="sourceBuffer">the TextBuffer this tagger should work with</param>
internal CBETagger(CBETaggerProvider provider, IWpfTextView textView)
{
if (provider == null || textView == null)
throw new ArgumentNullException("The arguments of CBETagger can't be null");
_TextView = textView;
// Getting services provided by VisualStudio
_TextStructureNavigator = provider.GetTextStructureNavigator(_TextView.TextBuffer);
// Hook up events
_TextView.TextBuffer.Changed += TextBuffer_Changed;
_TextView.LayoutChanged += OnTextViewLayoutChanged;
_TextView.Caret.PositionChanged += Caret_PositionChanged;
// Listen for package events
InitializePackage();
}
#endregion
#region TextBuffer changed
private void Caret_PositionChanged(object sender, CaretPositionChangedEventArgs e)
{
var start = Math.Min(e.OldPosition.BufferPosition.Position, e.NewPosition.BufferPosition.Position);
var end = Math.Max(e.OldPosition.BufferPosition.Position, e.NewPosition.BufferPosition.Position);
if (start != end)
{
InvalidateSpan(new Span(start, end - start), false);
}
}
private void TextBuffer_Changed(object sender, TextContentChangedEventArgs e)
{
foreach (var textChange in e.Changes)
{
OnTextChanged(textChange);
}
}
private void OnTextChanged(ITextChange textChange)
{
// remove or update tags in adornment cache
List<CBAdornmentData> remove = new();
foreach (var adornment in _adornmentCache)
{
if (!(adornment.HeaderStartPosition > textChange.OldEnd || adornment.EndPosition < textChange.OldPosition))
{
remove.Add(adornment);
}
else if (adornment.HeaderStartPosition > textChange.OldEnd)
{
adornment.HeaderStartPosition += textChange.Delta;
adornment.StartPosition += textChange.Delta;
adornment.EndPosition += textChange.Delta;
}
}
foreach (var adornment in remove)
{
RemoveFromCache(adornment);
}
}
private void RemoveFromCache(CBAdornmentData adornment)
{
if (adornment.Adornment is CBETagControl tag)
{
tag.TagClicked -= Adornment_TagClicked;
}
_adornmentCache.Remove(adornment);
}
#endregion
#region ITagger<IntraTextAdornmentTag>
IEnumerable<ITagSpan<IntraTextAdornmentTag>> ITagger<IntraTextAdornmentTag>.GetTags(NormalizedSnapshotSpanCollection spans)
{
// Check if content type (language) is supported and active for tagging
if (CBETagPackage.IsLanguageSupported(_TextView.TextBuffer.ContentType.TypeName))
{
// Second chance to hook up events
InitializePackage();
foreach (var span in spans)
{
var tags = GetTags(span);
foreach (var tag in tags)
{
yield return tag;
}
}
}
}
event EventHandler<SnapshotSpanEventArgs> ITagger<IntraTextAdornmentTag>.TagsChanged
{
add { _changedEvent += value; }
remove { _changedEvent -= value; }
}
#endregion
#region Tag placement
internal ReadOnlyCollection<ITagSpan<IntraTextAdornmentTag>> GetTags(SnapshotSpan span)
{
if (!CBETagPackage.CBETaggerEnabled ||
span.Snapshot != _TextView.TextBuffer.CurrentSnapshot ||
span.Length == 0)
{
return EmptyTagColllection;
}
// if big span, return only tags for visible area
if (span.Length > 1000 && _VisibleSpan != null && _VisibleSpan.HasValue)
{
var overlap = span.Overlap(_VisibleSpan.Value);
if (overlap != null && overlap.HasValue)
{
span = overlap.Value;
if (span.Length == 0)
return EmptyTagColllection;
}
}
return GetTagsCore(span);
}
#if DEBUG
private System.Diagnostics.Stopwatch _watch;
#endif
private ReadOnlyCollection<ITagSpan<IntraTextAdornmentTag>> GetTagsCore(SnapshotSpan span)
{
var list = new List<ITagSpan<IntraTextAdornmentTag>>();
var offset = span.Start.Position;
var snapshot = span.Snapshot;
// vars used in loop
SnapshotSpan cbSpan;
CBAdornmentData cbAdornmentData;
CBETagControl tagElement;
int cbStartPosition;
int cbEndPosition;
int cbHeaderPosition;
string cbHeader;
IntraTextAdornmentTag cbTag;
SnapshotSpan cbSnapshotSpan;
TagSpan<IntraTextAdornmentTag> cbTagSpan;
bool isSingleLineComment = false;
bool isMultiLineComment = false;
#if DEBUG
// Stop time
_watch ??= new System.Diagnostics.Stopwatch();
_watch.Restart();
#endif
try
{
// Find all closing bracets
for (int i = 0; i < span.Length; i++)
{
var position = i + offset;
var chr = snapshot[position];
// Skip comments
switch (chr)
{
case '/':
if (position > 0)
{
if (snapshot[position - 1] == '/')
isSingleLineComment = true;
if (snapshot[position - 1] == '*')
{
if (!isMultiLineComment)
{
// Multiline comment was not started in this span
// Every tag until now was inside a comment
foreach (var tag in list)
{
RemoveFromCache((tag.Tag.Adornment as CBETagControl)?.AdornmentData);
}
list.Clear();
}
isMultiLineComment = false;
}
}
break;
case '*':
if (position > 0 && snapshot[position - 1] == '/')
isMultiLineComment = true;
break;
case (char)10:
isSingleLineComment = false;
break;
case (char)13:
isSingleLineComment = false;
break;
}
if (chr != '}' || isSingleLineComment || isMultiLineComment)
continue;
// getting start and end position of code block
cbEndPosition = position;
if (position >= 0 && snapshot[position - 1] == '{')
{
// empty code block {}
cbStartPosition = position - 1;
cbSpan = new SnapshotSpan(snapshot, cbStartPosition, cbEndPosition - cbStartPosition);
}
else
{
// create inner span to navigate to get code block start
cbSpan = _TextStructureNavigator.GetSpanOfEnclosing(new SnapshotSpan(snapshot, position - 1, 1));
cbStartPosition = cbSpan.Start;
}
// Don't display tag for code blocks on same line
if (!snapshot.GetText(cbSpan).Contains('\n'))
continue;
// getting the code blocks header
cbHeaderPosition = -1;
if (snapshot[cbStartPosition] == '{')
{
// cbSpan does not contain the header
cbHeader = GetCodeBlockHeader(cbSpan, out cbHeaderPosition);
}
else
{
// cbSpan does contain the header
cbHeader = GetCodeBlockHeader(cbSpan, out cbHeaderPosition, position);
}
// Trim header
if (!string.IsNullOrEmpty(cbHeader))
{
cbHeader = cbHeader.Trim()
.Replace(Environment.NewLine, "")
.Replace('\t', ' ');
// Strip unnecessary spaces
while (cbHeader.Contains(" "))
{
cbHeader = cbHeader.Replace(" ", " ");
}
}
// Skip tag if option "only when header not visible"
if (_VisibleSpan != null && !IsTagVisible(cbHeaderPosition, cbEndPosition, _VisibleSpan, snapshot))
{
continue;
}
var iconMoniker = Microsoft.VisualStudio.Imaging.KnownMonikers.QuestionMark;
if (CBETagPackage.CBEDisplayMode != (int)CBEOptionPage.DisplayModes.Text &&
!string.IsNullOrWhiteSpace(cbHeader) && !cbHeader.Contains("{"))
{
iconMoniker = IconMonikerSelector.SelectMoniker(cbHeader);
}
// use cache or create new tag
cbAdornmentData = _adornmentCache
.Find(o =>
o.StartPosition == cbStartPosition &&
o.EndPosition == cbEndPosition);
if (cbAdornmentData?.Adornment != null)
{
tagElement = cbAdornmentData.Adornment as CBETagControl;
}
else
{
// create new adornment
tagElement = new CBETagControl()
{
Text = cbHeader,
IconMoniker = iconMoniker,
DisplayMode = CBETagPackage.CBEDisplayMode
};
tagElement.TagClicked += Adornment_TagClicked;
cbAdornmentData = new CBAdornmentData(cbStartPosition, cbEndPosition, cbHeaderPosition, tagElement);
tagElement.AdornmentData = cbAdornmentData;
_adornmentCache.Add(cbAdornmentData);
}
tagElement.SetResourceReference(CBETagControl.LineHeightProperty, EndTagColors.FontSizeKey);
tagElement.SetResourceReference(CBETagControl.TextColorProperty, EndTagColors.GetForegroundResourceKey(_TextView.TextBuffer.ContentType.TypeName));
// Add new tag to list
cbTag = new IntraTextAdornmentTag(tagElement, null);
cbSnapshotSpan = new SnapshotSpan(snapshot, position + 1, 0);
cbTagSpan = new TagSpan<IntraTextAdornmentTag>(cbSnapshotSpan, cbTag);
list.Add(cbTagSpan);
}
}
catch (NullReferenceException)
{
// May happen, when closing a text editor
}
#if DEBUG
_watch.Stop();
if (_watch.Elapsed.Milliseconds > 100)
{
System.Diagnostics.Debug.WriteLine("Time elapsed: " + _watch.Elapsed +
" on Thread: " + System.Threading.Thread.CurrentThread.ManagedThreadId +
" in Span: " + span.Start.Position + ":" + span.End.Position + " length: " + span.Length);
}
#endif
return new ReadOnlyCollection<ITagSpan<IntraTextAdornmentTag>>(list);
}
/// <summary>
/// Capture the header of a code block
/// Returns the text and outputs the start position within the snapshot
/// </summary>
private string GetCodeBlockHeader(SnapshotSpan cbSpan, out int headerStart, int maxEndPosition = 0)
{
if (maxEndPosition == 0)
{
maxEndPosition = cbSpan.Start;
}
var snapshot = cbSpan.Snapshot;
var currentSpan = cbSpan;
// set end of header to first start of code block {
for (int i = cbSpan.Start; i < cbSpan.End; i++)
{
if (snapshot[i] == '{')
{
maxEndPosition = i;
break;
}
}
Span headerSpan, headerSpan2;
string headerText, headerText2;
int loops = 0;
// check all enclosing spans until the header is complete
do
{
// abort if in endless loop
if (loops++ > 10)
break;
// get text of current span
headerStart = currentSpan.Start;
headerSpan = new Span(headerStart, Math.Min(maxEndPosition, currentSpan.Span.End) - headerStart);
if (headerSpan.Length == 0)
continue;
headerText = snapshot.GetText(headerSpan);
// found header if it begins with a letter or contains a lambda
if (!string.IsNullOrWhiteSpace(headerText))
//&& (char.IsLetter(headerText[0]) || headerText[0]=='[' || headerText.Contains("=>")))
{
// recognize "else if" too
if (headerText.StartsWith("if") && ((currentSpan = _TextStructureNavigator.GetSpanOfEnclosing(currentSpan)) != default))
{
// check what comes before the "if"
headerSpan2 = new Span(currentSpan.Start, Math.Min(maxEndPosition, currentSpan.Span.End) - currentSpan.Start);
headerText2 = snapshot.GetText(headerSpan2);
if (headerText2.StartsWith("else"))
{
headerStart = headerSpan2.Start;
headerText = headerText2;
}
}
else if (headerText.Contains('\r') || headerText.Contains('\n'))
{
// skip annotations
headerText = headerText.Replace('\r', '\n').Replace("\n\n", "\n");
string[] headerLines = headerText.Split('\n');
bool annotaions = true;
int openBracets = 0;
headerText = string.Empty;
foreach (var line in headerLines)
{
if (!string.IsNullOrWhiteSpace(line))
{
var trimmedline = line.Trim();
if (annotaions && (trimmedline[0] == '[' || openBracets > 0))
{
openBracets += trimmedline.Count(c => c == '[');
openBracets -= trimmedline.Count(c => c == ']');
continue;
}
annotaions = false;
if (!string.IsNullOrWhiteSpace(headerText))
headerText += Environment.NewLine;
headerText += trimmedline;
}
}
}
return headerText;
}
// get next enclosing span of current span
} while ((currentSpan = _TextStructureNavigator.GetSpanOfEnclosing(currentSpan)) != default);
// No header found
headerStart = -1;
return null;
}
#endregion
#region Tag Clicked Handler
/// <summary>
/// Handles the click event on a tag
/// </summary>
private void Adornment_TagClicked(CBAdornmentData adornment, bool jumpToHead)
{
if (_TextView != null)
{
SnapshotPoint targetPoint;
if (jumpToHead)
{
// Jump to header
targetPoint = new SnapshotPoint(_TextView.TextBuffer.CurrentSnapshot, adornment.HeaderStartPosition);
_TextView.DisplayTextLineContainingBufferPosition(targetPoint, 30, ViewRelativePosition.Top);
}
else
{
// Set caret behind closing bracet
targetPoint = new SnapshotPoint(_TextView.TextBuffer.CurrentSnapshot, adornment.EndPosition + 1);
}
_TextView.Caret.MoveTo(targetPoint);
}
}
#endregion
#region Options changed
/// <summary>
/// Handles the event when any package option is changed
/// </summary>
private void OnPackageOptionChanged(object sender)
{
int start = Math.Max(0, (_VisibleSpan?.Start) ?? 0);
int end = Math.Max(1, _VisibleSpan.HasValue ? _VisibleSpan.Value.End : 1);
InvalidateSpan(new Span(start, end - start));
}
/// <summary>
/// Invalidates all cached tags within or after the given span
/// </summary>
private void InvalidateSpan(Span invalidateSpan, bool clearCache = true)
{
// Remove tags from cache
if (clearCache)
{
_adornmentCache
.Where(a => a.HeaderStartPosition >= invalidateSpan.Start || a.EndPosition >= invalidateSpan.Start)
.ToList().ForEach(a => RemoveFromCache(a));
}
// Invalidate span
if (invalidateSpan.End <= _TextView.TextBuffer.CurrentSnapshot.Length)
{
_changedEvent?.Invoke(this, new SnapshotSpanEventArgs(
new SnapshotSpan(_TextView.TextBuffer.CurrentSnapshot, invalidateSpan)));
}
}
/// <summary>
/// Hooks up events at the package
/// Due to AsyncPackage usage, the Tagger may be initialized before the Package
/// So be safe about this
/// </summary>
private void InitializePackage()
{
if (isPackageInitialized || CBETagPackage.Instance == null || _Disposed) return;
CBETagPackage.Instance.PackageOptionChanged += OnPackageOptionChanged;
FontAndColorDefaultsCSharpTags.Instance.EnsureFontAndColorsInitialized();
isPackageInitialized = true;
}
private bool isPackageInitialized;
#endregion
#region IDisposable
/// <summary>
/// Clean up all events and references
/// </summary>
private void Dispose(bool disposing)
{
if (_Disposed)
{
return;
}
if (disposing)
{
if (CBETagPackage.Instance != null)
{
CBETagPackage.Instance.PackageOptionChanged -= OnPackageOptionChanged;
}
if (_TextView != null)
{
_TextView.LayoutChanged -= OnTextViewLayoutChanged;
if (_TextView.TextBuffer != null)
{
_TextView.TextBuffer.Changed -= TextBuffer_Changed;
}
}
_Disposed = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
#region visibility of tags
/// <summary>
/// Checks if a tag's header is visible
/// </summary>
/// <param name="start">Start position of code block</param>
/// <param name="end">End position of code block</param>
/// <param name="visibleSpan">the visible span in the textview</param>
/// <param name="snapshot">reference to text snapshot. Used for caret check</param>
/// <returns>true if the tag is visible (or if all tags are shown)</returns>
private bool IsTagVisible(int start, int end, Span? visibleSpan, ITextSnapshot snapshot)
{
bool isVisible = false;
// Check general condition
if (CBETagPackage.CBEVisibilityMode == (int)CBEOptionPage.VisibilityModes.Always
|| visibleSpan == null || !visibleSpan.HasValue)
{
isVisible = true;
}
// Check visible span
if (!isVisible)
{
var val = visibleSpan.Value;
isVisible = start < val.Start && end >= val.Start && end <= val.End;
}
// Check if caret is in this line
if (isVisible && _TextView != null)
{
var caretIndex = _TextView.Caret.Position.BufferPosition.Position;
var lineStart = Math.Min(caretIndex, end);
var lineEnd = Math.Max(caretIndex, end);
if (lineStart == lineEnd)
{
isVisible = false;
}
else if (lineStart >= 0 && lineEnd <= snapshot.Length)
{
string line = snapshot.GetText(lineStart, lineEnd - lineStart);
if (!line.Contains('\n'))
isVisible = false;
}
}
return isVisible;
}
/// <summary>
/// Returns the visible span for the given textview
/// </summary>
private Span? GetVisibleSpan(ITextView textView)
{
if (textView?.TextViewLines != null && textView.TextViewLines.Count > 2)
{
// Index 0 not yet visible
var firstVisibleLine = textView.TextViewLines[1];
// Last index not visible, too
var lastVisibleLine = textView.TextViewLines[textView.TextViewLines.Count - 2];
return new Span(firstVisibleLine.Start, lastVisibleLine.End - firstVisibleLine.Start);
}
return null;
}
#endregion
#region TextView scrolling
private void OnTextViewLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
{
// get new visible span
var visibleSpan = GetVisibleSpan(_TextView);
if (visibleSpan == null || !visibleSpan.HasValue)
return;
// only if new visible span is different from old
if (!_VisibleSpan.HasValue ||
_VisibleSpan.Value.Start != visibleSpan.Value.Start ||
_VisibleSpan.Value.End < visibleSpan.Value.End)
{
// invalidate new and/or old visible span
List<Span> invalidSpans = new();
var newSpan = visibleSpan.Value;
if (!_VisibleSpan.HasValue)
{
invalidSpans.Add(newSpan);
}
else
{
var oldSpan = _VisibleSpan.Value;
// invalidate two spans if old and new do not overlap
if (newSpan.Start > oldSpan.End || newSpan.End < oldSpan.Start)
{
invalidSpans.Add(newSpan);
invalidSpans.Add(oldSpan);
}
else
{
// invalidate one big span (old and new joined)
invalidSpans.Add(newSpan.Join(oldSpan));
}
}
_VisibleSpan = visibleSpan;
// refresh tags
foreach (var span in invalidSpans)
{
if (CBETagPackage.CBEVisibilityMode != (int)CBEOptionPage.VisibilityModes.Always)
InvalidateSpan(span, false);
}
}
}
#endregion
}
}