diff --git a/src/tools/Avalonia.Generators/NameGenerator/AvaloniaNameGenerator.cs b/src/tools/Avalonia.Generators/NameGenerator/AvaloniaNameGenerator.cs index 8d45afafa7f..fa20be721c1 100644 --- a/src/tools/Avalonia.Generators/NameGenerator/AvaloniaNameGenerator.cs +++ b/src/tools/Avalonia.Generators/NameGenerator/AvaloniaNameGenerator.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using Avalonia.Generators.Common.Domain; using Microsoft.CodeAnalysis; @@ -31,7 +32,7 @@ public AvaloniaNameGenerator( _code = code; } - public IReadOnlyList GenerateNameReferences(IEnumerable additionalFiles) + public IEnumerable GenerateNameReferences(IEnumerable additionalFiles, CancellationToken cancellationToken) { var resolveViews = from file in additionalFiles @@ -39,7 +40,8 @@ from file in additionalFiles 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; @@ -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 diff --git a/src/tools/Avalonia.Generators/NameGenerator/AvaloniaNameSourceGenerator.cs b/src/tools/Avalonia.Generators/NameGenerator/AvaloniaNameSourceGenerator.cs index fa1dcde0646..407cf4f35fb 100644 --- a/src/tools/Avalonia.Generators/NameGenerator/AvaloniaNameSourceGenerator.cs +++ b/src/tools/Avalonia.Generators/NameGenerator/AvaloniaNameSourceGenerator.cs @@ -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) { diff --git a/src/tools/Avalonia.Generators/NameGenerator/INameGenerator.cs b/src/tools/Avalonia.Generators/NameGenerator/INameGenerator.cs index 922d8003329..5b44de43c1a 100644 --- a/src/tools/Avalonia.Generators/NameGenerator/INameGenerator.cs +++ b/src/tools/Avalonia.Generators/NameGenerator/INameGenerator.cs @@ -1,11 +1,12 @@ using System.Collections.Generic; +using System.Threading; using Microsoft.CodeAnalysis; namespace Avalonia.Generators.NameGenerator; internal interface INameGenerator { - IReadOnlyList GenerateNameReferences(IEnumerable additionalFiles); + IEnumerable GenerateNameReferences(IEnumerable additionalFiles, CancellationToken cancellationToken); } internal record GeneratedPartialClass(string FileName, string Content);