/
SymbolFinder.cs
235 lines (210 loc) · 10.6 KB
/
SymbolFinder.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// 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.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.FindSymbols
{
public static partial class SymbolFinder
{
/// <summary>
/// Obsolete. Use <see cref="FindSymbolAtPositionAsync(SemanticModel, int, Workspace, CancellationToken)"/>.
/// </summary>
[Obsolete("Use FindSymbolAtPositionAsync instead.")]
public static ISymbol FindSymbolAtPosition(
SemanticModel semanticModel,
int position,
Workspace workspace,
CancellationToken cancellationToken = default)
{
return FindSymbolAtPositionAsync(semanticModel, position, workspace, cancellationToken).WaitAndGetResult(cancellationToken);
}
/// <summary>
/// Finds the symbol that is associated with a position in the text of a document.
/// </summary>
/// <param name="semanticModel">The semantic model associated with the document.</param>
/// <param name="position">The character position within the document.</param>
/// <param name="workspace">A workspace to provide context.</param>
/// <param name="cancellationToken">A CancellationToken.</param>
public static async Task<ISymbol> FindSymbolAtPositionAsync(
SemanticModel semanticModel,
int position,
Workspace workspace,
CancellationToken cancellationToken = default)
{
var semanticInfo = await GetSemanticInfoAtPositionAsync(
semanticModel, position, workspace, cancellationToken: cancellationToken).ConfigureAwait(false);
return semanticInfo.GetAnySymbol(includeType: false);
}
internal static async Task<TokenSemanticInfo> GetSemanticInfoAtPositionAsync(
SemanticModel semanticModel,
int position,
Workspace workspace,
CancellationToken cancellationToken)
{
var token = await GetTokenAtPositionAsync(semanticModel, position, workspace, cancellationToken).ConfigureAwait(false);
if (token != default &&
token.Span.IntersectsWith(position))
{
return semanticModel.GetSemanticInfo(token, workspace, cancellationToken);
}
return TokenSemanticInfo.Empty;
}
private static Task<SyntaxToken> GetTokenAtPositionAsync(
SemanticModel semanticModel,
int position,
Workspace workspace,
CancellationToken cancellationToken)
{
var syntaxTree = semanticModel.SyntaxTree;
var syntaxFacts = workspace.Services.GetLanguageServices(semanticModel.Language).GetService<ISyntaxFactsService>();
return syntaxTree.GetTouchingTokenAsync(position, syntaxFacts.IsBindableToken, cancellationToken, findInsideTrivia: true);
}
public static async Task<ISymbol> FindSymbolAtPositionAsync(
Document document,
int position,
CancellationToken cancellationToken = default)
{
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
return await FindSymbolAtPositionAsync(semanticModel, position, document.Project.Solution.Workspace, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Finds the definition symbol declared in source code for a corresponding reference symbol.
/// Returns null if no such symbol can be found in the specified solution.
/// </summary>
public static async Task<ISymbol> FindSourceDefinitionAsync(
ISymbol symbol, Solution solution, CancellationToken cancellationToken = default)
{
var result = await FindSourceDefinitionAsync(
SymbolAndProjectId.Create(symbol, projectId: null),
solution, cancellationToken).ConfigureAwait(false);
return result.Symbol;
}
internal static Task<SymbolAndProjectId> FindSourceDefinitionAsync(
SymbolAndProjectId symbolAndProjectId, Solution solution, CancellationToken cancellationToken = default)
{
var symbol = symbolAndProjectId.Symbol;
if (symbol != null)
{
symbol = symbol.GetOriginalUnreducedDefinition();
symbolAndProjectId = symbolAndProjectId.WithSymbol(symbol);
switch (symbol.Kind)
{
case SymbolKind.Event:
case SymbolKind.Field:
case SymbolKind.Method:
case SymbolKind.Local:
case SymbolKind.NamedType:
case SymbolKind.Parameter:
case SymbolKind.Property:
case SymbolKind.TypeParameter:
case SymbolKind.Namespace:
return FindSourceDefinitionWorkerAsync(symbolAndProjectId, solution, cancellationToken);
}
}
return SpecializedTasks.Default<SymbolAndProjectId>();
}
private static async Task<SymbolAndProjectId> FindSourceDefinitionWorkerAsync(
SymbolAndProjectId symbolAndProjectId,
Solution solution,
CancellationToken cancellationToken)
{
var symbol = symbolAndProjectId.Symbol;
// If it's already in source, then we might already be done
if (InSource(symbol))
{
// If our symbol doesn't have a containing assembly, there's nothing better we can do to map this
// symbol somewhere else. The common case for this is a merged INamespaceSymbol that spans assemblies.
if (symbol.ContainingAssembly == null)
{
return symbolAndProjectId;
}
// Just because it's a source symbol doesn't mean we have the final symbol we actually want. In retargeting cases,
// the retargeted symbol is from "source" but isn't equal to the actual source definition in the other project. Thus,
// we only want to return symbols from source here if it actually came from a project's compilation's assembly. If it isn't
// then we have a retargeting scenario and want to take our usual path below as if it was a metadata reference
foreach (var sourceProject in solution.Projects)
{
// If our symbol is actually a "regular" source symbol, then we know the compilation is holding the symbol alive
// and thus TryGetCompilation is sufficient. For another example of this pattern, see Solution.GetProject(IAssemblySymbol)
// which we happen to call below.
if (sourceProject.TryGetCompilation(out var compilation))
{
if (symbol.ContainingAssembly.Equals(compilation.Assembly))
{
return SymbolAndProjectId.Create(symbol, sourceProject.Id);
}
}
}
}
else if (!symbol.Locations.Any(loc => loc.IsInMetadata))
{
// We have a symbol that's neither in source nor metadata
return default;
}
var project = solution.GetProject(symbol.ContainingAssembly, cancellationToken);
if (project != null && project.SupportsCompilation)
{
var symbolId = symbol.GetSymbolKey();
var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
var result = symbolId.Resolve(compilation, ignoreAssemblyKey: true, cancellationToken: cancellationToken);
if (result.Symbol != null && InSource(result.Symbol))
{
return SymbolAndProjectId.Create(result.Symbol, project.Id);
}
else
{
return SymbolAndProjectId.Create(result.CandidateSymbols.FirstOrDefault(InSource), project.Id);
}
}
return default;
}
private static bool InSource(ISymbol symbol)
{
if (symbol == null)
{
return false;
}
return symbol.Locations.Any(loc => loc.IsInSource);
}
/// <summary>
/// Finds symbols in the given compilation that are similar to the specified symbol.
///
/// A found symbol may be the exact same symbol instance if the compilation is the origin of the specified symbol,
/// or it may be a different symbol instance if the compilation is not the originating compilation.
///
/// Multiple symbols may be returned if there are ambiguous matches.
/// No symbols may be returned if the compilation does not define or have access to a similar symbol.
/// </summary>
/// <param name="symbol">The symbol to find corresponding matches for.</param>
/// <param name="compilation">A compilation to find the corresponding symbol within. The compilation may or may not be the origin of the symbol.</param>
/// <param name="cancellationToken">A CancellationToken.</param>
/// <returns></returns>
public static IEnumerable<TSymbol> FindSimilarSymbols<TSymbol>(TSymbol symbol, Compilation compilation, CancellationToken cancellationToken = default)
where TSymbol : ISymbol
{
if (symbol == null)
{
throw new ArgumentNullException(nameof(symbol));
}
if (compilation == null)
{
throw new ArgumentNullException(nameof(compilation));
}
var key = symbol.GetSymbolKey();
// We may be talking about different compilations. So do not try to resolve locations.
var result = new HashSet<TSymbol>();
var resolution = key.Resolve(compilation, resolveLocations: false, cancellationToken: cancellationToken);
foreach (var current in resolution.OfType<TSymbol>())
{
result.Add(current);
}
return result;
}
}
}