/
Simplifier.cs
204 lines (177 loc) · 9.49 KB
/
Simplifier.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
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.Simplification
{
/// <summary>
/// Expands and Reduces subtrees.
///
/// Expansion:
/// 1) Makes inferred names explicit (on anonymous types and tuples).
/// 2) Replaces names with fully qualified dotted names.
/// 3) Adds parentheses around expressions
/// 4) Adds explicit casts/conversions where implicit conversions exist
/// 5) Adds escaping to identifiers
/// 6) Rewrites extension method invocations with explicit calls on the class containing the extension method.
///
/// Reduction:
/// 1) Shortens dotted names to their minimally qualified form
/// 2) Removes unnecessary parentheses
/// 3) Removes unnecessary casts/conversions
/// 4) Removes unnecessary escaping
/// 5) Rewrites explicit calls to extension methods to use dot notation
/// 6) Removes unnecessary tuple element names and anonymous type member names
/// </summary>
public static partial class Simplifier
{
/// <summary>
/// The annotation the reducer uses to identify sub trees to be reduced.
/// The Expand operations add this annotation to nodes so that the Reduce operations later find them.
/// </summary>
public static SyntaxAnnotation Annotation { get; } = new SyntaxAnnotation();
/// <summary>
/// This is the annotation used by the simplifier and expander to identify Predefined type and preserving
/// them from over simplification
/// </summary>
public static SyntaxAnnotation SpecialTypeAnnotation { get; } = new SyntaxAnnotation();
/// <summary>
/// Expand qualifying parts of the specified subtree, annotating the parts using the <see cref="Annotation" /> annotation.
/// </summary>
public static async Task<TNode> ExpandAsync<TNode>(TNode node, Document document, Func<SyntaxNode, bool> expandInsideNode = null, bool expandParameter = false, CancellationToken cancellationToken = default) where TNode : SyntaxNode
{
if (node == null)
{
throw new ArgumentNullException(nameof(node));
}
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
return Expand(node, semanticModel, document.Project.Solution.Workspace, expandInsideNode, expandParameter, cancellationToken);
}
/// <summary>
/// Expand qualifying parts of the specified subtree, annotating the parts using the <see cref="Annotation" /> annotation.
/// </summary>
public static TNode Expand<TNode>(TNode node, SemanticModel semanticModel, Workspace workspace, Func<SyntaxNode, bool> expandInsideNode = null, bool expandParameter = false, CancellationToken cancellationToken = default) where TNode : SyntaxNode
{
if (node == null)
{
throw new ArgumentNullException(nameof(node));
}
if (semanticModel == null)
{
throw new ArgumentNullException(nameof(semanticModel));
}
if (workspace == null)
{
throw new ArgumentNullException(nameof(workspace));
}
var result = workspace.Services.GetLanguageServices(node.Language).GetService<ISimplificationService>()
.Expand(node, semanticModel, annotationForReplacedAliasIdentifier: null, expandInsideNode: expandInsideNode, expandParameter: expandParameter, cancellationToken: cancellationToken);
return (TNode)result;
}
/// <summary>
/// Expand qualifying parts of the specified subtree, annotating the parts using the <see cref="Annotation" /> annotation.
/// </summary>
public static async Task<SyntaxToken> ExpandAsync(SyntaxToken token, Document document, Func<SyntaxNode, bool> expandInsideNode = null, CancellationToken cancellationToken = default)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
return Expand(token, semanticModel, document.Project.Solution.Workspace, expandInsideNode, cancellationToken);
}
/// <summary>
/// Expand qualifying parts of the specified subtree, annotating the parts using the <see cref="Annotation" /> annotation.
/// </summary>
public static SyntaxToken Expand(SyntaxToken token, SemanticModel semanticModel, Workspace workspace, Func<SyntaxNode, bool> expandInsideNode = null, CancellationToken cancellationToken = default)
{
if (semanticModel == null)
{
throw new ArgumentNullException(nameof(semanticModel));
}
if (workspace == null)
{
throw new ArgumentNullException(nameof(workspace));
}
return workspace.Services.GetLanguageServices(token.Language).GetService<ISimplificationService>()
.Expand(token, semanticModel, expandInsideNode, cancellationToken);
}
/// <summary>
/// Reduce all sub-trees annotated with <see cref="Annotation" /> found within the document. The annotated node and all child nodes will be reduced.
/// </summary>
public static async Task<Document> ReduceAsync(Document document, OptionSet optionSet = null, CancellationToken cancellationToken = default)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
return await ReduceAsync(document, root.FullSpan, optionSet, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Reduce the sub-trees annotated with <see cref="Annotation" /> found within the subtrees identified with the specified <paramref name="annotation"/>.
/// The annotated node and all child nodes will be reduced.
/// </summary>
public static async Task<Document> ReduceAsync(Document document, SyntaxAnnotation annotation, OptionSet optionSet = null, CancellationToken cancellationToken = default)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
if (annotation == null)
{
throw new ArgumentNullException(nameof(annotation));
}
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
return await ReduceAsync(document, root.GetAnnotatedNodesAndTokens(annotation).Select(t => t.FullSpan), optionSet, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Reduce the sub-trees annotated with <see cref="Annotation" /> found within the specified span.
/// The annotated node and all child nodes will be reduced.
/// </summary>
public static Task<Document> ReduceAsync(Document document, TextSpan span, OptionSet optionSet = null, CancellationToken cancellationToken = default)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
return ReduceAsync(document, SpecializedCollections.SingletonEnumerable(span), optionSet, cancellationToken);
}
/// <summary>
/// Reduce the sub-trees annotated with <see cref="Annotation" /> found within the specified spans.
/// The annotated node and all child nodes will be reduced.
/// </summary>
public static Task<Document> ReduceAsync(Document document, IEnumerable<TextSpan> spans, OptionSet optionSet = null, CancellationToken cancellationToken = default)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
if (spans == null)
{
throw new ArgumentNullException(nameof(spans));
}
return document.GetLanguageService<ISimplificationService>().ReduceAsync(
document, spans.ToImmutableArrayOrEmpty(), optionSet, cancellationToken: cancellationToken);
}
internal static async Task<Document> ReduceAsync(
Document document, ImmutableArray<AbstractReducer> reducers, OptionSet optionSet = null, CancellationToken cancellationToken = default)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
return await document.GetLanguageService<ISimplificationService>()
.ReduceAsync(document, ImmutableArray.Create(root.FullSpan), optionSet,
reducers, cancellationToken).ConfigureAwait(false);
}
}
}