Skip to content

Commit 6fb917f

Browse files
committed
- Reintroduce support for WrapperList third party types.
- Small update to readme to indicate this library is an incremental source generator.
1 parent d4a9447 commit 6fb917f

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

DecoratorGenerator/Main.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.CodeAnalysis.Text;
44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using System.Text;
78
using System.Threading;
89

@@ -18,7 +19,10 @@ public void Initialize(IncrementalGeneratorInitializationContext context) {
1819
transform: GetSemanticTargetForGeneration
1920
);
2021

22+
var thirdPartyTypes = context.CompilationProvider.SelectMany(GetThirdPartySemanticTargetsForGeneration);
23+
2124
context.RegisterSourceOutput(types, Execute);
25+
context.RegisterSourceOutput(thirdPartyTypes, Execute);
2226
}
2327

2428
private static bool IsSyntaxTargetForGeneration(SyntaxNode syntaxNode, CancellationToken token) {
@@ -37,5 +41,41 @@ private static void Execute(SourceProductionContext context, INamedTypeSymbol ty
3741

3842
context.AddSource($"{className}.generated.cs", SourceText.From(source, Encoding.UTF8, SourceHashAlgorithm.Sha256));
3943
}
44+
45+
private IEnumerable<INamedTypeSymbol> GetThirdPartySemanticTargetsForGeneration(Compilation compilation, CancellationToken _) {
46+
var wrapperSymbolWithoutNamespace = compilation.Assembly.GetTypeByMetadataName("WrapperList");
47+
48+
var wrapperListTypes =
49+
(wrapperSymbolWithoutNamespace == null)
50+
? GetAllTypes(compilation.Assembly.GlobalNamespace, x => x.Name == "WrapperList")
51+
: new[] { wrapperSymbolWithoutNamespace };
52+
53+
return
54+
wrapperListTypes.SelectMany(x => x.GetMembers()
55+
.Where(m => m.Name != ".ctor")
56+
.Select(m => m as IFieldSymbol)
57+
.Select(f => f.Type)
58+
.Select(t => t as INamedTypeSymbol));
59+
}
60+
61+
/// <summary>
62+
/// Gets all Types inside the namespace matching the predicate including nested namespaces.
63+
/// </summary>
64+
/// <param name="input"></param>
65+
/// <param name="predicate"></param>
66+
/// <returns></returns>
67+
private IEnumerable<INamedTypeSymbol> GetAllTypes(INamespaceSymbol input, Func<INamedTypeSymbol, bool> predicate) {
68+
foreach (var space in input.GetNamespaceMembers()) {
69+
foreach (var item in space.GetTypeMembers()) {
70+
if (predicate(item)) {
71+
yield return item;
72+
}
73+
}
74+
75+
foreach (var nestedItem in GetAllTypes(space, predicate)) {
76+
yield return nestedItem;
77+
}
78+
}
79+
}
4080
}
4181
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public interface ICat
4040
}
4141
```
4242

43-
Build the project so the abstract class is generated for the interface. The generated class will be named after the interface, but without the `I` prefix. In this case, since the interface is `ICat` the class will be `CatDecorator`. Then create your decorator class as usual:
43+
Since this library is an [incremental source generator](https://github.com/dotnet/roslyn/blob/d8c21d64ca958840bdaa2898cb2324397dc57bbb/docs/features/incremental-generators.md), the abstract class should be generated after saving the changes to interface's file. The generated class will be named after the interface, but without the `I` prefix. In this case, since the interface is `ICat` the class will be `CatDecorator`. Then create your decorator class as usual:
4444

4545
```c#
4646
namespace SampleLibrary;

0 commit comments

Comments
 (0)