Skip to content

Commit

Permalink
use cancellationToken
Browse files Browse the repository at this point in the history
  • Loading branch information
kzrnm committed Nov 29, 2020
1 parent 9d0129c commit 19bb79e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Source/SourceExpander.Embedder/EmbedderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public void Execute(GeneratorExecutionContext context)
var resolver = new EmbeddingResolver(
(CSharpCompilation)context.Compilation,
(CSharpParseOptions)context.ParseOptions,
new DiagnosticReporter(context));
new DiagnosticReporter(context),
context.CancellationToken);

foreach (var (path, source) in resolver.EnumerateEmbeddingSources())
{
Expand Down
22 changes: 14 additions & 8 deletions Source/SourceExpander.Embedder/EmbeddingResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -17,14 +18,18 @@ public class EmbeddingResolver
private readonly CSharpCompilation compilation;
private readonly CSharpParseOptions parseOptions;
private readonly IDiagnosticReporter reporter;

public EmbeddingResolver(CSharpCompilation compilation, CSharpParseOptions parseOptions, IDiagnosticReporter reporter)
private readonly CancellationToken cancellationToken;
public EmbeddingResolver(
CSharpCompilation compilation,
CSharpParseOptions parseOptions,
IDiagnosticReporter reporter,
CancellationToken cancellationToken = default)
{
this.compilation = compilation;
this.parseOptions = parseOptions;
this.reporter = reporter;
this.cancellationToken = cancellationToken;
}

public IEnumerable<(string name, SourceText sourceText)> EnumerateEmbeddingSources()
{
var infos = ResolveFiles();
Expand Down Expand Up @@ -78,11 +83,11 @@ private IEnumerable<SourceFileInfo> ResolveRaw(IEnumerable<SourceFileInfoRaw> in
IEnumerable<string> GetDependencies(SourceFileInfoRaw raw)
{
var tree = raw.SyntaxTree;
var root = (CompilationUnitSyntax)tree.GetRoot();
var root = (CompilationUnitSyntax)tree.GetRoot(cancellationToken);

var semanticModel = compilation.GetSemanticModel(tree, true);
var typeQueue = new Queue<string>(root.DescendantNodes()
.Select(s => GetTypeNameFromSymbol(semanticModel.GetSymbolInfo(s).Symbol?.OriginalDefinition))
.Select(s => GetTypeNameFromSymbol(semanticModel.GetSymbolInfo(s, cancellationToken).Symbol?.OriginalDefinition))
.OfType<string>()
.Distinct());

Expand Down Expand Up @@ -115,7 +120,7 @@ IEnumerable<string> GetDependencies(SourceFileInfoRaw raw)
private SourceFileInfoRaw ParseSource(SyntaxTree tree, string commonPrefix)
{
var semanticModel = compilation.GetSemanticModel(tree, true);
var root = (CompilationUnitSyntax)tree.GetRoot();
var root = (CompilationUnitSyntax)tree.GetRoot(cancellationToken);
var usings = root.Usings.Select(u => u.ToString().Trim()).ToArray();

var remover = new MinifyRewriter();
Expand All @@ -128,13 +133,14 @@ private SourceFileInfoRaw ParseSource(SyntaxTree tree, string commonPrefix)

var typeNames = root.DescendantNodes()
.Where(s => s is BaseTypeDeclarationSyntax || s is DelegateDeclarationSyntax)
.Select(syntax => semanticModel.GetDeclaredSymbol(syntax)?.ToDisplayString())
.Select(syntax => semanticModel.GetDeclaredSymbol(syntax, cancellationToken)?.ToDisplayString())
.OfType<string>()
.Distinct()
.ToArray();

return new SourceFileInfoRaw(tree, fileName, typeNames, usings,
remover.Visit(CSharpSyntaxTree.ParseText(newRoot.ToString()).GetRoot())!.ToString());
remover.Visit(CSharpSyntaxTree.ParseText(newRoot.ToString(), cancellationToken: cancellationToken)
.GetRoot(cancellationToken))!.ToString());
}

public bool HasType(string typeFullName)
Expand Down

0 comments on commit 19bb79e

Please sign in to comment.