com.wallstop-studios.dxmessaging 3.0.1 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/CHANGELOG.md +211 -2
  2. package/Editor/Analyzers/DxMessagingConsoleHarvester.cs +69 -62
  3. package/Editor/Analyzers/Microsoft.CodeAnalysis.CSharp.dll.meta +3 -3
  4. package/Editor/Analyzers/Microsoft.CodeAnalysis.dll.meta +3 -3
  5. package/Editor/Analyzers/System.Collections.Immutable.dll.meta +3 -3
  6. package/Editor/Analyzers/System.Reflection.Metadata.dll.meta +3 -3
  7. package/Editor/Analyzers/System.Runtime.CompilerServices.Unsafe.dll.meta +3 -3
  8. package/Editor/Analyzers/WallstopStudios.DxMessaging.Analyzer.dll +0 -0
  9. package/Editor/Analyzers/WallstopStudios.DxMessaging.Analyzer.dll.meta +15 -2
  10. package/Editor/Analyzers/WallstopStudios.DxMessaging.SourceGenerators.dll +0 -0
  11. package/Editor/Analyzers/WallstopStudios.DxMessaging.SourceGenerators.dll.meta +2 -2
  12. package/Editor/AssemblyInfo.cs.meta +9 -1
  13. package/Editor/CustomEditors/MessageAwareComponentInspectorOverlay.cs +24 -15
  14. package/Editor/CustomEditors/MessagingComponentEditor.cs.meta +9 -1
  15. package/Editor/DxMessagingEditorIdle.cs +62 -0
  16. package/{Runtime/Core/Internal/TypedDispatchLinkIndex.cs.meta → Editor/DxMessagingEditorIdle.cs.meta} +1 -1
  17. package/Editor/DxMessagingEditorInitializer.cs +112 -15
  18. package/Editor/DxMessagingEditorInitializer.cs.meta +9 -1
  19. package/Editor/DxMessagingEditorLog.cs +32 -0
  20. package/Editor/DxMessagingEditorLog.cs.meta +11 -0
  21. package/Editor/Settings/DxMessagingBaseCallIgnoreSync.cs +135 -12
  22. package/Editor/Settings/DxMessagingSettings.cs +92 -31
  23. package/Editor/Settings/DxMessagingSettings.cs.meta +9 -1
  24. package/Editor/Settings/DxMessagingSettingsProvider.cs.meta +9 -1
  25. package/Editor/SetupCscRsp.cs +339 -173
  26. package/Editor/SetupCscRsp.cs.meta +9 -1
  27. package/Editor/Testing/MessagingComponentEditorHarness.cs +1 -1
  28. package/Editor/Testing/MessagingComponentEditorHarness.cs.meta +9 -1
  29. package/README.md +17 -18
  30. package/Runtime/AssemblyInfo.cs.meta +9 -1
  31. package/Runtime/Core/Attributes/DxAutoConstructorAttribute.cs.meta +9 -1
  32. package/Runtime/Core/Attributes/DxBroadcastMessageAttribute.cs.meta +9 -1
  33. package/Runtime/Core/Attributes/DxOptionalParameterAttribute.cs +2 -4
  34. package/Runtime/Core/Attributes/DxOptionalParameterAttribute.cs.meta +9 -1
  35. package/Runtime/Core/Attributes/DxTargetedMessageAttribute.cs.meta +9 -1
  36. package/Runtime/Core/Attributes/DxUntargetedMessageAttribute.cs.meta +9 -1
  37. package/Runtime/Core/Attributes/Il2CppSetOptionAttribute.cs +56 -0
  38. package/Runtime/Core/Attributes/Il2CppSetOptionAttribute.cs.meta +11 -0
  39. package/Runtime/Core/DataStructure/CyclicBuffer.cs +44 -26
  40. package/Runtime/Core/DataStructure/CyclicBuffer.cs.meta +9 -1
  41. package/Runtime/Core/Diagnostics/MessageEmissionData.cs.meta +9 -1
  42. package/Runtime/Core/Diagnostics/MessageRegistrationData.cs.meta +9 -1
  43. package/Runtime/Core/Diagnostics/MessageRegistrationType.cs.meta +9 -1
  44. package/Runtime/Core/Extensions/EnumExtensions.cs +6 -5
  45. package/Runtime/Core/Extensions/EnumExtensions.cs.meta +9 -1
  46. package/Runtime/Core/Extensions/IListExtensions.cs.meta +9 -1
  47. package/Runtime/Core/Extensions/MessageExtensions.cs +0 -60
  48. package/Runtime/Core/Helper/MessageCache.cs.meta +9 -1
  49. package/Runtime/Core/Helper/MessageHelperIndexer.cs.meta +9 -1
  50. package/Runtime/Core/InstanceId.cs +25 -1
  51. package/Runtime/Core/Internal/DxUnsafe.cs +60 -0
  52. package/Runtime/Core/Internal/DxUnsafe.cs.meta +11 -0
  53. package/Runtime/Core/Internal/FlatDispatch.cs +198 -0
  54. package/Runtime/Core/Internal/FlatDispatch.cs.meta +11 -0
  55. package/Runtime/Core/Internal/TypedSlots.cs +5 -21
  56. package/Runtime/Core/MessageBus/IMessageBus.cs +12 -12
  57. package/Runtime/Core/MessageBus/IMessageRegistrationBuilder.cs +1 -0
  58. package/Runtime/Core/MessageBus/Internal/BusSlots.cs +7 -6
  59. package/Runtime/Core/MessageBus/MessageBus.cs +2313 -2936
  60. package/Runtime/Core/MessageBus/MessageRegistrationBuilder.cs +187 -14
  61. package/Runtime/Core/MessageHandler.cs +1023 -1143
  62. package/Runtime/Core/MessageRegistrationToken.cs +425 -47
  63. package/Runtime/Core/Messages/GlobalStringMessage.cs.meta +9 -1
  64. package/Runtime/Core/Messages/ReflexiveMessage.cs.meta +9 -1
  65. package/Runtime/Core/Messages/StringMessage.cs.meta +9 -1
  66. package/Runtime/Unity/Integrations/Reflex/AssemblyInfo.cs.meta +9 -1
  67. package/Runtime/Unity/Integrations/VContainer/AssemblyInfo.cs.meta +9 -1
  68. package/Runtime/Unity/Integrations/Zenject/AssemblyInfo.cs.meta +9 -1
  69. package/Runtime/Unity/MessageAwareComponent.cs +46 -1
  70. package/Runtime/Unity/MessagingComponent.cs +43 -10
  71. package/Runtime/WallstopStudios.DxMessaging.asmdef +1 -1
  72. package/Samples~/DI/README.md +7 -7
  73. package/SourceGenerators/Directory.Build.props +50 -3
  74. package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxAutoConstructorGenerator.cs +96 -63
  75. package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxMessageIdGenerator.cs +745 -87
  76. package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxMessageIdGenerator.cs.meta +9 -1
  77. package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators.csproj +39 -46
  78. package/SourceGenerators/global.json +7 -0
  79. package/SourceGenerators/global.json.meta +7 -0
  80. package/package.json +27 -40
  81. package/Runtime/Core/Internal/TypedDispatchLinkIndex.cs +0 -51
@@ -19,6 +19,8 @@ namespace DxMessaging.Unity
19
19
  /// - <see cref="Awake"/> creates the token and calls <see cref="RegisterMessageHandlers"/>.
20
20
  /// - <see cref="OnEnable"/>/<see cref="OnDisable"/> enable/disable the token when
21
21
  /// <see cref="MessageRegistrationTiedToEnableStatus"/> is true.
22
+ /// - <see cref="OnEnable"/> can additionally re-create a released token when
23
+ /// <see cref="ReregisterOnEnableAfterRelease"/> is overridden to true.
22
24
  /// - <see cref="OnDestroy"/> disables the token and releases references.
23
25
  /// </remarks>
24
26
  /// <example>
@@ -61,6 +63,25 @@ namespace DxMessaging.Unity
61
63
  /// </summary>
62
64
  protected virtual bool MessageRegistrationTiedToEnableStatus => true;
63
65
 
66
+ /// <summary>
67
+ /// Controls whether enabling this component re-creates its registration token and replays
68
+ /// <see cref="RegisterMessageHandlers"/> after the token was released (for example via
69
+ /// <see cref="MessagingComponent.Release"/>).
70
+ /// </summary>
71
+ /// <remarks>
72
+ /// Default <c>false</c>: once released, the component stays unregistered across
73
+ /// enable/disable cycles until <see cref="MessagingComponent.Create"/> is invoked manually.
74
+ /// Override to return <c>true</c> to opt into automatic recovery on the next enable:
75
+ /// if no live token exists, a fresh one is created and <see cref="RegisterMessageHandlers"/>
76
+ /// replays exactly once; if a manual <see cref="MessagingComponent.Create"/> already minted
77
+ /// a fresh token since the release, that token is adopted as-is (the manual creator owns
78
+ /// staging, so nothing is replayed and nothing double-stages).
79
+ /// When <see cref="MessageRegistrationTiedToEnableStatus"/> is false, the recovery still
80
+ /// re-creates and stages but does NOT enable the new token; call
81
+ /// <see cref="MessageRegistrationToken.Enable"/> yourself, exactly as for the original token.
82
+ /// </remarks>
83
+ protected virtual bool ReregisterOnEnableAfterRelease => false;
84
+
64
85
  /// <summary>
65
86
  /// If true, registers demo handlers for <see cref="Core.Messages.StringMessage"/> and
66
87
  /// <see cref="Core.Messages.GlobalStringMessage"/>. Override and return <c>false</c> to disable.
@@ -138,9 +159,33 @@ namespace DxMessaging.Unity
138
159
 
139
160
  /// <summary>
140
161
  /// Enables the token if <see cref="MessageRegistrationTiedToEnableStatus"/> is true.
162
+ /// When <see cref="ReregisterOnEnableAfterRelease"/> is true and the token was released,
163
+ /// a fresh token is created and <see cref="RegisterMessageHandlers"/> replays first.
141
164
  /// </summary>
142
165
  protected virtual void OnEnable()
143
166
  {
167
+ if (
168
+ ReregisterOnEnableAfterRelease
169
+ && _messageRegistrationToken != null
170
+ && _messagingComponent != null
171
+ )
172
+ {
173
+ if (_messagingComponent.TryGetToken(this, out MessageRegistrationToken liveToken))
174
+ {
175
+ // A manual MessagingComponent.Create(this) between the release and
176
+ // this enable minted a fresh token the field does not know about;
177
+ // adopt it so Token/Enable() operate on the live registration. The
178
+ // manual creator owns staging in that case, so RegisterMessageHandlers
179
+ // is NOT replayed here (no double-staging).
180
+ _messageRegistrationToken = liveToken;
181
+ }
182
+ else
183
+ {
184
+ _messageRegistrationToken = _messagingComponent.Create(this);
185
+ RegisterMessageHandlers();
186
+ }
187
+ }
188
+
144
189
  if (MessageRegistrationTiedToEnableStatus)
145
190
  {
146
191
  _messageRegistrationToken?.Enable();
@@ -159,7 +204,7 @@ namespace DxMessaging.Unity
159
204
  Debug.LogError(
160
205
  $"[DxMessaging] {GetType().Name} appears to be missing a base.Awake() call; "
161
206
  + "no registration token exists, so this component will not receive messages. "
162
- + "See https://github.com/wallstop/DxMessaging/blob/master/docs/reference/analyzers.md#dxmsg006-missing-base-call",
207
+ + "See https://github.com/Ambiguous-Interactive/DxMessaging/blob/master/docs/reference/analyzers.md#dxmsg006-missing-base-call",
163
208
  this
164
209
  );
165
210
  }
@@ -185,8 +185,9 @@ namespace DxMessaging.Unity
185
185
  /// </summary>
186
186
  /// <param name="listener">Listener whose token should be released.</param>
187
187
  /// <remarks>
188
- /// Invokes <see cref="MessageRegistrationToken.Disable"/> and removes the listener from the internal cache.
189
- /// Safe to call multiple times.
188
+ /// Invokes <see cref="MessageRegistrationToken.Dispose"/> and removes the listener from the internal cache after
189
+ /// disposal succeeds. If token disposal throws, the listener remains cached so release can be retried. Safe to call
190
+ /// multiple times after a successful release.
190
191
  /// </remarks>
191
192
  public bool Release(MonoBehaviour listener)
192
193
  {
@@ -195,9 +196,10 @@ namespace DxMessaging.Unity
195
196
  return false;
196
197
  }
197
198
 
198
- if (_registeredListeners.Remove(listener, out MessageRegistrationToken token))
199
+ if (_registeredListeners.TryGetValue(listener, out MessageRegistrationToken token))
199
200
  {
200
- token?.Disable();
201
+ token?.Dispose();
202
+ _ = _registeredListeners.Remove(listener);
201
203
  return true;
202
204
  }
203
205
 
@@ -225,34 +227,65 @@ namespace DxMessaging.Unity
225
227
  /// <summary>
226
228
  /// Activates the underlying handler when this component becomes enabled.
227
229
  /// </summary>
230
+ /// <remarks>
231
+ /// When <see cref="emitMessagesWhenDisabled"/> is true, the Unity enable/disable
232
+ /// lifecycle leaves the handler untouched in BOTH directions, so an explicit
233
+ /// <see cref="ToggleMessageHandler"/> decision survives enable/disable cycles.
234
+ /// </remarks>
228
235
  public void OnEnable()
229
236
  {
230
- ToggleMessageHandler(true);
237
+ if (!emitMessagesWhenDisabled)
238
+ {
239
+ ToggleMessageHandler(true);
240
+ }
231
241
  }
232
242
 
233
243
  /// <summary>
234
244
  /// Deactivates the underlying handler when this component becomes disabled.
235
245
  /// </summary>
246
+ /// <remarks>
247
+ /// When <see cref="emitMessagesWhenDisabled"/> is true, disabling leaves the handler
248
+ /// active so the GameObject keeps emitting (and listeners whose tokens are not tied to
249
+ /// the Unity lifecycle keep receiving).
250
+ /// </remarks>
236
251
  public void OnDisable()
237
252
  {
238
- ToggleMessageHandler(false);
253
+ if (!emitMessagesWhenDisabled)
254
+ {
255
+ ToggleMessageHandler(false);
256
+ }
239
257
  }
240
258
 
241
259
  /// <summary>
242
260
  /// Explicitly toggle the underlying handler's active state.
243
261
  /// </summary>
244
262
  /// <param name="newActive">Desired active state.</param>
263
+ /// <remarks>
264
+ /// Explicit calls always win: unlike the Unity lifecycle, this method is never gated by
265
+ /// <see cref="emitMessagesWhenDisabled"/>.
266
+ /// </remarks>
245
267
  public void ToggleMessageHandler(bool newActive)
246
268
  {
247
- if (!newActive && emitMessagesWhenDisabled)
269
+ if (_messageHandler != null && _messageHandler.active != newActive)
248
270
  {
249
- return;
271
+ _messageHandler.active = newActive;
250
272
  }
273
+ }
251
274
 
252
- if (_messageHandler != null && _messageHandler.active != newActive)
275
+ /// <summary>
276
+ /// Fetches the live (created and not released) registration token for
277
+ /// <paramref name="listener"/> without the double-create warning
278
+ /// <see cref="Create(MonoBehaviour)"/> logs.
279
+ /// </summary>
280
+ internal bool TryGetToken(MonoBehaviour listener, out MessageRegistrationToken token)
281
+ {
282
+ if (listener == null)
253
283
  {
254
- _messageHandler.active = newActive;
284
+ token = null;
285
+ return false;
255
286
  }
287
+
288
+ return _registeredListeners.TryGetValue(listener, out token);
256
289
  }
257
290
 
258
291
  /// <summary>
@@ -6,7 +6,7 @@
6
6
  "excludePlatforms": [],
7
7
  "allowUnsafeCode": true,
8
8
  "overrideReferences": false,
9
- "precompiledReferences": ["System.Runtime.CompilerServices.Unsafe.dll"],
9
+ "precompiledReferences": [],
10
10
  "autoReferenced": true,
11
11
  "defineConstraints": [],
12
12
  "versionDefines": [],
@@ -31,15 +31,15 @@ Each sample shows:
31
31
  1. **Place the prefab**
32
32
  Drag `Prefabs/MessagingInstallerSample.prefab` into your test scene. The root object carries `MessagingComponentInstaller` with its provider handle already pointing at the global provider ScriptableObject.
33
33
 
34
- 1. **Hook up the container**
35
- - **Zenject**:
36
- - Add `DxMessagingRegistrationInstaller` (from [Runtime/Unity/Integrations](../../Runtime/Unity/Integrations/)) to your ProjectContext or scene installer list.
34
+ 1. **Hook up the container**
35
+ - **Zenject**:
36
+ - Add `DxMessagingRegistrationInstaller` (from [Runtime/Unity/Integrations](../../Runtime/Unity/Integrations/)) to your ProjectContext or scene installer list.
37
37
  - Drop [SampleInstaller.cs](./Zenject/SampleInstaller.cs) into your project and register it alongside other installers. When the scene runs, the installer resolves `IMessageRegistrationBuilder`, stages a `PlayerSpawned` listener, and activates via the Zenject lifecycle.
38
- - **VContainer**:
39
- - Define `VCONTAINER_PRESENT` and reference the optional extension under [VContainerRegistrationExtensions.cs](../../Runtime/Unity/Integrations/VContainerRegistrationExtensions.cs).
38
+ - **VContainer**:
39
+ - Define `VCONTAINER_PRESENT` and reference the optional extension under [VContainerRegistrationExtensions.cs](../../Runtime/Unity/Integrations/VContainer/VContainerRegistrationExtensions.cs).
40
40
  - Add [SampleLifetimeScope.cs](./VContainer/SampleLifetimeScope.cs) to the scene (or derive from it); the sample scope registers the builder and an entry point that emits/consumes `ScoreUpdated` messages each tick.
41
- - **Reflex**:
42
- - Enable `REFLEX_PRESENT` and install `DxMessagingRegistrationInstaller` into your container bootstrap.
41
+ - **Reflex**:
42
+ - Enable `REFLEX_PRESENT` and install `DxMessagingRegistrationInstaller` into your container bootstrap.
43
43
  - Include [SampleInstaller.cs](./Reflex/SampleInstaller.cs) in your installer chain. The sample service resolves `IMessageRegistrationBuilder`, subscribes to `PlayerAlert`, and can emit alerts via `EmitAlertFor`.
44
44
 
45
45
  1. **Emit a message**
@@ -1,9 +1,56 @@
1
1
  <Project>
2
- <PropertyGroup Condition="'$(MSBuildProjectName)' == 'WallstopStudios.DxMessaging.SourceGenerators.Tests'">
2
+ <!--
3
+ Redirect ALL build output for every project under SourceGenerators/ into the
4
+ repo's Unity-ignored, git-ignored .artifacts/ tree.
5
+
6
+ WHY: this repo IS a Unity UPM package and the repo root is the package root,
7
+ so Unity imports everything under it that is not in a Unity-ignored location.
8
+ Unity ignores files/folders whose names start with '.' (e.g. .artifacts) or
9
+ end with '~'. If a SourceGenerators project builds its obj/ or bin/ in-tree,
10
+ Unity imports those build outputs. The analyzer/source-generator DLLs there
11
+ have the SAME assembly names as the correctly-configured shipped analyzers in
12
+ Editor/Analyzers/, but their Unity-auto-generated .meta lack the
13
+ RoslynAnalyzer label / validateReferences:0 / all-platforms-disabled config.
14
+ Unity then fails to resolve their Microsoft.CodeAnalysis reference and the
15
+ duplicate shadows the real analyzers, so the source generator never runs and
16
+ every [Dx*Message] type fails to implement its generated interface
17
+ (CS0315/CS0452) project-wide.
18
+
19
+ Building these projects is REQUIRED by the dev workflow (the main projects'
20
+ PostBuildCopyAnalyzers target copies the freshly-built DLL into
21
+ Editor/Analyzers/), so the build MUST go somewhere Unity ignores.
22
+
23
+ HOW: redirect per-project obj/ and bin/ to
24
+ $(SolutionDir).artifacts/$(MSBuildProjectName)/{obj,bin}/$(Configuration)/.
25
+ BaseIntermediateOutputPath is set here because Directory.Build.props is
26
+ imported BEFORE the .NET SDK's default-path logic (Sdk.props) that derives
27
+ obj/ paths and before NuGet restore writes project.assets.json; setting it
28
+ here ensures even 'dotnet restore' writes obj/project.assets.json under
29
+ .artifacts/ instead of an in-tree obj/. IntermediateOutputPath (the
30
+ Configuration-scoped obj subfolder) and OutputPath (bin) are set for the
31
+ build itself. $(TargetPath) therefore resolves under .artifacts/, and the
32
+ PostBuildCopyAnalyzers targets still copy the built DLL into
33
+ Editor/Analyzers/ correctly.
34
+ -->
35
+ <PropertyGroup>
3
36
  <SolutionDir Condition="'$(SolutionDir)' == ''">$(MSBuildThisFileDirectory)..\</SolutionDir>
4
- <ArtifactsRoot>$(SolutionDir).artifacts/SourceGenerators.Tests/</ArtifactsRoot>
5
- <IntermediateOutputPath>$(ArtifactsRoot)obj/$(Configuration)/</IntermediateOutputPath>
37
+ <ArtifactsRoot Condition="'$(ArtifactsRoot)' == ''"
38
+ >$(SolutionDir).artifacts/$(MSBuildProjectName)/</ArtifactsRoot>
39
+ <!-- Set BaseIntermediateOutputPath early so NuGet restore's obj/ (and
40
+ project.assets.json) is written under .artifacts/, never in-tree. The
41
+ trailing slash is required by the SDK's path-joining logic. -->
42
+ <BaseIntermediateOutputPath>$(ArtifactsRoot)obj/</BaseIntermediateOutputPath>
43
+ <IntermediateOutputPath>$(BaseIntermediateOutputPath)$(Configuration)/</IntermediateOutputPath>
6
44
  <OutputPath>$(ArtifactsRoot)bin/$(Configuration)/</OutputPath>
7
45
  <VSTestResultsDirectory>$(ArtifactsRoot)TestResults/</VSTestResultsDirectory>
46
+ <LangVersion>10.0</LangVersion>
47
+ <Deterministic>true</Deterministic>
48
+ <ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
49
+ <IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
50
+ <DebugType>none</DebugType>
51
+ <DebugSymbols>false</DebugSymbols>
52
+ <CopyAnalyzerPayload Condition="'$(CopyAnalyzerPayload)' == ''">true</CopyAnalyzerPayload>
53
+ <AnalyzerPayloadOutputDir Condition="'$(AnalyzerPayloadOutputDir)' == ''"
54
+ >$([System.IO.Path]::Combine('$(SolutionDir)', 'Editor', 'Analyzers'))</AnalyzerPayloadOutputDir>
8
55
  </PropertyGroup>
9
56
  </Project>
@@ -13,8 +13,8 @@ namespace WallstopStudios.DxMessaging.SourceGenerators
13
13
  using Microsoft.CodeAnalysis.CSharp.Syntax;
14
14
  using Microsoft.CodeAnalysis.Text;
15
15
 
16
- [Generator(LanguageNames.CSharp)]
17
- public sealed class DxAutoConstructorGenerator : IIncrementalGenerator
16
+ [Generator]
17
+ public sealed class DxAutoConstructorGenerator : ISourceGenerator
18
18
  {
19
19
  private static readonly DiagnosticDescriptor NonPartialContainerDiagnostic = new(
20
20
  id: "DXMSG003",
@@ -57,46 +57,34 @@ namespace WallstopStudios.DxMessaging.SourceGenerators
57
57
  );
58
58
 
59
59
  /// <summary>
60
- /// Configures the incremental generator pipeline that discovers annotated types and emits constructors.
60
+ /// Configures syntax collection for annotated types that need generated constructors.
61
61
  /// </summary>
62
62
  /// <param name="context">Initialization context provided by Roslyn.</param>
63
- public void Initialize(IncrementalGeneratorInitializationContext context)
63
+ public void Initialize(GeneratorInitializationContext context)
64
64
  {
65
- // Find all class/struct/record declarations that have attribute lists
66
- IncrementalValuesProvider<TypeDeclarationSyntax> potentialTypeDeclarations =
67
- context.SyntaxProvider.CreateSyntaxProvider(
68
- predicate: static (node, _) => IsSyntaxTargetForGeneration(node),
69
- transform: static (ctx, _) => (TypeDeclarationSyntax)ctx.Node
70
- );
65
+ context.RegisterForSyntaxNotifications(static () => new TypeSyntaxReceiver());
66
+ }
71
67
 
72
- // Get semantic info for potential types
73
- IncrementalValuesProvider<TypeToGenerateInfo?> semanticTargets =
74
- potentialTypeDeclarations
75
- .Combine(context.CompilationProvider)
76
- .Select(
77
- static (data, ct) =>
78
- GetSemanticTargetForGeneration(data.Left, data.Right, ct)
79
- );
68
+ public void Execute(GeneratorExecutionContext context)
69
+ {
70
+ if (context.SyntaxReceiver is not TypeSyntaxReceiver receiver)
71
+ {
72
+ return;
73
+ }
80
74
 
81
- // Filter out nulls (types that aren't valid for auto-gen constructor)
82
- IncrementalValuesProvider<TypeToGenerateInfo> validSemanticTargets = semanticTargets
75
+ ImmutableArray<TypeToGenerateInfo> typesToGenerate = receiver
76
+ .Candidates.Select(typeDeclarationSyntax =>
77
+ GetSemanticTargetForGeneration(
78
+ typeDeclarationSyntax,
79
+ context.Compilation,
80
+ context.CancellationToken
81
+ )
82
+ )
83
83
  .Where(static target => target.HasValue)
84
- .Select(static (target, _) => target!.Value);
85
-
86
- // Collect all valid types for generation
87
- IncrementalValueProvider<ImmutableArray<TypeToGenerateInfo>> collectedTargets =
88
- validSemanticTargets.Collect();
89
-
90
- IncrementalValueProvider<(
91
- Compilation,
92
- ImmutableArray<TypeToGenerateInfo>
93
- )> compilationAndTypes = context.CompilationProvider.Combine(collectedTargets);
84
+ .Select(static target => target!.Value)
85
+ .ToImmutableArray();
94
86
 
95
- // Register the source output step
96
- context.RegisterSourceOutput(
97
- compilationAndTypes,
98
- static (spc, source) => Execute(source.Item1, source.Item2, spc)
99
- );
87
+ Execute(typesToGenerate, context);
100
88
  }
101
89
 
102
90
  private static bool IsSyntaxTargetForGeneration(SyntaxNode node) =>
@@ -105,7 +93,11 @@ namespace WallstopStudios.DxMessaging.SourceGenerators
105
93
  typeDecl.IsKind(SyntaxKind.ClassDeclaration)
106
94
  || typeDecl.IsKind(SyntaxKind.StructDeclaration)
107
95
  || typeDecl.IsKind(SyntaxKind.RecordDeclaration)
108
- || typeDecl.IsKind(SyntaxKind.RecordStructDeclaration)
96
+ || string.Equals(
97
+ typeDecl.Kind().ToString(),
98
+ "RecordStructDeclaration",
99
+ StringComparison.Ordinal
100
+ )
109
101
  );
110
102
 
111
103
  private static TypeToGenerateInfo? GetSemanticTargetForGeneration(
@@ -163,9 +155,8 @@ namespace WallstopStudios.DxMessaging.SourceGenerators
163
155
  }
164
156
 
165
157
  private static void Execute(
166
- Compilation compilation,
167
158
  ImmutableArray<TypeToGenerateInfo> typesToGenerate,
168
- SourceProductionContext context
159
+ GeneratorExecutionContext context
169
160
  )
170
161
  {
171
162
  if (typesToGenerate.IsDefaultOrEmpty)
@@ -235,7 +226,11 @@ namespace WallstopStudios.DxMessaging.SourceGenerators
235
226
  }
236
227
 
237
228
  // Generate the partial class/struct with the constructor
238
- string generatedSource = GenerateConstructorSource(compilation, typeInfo, context);
229
+ string generatedSource = GenerateConstructorSource(
230
+ context.Compilation,
231
+ typeInfo,
232
+ context
233
+ );
239
234
  string hintName =
240
235
  $"{typeInfo.TypeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}.AutoGenConstructor.g.cs"
241
236
  .Replace("global::", "")
@@ -250,7 +245,7 @@ namespace WallstopStudios.DxMessaging.SourceGenerators
250
245
  private static string GenerateConstructorSource(
251
246
  Compilation compilation,
252
247
  TypeToGenerateInfo typeInfo,
253
- SourceProductionContext spc
248
+ GeneratorExecutionContext spc
254
249
  )
255
250
  {
256
251
  INamedTypeSymbol typeSymbol = typeInfo.TypeSymbol;
@@ -290,10 +285,11 @@ namespace WallstopStudios.DxMessaging.SourceGenerators
290
285
  _ => "internal",
291
286
  };
292
287
 
288
+ bool containerIsRecord = IsRecordDeclaration(container);
293
289
  string containerKind = container.TypeKind switch
294
290
  {
295
- TypeKind.Class => container.IsRecord ? "record class" : "class",
296
- TypeKind.Struct => container.IsRecord ? "record struct" : "struct",
291
+ TypeKind.Class => containerIsRecord ? "record class" : "class",
292
+ TypeKind.Struct => containerIsRecord ? "record struct" : "struct",
297
293
  _ => "class",
298
294
  };
299
295
 
@@ -323,10 +319,11 @@ namespace WallstopStudios.DxMessaging.SourceGenerators
323
319
  + ">"
324
320
  : string.Empty;
325
321
  string typeName = typeSymbol.Name + typeGenericParams;
322
+ bool typeIsRecord = IsRecordDeclaration(typeSymbol);
326
323
  string typeKind = typeSymbol.TypeKind switch
327
324
  {
328
- TypeKind.Class => typeSymbol.IsRecord ? "record class" : "class",
329
- TypeKind.Struct => typeSymbol.IsRecord ? "record struct" : "struct",
325
+ TypeKind.Class => typeIsRecord ? "record class" : "class",
326
+ TypeKind.Struct => typeIsRecord ? "record struct" : "struct",
330
327
  _ => throw new InvalidOperationException(
331
328
  "Unsupported type kind for constructor generation"
332
329
  ),
@@ -524,25 +521,30 @@ namespace WallstopStudios.DxMessaging.SourceGenerators
524
521
  containersClose.Append(currentIndent).AppendLine("}");
525
522
  }
526
523
 
527
- return $$"""
528
- // <auto-generated by DxAutoGenConstructorGenerator/>
529
- #pragma warning disable
530
- #nullable enable annotations
531
-
532
- {{namespaceBlockOpen}}
533
- {{containersOpen}}{{innerIndent}}{{typeAccessibility}} partial {{typeKind}} {{typeName}}
534
- {{innerIndent}}{
535
- {{Indent}} /// <summary>
536
- {{Indent}} /// Auto-generated constructor by DxAutoGenConstructorGenerator.
537
- {{Indent}} /// </summary>
538
- {{innerIndent}} {{constructorAccessibility}} {{typeSymbol.Name}}({{constructorParams}})
539
- {{innerIndent}} {
540
- {{constructorBody}}
541
- {{innerIndent}} }
542
- {{innerIndent}}}
543
- {{containersClose}}
544
- {{namespaceBlockClose}}
545
- """;
524
+ return string.Join(
525
+ "\r\n",
526
+ new[]
527
+ {
528
+ "// <auto-generated by DxAutoGenConstructorGenerator/>",
529
+ "#pragma warning disable",
530
+ "#nullable enable annotations",
531
+ string.Empty,
532
+ namespaceBlockOpen,
533
+ $"{containersOpen}{innerIndent}{typeAccessibility} partial {typeKind} {typeName}",
534
+ $"{innerIndent}{{",
535
+ $"{Indent} /// <summary>",
536
+ $"{Indent} /// Auto-generated constructor by DxAutoGenConstructorGenerator.",
537
+ $"{Indent} /// </summary>",
538
+ $"{innerIndent} {constructorAccessibility} {typeSymbol.Name}({constructorParams})",
539
+ $"{innerIndent} {{",
540
+ $"{constructorBody}",
541
+ $"{innerIndent} }}",
542
+ $"{innerIndent}}}",
543
+ $"{containersClose}",
544
+ namespaceBlockClose,
545
+ string.Empty,
546
+ }
547
+ );
546
548
  }
547
549
 
548
550
  private static string FormatLiteral(object value, ITypeSymbol type)
@@ -651,6 +653,23 @@ namespace WallstopStudios.DxMessaging.SourceGenerators
651
653
  }
652
654
  }
653
655
 
656
+ private static bool IsRecordDeclaration(INamedTypeSymbol symbol)
657
+ {
658
+ foreach (SyntaxReference syntaxReference in symbol.DeclaringSyntaxReferences)
659
+ {
660
+ if (syntaxReference.GetSyntax() is TypeDeclarationSyntax declaration)
661
+ {
662
+ string kind = declaration.Kind().ToString();
663
+ if (kind.IndexOf("Record", StringComparison.Ordinal) >= 0)
664
+ {
665
+ return true;
666
+ }
667
+ }
668
+ }
669
+
670
+ return false;
671
+ }
672
+
654
673
  private static List<INamedTypeSymbol> GetNonPartialContainers(INamedTypeSymbol typeSymbol)
655
674
  {
656
675
  List<INamedTypeSymbol> result = new();
@@ -694,5 +713,19 @@ namespace WallstopStudios.DxMessaging.SourceGenerators
694
713
 
695
714
  return true;
696
715
  }
716
+
717
+ private sealed class TypeSyntaxReceiver : ISyntaxReceiver
718
+ {
719
+ public List<TypeDeclarationSyntax> Candidates { get; } =
720
+ new List<TypeDeclarationSyntax>();
721
+
722
+ public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
723
+ {
724
+ if (IsSyntaxTargetForGeneration(syntaxNode))
725
+ {
726
+ Candidates.Add((TypeDeclarationSyntax)syntaxNode);
727
+ }
728
+ }
729
+ }
697
730
  }
698
731
  }