@@ -1006,27 +1006,24 @@ public async Task AddDocument(SharpIdeFile fileModel, string content)
10061006 return ;
10071007 }
10081008
1009- var existingDocument = fileModel switch
1010- {
1011- { IsRazorFile : true } => probableProject . AdditionalDocuments . SingleOrDefault ( s => s . FilePath == fileModel . Path ) ,
1012- { IsCsharpFile : true } => probableProject . Documents . SingleOrDefault ( s => s . FilePath == fileModel . Path ) ,
1013- _ => throw new InvalidOperationException ( "AddDocument failed: File is not a workspace file" )
1014- } ;
1015- if ( existingDocument is not null )
1009+ var existingDocumentIdsWithFilePath = _workspace ! . CurrentSolution . GetDocumentIdsWithFilePath ( fileModel . Path ) ;
1010+ if ( ! existingDocumentIdsWithFilePath . IsEmpty )
10161011 {
10171012 throw new InvalidOperationException ( $ "AddDocument failed: Document '{ fileModel . Path } ' already exists in workspace") ;
10181013 }
10191014
10201015 var sourceText = SourceText . From ( content , Encoding . UTF8 ) ;
1016+ var documentId = DocumentId . CreateNewId ( probableProject . Id ) ;
10211017
1022- var newSolution = fileModel switch
1018+ _workspace . SetCurrentSolution ( oldSolution =>
10231019 {
1024- { IsRazorFile : true } => _workspace . CurrentSolution . AddAdditionalDocument ( DocumentId . CreateNewId ( probableProject . Id ) , fileModel . Name , sourceText , filePath : fileModel . Path ) ,
1025- { IsCsharpFile : true } => _workspace . CurrentSolution . AddDocument ( DocumentId . CreateNewId ( probableProject . Id ) , fileModel . Name , sourceText , filePath : fileModel . Path ) ,
1026- _ => throw new InvalidOperationException ( "AddDocument failed: File is not in workspace" )
1027- } ;
1028-
1029- _workspace . TryApplyChanges ( newSolution ) ;
1020+ var newSolution = fileModel switch
1021+ {
1022+ { IsCsharpFile : true } => _workspace . CurrentSolution . AddDocument ( documentId , fileModel . Name , sourceText , filePath : fileModel . Path ) ,
1023+ _ => _workspace . CurrentSolution . AddAdditionalDocument ( documentId , fileModel . Name , sourceText , filePath : fileModel . Path ) ,
1024+ } ;
1025+ return newSolution ;
1026+ } , WorkspaceChangeKind . DocumentAdded , documentId : documentId ) ;
10301027 }
10311028
10321029 public async Task RemoveDocument ( SharpIdeFile fileModel )
0 commit comments