Skip to content

Commit 648ad08

Browse files
authored
Don't add newlines at the start of implicit expressions in on type formatting (#12489)
Fixes #12481
2 parents 7d19a86 + a0b00a6 commit 648ad08

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/CSharpOnTypeFormattingPass.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,14 @@ private static void CleanupSourceMappingStart(FormattingContext context, LinePos
339339

340340
var text = context.SourceText;
341341
var sourceMappingSpan = text.GetTextSpan(sourceMappingRange);
342-
if (!ShouldFormat(context, sourceMappingSpan, allowImplicitStatements: false, out var owner))
342+
if (!ShouldFormat(context,
343+
sourceMappingSpan,
344+
new ShouldFormatOptions(
345+
AllowImplicitStatements: false,
346+
AllowImplicitExpressions: false,
347+
AllowSingleLineExplicitExpressions: true,
348+
IsLineRequest: false),
349+
out var owner))
343350
{
344351
// We don't want to run cleanup on this range.
345352
return;

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private protected async Task RunOnTypeFormattingTestAsync(
166166
filePathService, new TestDocumentContextFactory(), LoggerFactory);
167167
var languageKind = codeDocument.GetLanguageKind(positionAfterTrigger, rightAssociative: false);
168168

169-
var formattingService = await TestRazorFormattingService.CreateWithFullSupportAsync(LoggerFactory, TestOutputHelper, codeDocument, razorLSPOptions, languageServerFeatureOptions);
169+
var formattingService = await TestRazorFormattingService.CreateWithFullSupportAsync(LoggerFactory, TestOutputHelper, codeDocument, razorLSPOptions, languageServerFeatureOptions, debugAssertsEnabled: true);
170170
var options = new FormattingOptions()
171171
{
172172
TabSize = tabSize,

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/OnTypeFormattingTest.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,44 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
1818
public class OnTypeFormattingTest(FormattingTestContext context, HtmlFormattingFixture fixture, ITestOutputHelper testOutput)
1919
: FormattingTestBase(context, fixture.Service, testOutput), IClassFixture<FormattingTestContext>
2020
{
21+
[FormattingTestFact]
22+
public async Task FormatsElseCloseBrace()
23+
{
24+
await RunOnTypeFormattingTestAsync(
25+
input: """
26+
@page "/"
27+
28+
@if (true)
29+
{
30+
<option value="@streetKind">@streetKind</option>
31+
}
32+
else {
33+
<input />
34+
}$$
35+
36+
@code {
37+
private string? streetKind;
38+
}
39+
""",
40+
expected: """
41+
@page "/"
42+
43+
@if (true)
44+
{
45+
<option value="@streetKind">@streetKind</option>
46+
}
47+
else
48+
{
49+
<input />
50+
}
51+
52+
@code {
53+
private string? streetKind;
54+
}
55+
""",
56+
triggerCharacter: '}');
57+
}
58+
2159
[FormattingTestFact]
2260
public async Task FormatsIfStatementInComponent()
2361
{

src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/Formatting/FormattingTestBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ private protected async Task RunOnTypeFormattingTestAsync(
123123
var position = inputText.GetPosition(input.Position);
124124

125125
var formattingService = (RazorFormattingService)OOPExportProvider.GetExportedValue<IRazorFormattingService>();
126-
formattingService.GetTestAccessor().SetFormattingLoggerFactory(new TestFormattingLoggerFactory(TestOutputHelper));
126+
var accessor = formattingService.GetTestAccessor();
127+
accessor.SetDebugAssertsEnabled(debugAssertsEnabled: true);
128+
accessor.SetFormattingLoggerFactory(new TestFormattingLoggerFactory(TestOutputHelper));
127129

128130
var generatedHtml = await RemoteServiceInvoker.TryInvokeAsync<IRemoteHtmlDocumentService, string?>(document.Project.Solution,
129131
(service, solutionInfo, ct) => service.GetHtmlDocumentTextAsync(solutionInfo, document.Id, ct),

0 commit comments

Comments
 (0)