Refactor remove document

This commit is contained in:
Matt Parker
2025-11-23 01:21:55 +10:00
parent 27bc8eab83
commit fa1a6413ee

View File

@@ -975,23 +975,21 @@ public class RoslynAnalysis(ILogger<RoslynAnalysis> logger, BuildService buildSe
await _solutionLoadedTcs.Task; await _solutionLoadedTcs.Task;
Guard.Against.Null(fileModel, nameof(fileModel)); Guard.Against.Null(fileModel, nameof(fileModel));
var project = GetProjectForSharpIdeFile(fileModel); var documentId = _workspace!.CurrentSolution.GetDocumentIdsWithFilePath(fileModel.Path).Single();
var documentKind = _workspace.CurrentSolution.GetDocumentKind(documentId);
var document = fileModel switch if (documentKind is null)
{ {
{ IsRazorFile: true } => project.AdditionalDocuments.Single(s => s.FilePath == fileModel.Path), _logger.LogError("Attempted to remove document not in workspace: '{DocumentPath}'", fileModel.Path);
{ IsCsharpFile: true } => project.Documents.Single(s => s.FilePath == fileModel.Path), return;
_ => throw new InvalidOperationException("UpdateDocument failed: File is not in workspace") }
};
var newSolution = fileModel switch switch (documentKind)
{ {
{ IsRazorFile: true } => _workspace.CurrentSolution.RemoveAdditionalDocument(document.Id), case TextDocumentKind.Document: _workspace.OnDocumentRemoved(documentId); break;
{ IsCsharpFile: true } => _workspace.CurrentSolution.RemoveDocument(document.Id), case TextDocumentKind.AdditionalDocument: _workspace.OnAdditionalDocumentRemoved(documentId); break;
_ => throw new InvalidOperationException("AddDocument failed: File is not in workspace") case TextDocumentKind.AnalyzerConfigDocument: _workspace.OnAnalyzerConfigDocumentRemoved(documentId); break;
}; default: throw new ArgumentOutOfRangeException(nameof(documentKind));
}
_workspace.TryApplyChanges(newSolution);
} }
public async Task MoveDocument(SharpIdeFile sharpIdeFile, string oldFilePath) public async Task MoveDocument(SharpIdeFile sharpIdeFile, string oldFilePath)