Skip to content

Commit fa1a641

Browse files
committed
Refactor remove document
1 parent 27bc8ea commit fa1a641

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -975,23 +975,21 @@ public async Task RemoveDocument(SharpIdeFile fileModel)
975975
await _solutionLoadedTcs.Task;
976976
Guard.Against.Null(fileModel, nameof(fileModel));
977977

978-
var project = GetProjectForSharpIdeFile(fileModel);
979-
980-
var document = fileModel switch
978+
var documentId = _workspace!.CurrentSolution.GetDocumentIdsWithFilePath(fileModel.Path).Single();
979+
var documentKind = _workspace.CurrentSolution.GetDocumentKind(documentId);
980+
if (documentKind is null)
981981
{
982-
{ IsRazorFile: true } => project.AdditionalDocuments.Single(s => s.FilePath == fileModel.Path),
983-
{ IsCsharpFile: true } => project.Documents.Single(s => s.FilePath == fileModel.Path),
984-
_ => throw new InvalidOperationException("UpdateDocument failed: File is not in workspace")
985-
};
982+
_logger.LogError("Attempted to remove document not in workspace: '{DocumentPath}'", fileModel.Path);
983+
return;
984+
}
986985

987-
var newSolution = fileModel switch
986+
switch (documentKind)
988987
{
989-
{ IsRazorFile: true } => _workspace.CurrentSolution.RemoveAdditionalDocument(document.Id),
990-
{ IsCsharpFile: true } => _workspace.CurrentSolution.RemoveDocument(document.Id),
991-
_ => throw new InvalidOperationException("AddDocument failed: File is not in workspace")
992-
};
993-
994-
_workspace.TryApplyChanges(newSolution);
988+
case TextDocumentKind.Document: _workspace.OnDocumentRemoved(documentId); break;
989+
case TextDocumentKind.AdditionalDocument: _workspace.OnAdditionalDocumentRemoved(documentId); break;
990+
case TextDocumentKind.AnalyzerConfigDocument: _workspace.OnAnalyzerConfigDocumentRemoved(documentId); break;
991+
default: throw new ArgumentOutOfRangeException(nameof(documentKind));
992+
}
995993
}
996994

997995
public async Task MoveDocument(SharpIdeFile sharpIdeFile, string oldFilePath)

0 commit comments

Comments
 (0)