Move SemanticModelProvider to shared generation project
このコミットが含まれているのは:
1 親
9a51331910
コミット
aaf8392785
12ファイル変更
+11
-57
検索条件に一致するファイルはありません
@@ -15,5 +15,6 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\IncrementalValuesProviderExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ISymbolExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\HashCode.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\SemanticModelProvider.cs" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,47 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// A type providing <see cref="SemanticModel"/> instances for nodes.
|
||||
/// </summary>
|
||||
internal sealed class SemanticModelProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// The source <see cref="Compilation"/> instance.
|
||||
/// </summary>
|
||||
private readonly Compilation compilation;
|
||||
|
||||
/// <summary>
|
||||
/// The map of loaded <see cref="SemanticModel"/> instances.
|
||||
/// </summary>
|
||||
private readonly Dictionary<SyntaxTree, SemanticModel> semanticModelsMap;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="SemanticModelProvider"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="compilation">The source <see cref="Compilation"/> instance.</param>
|
||||
public SemanticModelProvider(Compilation compilation)
|
||||
{
|
||||
this.compilation = compilation;
|
||||
this.semanticModelsMap = new Dictionary<SyntaxTree, SemanticModel>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="SemanticModel"/> instance with info on a given <see cref="SyntaxNode"/>.
|
||||
/// </summary>
|
||||
/// <param name="syntaxNode">The input <see cref="SyntaxNode"/> to get info for.</param>
|
||||
/// <returns>A <see cref="SemanticModel"/> instance containing info on <paramref name="syntaxNode"/>.</returns>
|
||||
public SemanticModel For(SyntaxNode syntaxNode)
|
||||
{
|
||||
if (!this.semanticModelsMap.TryGetValue(syntaxNode.SyntaxTree, out SemanticModel semanticModel))
|
||||
{
|
||||
semanticModel = this.compilation.GetSemanticModel(syntaxNode.SyntaxTree);
|
||||
|
||||
this.semanticModelsMap.Add(syntaxNode.SyntaxTree, semanticModel);
|
||||
}
|
||||
|
||||
return semanticModel;
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする