Skip to content

Commit

Permalink
Combine identical dependencies in comments into one for smart tags
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Dec 26, 2024
1 parent 6c0956c commit f7af60f
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/Pure.DI.Core/Core/Code/TagClassBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => $"/// <item>{k.target} &lt;-- {k.injectin}{k.tag} -- {k.source} as {k.sourceLifetime}</item>")
.Distinct()
.OrderBy(k => k))));

cancellationToken.ThrowIfCancellationRequested();

var code = new LinesBuilder();
var tags = smartTags.GetAll();
// ReSharper disable once InvertIf
code.AppendLine("// <auto-generated/>");
code.AppendLine($"// by {information.Description}");

Expand All @@ -49,23 +64,18 @@ where dependency.Injection.Tag is string

code.AppendLine("/// <summary>");
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("/// <br/>");
var groupByComposition = dependencies.GroupBy(i => i.composition.Source.Source.Name.FullName);
foreach (var compositionGroup in groupByComposition)
foreach (var (composition, dependencies) in dependenciesByComposition)
{
code.AppendLine("/// <br/>");
code.AppendLine($"/// class {formatter.FormatRef(compositionGroup.Key)}");
code.AppendLine($"/// class {formatter.FormatRef(composition.Source.Source.Name.FullName)}");
code.AppendLine("/// <list type=\"number\">");
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($"/// <item>{formatter.FormatRef(target.Type)} &lt;-- {injectionTypeStr}{tagStr} -- {formatter.FormatRef(source.Type)} as {formatter.FormatRef(source.Lifetime)}</item>");
code.AppendLine(dependency);
}

code.AppendLine("/// </list>");
Expand Down

0 comments on commit f7af60f

Please sign in to comment.