@@ -155,15 +155,18 @@ protected static async Task GenerateFileAndVerifyAsync(
155
155
156
156
var masWorkspace = service . TryGetWorkspace ( ) ;
157
157
158
- using var fileStream = File . OpenRead ( file . FilePath ) ;
159
- var sourceText = SourceText . From ( fileStream ) ;
160
- var text = SourceText . From ( File . ReadAllText ( file . FilePath ) ) ;
161
-
162
158
var pdbService = ( PdbSourceDocumentMetadataAsSourceFileProvider ) workspace . ExportProvider . GetExportedValues < IMetadataAsSourceFileProvider > ( ) . Single ( s => s is PdbSourceDocumentMetadataAsSourceFileProvider ) ;
163
159
164
- var documentInfo = pdbService . GetTestAccessor ( ) . Documents [ file . FilePath ] ;
165
- masWorkspace ! . OnDocumentAdded ( documentInfo . DocumentInfo ) ;
166
- var document = masWorkspace ! . CurrentSolution . Projects . First ( ) . Documents . First ( d => d . FilePath == file . FilePath ) ;
160
+ // Add the document to the workspace. We provide an empty static source text as the API requires it to open the document.
161
+ // We're not really trying to verify that the source text the editor hands to us is the right encoding - just that the document we added has the right encoding.
162
+ var result = pdbService . TryAddDocumentToWorkspace ( ( MetadataAsSourceWorkspace ) masWorkspace ! , file . FilePath , new StaticSourceTextContainer ( SourceText . From ( string . Empty ) ) , out _ ) ;
163
+ Assert . True ( result ) ;
164
+
165
+ // Immediately close the document so that we get the source text provided by the workspace (instead of the empty one we passed).
166
+ var info = pdbService . GetTestAccessor ( ) . Documents [ file . FilePath ] ;
167
+ masWorkspace ! . OnDocumentClosed ( info . DocumentId , new WorkspaceFileTextLoader ( workspace . Services . SolutionServices , file . FilePath , info . Encoding ) ) ;
168
+
169
+ var document = masWorkspace ! . CurrentSolution . GetRequiredDocument ( info . DocumentId ) ;
167
170
168
171
// Mapping the project from the generated document should map back to the original project
169
172
var provider = workspace . ExportProvider . GetExportedValues < IMetadataAsSourceFileProvider > ( ) . OfType < PdbSourceDocumentMetadataAsSourceFileProvider > ( ) . Single ( ) ;
@@ -339,7 +342,7 @@ protected static string GetPdbPath(string path)
339
342
return Path . Combine ( path , "reference.pdb" ) ;
340
343
}
341
344
342
- private class StaticSourceTextContainer ( SourceText sourceText ) : SourceTextContainer
345
+ protected class StaticSourceTextContainer ( SourceText sourceText ) : SourceTextContainer
343
346
{
344
347
public override SourceText CurrentText => sourceText ;
345
348
0 commit comments