Skip to content

Commit 8f4daa6

Browse files
committed
Allow RazorFormattingOptions to contain C# syntax formatting options
1 parent a67c6a7 commit 8f4daa6

File tree

7 files changed

+23
-61
lines changed

7 files changed

+23
-61
lines changed

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Microsoft.CodeAnalysis;
1212
using Microsoft.CodeAnalysis.CSharp.Syntax;
1313
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
14-
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Features;
1514
using Microsoft.CodeAnalysis.Host;
1615
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
1716
using Microsoft.CodeAnalysis.Text;
@@ -29,15 +28,14 @@ public async Task<ImmutableArray<TextChange>> FormatAsync(
2928
Document csharpDocument,
3029
FormattingContext context,
3130
LinePositionSpan spanToFormat,
32-
RazorCSharpSyntaxFormattingOptions? formattingOptionsOverride,
3331
CancellationToken cancellationToken)
3432
{
3533
if (!_documentMappingService.TryMapToCSharpDocumentRange(context.CodeDocument.GetRequiredCSharpDocument(), spanToFormat, out var projectedSpan))
3634
{
3735
return [];
3836
}
3937

40-
var edits = await GetFormattingEditsAsync(hostWorkspaceServices, csharpDocument, projectedSpan, context.Options.ToIndentationOptions(), formattingOptionsOverride, cancellationToken).ConfigureAwait(false);
38+
var edits = await GetFormattingEditsAsync(hostWorkspaceServices, csharpDocument, projectedSpan, context.Options, cancellationToken).ConfigureAwait(false);
4139
var mappedEdits = MapEditsToHostDocument(context.CodeDocument, edits);
4240
return mappedEdits;
4341
}
@@ -46,14 +44,13 @@ public static async Task<IReadOnlyDictionary<int, int>> GetCSharpIndentationAsyn
4644
FormattingContext context,
4745
HashSet<int> projectedDocumentLocations,
4846
HostWorkspaceServices hostWorkspaceServices,
49-
RazorCSharpSyntaxFormattingOptions? formattingOptionsOverride,
5047
CancellationToken cancellationToken)
5148
{
5249
// Sorting ensures we count the marker offsets correctly.
5350
// We also want to ensure there are no duplicates to avoid duplicate markers.
5451
var filteredLocations = projectedDocumentLocations.OrderAsArray();
5552

56-
var indentations = await GetCSharpIndentationCoreAsync(context, filteredLocations, hostWorkspaceServices, formattingOptionsOverride, cancellationToken).ConfigureAwait(false);
53+
var indentations = await GetCSharpIndentationCoreAsync(context, filteredLocations, hostWorkspaceServices, cancellationToken).ConfigureAwait(false);
5754
return indentations;
5855
}
5956

@@ -68,24 +65,22 @@ private static async Task<ImmutableArray<TextChange>> GetFormattingEditsAsync(
6865
HostWorkspaceServices hostWorkspaceServices,
6966
Document csharpDocument,
7067
LinePositionSpan projectedSpan,
71-
RazorIndentationOptions indentationOptions,
72-
RazorCSharpSyntaxFormattingOptions? formattingOptionsOverride,
68+
RazorFormattingOptions options,
7369
CancellationToken cancellationToken)
7470
{
7571
var root = await csharpDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
7672
var csharpSourceText = await csharpDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);
7773
var spanToFormat = csharpSourceText.GetTextSpan(projectedSpan);
7874
Assumes.NotNull(root);
7975

80-
var changes = RazorCSharpFormattingInteractionService.GetFormattedTextChanges(hostWorkspaceServices, root, spanToFormat, indentationOptions, formattingOptionsOverride, cancellationToken);
76+
var changes = RazorCSharpFormattingInteractionService.GetFormattedTextChanges(hostWorkspaceServices, root, spanToFormat, options.ToIndentationOptions(), options.CSharpSyntaxFormattingOptions, cancellationToken);
8177
return [.. changes];
8278
}
8379

8480
private static async Task<Dictionary<int, int>> GetCSharpIndentationCoreAsync(
8581
FormattingContext context,
8682
ImmutableArray<int> projectedDocumentLocations,
8783
HostWorkspaceServices hostWorkspaceServices,
88-
RazorCSharpSyntaxFormattingOptions? formattingOptionsOverride,
8984
CancellationToken cancellationToken)
9085
{
9186
// No point calling the C# formatting if we won't be interested in any of its work anyway
@@ -102,7 +97,7 @@ private static async Task<Dictionary<int, int>> GetCSharpIndentationCoreAsync(
10297

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

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private async Task<ImmutableArray<TextChange>> FormatCSharpAsync(FormattingConte
8787
// These should already be remapped.
8888
var spanToFormat = sourceText.GetLinePositionSpan(span);
8989

90-
var changes = await _csharpFormatter.FormatAsync(hostWorkspaceServices, csharpDocument, context, spanToFormat, _csharpSyntaxFormattingOptionsOverride, cancellationToken).ConfigureAwait(false);
90+
var changes = await _csharpFormatter.FormatAsync(hostWorkspaceServices, csharpDocument, context, spanToFormat, cancellationToken).ConfigureAwait(false);
9191
csharpChanges.AddRange(changes.Where(e => spanToFormat.Contains(sourceText.GetLinePositionSpan(e.Span))));
9292
}
9393

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
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;
1716
using Microsoft.CodeAnalysis.Host;
1817
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
1918
using Microsoft.CodeAnalysis.Razor.Logging;
@@ -29,8 +28,6 @@ internal abstract partial class CSharpFormattingPassBase(IDocumentMappingService
2928

3029
protected IDocumentMappingService DocumentMappingService { get; } = documentMappingService;
3130

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

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

172169
// Build source mapping indentation scopes.
173170
var sourceMappingIndentations = new SortedDictionary<int, IndentationData>();
@@ -698,14 +695,4 @@ public int GetIndentation(SortedDictionary<int, IndentationData> sourceMappingIn
698695
return _indentation;
699696
}
700697
}
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-
}
711698
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +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,
61+
context.Options.CSharpSyntaxFormattingOptions,
6262
cancellationToken).ConfigureAwait(false);
6363

6464
if (formattingChanges.IsEmpty)

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Microsoft.AspNetCore.Razor.PooledObjects;
1111
using Microsoft.CodeAnalysis.CSharp;
1212
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
13-
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Features;
1413
using Microsoft.CodeAnalysis.Razor.Logging;
1514
using Microsoft.CodeAnalysis.Razor.Workspaces;
1615
using Microsoft.CodeAnalysis.Text;
@@ -22,8 +21,6 @@ internal sealed partial class CSharpFormattingPass(IHostServicesProvider hostSer
2221
private readonly ILogger _logger = loggerFactory.GetOrCreateLogger<CSharpFormattingPass>();
2322
private readonly IHostServicesProvider _hostServicesProvider = hostServicesProvider;
2423

25-
private RazorCSharpSyntaxFormattingOptions? _csharpSyntaxFormattingOptionsOverride;
26-
2724
public async Task<ImmutableArray<TextChange>> ExecuteAsync(FormattingContext context, ImmutableArray<TextChange> changes, CancellationToken cancellationToken)
2825
{
2926
// Process changes from previous passes
@@ -36,7 +33,7 @@ public async Task<ImmutableArray<TextChange>> ExecuteAsync(FormattingContext con
3633

3734
var generatedCSharpText = generatedDocument.SourceText;
3835
_logger.LogTestOnly($"Generated C# document:\r\n{generatedCSharpText}");
39-
var formattedCSharpText = await FormatCSharpAsync(generatedCSharpText, context.Options.ToIndentationOptions(), cancellationToken).ConfigureAwait(false);
36+
var formattedCSharpText = await FormatCSharpAsync(generatedCSharpText, context.Options, cancellationToken).ConfigureAwait(false);
4037
_logger.LogTestOnly($"Formatted generated C# document:\r\n{formattedCSharpText}");
4138

4239
// We now have a formatted C# document, and an original document, but we can't just apply the changes to the original
@@ -217,28 +214,18 @@ public async Task<ImmutableArray<TextChange>> ExecuteAsync(FormattingContext con
217214
return changedText.GetTextChangesArray(context.SourceText);
218215
}
219216

220-
private async Task<SourceText> FormatCSharpAsync(SourceText generatedCSharpText, RazorIndentationOptions options, CancellationToken cancellationToken)
217+
private async Task<SourceText> FormatCSharpAsync(SourceText generatedCSharpText, RazorFormattingOptions options, CancellationToken cancellationToken)
221218
{
222219
using var helper = new RoslynWorkspaceHelper(_hostServicesProvider);
223220

224221
var tree = CSharpSyntaxTree.ParseText(generatedCSharpText, cancellationToken: cancellationToken);
225222
var csharpRoot = await tree.GetRootAsync(cancellationToken).ConfigureAwait(false);
226-
var csharpChanges = RazorCSharpFormattingInteractionService.GetFormattedTextChanges(helper.HostWorkspaceServices, csharpRoot, csharpRoot.FullSpan, options, _csharpSyntaxFormattingOptionsOverride, cancellationToken);
223+
var csharpChanges = RazorCSharpFormattingInteractionService.GetFormattedTextChanges(helper.HostWorkspaceServices, csharpRoot, csharpRoot.FullSpan, options.ToIndentationOptions(), options.CSharpSyntaxFormattingOptions, cancellationToken);
227224

228225
return generatedCSharpText.WithChanges(csharpChanges);
229226
}
230227

231228
[Obsolete("Only for the syntax visualizer, do not call")]
232229
internal static string GetFormattingDocumentContentsForSyntaxVisualizer(RazorCodeDocument codeDocument)
233230
=> CSharpDocumentGenerator.Generate(codeDocument, new()).SourceText.ToString();
234-
235-
internal TestAccessor GetTestAccessor() => new TestAccessor(this);
236-
237-
internal readonly struct TestAccessor(CSharpFormattingPass instance)
238-
{
239-
public void SetCSharpSyntaxFormattingOptionsOverride(RazorCSharpSyntaxFormattingOptions? optionsOverride)
240-
{
241-
instance._csharpSyntaxFormattingOptionsOverride = optionsOverride;
242-
}
243-
}
244231
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Runtime.Serialization;
55
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
6+
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Features;
67

78
namespace Microsoft.CodeAnalysis.Razor.Formatting;
89

@@ -15,6 +16,8 @@ internal readonly record struct RazorFormattingOptions
1516
public int TabSize { get; init; } = 4;
1617
[DataMember(Order = 2)]
1718
public bool CodeBlockBraceOnNextLine { get; init; } = false;
19+
[DataMember(Order = 3)]
20+
public RazorCSharpSyntaxFormattingOptions? CSharpSyntaxFormattingOptions { get; init; }
1821

1922
public RazorFormattingOptions()
2023
{
@@ -28,6 +31,15 @@ public static RazorFormattingOptions From(FormattingOptions options, bool codeBl
2831
CodeBlockBraceOnNextLine = codeBlockBraceOnNextLine
2932
};
3033

34+
public static RazorFormattingOptions From(FormattingOptions options, bool codeBlockBraceOnNextLine, RazorCSharpSyntaxFormattingOptions csharpSyntaxFormattingOptions)
35+
=> new()
36+
{
37+
InsertSpaces = options.InsertSpaces,
38+
TabSize = options.TabSize,
39+
CodeBlockBraceOnNextLine = codeBlockBraceOnNextLine,
40+
CSharpSyntaxFormattingOptions = csharpSyntaxFormattingOptions
41+
};
42+
3143
public RazorIndentationOptions ToIndentationOptions()
3244
=> new(
3345
UseTabs: !InsertSpaces,

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Microsoft.AspNetCore.Razor.Language;
1212
using Microsoft.AspNetCore.Razor.PooledObjects;
1313
using Microsoft.CodeAnalysis;
14-
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Features;
1514
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
1615
using Microsoft.CodeAnalysis.Razor.Logging;
1716
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
@@ -396,23 +395,5 @@ public void SetDebugAssertsEnabled(bool debugAssertsEnabled)
396395
var contentValidationPass = service._validationPasses.OfType<FormattingContentValidationPass>().Single();
397396
contentValidationPass.DebugAssertsEnabled = debugAssertsEnabled;
398397
}
399-
400-
public void SetCSharpSyntaxFormattingOptionsOverride(RazorCSharpSyntaxFormattingOptions? optionsOverride)
401-
{
402-
if (service._documentFormattingPasses.OfType<CSharpFormattingPass>().SingleOrDefault() is { } pass)
403-
{
404-
pass.GetTestAccessor().SetCSharpSyntaxFormattingOptionsOverride(optionsOverride);
405-
}
406-
407-
if (service._documentFormattingPasses.OfType<CSharpOnTypeFormattingPass>().SingleOrDefault() is { } onTypePass)
408-
{
409-
onTypePass.GetTestAccessor().SetCSharpSyntaxFormattingOptionsOverride(optionsOverride);
410-
}
411-
412-
if (service._documentFormattingPasses.OfType<New.CSharpFormattingPass>().SingleOrDefault() is { } newPass)
413-
{
414-
newPass.GetTestAccessor().SetCSharpSyntaxFormattingOptionsOverride(optionsOverride);
415-
}
416-
}
417398
}
418399
}

0 commit comments

Comments
 (0)