From f7af60f3a53573bba510b0c1af1fbb023c5d5427 Mon Sep 17 00:00:00 2001 From: Nikolay Pianikov Date: Thu, 26 Dec 2024 12:09:04 +0300 Subject: [PATCH] Combine identical dependencies in comments into one for smart tags --- src/Pure.DI.Core/Core/Code/TagClassBuilder.cs | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/Pure.DI.Core/Core/Code/TagClassBuilder.cs b/src/Pure.DI.Core/Core/Code/TagClassBuilder.cs index 70dc277b..5bd48747 100644 --- a/src/Pure.DI.Core/Core/Code/TagClassBuilder.cs +++ b/src/Pure.DI.Core/Core/Code/TagClassBuilder.cs @@ -13,17 +13,32 @@ internal class TagClassBuilder( public TagCode Build(TagContext tagContext) { var tagToDependencies = ( - from composition in tagContext.Compositions - from dependency in composition.Source.Graph.Edges - where dependency.Injection.Tag is string - group (composition, dependency) by (string)dependency.Injection.Tag! - ).ToDictionary(i => i.Key, i => i.ToList()); + from composition in tagContext.Compositions + from dependency in composition.Source.Graph.Edges + let tag = dependency.Injection.Tag as string + where tag is not null + group dependency by (tag: tag!, composition) + into groupsByTagAndComposition + group groupsByTagAndComposition by groupsByTagAndComposition.Key.tag) + .ToDictionary( + i => i.Key, + i => i.Select(j => ( + j.Key.composition, + dependencies: j + .Select(k => ( + tag: comments.Escape(k.Injection.Tag != null && k.Injection.Tag is not MdTagOnSites ? $"({k.Injection.Tag})" : ""), + target: formatter.FormatRef(k.Target.Type), + injectin: k.Injection.Type.Equals(k.Source.Type, SymbolEqualityComparer.Default) ? "" : formatter.FormatRef(k.Injection.Type), + source: formatter.FormatRef(k.Source.Type), + sourceLifetime: formatter.FormatRef(k.Source.Lifetime))) + .Select(k => $"/// {k.target} <-- {k.injectin}{k.tag} -- {k.source} as {k.sourceLifetime}") + .Distinct() + .OrderBy(k => k)))); cancellationToken.ThrowIfCancellationRequested(); var code = new LinesBuilder(); var tags = smartTags.GetAll(); - // ReSharper disable once InvertIf code.AppendLine("// "); code.AppendLine($"// by {information.Description}"); @@ -49,23 +64,18 @@ where dependency.Injection.Tag is string code.AppendLine("/// "); code.AppendLine($"/// Atomically generated smart tag with value {comments.Escape(tag.Name.ValueToString())}."); - if (tagToDependencies.TryGetValue(tag.Name, out var dependencies)) + if (tagToDependencies.TryGetValue(tag.Name, out var dependenciesByComposition)) { code.AppendLine("/// It's used for:"); code.AppendLine("///
"); - var groupByComposition = dependencies.GroupBy(i => i.composition.Source.Source.Name.FullName); - foreach (var compositionGroup in groupByComposition) + foreach (var (composition, dependencies) in dependenciesByComposition) { code.AppendLine("///
"); - code.AppendLine($"/// class {formatter.FormatRef(compositionGroup.Key)}"); + code.AppendLine($"/// class {formatter.FormatRef(composition.Source.Source.Name.FullName)}"); code.AppendLine("/// "); - foreach (var (composition, (isResolved, source, injection, target)) in compositionGroup - .OrderBy(i => i.dependency.Target.Binding.Id) - .ThenBy(i => i.dependency.Source.Binding.Id)) + foreach (var dependency in dependencies) { - var tagStr = comments.Escape(injection.Tag != null && injection.Tag is not MdTagOnSites ? $"({injection.Tag})" : ""); - var injectionTypeStr = injection.Type.Equals(source.Type, SymbolEqualityComparer.Default) ? "" : formatter.FormatRef(injection.Type); - code.AppendLine($"/// {formatter.FormatRef(target.Type)} <-- {injectionTypeStr}{tagStr} -- {formatter.FormatRef(source.Type)} as {formatter.FormatRef(source.Lifetime)}"); + code.AppendLine(dependency); } code.AppendLine("/// ");