3
3
// See the LICENSE file in the project root for more information.
4
4
5
5
using System ;
6
- using System . Diagnostics ;
7
6
using System . Runtime . InteropServices ;
8
7
using Microsoft . CodeAnalysis ;
9
8
using Microsoft . CodeAnalysis . Diagnostics ;
15
14
using Microsoft . VisualStudio . ComponentModelHost ;
16
15
using Microsoft . VisualStudio . Editor ;
17
16
using Microsoft . VisualStudio . LanguageServices . Implementation . ProjectSystem ;
18
- using Microsoft . VisualStudio . Shell . Interop ;
19
17
using Microsoft . VisualStudio . Text ;
20
18
using Microsoft . VisualStudio . Text . Tagging ;
21
19
using Microsoft . VisualStudio . TextManager . Interop ;
@@ -61,35 +59,18 @@ internal partial class ContainedLanguage
61
59
/// </summary>
62
60
public ITextBuffer DataBuffer { get ; }
63
61
64
- // Set when a TextViewFIlter is set. We hold onto this to keep our TagSource objects alive even if Venus
62
+ // Set when a TextViewFilter is set. We hold onto this to keep our TagSource objects alive even if Venus
65
63
// disconnects the subject buffer from the view temporarily (which they do frequently). Otherwise, we have to
66
64
// re-compute all of the tag data when they re-connect it, and this causes issues like classification
67
65
// flickering.
68
66
private readonly ITagAggregator < ITag > _bufferTagAggregator ;
69
67
70
- public static string GetFilePathFromHierarchyAndItemId ( IVsHierarchy hierarchy , uint itemid )
71
- {
72
- if ( ! ErrorHandler . Succeeded ( ( ( IVsProject ) hierarchy ) . GetMkDocument ( itemid , out var filePath ) ) )
73
- {
74
- // we couldn't look up the document moniker from an hierarchy for an itemid.
75
- // Since we only use this moniker as a key, we could fall back to something else, like the document name.
76
- Debug . Assert ( false , "Could not get the document moniker for an item from its hierarchy." ) ;
77
- if ( ! hierarchy . TryGetItemName ( itemid , out filePath ! ) )
78
- {
79
- FatalError . ReportAndPropagate ( new InvalidOperationException ( "Failed to get document moniker for a contained document" ) ) ;
80
- }
81
- }
82
-
83
- return filePath ;
84
- }
85
-
86
68
internal ContainedLanguage (
87
69
IVsTextBufferCoordinator bufferCoordinator ,
88
70
IComponentModel componentModel ,
89
71
Workspace workspace ,
90
72
ProjectId projectId ,
91
73
VisualStudioProject ? project ,
92
- string filePath ,
93
74
Guid languageServiceGuid ,
94
75
AbstractFormattingRule ? vbHelperFormattingRule = null )
95
76
{
@@ -115,13 +96,16 @@ internal ContainedLanguage(
115
96
var bufferTagAggregatorFactory = ComponentModel . GetService < IBufferTagAggregatorFactoryService > ( ) ;
116
97
_bufferTagAggregator = bufferTagAggregatorFactory . CreateTagAggregator < ITag > ( SubjectBuffer ) ;
117
98
99
+ var filePath = GetFilePathFromBuffers ( ) ;
118
100
DocumentId documentId ;
119
101
120
102
if ( this . Project != null )
121
103
{
122
104
documentId = this . Project . AddSourceTextContainer (
123
- SubjectBuffer . AsTextContainer ( ) , filePath ,
124
- sourceCodeKind : SourceCodeKind . Regular , folders : default ,
105
+ SubjectBuffer . AsTextContainer ( ) ,
106
+ filePath ,
107
+ sourceCodeKind : SourceCodeKind . Regular ,
108
+ folders : default ,
125
109
designTimeOnly : true ,
126
110
documentServiceProvider : new ContainedDocument . DocumentServiceProvider ( DataBuffer ) ) ;
127
111
}
@@ -135,14 +119,14 @@ internal ContainedLanguage(
135
119
}
136
120
137
121
this . ContainedDocument = new ContainedDocument (
138
- componentModel . GetService < IThreadingContext > ( ) ,
122
+ ComponentModel . GetService < IThreadingContext > ( ) ,
139
123
documentId ,
140
124
subjectBuffer : SubjectBuffer ,
141
125
dataBuffer : DataBuffer ,
142
- bufferCoordinator ,
126
+ BufferCoordinator ,
143
127
Workspace ,
144
- project ,
145
- componentModel ,
128
+ Project ,
129
+ ComponentModel ,
146
130
vbHelperFormattingRule ) ;
147
131
148
132
// TODO: Can contained documents be linked or shared?
@@ -179,5 +163,24 @@ private void OnDataBufferChanged(object sender, TextContentChangedEventArgs e)
179
163
// when primary buffer has changed to update diagnostic positions.
180
164
_diagnosticAnalyzerService . Reanalyze ( this . Workspace , documentIds : SpecializedCollections . SingletonEnumerable ( this . ContainedDocument . Id ) ) ;
181
165
}
166
+
167
+ public string GetFilePathFromBuffers ( )
168
+ {
169
+ var textDocumentFactoryService = ComponentModel . GetService < ITextDocumentFactoryService > ( ) ;
170
+
171
+ // Try to get the file path from the secondary buffer
172
+ if ( ! textDocumentFactoryService . TryGetTextDocument ( SubjectBuffer , out var document ) )
173
+ {
174
+ // Fallback to the primary buffer
175
+ textDocumentFactoryService . TryGetTextDocument ( DataBuffer , out document ) ;
176
+ }
177
+
178
+ if ( document == null )
179
+ {
180
+ FatalError . ReportAndPropagate ( new InvalidOperationException ( "Failed to get an ITextDocument for a contained document" ) ) ;
181
+ }
182
+
183
+ return document ? . FilePath ?? string . Empty ;
184
+ }
182
185
}
183
186
}
0 commit comments