-
Notifications
You must be signed in to change notification settings - Fork 4k
/
SolutionEditor.cs
61 lines (52 loc) · 2.12 KB
/
SolutionEditor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.CodeAnalysis.Editing
{
/// <summary>
/// An editor for making changes to multiple documents in a solution.
/// </summary>
public class SolutionEditor
{
private readonly Solution _solution;
private readonly Dictionary<DocumentId, DocumentEditor> _documentEditors;
public SolutionEditor(Solution solution)
{
_solution = solution;
_documentEditors = new Dictionary<DocumentId, DocumentEditor>();
}
/// <summary>
/// The <see cref="Solution"/> that was specified when the <see cref="SolutionEditor"/> was constructed.
/// </summary>
public Solution OriginalSolution => _solution;
/// <summary>
/// Gets the <see cref="DocumentEditor"/> for the corresponding <see cref="DocumentId"/>.
/// </summary>
public async Task<DocumentEditor> GetDocumentEditorAsync(DocumentId id, CancellationToken cancellationToken = default)
{
if (!_documentEditors.TryGetValue(id, out var editor))
{
editor = await DocumentEditor.CreateAsync(_solution.GetDocument(id), cancellationToken).ConfigureAwait(false);
_documentEditors.Add(id, editor);
}
return editor;
}
/// <summary>
/// Returns the changed <see cref="Solution"/>.
/// </summary>
public Solution GetChangedSolution()
{
var changedSolution = _solution;
foreach (var docEd in _documentEditors.Values)
{
var currentDoc = changedSolution.GetDocument(docEd.OriginalDocument.Id);
var newDoc = currentDoc.WithSyntaxRoot(docEd.GetChangedRoot());
changedSolution = newDoc.Project.Solution;
}
return changedSolution;
}
}
}