Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honour users' settings as a starting point for Razor formatting #76066

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Composition;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;

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

public void SetGenerateConstructorFromMembersOptionsAddNullChecks(string language, bool value)
=> _globalOptions.SetGlobalOption(s_addNullChecks, language, value);

public SyntaxFormattingOptions GetSyntaxFormattingOptions(LanguageServices languageServices)
=> _globalOptions.GetSyntaxFormattingOptions(languageServices);
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Options.OmnisharpLegacyGlobalOpt
Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Options.OmnisharpLegacyGlobalOptionsWorkspaceService.GetGenerateConstructorFromMembersOptionsAddNullChecks(string! language) -> bool
Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Options.OmnisharpLegacyGlobalOptionsWorkspaceService.GetGenerateEqualsAndGetHashCodeFromMembersGenerateOperators(string! language) -> bool
Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Options.OmnisharpLegacyGlobalOptionsWorkspaceService.GetGenerateEqualsAndGetHashCodeFromMembersImplementIEquatable(string! language) -> bool
Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Options.OmnisharpLegacyGlobalOptionsWorkspaceService.GetSyntaxFormattingOptions(Microsoft.CodeAnalysis.Host.LanguageServices! languageServices) -> Microsoft.CodeAnalysis.Formatting.SyntaxFormattingOptions!
Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Options.OmnisharpLegacyGlobalOptionsWorkspaceService.InlineHintsOptionsDisplayAllOverride.get -> bool
Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Options.OmnisharpLegacyGlobalOptionsWorkspaceService.InlineHintsOptionsDisplayAllOverride.set -> void
Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Options.OmnisharpLegacyGlobalOptionsWorkspaceService.OmniSharpCleanCodeGenerationOptionsProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Composition;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;

Expand Down Expand Up @@ -54,5 +55,8 @@ public bool GetGenerateConstructorFromMembersOptionsAddNullChecks(string languag
public void SetGenerateConstructorFromMembersOptionsAddNullChecks(string language, bool value)
{
}

public SyntaxFormattingOptions GetSyntaxFormattingOptions(LanguageServices languageServices)
=> SyntaxFormattingOptions.CommonDefaults;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static async Task<ImmutableArray<TextChange>> GetFormattingChangesAsync(
return ImmutableArray<TextChange>.Empty;
}

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

public static SyntaxNode Format(
Expand All @@ -75,19 +75,26 @@ public static SyntaxNode Format(
CancellationToken cancellationToken)
{
Contract.ThrowIfFalse(root.Language is LanguageNames.CSharp);
return Formatter.Format(root, services.SolutionServices, GetFormattingOptions(indentationOptions), cancellationToken: cancellationToken);
return Formatter.Format(root, services.SolutionServices, GetFormattingOptions(services.SolutionServices, indentationOptions), cancellationToken: cancellationToken);
}

private static SyntaxFormattingOptions GetFormattingOptions(RazorIndentationOptions indentationOptions)
=> new CSharpSyntaxFormattingOptions()
private static SyntaxFormattingOptions GetFormattingOptions(SolutionServices services, RazorIndentationOptions indentationOptions)
{
var legacyOptionsService = services.GetService<ILegacyGlobalOptionsWorkspaceService>();
var formattingOptions = legacyOptionsService is null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't run OOP right? IIRC the legacy options service isn't available there

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way this won't work in cohosting. Options are not synced pro-actively to Roslyn OOP, each feature pulls what it needs via callbacks, so even if this service is available the underlying IGlobalOptionsService has only defaults in OOP. Callbacks didn't work for Razor services last time I tried (strange IVT-type issues with service hub assembly loading) so this will have to be solved separately.

Sadly cohosting makes some things worse, then we have to go and make them better again.

? new CSharpSyntaxFormattingOptions()
: legacyOptionsService.GetSyntaxFormattingOptions(services.GetLanguageServices(LanguageNames.CSharp));

return formattingOptions with
{
LineFormatting = new()
LineFormatting = formattingOptions.LineFormatting with
{
UseTabs = indentationOptions.UseTabs,
TabSize = indentationOptions.TabSize,
IndentationSize = indentationOptions.IndentationSize,
NewLine = CSharpSyntaxFormattingOptions.Default.NewLine
}
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host;

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

public bool GetGenerateConstructorFromMembersOptionsAddNullChecks(string language);
public void SetGenerateConstructorFromMembersOptionsAddNullChecks(string language, bool value);

SyntaxFormattingOptions GetSyntaxFormattingOptions(LanguageServices languageServices);
}
Loading