Skip to content

Remove UseProvidedBody string matching from DelegateBodySyntaxExtractor#96

Draft
Copilot wants to merge 3 commits intofeature/metehod_templatefrom
copilot/remove-looking-for-body-statement
Draft

Remove UseProvidedBody string matching from DelegateBodySyntaxExtractor#96
Copilot wants to merge 3 commits intofeature/metehod_templatefrom
copilot/remove-looking-for-body-statement

Conversation

Copy link
Contributor

Copilot AI commented Mar 25, 2026

DelegateBodySyntaxExtractor searched for lambdas by matching the string "UseProvidedBody" against method names in the syntax tree. This is fragile and unnecessary since BodyGenerationData.RuntimeDelegateBody already tells us whether a delegate body was provided.

Approach

Instead of string-matching a method name, use the data from BodyGenerationData to decide when to extract, and structural position to decide what to extract:

  • FluentBodyResult — Added HasDelegateBody flag, set when RuntimeDelegateBody is non-null
  • GeneratesMethodGenerationPipeline — Reversed the flow: execute the generator method first, then use HasDelegateBody to conditionally trigger syntax extraction
  • DelegateBodySyntaxExtractor — Replaced descendant-node scan for "UseProvidedBody" with structural lookup: find the outermost InvocationExpressionSyntax in the method's return expression and extract its lambda argument
-InvocationExpressionSyntax? invocation = generatorMethodSyntax
-    .DescendantNodes()
-    .OfType<InvocationExpressionSyntax>()
-    .FirstOrDefault(inv =>
-        inv.Expression is MemberAccessExpressionSyntax memberAccess &&
-        memberAccess.Name.Identifier.Text == "UseProvidedBody");
+ExpressionSyntax? returnExpression = GetReturnExpression(generatorMethodSyntax);
+if (returnExpression is not InvocationExpressionSyntax invocation)
+    return null;

Pre-existing fix

Added missing WithCompileTimeConstants and Stage5 implementations in DataMethodBodyBuilders.cs to fix build errors on this branch.

Original prompt

This section details on the original issue you should resolve

<issue_title>Remove looking for body statement</issue_title>
<issue_description>In a previous PR, #94, a new way to create body was added: to copy the code defined in UseProvidedBody method to the target body.

I like how that works, but I don't like how that is implemented. In DelegateBodySyntaxExtractor, the code looks for memberAccess.Name.Identifier.Text == "UseProvidedBody" which is not very clean.

In BodyGenerationData record, the delegate defined in UseProvidedBody is already stored. Is that not enough to copy the source code? Maybe UseProvidedBody instead of taking Action/Func delegate, should take some "Expression" parameter, or something, so it can capture the exact code, which would remove the need to look for "UseProvidedBody" string?</issue_description>

Comments on the Issue (you are @copilot in this section)


⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS or Windows machine with Raycast.

Copilot AI and others added 2 commits March 25, 2026 16:17
Refactor the delegate body extraction to use structural syntax analysis
instead of searching for the "UseProvidedBody" method name string.

Changes:
- DelegateBodySyntaxExtractor now finds the lambda by looking at the
  outermost invocation's lambda argument in the method's return
  expression, instead of scanning for "UseProvidedBody" string
- FluentBodyResult gets HasDelegateBody flag indicating RuntimeDelegateBody
  was set in BodyGenerationData
- BodyGenerationDataExtractor sets HasDelegateBody when RuntimeDelegateBody
  is non-null
- GeneratesMethodGenerationPipeline executes the method first, then uses
  HasDelegateBody to decide if syntax extraction is needed
- Add missing WithCompileTimeConstants implementations to fix pre-existing
  build errors in DataMethodBodyBuilders

Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dex3r/EasySourceGenerators/sessions/ea25cd90-2a07-42c1-8091-f255cb2c6687
Copilot AI changed the title [WIP] Remove looking for body statement Remove UseProvidedBody string matching from DelegateBodySyntaxExtractor Mar 25, 2026
Copilot AI requested a review from dex3r March 25, 2026 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants