Skip to content

Commit 8588037

Browse files
committed
Pass C# formatting settings override around more
1 parent 05c2a06 commit 8588037

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/CSharpFormatter.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,22 @@ public static async Task<IReadOnlyDictionary<int, int>> GetCSharpIndentationAsyn
4646
FormattingContext context,
4747
HashSet<int> projectedDocumentLocations,
4848
HostWorkspaceServices hostWorkspaceServices,
49+
RazorCSharpSyntaxFormattingOptions? formattingOptionsOverride,
4950
CancellationToken cancellationToken)
5051
{
5152
// Sorting ensures we count the marker offsets correctly.
5253
// We also want to ensure there are no duplicates to avoid duplicate markers.
5354
var filteredLocations = projectedDocumentLocations.OrderAsArray();
5455

55-
var indentations = await GetCSharpIndentationCoreAsync(context, filteredLocations, hostWorkspaceServices, cancellationToken).ConfigureAwait(false);
56+
var indentations = await GetCSharpIndentationCoreAsync(context, filteredLocations, hostWorkspaceServices, formattingOptionsOverride, cancellationToken).ConfigureAwait(false);
5657
return indentations;
5758
}
5859

5960
private ImmutableArray<TextChange> MapEditsToHostDocument(RazorCodeDocument codeDocument, ImmutableArray<TextChange> csharpEdits)
6061
{
6162
var actualEdits = _documentMappingService.GetHostDocumentEdits(codeDocument.GetRequiredCSharpDocument(), csharpEdits);
6263

63-
return actualEdits.ToImmutableArray();
64+
return [.. actualEdits];
6465
}
6566

6667
private static async Task<ImmutableArray<TextChange>> GetFormattingEditsAsync(
@@ -77,10 +78,15 @@ private static async Task<ImmutableArray<TextChange>> GetFormattingEditsAsync(
7778
Assumes.NotNull(root);
7879

7980
var changes = RazorCSharpFormattingInteractionService.GetFormattedTextChanges(hostWorkspaceServices, root, spanToFormat, indentationOptions, formattingOptionsOverride, cancellationToken);
80-
return changes.ToImmutableArray();
81+
return [.. changes];
8182
}
8283

83-
private static async Task<Dictionary<int, int>> GetCSharpIndentationCoreAsync(FormattingContext context, ImmutableArray<int> projectedDocumentLocations, HostWorkspaceServices hostWorkspaceServices, CancellationToken cancellationToken)
84+
private static async Task<Dictionary<int, int>> GetCSharpIndentationCoreAsync(
85+
FormattingContext context,
86+
ImmutableArray<int> projectedDocumentLocations,
87+
HostWorkspaceServices hostWorkspaceServices,
88+
RazorCSharpSyntaxFormattingOptions? formattingOptionsOverride,
89+
CancellationToken cancellationToken)
8490
{
8591
// No point calling the C# formatting if we won't be interested in any of its work anyway
8692
if (projectedDocumentLocations.Length == 0)
@@ -96,7 +102,7 @@ private static async Task<Dictionary<int, int>> GetCSharpIndentationCoreAsync(Fo
96102

97103
// At this point, we have added all the necessary markers and attached annotations.
98104
// Let's invoke the C# formatter and hope for the best.
99-
var formattedRoot = RazorCSharpFormattingInteractionService.Format(hostWorkspaceServices, root, context.Options.ToIndentationOptions(), cancellationToken);
105+
var formattedRoot = RazorCSharpFormattingInteractionService.Format(hostWorkspaceServices, root, context.Options.ToIndentationOptions(), formattingOptionsOverride, cancellationToken);
100106
var formattedText = formattedRoot.GetText();
101107

102108
var desiredIndentationMap = new Dictionary<int, int>();

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Threading;
88
using System.Threading.Tasks;
99
using Microsoft.AspNetCore.Razor.PooledObjects;
10-
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Features;
1110
using Microsoft.CodeAnalysis.Host;
1211
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
1312
using Microsoft.CodeAnalysis.Razor.Logging;
@@ -28,8 +27,6 @@ internal sealed class CSharpFormattingPass(
2827
private readonly CSharpFormatter _csharpFormatter = new(documentMappingService);
2928
private readonly ILogger _logger = loggerFactory.GetOrCreateLogger<CSharpFormattingPass>();
3029

31-
private RazorCSharpSyntaxFormattingOptions? _csharpSyntaxFormattingOptionsOverride;
32-
3330
protected async override Task<ImmutableArray<TextChange>> ExecuteCoreAsync(FormattingContext context, RoslynWorkspaceHelper roslynWorkspaceHelper, ImmutableArray<TextChange> changes, CancellationToken cancellationToken)
3431
{
3532
// Apply previous edits if any.
@@ -96,14 +93,4 @@ private async Task<ImmutableArray<TextChange>> FormatCSharpAsync(FormattingConte
9693

9794
return csharpChanges.ToImmutable();
9895
}
99-
100-
internal TestAccessor GetTestAccessor() => new TestAccessor(this);
101-
102-
internal readonly struct TestAccessor(CSharpFormattingPass instance)
103-
{
104-
public void SetCSharpSyntaxFormattingOptionsOverride(RazorCSharpSyntaxFormattingOptions? optionsOverride)
105-
{
106-
instance._csharpSyntaxFormattingOptionsOverride = optionsOverride;
107-
}
108-
}
10996
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.AspNetCore.Razor.Language.Extensions;
1414
using Microsoft.AspNetCore.Razor.Language.Syntax;
1515
using Microsoft.AspNetCore.Razor.PooledObjects;
16+
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Features;
1617
using Microsoft.CodeAnalysis.Host;
1718
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
1819
using Microsoft.CodeAnalysis.Razor.Logging;
@@ -28,6 +29,8 @@ internal abstract partial class CSharpFormattingPassBase(IDocumentMappingService
2829

2930
protected IDocumentMappingService DocumentMappingService { get; } = documentMappingService;
3031

32+
protected RazorCSharpSyntaxFormattingOptions? _csharpSyntaxFormattingOptionsOverride;
33+
3134
public async Task<ImmutableArray<TextChange>> ExecuteAsync(FormattingContext context, ImmutableArray<TextChange> changes, CancellationToken cancellationToken)
3235
{
3336
using var roslynWorkspaceHelper = new RoslynWorkspaceHelper(hostServicesProvider);
@@ -164,7 +167,7 @@ owner.Parent is RazorDirectiveSyntax containingDirective &&
164167
}
165168

166169
// Now, invoke the C# formatter to obtain the CSharpDesiredIndentation for all significant locations.
167-
var significantLocationIndentation = await CSharpFormatter.GetCSharpIndentationAsync(context, significantLocations, hostWorkspaceServices, cancellationToken).ConfigureAwait(false);
170+
var significantLocationIndentation = await CSharpFormatter.GetCSharpIndentationAsync(context, significantLocations, hostWorkspaceServices, _csharpSyntaxFormattingOptionsOverride, cancellationToken).ConfigureAwait(false);
168171

169172
// Build source mapping indentation scopes.
170173
var sourceMappingIndentations = new SortedDictionary<int, IndentationData>();
@@ -695,4 +698,14 @@ public int GetIndentation(SortedDictionary<int, IndentationData> sourceMappingIn
695698
return _indentation;
696699
}
697700
}
701+
702+
internal TestAccessor GetTestAccessor() => new(this);
703+
704+
internal readonly struct TestAccessor(CSharpFormattingPassBase instance)
705+
{
706+
public void SetCSharpSyntaxFormattingOptionsOverride(RazorCSharpSyntaxFormattingOptions? optionsOverride)
707+
{
708+
instance._csharpSyntaxFormattingOptionsOverride = optionsOverride;
709+
}
710+
}
698711
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ protected async override Task<ImmutableArray<TextChange>> ExecuteCoreAsync(Forma
5858
context.Options.ToIndentationOptions(),
5959
autoFormattingOptions,
6060
indentStyle: CodeAnalysis.Formatting.FormattingOptions.IndentStyle.Smart,
61+
_csharpSyntaxFormattingOptionsOverride,
6162
cancellationToken).ConfigureAwait(false);
6263

6364
if (formattingChanges.IsEmpty)

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,11 @@ public void SetCSharpSyntaxFormattingOptionsOverride(RazorCSharpSyntaxFormatting
404404
pass.GetTestAccessor().SetCSharpSyntaxFormattingOptionsOverride(optionsOverride);
405405
}
406406

407+
if (service._documentFormattingPasses.OfType<CSharpOnTypeFormattingPass>().SingleOrDefault() is { } onTypePass)
408+
{
409+
onTypePass.GetTestAccessor().SetCSharpSyntaxFormattingOptionsOverride(optionsOverride);
410+
}
411+
407412
if (service._documentFormattingPasses.OfType<New.CSharpFormattingPass>().SingleOrDefault() is { } newPass)
408413
{
409414
newPass.GetTestAccessor().SetCSharpSyntaxFormattingOptionsOverride(optionsOverride);

0 commit comments

Comments
 (0)