-
Notifications
You must be signed in to change notification settings - Fork 4k
/
DocumentEditor.cs
59 lines (50 loc) · 1.98 KB
/
DocumentEditor.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
// 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.
#nullable disable
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.CodeAnalysis.Editing
{
/// <summary>
/// An editor for making changes to a document's syntax tree.
/// </summary>
public class DocumentEditor : SyntaxEditor
{
private readonly Document _document;
private readonly SemanticModel _model;
private DocumentEditor(Document document, SemanticModel model, SyntaxNode root)
: base(root, document.Project.Solution.Workspace)
{
_document = document;
_model = model;
}
/// <summary>
/// Creates a new <see cref="DocumentEditor"/> instance.
/// </summary>
public static async Task<DocumentEditor> CreateAsync(Document document, CancellationToken cancellationToken = default)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var root = model.SyntaxTree.GetRoot(cancellationToken);
return new DocumentEditor(document, model, root);
}
/// <summary>
/// The <see cref="Document"/> specified when the editor was first created.
/// </summary>
public Document OriginalDocument => _document;
/// <summary>
/// The <see cref="CodeAnalysis.SemanticModel"/> of the original document.
/// </summary>
public SemanticModel SemanticModel => _model;
/// <summary>
/// Returns the changed <see cref="Document"/>.
/// </summary>
public Document GetChangedDocument()
=> _document.WithSyntaxRoot(this.GetChangedRoot());
}
}