-
Notifications
You must be signed in to change notification settings - Fork 199
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
Fix a couple of exceptions encountered when formatting documents with preprocessor directives #11373
Fix a couple of exceptions encountered when formatting documents with preprocessor directives #11373
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,9 +106,15 @@ static void ExtractTriviaAnnotations( | |
var formattedTriviaList = formattedRoot.GetAnnotatedTrivia(MarkerId); | ||
foreach (var trivia in formattedTriviaList) | ||
{ | ||
// We only expect one annotation because we built the entire trivia with a single annotation. | ||
var annotation = trivia.GetAnnotations(MarkerId).Single(); | ||
if (!int.TryParse(annotation.Data, out var projectedIndex)) | ||
// We only expect one annotation because we built the entire trivia with a single annotation, but | ||
// we need to be defensive here. Annotations are a little hard to work with though, so apologies for | ||
// the slightly odd method of validation. | ||
using var enumerator = trivia.GetAnnotations(MarkerId).GetEnumerator(); | ||
enumerator.MoveNext(); | ||
var annotation = enumerator.Current; | ||
// We shouldn't be able to enumerate any more, and we should be able to parse our data out of the annotation. | ||
if (enumerator.MoveNext() || | ||
!int.TryParse(annotation.Data, out var projectedIndex)) | ||
Comment on lines
-110
to
+117
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The defensive code seems fine to me, but what are the conditions where multiple annotations are returned? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given this document with source mappings at each
If DEBUG is not defined, then the whole span between directives is a single "DisabledTextTrivia", but obviously it contains two mappings, so two points to track. |
||
{ | ||
// This shouldn't happen realistically unless someone messed with the annotations we added. | ||
// Let's ignore this annotation. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -440,6 +440,46 @@ @typeparam TValue | |
} | ||
} | ||
|
||
[FormattingTestFact] | ||
public async Task PreprocessorDirectives() | ||
{ | ||
await RunFormattingTestAsync( | ||
input: """ | ||
<div Model="SomeModel"> | ||
<div /> | ||
@{ | ||
#if DEBUG | ||
} | ||
<div /> | ||
@{ | ||
#endif | ||
} | ||
</div> | ||
|
||
@code { | ||
private object SomeModel {get;set;} | ||
} | ||
""", | ||
expected: """ | ||
<div Model="SomeModel"> | ||
<div /> | ||
@{ | ||
#if DEBUG | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. huh... this looks wrong to me :/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this either TBH. Is the preprocessor directive increasing the indent level? It doesn't seem to in standalone C# file. E.g.
formatted becomes
which is what I would expect There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be very clear, this PR doesn't attempt to actually adjust how this document is formatted, it just makes the formatting engine not crash. The Having said that, in the tests there are no preprocessor defines (actually we have no way to pass them to the Razor compiler at all I don't think) so the code in the |
||
<div /> | ||
@{ | ||
#endif | ||
|
||
} | ||
</div> | ||
|
||
@code { | ||
private object SomeModel { get; set; } | ||
} | ||
""", | ||
allowDiagnostics: true); | ||
} | ||
|
||
private ImmutableArray<TagHelperDescriptor> GetComponents() | ||
{ | ||
AdditionalSyntaxTrees.Add(Parse(""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When does this happen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think due to FUSE, there was a zero-width marker token at the end of the document which upset this algorithm. It's entirely possible this never happens in real life, but being defensive doesn't hurt.