// A Razor file has been processed, this portion is responsible for the decision of whether we need to create or update // the Razor documents background C# representation. public override void DocumentProcessed(OmniSharpDocumentSnapshot document) { if (document is null) { throw new ArgumentNullException(nameof(document)); } _projectSnapshotManagerDispatcher.AssertDispatcherThread(); lock (_workspaceChangedLock) { if (FileKinds.IsComponentImport(document.FileKind)) { // Razor component imports don't have any C# to generate anyways, don't do the work. This doesn't capture _ViewImports.cshtml because we never // associated a FileKind with those files. return; } var openVirtualFilePath = document.FilePath + ActiveVirtualDocumentSuffix; var openDocument = _workspace.GetDocument(openVirtualFilePath); if (openDocument != null) { // This document is open in the editor, no reason for us to populate anything in the workspace the editor will do that. return; } var backgroundVirtualFilePath = document.FilePath + BackgroundVirtualDocumentSuffix; var currentDocument = _workspace.GetDocument(backgroundVirtualFilePath); if (currentDocument == null) { // Background document doesn't exist, we need to create it var roslynProject = GetRoslynProject(document.Project); if (roslynProject == null) { // There's no Roslyn project associated with the Razor document. _logger.LogTrace($"Could not find a Roslyn project for Razor virtual document '{backgroundVirtualFilePath}'."); return; } var documentId = DocumentId.CreateNewId(roslynProject.Id); var name = Path.GetFileName(backgroundVirtualFilePath); var emptyTextLoader = new EmptyTextLoader(backgroundVirtualFilePath); var documentInfo = DocumentInfo.Create(documentId, name, filePath: backgroundVirtualFilePath, loader: emptyTextLoader); _workspace.AddDocument(documentInfo); currentDocument = _workspace.GetDocument(backgroundVirtualFilePath); Debug.Assert(currentDocument != null, "We just added the document, it should definitely be there."); } // Update document content var sourceText = document.GetGeneratedCodeSourceText(); _workspace.OnDocumentChanged(currentDocument.Id, sourceText); } }
// A Razor file has been processed, this portion is responsible for the decision of whether we need to create or update // the Razor documents background C# representation. public override void DocumentProcessed(OmniSharpDocumentSnapshot document) { if (document is null) { throw new ArgumentNullException(nameof(document)); } _foregroundDispatcher.AssertForegroundThread(); if (!FileKinds.IsComponent(document.FileKind) || FileKinds.IsComponentImport(document.FileKind)) { // Today we only support publishing Razor component files to the workspace. We could choose to publish more but we're being // purposefuly restrictive. return; } var openVirtualFilePath = document.FilePath + ActiveVirtualDocumentSuffix; var openDocument = _workspace.GetDocument(openVirtualFilePath); if (openDocument != null) { // This document is open in the editor, no reason for us to populate anything in the workspace the editor will do that. return; } var backgroundVirtualFilePath = document.FilePath + BackgroundVirtualDocumentSuffix; var currentDocument = _workspace.GetDocument(backgroundVirtualFilePath); if (currentDocument == null) { // Background document doesn't exist, we need to create it var roslynProject = GetRoslynProject(document.Project); if (roslynProject == null) { // There's no Roslyn project associated with the Razor document. _logger.LogTrace($"Could not find a Roslyn project for Razor virtual document '{backgroundVirtualFilePath}'."); return; } var documentId = DocumentId.CreateNewId(roslynProject.Id); var name = Path.GetFileName(backgroundVirtualFilePath); var emptyTextLoader = new EmptyTextLoader(backgroundVirtualFilePath); var documentInfo = DocumentInfo.Create(documentId, name, filePath: backgroundVirtualFilePath, loader: emptyTextLoader); _workspace.AddDocument(documentInfo); currentDocument = _workspace.GetDocument(backgroundVirtualFilePath); Debug.Assert(currentDocument != null, "We just added the document, it should definitely be there."); } // Update document content var sourceText = document.GetGeneratedCodeSourceText(); _workspace.OnDocumentChanged(currentDocument.Id, sourceText); }