Skip to content

Commit 0d5c41c

Browse files
authored
Log when formatting operations are being abandoned (#9617)
2 parents 77042ab + f1db4df commit 0d5c41c

File tree

17 files changed

+513
-0
lines changed

17 files changed

+513
-0
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Extensions/RangeExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,9 @@ public static int CompareTo(this Range range1, Range range2)
130130

131131
return result;
132132
}
133+
134+
public static string ToDisplayString(this Range range)
135+
{
136+
return $"({range.Start.Line}, {range.Start.Character})-({range.End.Line}, {range.End.Character})";
137+
}
133138
}

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/FormattingContentValidationPass.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ public override Task<FormattingResult> ExecuteAsync(FormattingContext context, F
5757
// Looks like we removed some non-whitespace content as part of formatting. Oops.
5858
// Discard this formatting result.
5959

60+
_logger.LogWarning("{value}", SR.Format_operation_changed_nonwhitespace);
61+
62+
foreach (var edit in edits)
63+
{
64+
if (edit.NewText.Any(c => !char.IsWhiteSpace(c)))
65+
{
66+
_logger.LogWarning("{value}", SR.FormatEdit_at_adds(edit.Range.ToDisplayString(), edit.NewText));
67+
}
68+
else if (text.GetSubText(edit.Range.ToTextSpan(text)) is { } subText &&
69+
subText.GetFirstNonWhitespaceOffset(span: null, out _) is not null)
70+
{
71+
_logger.LogWarning("{value}", SR.FormatEdit_at_deletes(edit.Range.ToDisplayString(), subText.ToString()));
72+
}
73+
}
74+
6075
if (DebugAssertsEnabled)
6176
{
6277
Debug.Fail("A formatting result was rejected because it was going to change non-whitespace content in the document.");

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/FormattingDiagnosticValidationPass.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ public async override Task<FormattingResult> ExecuteAsync(FormattingContext cont
6565
// at all possible). Also worth noting the order has to be maintained in that case.
6666
if (!originalDiagnostics.SequenceEqual(changedDiagnostics, LocationIgnoringDiagnosticComparer.Instance))
6767
{
68+
// Yes, these log messages look weird, but this is how structured logging works. The first parameter, the "template" is not
69+
// supposed to change, or it causes lots of allocations. The second parameter is the "argument" which is supposed to change.
70+
// In our case, we never use structured logging such that the argument name is important, so we just use the same one, and
71+
// save some memory, and still log the expected values.
72+
_logger.LogWarning("{value}", SR.Format_operation_changed_diagnostics);
73+
_logger.LogWarning("{value}", SR.Diagnostics_before);
74+
foreach (var diagnostic in originalDiagnostics)
75+
{
76+
_logger.LogWarning("{value}", diagnostic);
77+
}
78+
79+
_logger.LogWarning("{value}", SR.Diagnostics_after);
80+
foreach (var diagnostic in changedDiagnostics)
81+
{
82+
_logger.LogWarning("{value}", diagnostic);
83+
}
84+
6885
if (DebugAssertsEnabled)
6986
{
7087
Debug.Fail("A formatting result was rejected because the formatted text produced different diagnostics compared to the original text.");

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Resources/SR.resx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,39 @@
120120
<data name="Blazor_directive_attributes" xml:space="preserve">
121121
<value>Blazor directive attributes</value>
122122
</data>
123+
<data name="Changes" xml:space="preserve">
124+
<value>Changes:</value>
125+
</data>
123126
<data name="Create_Component_FromTag_Title" xml:space="preserve">
124127
<value>Create component from tag</value>
125128
</data>
129+
<data name="Diagnostics_after" xml:space="preserve">
130+
<value>Diagnostics after:</value>
131+
</data>
132+
<data name="Diagnostics_before" xml:space="preserve">
133+
<value>Diagnostics before:</value>
134+
</data>
126135
<data name="Document_Not_Found" xml:space="preserve">
127136
<value>Document {0} was not found.</value>
128137
</data>
138+
<data name="Edit_at_adds" xml:space="preserve">
139+
<value>Edit at {0} adds the non-whitespace content '{1}'.</value>
140+
</data>
141+
<data name="Edit_at_deletes" xml:space="preserve">
142+
<value>Edit at {0} deletes the non-whitespace content '{1}'.</value>
143+
</data>
129144
<data name="ExtractTo_CodeBehind_Title" xml:space="preserve">
130145
<value>Extract block to code behind</value>
131146
</data>
132147
<data name="File_Externally_Modified" xml:space="preserve">
133148
<value>File was externally modified: {0}</value>
134149
</data>
150+
<data name="Format_operation_changed_diagnostics" xml:space="preserve">
151+
<value>A format operation is being abandoned because it would introduce or remove one of more diagnostics.</value>
152+
</data>
153+
<data name="Format_operation_changed_nonwhitespace" xml:space="preserve">
154+
<value>A format operation is being abandoned because it would add or delete non-whitespace content.</value>
155+
</data>
135156
<data name="Generate_Async_Event_Handler_Title" xml:space="preserve">
136157
<value>Generate Async Event Handler '{0}'</value>
137158
</data>

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Resources/xlf/SR.cs.xlf

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Resources/xlf/SR.de.xlf

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Resources/xlf/SR.es.xlf

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Resources/xlf/SR.fr.xlf

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Resources/xlf/SR.it.xlf

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)