forked from NoahRic/GoToDef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassifier.cs
139 lines (113 loc) · 4.64 KB
/
Classifier.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
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Windows.Media;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Utilities;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Tagging;
namespace GoToDef
{
#region Classification type/format exports
[Export(typeof(EditorFormatDefinition))]
[ClassificationType(ClassificationTypeNames = "UnderlineClassification")]
[Name("UnderlineClassificationFormat")]
[UserVisible(true)]
[Order(After = Priority.High)]
internal sealed class UnderlineFormatDefinition : ClassificationFormatDefinition
{
public UnderlineFormatDefinition()
{
this.DisplayName = "Underline";
this.TextDecorations = System.Windows.TextDecorations.Underline;
this.ForegroundColor = Colors.Blue;
}
}
#endregion
#region Provider definition
[Export(typeof(IViewTaggerProvider))]
[ContentType("text")]
[TagType(typeof(ClassificationTag))]
internal class UnderlineClassifierProvider : IViewTaggerProvider
{
[Import]
internal IClassificationTypeRegistryService ClassificationRegistry = null;
[Export(typeof(ClassificationTypeDefinition))]
[Name("UnderlineClassification")]
internal static ClassificationTypeDefinition underlineClassificationType = null;
static IClassificationType UnderlineClassification;
public static UnderlineClassifier GetClassifierForView(ITextView view)
{
if (UnderlineClassification == null)
return null;
return view.Properties.GetOrCreateSingletonProperty(() => new UnderlineClassifier(view, UnderlineClassification));
}
public ITagger<T> CreateTagger<T>(ITextView textView, ITextBuffer buffer) where T : ITag
{
if (UnderlineClassification == null)
UnderlineClassification = ClassificationRegistry.GetClassificationType("UnderlineClassification");
if (textView.TextBuffer != buffer)
return null;
return GetClassifierForView(textView) as ITagger<T>;
}
}
#endregion
internal class UnderlineClassifier : ITagger<ClassificationTag>
{
IClassificationType _classificationType;
ITextView _textView;
SnapshotSpan? _underlineSpan;
internal UnderlineClassifier(ITextView textView, IClassificationType classificationType)
{
_textView = textView;
_classificationType = classificationType;
_underlineSpan = null;
}
#region Private helpers
void SendEvent(SnapshotSpan span)
{
var temp = this.TagsChanged;
if (temp != null)
temp(this, new SnapshotSpanEventArgs(span));
}
#endregion
#region UnderlineClassification public members
public SnapshotSpan? CurrentUnderlineSpan { get { return _underlineSpan; } }
public void SetUnderlineSpan(SnapshotSpan? span)
{
var oldSpan = _underlineSpan;
_underlineSpan = span;
if (!oldSpan.HasValue && !_underlineSpan.HasValue)
return;
else if (oldSpan.HasValue && _underlineSpan.HasValue && oldSpan == _underlineSpan)
return;
if (!_underlineSpan.HasValue)
{
this.SendEvent(oldSpan.Value);
}
else
{
SnapshotSpan updateSpan = _underlineSpan.Value;
if (oldSpan.HasValue)
updateSpan = new SnapshotSpan(updateSpan.Snapshot,
Span.FromBounds(Math.Min(updateSpan.Start, oldSpan.Value.Start),
Math.Max(updateSpan.End, oldSpan.Value.End)));
this.SendEvent(updateSpan);
}
}
#endregion
public IEnumerable<ITagSpan<ClassificationTag>> GetTags(NormalizedSnapshotSpanCollection spans)
{
if (!_underlineSpan.HasValue || spans.Count == 0)
yield break;
SnapshotSpan request = new SnapshotSpan(spans[0].Start, spans[spans.Count - 1].End);
SnapshotSpan underline = _underlineSpan.Value.TranslateTo(request.Snapshot, SpanTrackingMode.EdgeInclusive);
if (underline.IntersectsWith(request))
{
yield return new TagSpan<ClassificationTag>(underline, new ClassificationTag(_classificationType));
}
}
public event EventHandler<SnapshotSpanEventArgs> TagsChanged;
}
}