Skip to content

Commit

Permalink
Merge pull request #12043 from Altua/generator-cancellation-token
Browse files Browse the repository at this point in the history
Use CancellationToken in AvaloniaNameGenerator
  • Loading branch information
jmacato authored Jul 6, 2023
2 parents 605c880 + f0b5220 commit 9806f27
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Avalonia.Generators.Common.Domain;
using Microsoft.CodeAnalysis;

Expand Down Expand Up @@ -31,15 +32,16 @@ public AvaloniaNameGenerator(
_code = code;
}

public IReadOnlyList<GeneratedPartialClass> GenerateNameReferences(IEnumerable<AdditionalText> additionalFiles)
public IEnumerable<GeneratedPartialClass> GenerateNameReferences(IEnumerable<AdditionalText> additionalFiles, CancellationToken cancellationToken)
{
var resolveViews =
from file in additionalFiles
where (file.Path.EndsWith(".xaml") ||
file.Path.EndsWith(".paml") ||
file.Path.EndsWith(".axaml")) &&
_pathPattern.Matches(file.Path)
let xaml = file.GetText()!.ToString()
let xaml = file.GetText(cancellationToken)?.ToString()
where xaml != null
let view = _classes.ResolveView(xaml)
where view != null && _namespacePattern.Matches(view.Namespace)
select view;
Expand All @@ -51,7 +53,7 @@ from view in resolveViews
let fileName = ResolveViewFileName(view, _naming)
select new GeneratedPartialClass(fileName, code);

return query.ToList();
return query;
}

private static string ResolveViewFileName(ResolvedView view, ViewFileNamingStrategy strategy) => strategy switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,19 @@ public void Execute(GeneratorExecutionContext context)
return;
}

var partials = generator.GenerateNameReferences(context.AdditionalFiles);
foreach (var (fileName, content) in partials) context.AddSource(fileName, content);
var partials = generator.GenerateNameReferences(context.AdditionalFiles, context.CancellationToken);
foreach (var (fileName, content) in partials)
{
if(context.CancellationToken.IsCancellationRequested)
{
break;
}

context.AddSource(fileName, content);
}
}
catch (OperationCanceledException)
{
}
catch (Exception exception)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Collections.Generic;
using System.Threading;
using Microsoft.CodeAnalysis;

namespace Avalonia.Generators.NameGenerator;

internal interface INameGenerator
{
IReadOnlyList<GeneratedPartialClass> GenerateNameReferences(IEnumerable<AdditionalText> additionalFiles);
IEnumerable<GeneratedPartialClass> GenerateNameReferences(IEnumerable<AdditionalText> additionalFiles, CancellationToken cancellationToken);
}

internal record GeneratedPartialClass(string FileName, string Content);

0 comments on commit 9806f27

Please sign in to comment.