@@ -39,10 +39,28 @@ public TemplateClassifier(TemplateClassifierProviderBase provider, ITextBuffer t
39
39
40
40
public override event EventHandler < ClassificationChangedEventArgs > ClassificationChanged ;
41
41
42
+ internal static HtmlEditorDocument HtmlEditorDocumentFromTextBuffer ( ITextBuffer buffer ) {
43
+ var doc = HtmlEditorDocument . FromTextBuffer ( buffer ) ;
44
+ #if DEV14_OR_LATER
45
+ if ( doc == null ) {
46
+ var projBuffer = buffer as IProjectionBuffer ;
47
+ if ( projBuffer != null ) {
48
+ foreach ( var b in projBuffer . SourceBuffers ) {
49
+ if ( b . ContentType . IsOfType ( TemplateHtmlContentType . ContentTypeName ) &&
50
+ ( doc = HtmlEditorDocument . TryFromTextBuffer ( b ) ) != null ) {
51
+ return doc ;
52
+ }
53
+ }
54
+ }
55
+ }
56
+ #endif
57
+ return doc ;
58
+ }
59
+
42
60
public override IList < ClassificationSpan > GetClassificationSpans ( SnapshotSpan span ) {
43
61
var spans = new List < ClassificationSpan > ( ) ;
44
62
45
- var htmlDoc = HtmlEditorDocument . TryFromTextBuffer ( span . Snapshot . TextBuffer ) ;
63
+ var htmlDoc = HtmlEditorDocumentFromTextBuffer ( span . Snapshot . TextBuffer ) ;
46
64
if ( htmlDoc == null ) {
47
65
return spans ;
48
66
}
@@ -64,35 +82,49 @@ public override IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan sp
64
82
return spans ;
65
83
}
66
84
67
- var projSnapshot = _htmlDoc . PrimaryView . TextSnapshot as IProjectionSnapshot ;
68
- if ( projSnapshot == null ) {
69
- return spans ;
70
- }
71
-
72
- var primarySpans = projSnapshot . MapFromSourceSnapshot ( span ) ;
73
- foreach ( var primarySpan in primarySpans ) {
74
- var index = _htmlDoc . HtmlEditorTree . ArtifactCollection . GetItemContaining ( primarySpan . Start ) ;
75
- if ( index < 0 ) {
76
- continue ;
77
- }
78
-
79
- var artifact = _htmlDoc . HtmlEditorTree . ArtifactCollection [ index ] as TemplateArtifact ;
80
- if ( artifact == null ) {
81
- continue ;
85
+ // The provided span may be in a projection snapshot, so we need to
86
+ // map back to the source snapshot to find the correct
87
+ // classification. If projSnapshot is null, we are already in the
88
+ // correct snapshot.
89
+ var projSnapshot = span . Snapshot as IProjectionSnapshot ;
90
+ var sourceSnapshot = span . Snapshot ;
91
+
92
+ var sourceStartIndex = span . Start . Position ;
93
+ if ( projSnapshot != null ) {
94
+ var pt = projSnapshot . MapToSourceSnapshot ( sourceStartIndex ) ;
95
+ sourceStartIndex = pt . Position ;
96
+ sourceSnapshot = pt . Snapshot ;
97
+ if ( HtmlEditorDocument . TryFromTextBuffer ( sourceSnapshot . TextBuffer ) != _htmlDoc ) {
98
+ return spans ;
82
99
}
100
+ }
83
101
84
- var artifactStart = projSnapshot . MapToSourceSnapshot ( artifact . InnerRange . Start ) ;
85
- if ( artifactStart . Snapshot != span . Snapshot ) {
86
- continue ;
87
- }
102
+ var index = _htmlDoc . HtmlEditorTree . ArtifactCollection . GetItemContaining ( sourceStartIndex ) ;
103
+ if ( index < 0 ) {
104
+ return spans ;
105
+ }
88
106
89
- var artifactText = _htmlDoc . HtmlEditorTree . ParseTree . Text . GetText ( artifact . InnerRange ) ;
90
- artifact . Parse ( artifactText ) ;
107
+ var artifact = _htmlDoc . HtmlEditorTree . ArtifactCollection [ index ] as TemplateArtifact ;
108
+ if ( artifact == null ) {
109
+ return spans ;
110
+ }
91
111
92
- var classifications = artifact . GetClassifications ( ) ;
93
- foreach ( var classification in classifications ) {
94
- var classificationSpan = ToClassificationSpan ( classification , span . Snapshot , artifactStart . Position ) ;
95
- spans . Add ( classificationSpan ) ;
112
+ int artifactStart = artifact . InnerRange . Start ;
113
+ var artifactText = _htmlDoc . HtmlEditorTree . ParseTree . Text . GetText ( artifact . InnerRange ) ;
114
+ artifact . Parse ( artifactText ) ;
115
+
116
+ var classifications = artifact . GetClassifications ( ) ;
117
+ foreach ( var classification in classifications ) {
118
+ var cls = GetClassification ( classification . Classification ) ;
119
+ int clsStart = artifactStart + classification . Span . Start ;
120
+ int clsLen = Math . Min ( sourceSnapshot . Length - clsStart , classification . Span . Length ) ;
121
+ var clsSpan = new SnapshotSpan ( sourceSnapshot , clsStart , clsLen ) ;
122
+ if ( projSnapshot != null ) {
123
+ foreach ( var sp in projSnapshot . MapFromSourceSnapshot ( clsSpan ) ) {
124
+ spans . Add ( new ClassificationSpan ( new SnapshotSpan ( span . Snapshot , sp ) , cls ) ) ;
125
+ }
126
+ } else {
127
+ spans . Add ( new ClassificationSpan ( clsSpan , cls ) ) ;
96
128
}
97
129
}
98
130
0 commit comments