Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CancellationToken in AvaloniaNameGenerator #12043

Merged
merged 4 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,16 @@ 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 (Exception exception)
jmacato marked this conversation as resolved.
Show resolved Hide resolved
{
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);