-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmlCommentShrink.cs
86 lines (74 loc) · 2.8 KB
/
XmlCommentShrink.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
using System;
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;
namespace XmlCommentShrink
{
[Export(typeof(IWpfTextViewCreationListener))]
[ContentType("code")]
[TextViewRole(PredefinedTextViewRoles.Document)]
public class Listener : IWpfTextViewCreationListener
{
[Import]
private IClassificationFormatMapService _formatMapService;
public void TextViewCreated(IWpfTextView textView)
{
textView.Properties.GetOrCreateSingletonProperty(
() => new XmlCommentShrinker(textView, _formatMapService.GetClassificationFormatMap(textView)));
}
}
internal class XmlCommentShrinker
{
private bool _updating;
private readonly IClassificationFormatMap _formatMap;
public XmlCommentShrinker(ITextView view, IClassificationFormatMap formatMap)
{
_formatMap = formatMap;
_formatMap.ClassificationFormatMappingChanged += (sender, e) => ShrinkComments();
view.GotAggregateFocus += OnGotAggregateFocus;
ShrinkComments();
}
private void OnGotAggregateFocus(object sender, EventArgs e)
{
((ITextView)sender).GotAggregateFocus -= OnGotAggregateFocus;
ShrinkComments();
}
private void ShrinkComments()
{
if (_updating)
return;
try
{
_updating = true;
foreach (var classification in from c in _formatMap.CurrentPriorityOrder
where c != null
let name = c.Classification.ToLowerInvariant()
where name.Contains("doc")
select c)
{
Shrink(classification);
}
}
finally
{
_updating = false;
}
}
private void Shrink(IClassificationType classification)
{
var properties = _formatMap.GetTextProperties(classification);
var typeface = properties.Typeface;
if (typeface.Style == FontStyles.Italic)
return;
_formatMap.SetTextProperties(
classification,
properties
.SetTypeface(new Typeface(typeface.FontFamily, FontStyles.Italic, typeface.Weight, typeface.Stretch))
.SetFontRenderingEmSize(6));
}
}
}