/
ViewCreationListener.cs
93 lines (77 loc) · 2.9 KB
/
ViewCreationListener.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
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Utilities;
using System;
using System.Diagnostics;
namespace ItalicComments
{
[Export(typeof(IWpfTextViewCreationListener))]
[ContentType("code")]
[TextViewRole(PredefinedTextViewRoles.Document)]
internal sealed class ViewCreationListener : IWpfTextViewCreationListener
{
[Import]
IClassificationFormatMapService formatMapService = null;
[Import]
IClassificationTypeRegistryService typeRegistry = null;
public void TextViewCreated(IWpfTextView textView)
{
textView.Properties.GetOrCreateSingletonProperty(() =>
new FormatMapWatcher(textView, formatMapService.GetClassificationFormatMap(textView), typeRegistry));
}
}
internal sealed class FormatMapWatcher
{
bool inUpdate = false;
IClassificationFormatMap formatMap;
IClassificationTypeRegistryService typeRegistry;
IClassificationType text;
public FormatMapWatcher(ITextView view, IClassificationFormatMap formatMap, IClassificationTypeRegistryService typeRegistry)
{
this.formatMap = formatMap;
this.text = typeRegistry.GetClassificationType("text");
this.typeRegistry = typeRegistry;
this.ClearUrlForegroundColor();
this.formatMap.ClassificationFormatMappingChanged += FormatMapChanged;
view.GotAggregateFocus += FirstGotFocus;
}
void FirstGotFocus(object sender, EventArgs e)
{
((ITextView)sender).GotAggregateFocus -= FirstGotFocus;
Debug.Assert(!inUpdate, "How can we be updating *while* the view is getting focus?");
this.ClearUrlForegroundColor();
}
void FormatMapChanged(object sender, System.EventArgs e)
{
if (!inUpdate)
this.ClearUrlForegroundColor();
}
internal void ClearUrlForegroundColor()
{
try
{
inUpdate = true;
var urlClassificationType = typeRegistry.GetClassificationType("url");
if (urlClassificationType != null)
ClearForeground(urlClassificationType);
}
finally
{
inUpdate = false;
}
}
void ClearForeground(IClassificationType classification)
{
var properties = formatMap.GetTextProperties(classification);
// If this is already cleared out, skip it
if (properties.ForegroundBrushEmpty)
return;
formatMap.SetTextProperties(classification, properties.ClearForegroundBrush());
}
}
}