From a2a5fb167643ec84961b848fe3824286d9707784 Mon Sep 17 00:00:00 2001 From: Jason Malinowski Date: Fri, 19 Apr 2019 15:56:28 -0400 Subject: [PATCH] Add a Solution.AddAdditionalDocuments method We only allowed adding one-at-at-time. This aligns us with regular documents and AnalyzerConfig documents. --- .../Core/Portable/PublicAPI.Unshipped.txt | 1 + .../Workspace/Solution/ProjectState.cs | 8 +++---- .../Portable/Workspace/Solution/Solution.cs | 7 +++++- .../Workspace/Solution/SolutionState.cs | 24 ++++--------------- 4 files changed, 15 insertions(+), 25 deletions(-) diff --git a/src/Workspaces/Core/Portable/PublicAPI.Unshipped.txt b/src/Workspaces/Core/Portable/PublicAPI.Unshipped.txt index 6b111569a29..a711f7fb564 100644 --- a/src/Workspaces/Core/Portable/PublicAPI.Unshipped.txt +++ b/src/Workspaces/Core/Portable/PublicAPI.Unshipped.txt @@ -1,4 +1,5 @@ *REMOVED*Microsoft.CodeAnalysis.Workspace.ClearOpenDocument(Microsoft.CodeAnalysis.DocumentId documentId, bool isSolutionClosing = false) -> void +Microsoft.CodeAnalysis.Solution.AddAdditionalDocuments(System.Collections.Immutable.ImmutableArray documentInfos) -> Microsoft.CodeAnalysis.Solution Microsoft.CodeAnalysis.Solution.AddAnalyzerConfigDocuments(System.Collections.Immutable.ImmutableArray documentInfos) -> Microsoft.CodeAnalysis.Solution Microsoft.CodeAnalysis.Solution.ContainsAnalyzerConfigDocument(Microsoft.CodeAnalysis.DocumentId documentId) -> bool Microsoft.CodeAnalysis.Solution.RemoveAnalyzerConfigDocument(Microsoft.CodeAnalysis.DocumentId documentId) -> Microsoft.CodeAnalysis.Solution diff --git a/src/Workspaces/Core/Portable/Workspace/Solution/ProjectState.cs b/src/Workspaces/Core/Portable/Workspace/Solution/ProjectState.cs index 186f83ffabc..0d862d93656 100644 --- a/src/Workspaces/Core/Portable/Workspace/Solution/ProjectState.cs +++ b/src/Workspaces/Core/Portable/Workspace/Solution/ProjectState.cs @@ -675,14 +675,14 @@ public ProjectState AddDocuments(ImmutableArray documents) documentStates: _documentStates.AddRange(documents.Select(d => KeyValuePairUtil.Create(d.Id, d)))); } - public ProjectState AddAdditionalDocument(TextDocumentState document) + public ProjectState AddAdditionalDocuments(ImmutableArray documents) { - Debug.Assert(!this.AdditionalDocumentStates.ContainsKey(document.Id)); + Debug.Assert(!documents.Any(d => this.AdditionalDocumentStates.ContainsKey(d.Id))); return this.With( projectInfo: this.ProjectInfo.WithVersion(this.Version.GetNewerVersion()), - additionalDocumentIds: _additionalDocumentIds.Add(document.Id), - additionalDocumentStates: _additionalDocumentStates.Add(document.Id, document)); + additionalDocumentIds: _additionalDocumentIds.AddRange(documents.Select(d => d.Id)), + additionalDocumentStates: _additionalDocumentStates.AddRange(documents.Select(d => KeyValuePairUtil.Create(d.Id, d)))); } public ProjectState AddAnalyzerConfigDocuments(ImmutableArray documents) diff --git a/src/Workspaces/Core/Portable/Workspace/Solution/Solution.cs b/src/Workspaces/Core/Portable/Workspace/Solution/Solution.cs index 3aeaa7169ca..ff34c62f317 100644 --- a/src/Workspaces/Core/Portable/Workspace/Solution/Solution.cs +++ b/src/Workspaces/Core/Portable/Workspace/Solution/Solution.cs @@ -776,7 +776,12 @@ public Solution AddAdditionalDocument(DocumentId documentId, string name, Source public Solution AddAdditionalDocument(DocumentInfo documentInfo) { - var newState = _state.AddAdditionalDocument(documentInfo); + return AddAdditionalDocuments(ImmutableArray.Create(documentInfo)); + } + + public Solution AddAdditionalDocuments(ImmutableArray documentInfos) + { + var newState = _state.AddAdditionalDocuments(documentInfos); if (newState == _state) { return this; diff --git a/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs b/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs index 8f7c8c4f3cb..42d9fc293f1 100644 --- a/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs +++ b/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs @@ -1196,27 +1196,11 @@ public SolutionState AddDocuments(ImmutableArray documentInfos) return newSolutionState; } - public SolutionState AddAdditionalDocument(DocumentInfo documentInfo) + public SolutionState AddAdditionalDocuments(ImmutableArray documentInfos) { - if (documentInfo == null) - { - throw new ArgumentNullException(nameof(documentInfo)); - } - - CheckContainsProject(documentInfo.Id.ProjectId); - CheckNotContainsAdditionalDocument(documentInfo.Id); - - var oldProject = this.GetProjectState(documentInfo.Id.ProjectId); - - var state = new TextDocumentState( - documentInfo, - _solutionServices); - - var newProject = oldProject.AddAdditionalDocument(state); - var documentStates = SpecializedCollections.SingletonEnumerable(newProject.GetAdditionalDocumentState(documentInfo.Id)); - - return this.ForkProject(newProject, - newFilePathToDocumentIdsMap: CreateFilePathToDocumentIdsMapWithAddedDocuments(documentStates)); + return AddDocumentsToMultipleProjects(documentInfos, + (documentInfo, project) => new TextDocumentState(documentInfo, _solutionServices), + (projectState, documents) => (projectState.AddAdditionalDocuments(documents), translationAction: null)); } public SolutionState AddAnalyzerConfigDocuments(ImmutableArray documentInfos) -- GitLab