Skip to content

Commit d066a2c

Browse files
committed
Use users settings as a starting point for Razor formatting
1 parent 530601e commit d066a2c

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

src/EditorFeatures/Core/Options/LegacyGlobalOptionsWorkspaceService.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Composition;
77
using Microsoft.CodeAnalysis.Formatting;
8+
using Microsoft.CodeAnalysis.Host;
89
using Microsoft.CodeAnalysis.Host.Mef;
910

1011
namespace Microsoft.CodeAnalysis.Options;
@@ -63,4 +64,7 @@ public bool GetGenerateConstructorFromMembersOptionsAddNullChecks(string languag
6364

6465
public void SetGenerateConstructorFromMembersOptionsAddNullChecks(string language, bool value)
6566
=> _globalOptions.SetGlobalOption(s_addNullChecks, language, value);
67+
68+
public SyntaxFormattingOptions GetSyntaxFormattingOptions(LanguageServices languageServices)
69+
=> _globalOptions.GetSyntaxFormattingOptions(languageServices);
6670
}

src/Features/ExternalAccess/OmniSharp/InternalAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Options.OmnisharpLegacyGlobalOpt
213213
Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Options.OmnisharpLegacyGlobalOptionsWorkspaceService.GetGenerateConstructorFromMembersOptionsAddNullChecks(string! language) -> bool
214214
Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Options.OmnisharpLegacyGlobalOptionsWorkspaceService.GetGenerateEqualsAndGetHashCodeFromMembersGenerateOperators(string! language) -> bool
215215
Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Options.OmnisharpLegacyGlobalOptionsWorkspaceService.GetGenerateEqualsAndGetHashCodeFromMembersImplementIEquatable(string! language) -> bool
216+
Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Options.OmnisharpLegacyGlobalOptionsWorkspaceService.GetSyntaxFormattingOptions(Microsoft.CodeAnalysis.Host.LanguageServices! languageServices) -> Microsoft.CodeAnalysis.Formatting.SyntaxFormattingOptions!
216217
Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Options.OmnisharpLegacyGlobalOptionsWorkspaceService.InlineHintsOptionsDisplayAllOverride.get -> bool
217218
Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Options.OmnisharpLegacyGlobalOptionsWorkspaceService.InlineHintsOptionsDisplayAllOverride.set -> void
218219
Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Options.OmnisharpLegacyGlobalOptionsWorkspaceService.OmniSharpCleanCodeGenerationOptionsProvider

src/Features/ExternalAccess/OmniSharp/Options/OmnisharpLegacyGlobalOptionsWorkspaceService.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Composition;
77
using Microsoft.CodeAnalysis.Formatting;
8+
using Microsoft.CodeAnalysis.Host;
89
using Microsoft.CodeAnalysis.Host.Mef;
910
using Microsoft.CodeAnalysis.Options;
1011

@@ -54,5 +55,8 @@ public bool GetGenerateConstructorFromMembersOptionsAddNullChecks(string languag
5455
public void SetGenerateConstructorFromMembersOptionsAddNullChecks(string language, bool value)
5556
{
5657
}
58+
59+
public SyntaxFormattingOptions GetSyntaxFormattingOptions(LanguageServices languageServices)
60+
=> SyntaxFormattingOptions.CommonDefaults;
5761
}
5862
}

src/Tools/ExternalAccess/Razor/RazorCSharpFormattingInteractionService.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static async Task<ImmutableArray<TextChange>> GetFormattingChangesAsync(
4747
return ImmutableArray<TextChange>.Empty;
4848
}
4949

50-
var formattingOptions = GetFormattingOptions(indentationOptions);
50+
var formattingOptions = GetFormattingOptions(document.Project.Solution.Services, indentationOptions);
5151
var roslynIndentationOptions = new IndentationOptions(formattingOptions)
5252
{
5353
AutoFormattingOptions = autoFormattingOptions.UnderlyingObject,
@@ -65,7 +65,7 @@ public static IList<TextChange> GetFormattedTextChanges(
6565
CancellationToken cancellationToken)
6666
{
6767
Contract.ThrowIfFalse(root.Language is LanguageNames.CSharp);
68-
return Formatter.GetFormattedTextChanges(root, span, services.SolutionServices, GetFormattingOptions(indentationOptions), cancellationToken);
68+
return Formatter.GetFormattedTextChanges(root, span, services.SolutionServices, GetFormattingOptions(services.SolutionServices, indentationOptions), cancellationToken);
6969
}
7070

7171
public static SyntaxNode Format(
@@ -75,19 +75,26 @@ public static SyntaxNode Format(
7575
CancellationToken cancellationToken)
7676
{
7777
Contract.ThrowIfFalse(root.Language is LanguageNames.CSharp);
78-
return Formatter.Format(root, services.SolutionServices, GetFormattingOptions(indentationOptions), cancellationToken: cancellationToken);
78+
return Formatter.Format(root, services.SolutionServices, GetFormattingOptions(services.SolutionServices, indentationOptions), cancellationToken: cancellationToken);
7979
}
8080

81-
private static SyntaxFormattingOptions GetFormattingOptions(RazorIndentationOptions indentationOptions)
82-
=> new CSharpSyntaxFormattingOptions()
81+
private static SyntaxFormattingOptions GetFormattingOptions(SolutionServices services, RazorIndentationOptions indentationOptions)
82+
{
83+
var legacyOptionsService = services.GetService<ILegacyGlobalOptionsWorkspaceService>();
84+
var formattingOptions = legacyOptionsService is null
85+
? new CSharpSyntaxFormattingOptions()
86+
: legacyOptionsService.GetSyntaxFormattingOptions(services.GetLanguageServices(LanguageNames.CSharp));
87+
88+
return formattingOptions with
8389
{
84-
LineFormatting = new()
90+
LineFormatting = formattingOptions.LineFormatting with
8591
{
8692
UseTabs = indentationOptions.UseTabs,
8793
TabSize = indentationOptions.TabSize,
8894
IndentationSize = indentationOptions.IndentationSize,
8995
NewLine = CSharpSyntaxFormattingOptions.Default.NewLine
9096
}
9197
};
98+
}
9299
}
93100
}

src/Workspaces/Core/Portable/Options/ILegacyGlobalOptionsWorkspaceService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.CodeAnalysis.Formatting;
56
using Microsoft.CodeAnalysis.Host;
67

78
namespace Microsoft.CodeAnalysis.Options;
@@ -25,4 +26,6 @@ internal interface ILegacyGlobalOptionsWorkspaceService : IWorkspaceService
2526

2627
public bool GetGenerateConstructorFromMembersOptionsAddNullChecks(string language);
2728
public void SetGenerateConstructorFromMembersOptionsAddNullChecks(string language, bool value);
29+
30+
SyntaxFormattingOptions GetSyntaxFormattingOptions(LanguageServices languageServices);
2831
}

0 commit comments

Comments
 (0)