RSCG–ISourceGenerator porting to IIncrementalGenerator in 3 steps
Roslyn Source Code Generators (RSCG) have deprecated the ISourceGenerator (roslyn/source-generators.md at main · dotnet/roslyn · GitHub ) vs the new interface IIncrementalGenerator , roslyn/incremental-generators.md at main · dotnet/roslyn · GitHub . That because of the idea of speed of generating code – and the idea of not generating twice the same code.
How is the implementation of IIncrementalGenerator , when you have already an ISourceGenerator ?
I have moved the https://github.com/ignatandrei/rsCG_timeBombComment/ ( the generates code to transform comments / obsolete attribute as errors , after a time) and https://github.com/ignatandrei/SkinnyControllersGenerator/ those are my findings
- Read the theory from https://github.com/dotnet/roslyn/blob/main/docs/features/incremental-generators.md . The ISourceGenerator was simpler to understand. After you read you can see how the IIncrementalGenerator makes sense for increasing performance of RSCG.
- The code from ISourceGenerator could be copy and paste to IIncrementalGenerator . The code from ISourceGenerator.Initialize belongs now to context.SyntaxProvider.CreateSyntaxProvider –> predicate , where the code that obtain the datas is written after this
- The real code that generates the new source code belong to context.RegisterSourceOutput
There are some problems
- Register a diagnostic occurs later – could be fixed with Analyzers – see Incremental Roslyn Source Generators In .NET 6: Better Experience Through Roslyn Analyzers & Code Fixes – Part 2 – Thinktecture AG
- The additional files also occurs later – could be fixed by having a prefix in the file name in the order to not load all files content
Overall , this is a nicer way to wrote code ( although the learning curve is bigger). You can see the result at file RSCG_TimeBombComment\src\RSCG_TimeBombComment\RSCG_TimeBombComment\GenerateFromComments.cs vs RSCG_TimeBombComment\src\RSCG_TimeBombComment\RSCG_TimeBombComment\GenerateFromCommentsIncremental.cs from repo https://github.com/ignatandrei/rsCG_timeBombComment/
If you want to see more RSCG with the new method, keep an eye to https://ignatandrei.github.io/RSCG_Examples/v2/