-
Notifications
You must be signed in to change notification settings - Fork 4k
/
ImportAdder.cs
126 lines (113 loc) · 7.31 KB
/
ImportAdder.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Editing
{
public static class ImportAdder
{
/// <summary>
/// Adds namespace imports / using directives for namespace references found in the document.
/// </summary>
public static Task<Document> AddImportsAsync(Document document, OptionSet? options = null, CancellationToken cancellationToken = default)
=> AddImportsFromSyntaxesAsync(document, options, cancellationToken);
/// <summary>
/// Adds namespace imports / using directives for namespace references found in the document within the span specified.
/// </summary>
public static Task<Document> AddImportsAsync(Document document, TextSpan span, OptionSet? options = null, CancellationToken cancellationToken = default)
=> AddImportsFromSyntaxesAsync(document, new[] { span }, options, cancellationToken);
/// <summary>
/// Adds namespace imports / using directives for namespace references found in the document within the sub-trees annotated with the <see cref="SyntaxAnnotation"/>.
/// </summary>
public static Task<Document> AddImportsAsync(Document document, SyntaxAnnotation annotation, OptionSet? options = null, CancellationToken cancellationToken = default)
=> AddImportsFromSyntaxesAsync(document, annotation, options, cancellationToken);
/// <summary>
/// Adds namespace imports / using directives for namespace references found in the document within the spans specified.
/// </summary>
public static Task<Document> AddImportsAsync(Document document, IEnumerable<TextSpan> spans, OptionSet? options = null, CancellationToken cancellationToken = default)
=> AddImportsFromSyntaxesAsync(document, spans, options, cancellationToken);
/// <summary>
/// Adds namespace imports / using directives for namespace references found in the document.
/// </summary>
internal static async Task<Document> AddImportsFromSyntaxesAsync(Document document, OptionSet? options = null, CancellationToken cancellationToken = default)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(root);
return await AddImportsFromSyntaxesAsync(document, root.FullSpan, options, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Adds namespace imports / using directives for namespace references found in the document within the span specified.
/// </summary>
internal static Task<Document> AddImportsFromSyntaxesAsync(Document document, TextSpan span, OptionSet? options = null, CancellationToken cancellationToken = default)
=> AddImportsFromSyntaxesAsync(document, new[] { span }, options, cancellationToken);
/// <summary>
/// Adds namespace imports / using directives for namespace references found in the document within the sub-trees annotated with the <see cref="SyntaxAnnotation"/>.
/// </summary>
internal static async Task<Document> AddImportsFromSyntaxesAsync(Document document, SyntaxAnnotation annotation, OptionSet? options = null, CancellationToken cancellationToken = default)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(root);
return await AddImportsFromSyntaxesAsync(document, root.GetAnnotatedNodesAndTokens(annotation).Select(t => t.FullSpan), options, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Adds namespace imports / using directives for namespace references found in the document within the spans specified.
/// </summary>
internal static Task<Document> AddImportsFromSyntaxesAsync(Document document, IEnumerable<TextSpan> spans, OptionSet? options = null, CancellationToken cancellationToken = default)
{
var service = document.GetLanguageService<ImportAdderService>();
if (service != null)
{
return service.AddImportsAsync(document, spans, ImportAdderService.Strategy.AddImportsFromSyntaxes, options, cancellationToken);
}
else
{
return Task.FromResult(document);
}
}
/// <summary>
/// Adds namespace imports / using directives for namespace references found in the document.
/// </summary>
internal static async Task<Document> AddImportsFromSymbolAnnotationAsync(Document document, OptionSet? options = null, CancellationToken cancellationToken = default)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(root);
return await AddImportsFromSymbolAnnotationAsync(document, root.FullSpan, options, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Adds namespace imports / using directives for namespace references found in the document within the span specified.
/// </summary>
internal static Task<Document> AddImportsFromSymbolAnnotationAsync(Document document, TextSpan span, OptionSet? options = null, CancellationToken cancellationToken = default)
=> AddImportsFromSymbolAnnotationAsync(document, new[] { span }, options, cancellationToken);
/// <summary>
/// Adds namespace imports / using directives for namespace references found in the document within the sub-trees annotated with the <see cref="SyntaxAnnotation"/>.
/// </summary>
internal static async Task<Document> AddImportsFromSymbolAnnotationAsync(Document document, SyntaxAnnotation annotation, OptionSet? options = null, CancellationToken cancellationToken = default)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(root);
return await AddImportsFromSymbolAnnotationAsync(document, root.GetAnnotatedNodesAndTokens(annotation).Select(t => t.FullSpan), options, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Adds namespace imports / using directives for namespace references found in the document within the spans specified.
/// </summary>
internal static Task<Document> AddImportsFromSymbolAnnotationAsync(Document document, IEnumerable<TextSpan> spans, OptionSet? options = null, CancellationToken cancellationToken = default)
{
var service = document.GetLanguageService<ImportAdderService>();
if (service != null)
{
return service.AddImportsAsync(document, spans, ImportAdderService.Strategy.AddImportsFromSymbolAnnotations, options, cancellationToken);
}
else
{
return Task.FromResult(document);
}
}
}
}