Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting engine changes to allow it to (mostly) run on runtime code-gen #11303

Merged
merged 10 commits into from
Dec 17, 2024

Conversation

davidwengier
Copy link
Member

@davidwengier davidwengier commented Dec 13, 2024

Part of #10402

This PR makes various updates to our formatting engine to allow it to run on runtime code-gen. After this PR there are still 5 formatting tests that fail, representing two different bugs (the one in the issue, and a cascading value parameter issue), but those are proving very stubborn, so thought I'd put this up first. Plus it's Friday afternoon :)

Comments in the code hopefully explain what is going on, but honestly, it doesn't matter if anyone understands the formatting engine, as long as the tests pass :D

Each commit is a self-contained change for those who are extra curious about which bit fixes which failure.

@davidwengier davidwengier requested a review from a team as a code owner December 13, 2024 05:14
@davidwengier davidwengier requested review from Copilot and removed request for a team December 13, 2024 05:14

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 5 out of 9 changed files in this pull request and generated no comments.

Files not reviewed (4)
  • src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/RazorSyntaxNodeExtensions.cs: Evaluated as low risk
  • src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/CSharpFormatter.cs: Evaluated as low risk
  • src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/FormattingVisitor.cs: Evaluated as low risk
  • src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/CSharpFormattingPass.cs: Evaluated as low risk
Comments suppressed due to low confidence (1)

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/RazorFormattingPass.cs:24

  • The constructor syntax is incorrect. It should be internal sealed class RazorFormattingPass : IFormattingPass { private readonly ILogger _logger; public RazorFormattingPass(ILoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger<RazorFormattingPass>(); } }
internal sealed class RazorFormattingPass(ILoggerFactory loggerFactory) : IFormattingPass
@davidwengier davidwengier requested a review from a team December 13, 2024 05:45
Copy link
Contributor

@ryzngard ryzngard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my brain hurts

var token = root.FindToken(projectedDocumentIndex, findInsideTrivia: true);

// We use a marker if the projected location is in trivia, because we can't add annotations to a specific piece of trivia
var isInTrivia = projectedDocumentIndex < token.SpanStart || projectedDocumentIndex >= token.Span.End;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check if the trivia is something other than whitespace?

Copy link
Member Author

@davidwengier davidwengier Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I fully understand your question. We want to do this if the token is in trivia, or whitespace, and as far as I'm aware in Roslyn whitespace can only ever be trivia, or part thereof, so this should cover all of the bases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I might have just misunderstood what was going on. At first, I thought this was specifically looking for whitespace, not all trivia

changes.Add(new TextChange(new TextSpan(sourceText.Lines[i].Start, 0), indentationString));
}
}

var children = code.Children;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: if we're going to keep a local it should be moved up and used on line 102

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this suggestion, it turns out I can go one better, and just capture children in the pattern instead of code. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why you get paid the not publicly available number of bucks!

@davidwengier
Copy link
Member Author

Don't worry, eventually it calcifies and the pain stops :)

@@ -93,13 +93,13 @@ private static bool TryFormatSectionBlock(FormattingContext context, ref PooledA
if (node is CSharpCodeBlockSyntax directiveCode &&
directiveCode.Children is [RazorDirectiveSyntax directive, ..] &&
directive.DirectiveDescriptor?.Directive == SectionDirective.Directive.Directive &&
directive.Body is RazorDirectiveBodySyntax { CSharpCode: { } code })
directive.Body is RazorDirectiveBodySyntax { CSharpCode: { Children: var children } })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't comment on the commit message but I will say it made me chuckle

@davidwengier davidwengier merged commit 9f1eb13 into dotnet:main Dec 17, 2024
12 checks passed
@davidwengier davidwengier deleted the FuseInFormatting branch December 17, 2024 23:55
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Dec 17, 2024
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