From 23ea8a57465c3ca5e8778d830f286fd229c59d44 Mon Sep 17 00:00:00 2001 From: Jason Malinowski Date: Fri, 26 Apr 2019 19:19:00 -0400 Subject: [PATCH] Add Solution.WithAnalyzerConfigDocumentText --- .../Core/Portable/PublicAPI.Unshipped.txt | 1 + .../Solution/AnalyzerConfigDocumentState.cs | 5 ++++ .../Portable/Workspace/Solution/Solution.cs | 15 +++++++++++ .../Workspace/Solution/SolutionState.cs | 27 +++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/src/Workspaces/Core/Portable/PublicAPI.Unshipped.txt b/src/Workspaces/Core/Portable/PublicAPI.Unshipped.txt index 5a3f939de6a..99b0a556fae 100644 --- a/src/Workspaces/Core/Portable/PublicAPI.Unshipped.txt +++ b/src/Workspaces/Core/Portable/PublicAPI.Unshipped.txt @@ -10,6 +10,7 @@ Microsoft.CodeAnalysis.Solution.AddAnalyzerConfigDocuments(System.Collections.Im Microsoft.CodeAnalysis.Solution.ContainsAnalyzerConfigDocument(Microsoft.CodeAnalysis.DocumentId documentId) -> bool Microsoft.CodeAnalysis.Solution.GetAnalyzerConfigDocument(Microsoft.CodeAnalysis.DocumentId documentId) -> Microsoft.CodeAnalysis.AnalyzerConfigDocument Microsoft.CodeAnalysis.Solution.RemoveAnalyzerConfigDocument(Microsoft.CodeAnalysis.DocumentId documentId) -> Microsoft.CodeAnalysis.Solution +Microsoft.CodeAnalysis.Solution.WithAnalyzerConfigDocumentText(Microsoft.CodeAnalysis.DocumentId documentId, Microsoft.CodeAnalysis.Text.SourceText text, Microsoft.CodeAnalysis.PreservationMode mode = Microsoft.CodeAnalysis.PreservationMode.PreserveValue) -> Microsoft.CodeAnalysis.Solution Microsoft.CodeAnalysis.Solution.WithAnalyzerConfigDocumentTextLoader(Microsoft.CodeAnalysis.DocumentId documentId, Microsoft.CodeAnalysis.TextLoader loader, Microsoft.CodeAnalysis.PreservationMode mode) -> Microsoft.CodeAnalysis.Solution Microsoft.CodeAnalysis.Workspace.CheckAnalyzerConfigDocumentIsInCurrentSolution(Microsoft.CodeAnalysis.DocumentId documentId) -> void Microsoft.CodeAnalysis.Workspace.CheckAnalyzerConfigDocumentIsNotInCurrentSolution(Microsoft.CodeAnalysis.DocumentId documentId) -> void diff --git a/src/Workspaces/Core/Portable/Workspace/Solution/AnalyzerConfigDocumentState.cs b/src/Workspaces/Core/Portable/Workspace/Solution/AnalyzerConfigDocumentState.cs index 4eb52315b32..74792a0ad43 100644 --- a/src/Workspaces/Core/Portable/Workspace/Solution/AnalyzerConfigDocumentState.cs +++ b/src/Workspaces/Core/Portable/Workspace/Solution/AnalyzerConfigDocumentState.cs @@ -48,6 +48,11 @@ public new AnalyzerConfigDocumentState UpdateText(TextLoader loader, Preservatio return (AnalyzerConfigDocumentState)base.UpdateText(loader, mode); } + public new AnalyzerConfigDocumentState UpdateText(SourceText text, PreservationMode mode) + { + return (AnalyzerConfigDocumentState)base.UpdateText(text, mode); + } + protected override TextDocumentState UpdateText(ValueSource newTextSource, PreservationMode mode, bool incremental) { return new AnalyzerConfigDocumentState( diff --git a/src/Workspaces/Core/Portable/Workspace/Solution/Solution.cs b/src/Workspaces/Core/Portable/Workspace/Solution/Solution.cs index 363ce671103..552f789bcbc 100644 --- a/src/Workspaces/Core/Portable/Workspace/Solution/Solution.cs +++ b/src/Workspaces/Core/Portable/Workspace/Solution/Solution.cs @@ -1024,6 +1024,21 @@ public Solution WithAdditionalDocumentTextLoader(DocumentId documentId, TextLoad return new Solution(newState); } + /// + /// Creates a new solution instance with the analyzer config document specified updated to have the text + /// supplied by the text loader. + /// + public Solution WithAnalyzerConfigDocumentText(DocumentId documentId, SourceText text, PreservationMode mode = PreservationMode.PreserveValue) + { + var newState = _state.WithAnalyzerConfigDocumentText(documentId, text, mode); + if (newState == _state) + { + return this; + } + + return new Solution(newState); + } + /// /// Creates a new solution instance with the analyzer config document specified updated to have the text /// supplied by the text loader. diff --git a/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs b/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs index 42d9fc293f1..d288946cd18 100644 --- a/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs +++ b/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs @@ -1380,6 +1380,33 @@ public SolutionState WithAdditionalDocumentText(DocumentId documentId, SourceTex return newSolution; } + /// + /// Creates a new solution instance with the document specified updated to have the text + /// specified. + /// + public SolutionState WithAnalyzerConfigDocumentText(DocumentId documentId, SourceText text, PreservationMode mode = PreservationMode.PreserveValue) + { + if (documentId == null) + { + throw new ArgumentNullException(nameof(documentId)); + } + + if (text == null) + { + throw new ArgumentNullException(nameof(text)); + } + + CheckContainsAnalyzerConfigDocument(documentId); + + var oldDocument = this.GetAnalyzerConfigDocumentState(documentId); + if (oldDocument.TryGetText(out var oldText) && text == oldText) + { + return this; + } + + return this.WithAnalyzerConfigDocumentState(oldDocument.UpdateText(text, mode), textChanged: true); + } + /// /// Creates a new solution instance with the document specified updated to have the text /// and version specified. -- GitLab