Skip to content

Commit

Permalink
Implement legacy options provider for code actions (#66798)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat authored Mar 1, 2023
1 parent 0df38c3 commit 683a5cb
Show file tree
Hide file tree
Showing 25 changed files with 150 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

using System;
using System.Composition;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.CodeGeneration;
using Microsoft.CodeAnalysis.GenerateEqualsAndGetHashCodeFromMembers;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.InlineHints;

Expand All @@ -20,7 +18,6 @@ namespace Microsoft.CodeAnalysis.Options
internal sealed class LegacyGlobalOptionsWorkspaceService : ILegacyGlobalOptionsWorkspaceService
{
private readonly IGlobalOptionService _globalOptions;
private readonly CodeActionOptionsStorage.Provider _provider;

private static readonly Option2<bool> s_generateOverridesOption = new(
"dotnet_generate_overrides_for_all_members", defaultValue: true);
Expand All @@ -42,7 +39,6 @@ internal sealed class LegacyGlobalOptionsWorkspaceService : ILegacyGlobalOptions
public LegacyGlobalOptionsWorkspaceService(IGlobalOptionService globalOptions)
{
_globalOptions = globalOptions;
_provider = _globalOptions.CreateProvider();
}

public bool GenerateOverrides
Expand All @@ -64,9 +60,6 @@ public bool InlineHintsOptionsDisplayAllOverride
set => _globalOptions.SetGlobalOption(InlineHintsGlobalStateOption.DisplayAllOverride, value);
}

public CleanCodeGenerationOptionsProvider CleanCodeGenerationOptionsProvider
=> _provider;

public bool GetGenerateEqualsAndGetHashCodeFromMembersGenerateOperators(string language)
=> _globalOptions.GetOption(s_implementIEquatable, language);

Expand Down
6 changes: 3 additions & 3 deletions src/EditorFeatures/Core/Options/TextBufferOptionProviders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static SyntaxFormattingOptions GetSyntaxFormattingOptionsImpl(ITextBuffe
{
var configOptions = editorOptions.ToAnalyzerConfigOptions();
var fallbackOptions = globalOptions.GetSyntaxFormattingOptions(languageServices);
var options = configOptions.GetSyntaxFormattingOptions(fallbackOptions, languageServices);
var options = configOptions.GetSyntaxFormattingOptions(languageServices, fallbackOptions);
var lineFormattingOptions = GetLineFormattingOptionsImpl(textBuffer, editorOptions, indentationManager, explicitFormat);

return options with { LineFormatting = lineFormattingOptions };
Expand All @@ -74,7 +74,7 @@ public static AddImportPlacementOptions GetAddImportPlacementOptions(this ITextB
var editorOptions = optionsProvider.Factory.GetOptions(textBuffer);
var configOptions = editorOptions.ToAnalyzerConfigOptions();
var fallbackOptions = optionsProvider.GlobalOptions.GetAddImportPlacementOptions(languageServices);
return configOptions.GetAddImportPlacementOptions(allowInHiddenRegions, fallbackOptions, languageServices);
return configOptions.GetAddImportPlacementOptions(languageServices, allowInHiddenRegions, fallbackOptions);
}

public static CodeCleanupOptions GetCodeCleanupOptions(this ITextBuffer textBuffer, EditorOptionsService optionsProvider, LanguageServices languageServices, bool explicitFormat, bool allowImportsInHiddenRegions)
Expand All @@ -83,7 +83,7 @@ public static CodeCleanupOptions GetCodeCleanupOptions(this ITextBuffer textBuff
var configOptions = editorOptions.ToAnalyzerConfigOptions();
var fallbackOptions = optionsProvider.GlobalOptions.GetCodeCleanupOptions(languageServices);

var options = configOptions.GetCodeCleanupOptions(allowImportsInHiddenRegions, fallbackOptions, languageServices);
var options = configOptions.GetCodeCleanupOptions(languageServices, allowImportsInHiddenRegions, fallbackOptions);
var lineFormattingOptions = GetLineFormattingOptionsImpl(textBuffer, editorOptions, optionsProvider.IndentationManager, explicitFormat);

return options with { FormattingOptions = options.FormattingOptions with { LineFormatting = lineFormattingOptions } };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal async Task TestAsync(string testCode, string expected, OptionsCollectio
var languageServices = document.Project.Services;

var cleanupOptions =
options?.GetCodeCleanupOptions(allowImportsInHiddenRegions: false, fallbackOptions: null, languageServices) ??
options?.GetCodeCleanupOptions(languageServices, allowImportsInHiddenRegions: false, fallbackOptions: null) ??
CodeCleanupOptions.GetDefault(languageServices);

var formattingService = document.GetRequiredLanguageService<INewDocumentFormattingService>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public AbstractMemberInsertingCompletionProvider()
public override async Task<CompletionChange> GetChangeAsync(Document document, CompletionItem item, char? commitKey = null, CancellationToken cancellationToken = default)
{
// TODO: pass fallback options: https://github.com/dotnet/roslyn/issues/60786
var globalOptions = document.Project.Solution.Services.GetService<ILegacyGlobalOptionsWorkspaceService>();
var fallbackOptions = globalOptions?.CleanCodeGenerationOptionsProvider ?? CodeActionOptions.DefaultProvider;
var globalOptions = document.Project.Solution.Services.GetService<ILegacyGlobalCleanCodeGenerationOptionsWorkspaceService>();
var fallbackOptions = globalOptions?.Provider ?? CodeActionOptions.DefaultProvider;

var newDocument = await DetermineNewDocumentAsync(document, item, fallbackOptions, cancellationToken).ConfigureAwait(false);
var newText = await newDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ public override async Task<CompletionChange> GetChangeAsync(
var generator = document.GetRequiredLanguageService<SyntaxGenerator>();

// TODO: fallback options https://github.com/dotnet/roslyn/issues/60786
var globalOptions = document.Project.Solution.Services.GetService<ILegacyGlobalOptionsWorkspaceService>();
var fallbackOptions = globalOptions?.CleanCodeGenerationOptionsProvider ?? CodeActionOptions.DefaultProvider;
var globalOptions = document.Project.Solution.Services.GetService<ILegacyGlobalCleanCodeGenerationOptionsWorkspaceService>();
var fallbackOptions = globalOptions?.Provider ?? CodeActionOptions.DefaultProvider;

var addImportsOptions = await document.GetAddImportPlacementOptionsAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);
var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,5 @@ public static ValueTask<AddImportPlacementOptions> GetAddImportPlacementOptionsA
=> document.GetAddImportPlacementOptionsAsync(globalOptions.GetAddImportPlacementOptions(document.Project.Services), cancellationToken);

public static AddImportPlacementOptions GetAddImportPlacementOptions(this IGlobalOptionService globalOptions, LanguageServices languageServices)
=> languageServices.GetRequiredService<IAddImportsService>().GetAddImportOptions(
globalOptions,
allowInHiddenRegions: AddImportPlacementOptions.Default.AllowInHiddenRegions, // no global option available
fallbackOptions: null);
=> globalOptions.GetAddImportPlacementOptions(languageServices, allowInHiddenRegions: null, fallbackOptions: null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Simplification;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.AddImport;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CodeCleanup;

Expand All @@ -18,12 +14,6 @@ internal static class CodeCleanupOptionsStorage
public static ValueTask<CodeCleanupOptions> GetCodeCleanupOptionsAsync(this Document document, IGlobalOptionService globalOptions, CancellationToken cancellationToken)
=> document.GetCodeCleanupOptionsAsync(globalOptions.GetCodeCleanupOptions(document.Project.Services), cancellationToken);

public static CodeCleanupOptions GetCodeCleanupOptions(this IGlobalOptionService globalOptions, LanguageServices languageServices)
=> new()
{
FormattingOptions = globalOptions.GetSyntaxFormattingOptions(languageServices),
SimplifierOptions = globalOptions.GetSimplifierOptions(languageServices),
AddImportOptions = globalOptions.GetAddImportPlacementOptions(languageServices),
DocumentFormattingOptions = globalOptions.GetDocumentFormattingOptions(languageServices.Language)
};
public static CodeCleanupOptions GetCodeCleanupOptions(this IOptionsReader globalOptions, LanguageServices languageServices)
=> globalOptions.GetCodeCleanupOptions(languageServices, allowImportsInHiddenRegions: null, fallbackOptions: null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.AddImport;
using Microsoft.CodeAnalysis.CodeCleanup;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Options;

Expand All @@ -20,19 +18,11 @@ public static ValueTask<CleanCodeGenerationOptions> GetCleanCodeGenerationOption
=> document.GetCleanCodeGenerationOptionsAsync(globalOptions.GetCleanCodeGenerationOptions(document.Project.Services), cancellationToken);

public static CodeGenerationOptions GetCodeGenerationOptions(this IGlobalOptionService globalOptions, LanguageServices languageServices)
=> languageServices.GetRequiredService<ICodeGenerationService>().GetCodeGenerationOptions(globalOptions, fallbackOptions: null);
=> globalOptions.GetCodeGenerationOptions(languageServices, fallbackOptions: null);

public static CodeAndImportGenerationOptions GetCodeAndImportGenerationOptions(this IGlobalOptionService globalOptions, LanguageServices languageServices)
=> new()
{
GenerationOptions = globalOptions.GetCodeGenerationOptions(languageServices),
AddImportOptions = globalOptions.GetAddImportPlacementOptions(languageServices)
};
=> globalOptions.GetCodeAndImportGenerationOptions(languageServices, allowImportsInHiddenRegions: null, fallbackOptions: null);

public static CleanCodeGenerationOptions GetCleanCodeGenerationOptions(this IGlobalOptionService globalOptions, LanguageServices languageServices)
=> new()
{
GenerationOptions = globalOptions.GetCodeGenerationOptions(languageServices),
CleanupOptions = globalOptions.GetCodeCleanupOptions(languageServices)
};
=> globalOptions.GetCleanCodeGenerationOptions(languageServices, allowImportsInHiddenRegions: null, fallbackOptions: null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,18 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.Formatting;

internal static class DocumentFormattingOptionsStorage
{
public static ValueTask<DocumentFormattingOptions> GetDocumentFormattingOptionsAsync(this Document document, IGlobalOptionService globalOptions, CancellationToken cancellationToken)
=> document.GetDocumentFormattingOptionsAsync(globalOptions.GetDocumentFormattingOptions(document.Project.Language), cancellationToken);
=> document.GetDocumentFormattingOptionsAsync(globalOptions.GetDocumentFormattingOptions(), cancellationToken);

#pragma warning disable IDE0060 // Unused parameters to match common pattern
public static DocumentFormattingOptions GetDocumentFormattingOptions(this IGlobalOptionService globalOptions, string language)
=> new(
// FileHeaderTemplate not stored in global options (does not have a storage other than editorconfig)
// InsertFinalNewLine not stored in global options (does not have a storage other than editorconfig)
);
#pragma warning restore
public static DocumentFormattingOptions GetDocumentFormattingOptions(this IGlobalOptionService globalOptions)
=> globalOptions.GetDocumentFormattingOptions(fallbackOptions: null);
}

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ValueTask<LineFormattingOptions> OptionsProvider<LineFormattingOptions>.GetOptio
=> ValueTaskFactory.FromResult(_globalOptions.GetLineFormattingOptions(languageServices.Language));

ValueTask<DocumentFormattingOptions> OptionsProvider<DocumentFormattingOptions>.GetOptionsAsync(LanguageServices languageServices, CancellationToken cancellationToken)
=> ValueTaskFactory.FromResult(_globalOptions.GetDocumentFormattingOptions(languageServices.Language));
=> ValueTaskFactory.FromResult(_globalOptions.GetDocumentFormattingOptions());

ValueTask<SyntaxFormattingOptions> OptionsProvider<SyntaxFormattingOptions>.GetOptionsAsync(LanguageServices languageServices, CancellationToken cancellationToken)
=> ValueTaskFactory.FromResult(_globalOptions.GetSyntaxFormattingOptions(languageServices));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.Formatting;
Expand All @@ -18,12 +14,6 @@ public static ValueTask<LineFormattingOptions> GetLineFormattingOptionsAsync(thi
=> document.GetLineFormattingOptionsAsync(globalOptions.GetLineFormattingOptions(document.Project.Language), cancellationToken);

public static LineFormattingOptions GetLineFormattingOptions(this IGlobalOptionService globalOptions, string language)
=> new()
{
UseTabs = globalOptions.GetOption(FormattingOptions2.UseTabs, language),
TabSize = globalOptions.GetOption(FormattingOptions2.TabSize, language),
IndentationSize = globalOptions.GetOption(FormattingOptions2.IndentationSize, language),
NewLine = globalOptions.GetOption(FormattingOptions2.NewLine, language)
};
=> globalOptions.GetLineFormattingOptions(language, fallbackOptions: null);
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Simplification;

namespace Microsoft.CodeAnalysis.CodeStyle;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.OrganizeImports;
Expand All @@ -16,10 +14,5 @@ public static ValueTask<OrganizeImportsOptions> GetOrganizeImportsOptionsAsync(t
=> document.GetOrganizeImportsOptionsAsync(globalOptions.GetOrganizeImportsOptions(document.Project.Language), cancellationToken);

public static OrganizeImportsOptions GetOrganizeImportsOptions(this IGlobalOptionService globalOptions, string language)
=> new()
{
PlaceSystemNamespaceFirst = globalOptions.GetOption(GenerationOptions.PlaceSystemNamespaceFirst, language),
SeparateImportDirectiveGroups = globalOptions.GetOption(GenerationOptions.SeparateImportDirectiveGroups, language),
NewLine = globalOptions.GetOption(FormattingOptions2.NewLine, language)
};
=> globalOptions.GetOrganizeImportsOptions(language, fallbackOptions: null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ internal static class SimplifierOptionsStorage
public static ValueTask<SimplifierOptions> GetSimplifierOptionsAsync(this Document document, IGlobalOptionService globalOptions, CancellationToken cancellationToken)
=> document.GetSimplifierOptionsAsync(globalOptions.GetSimplifierOptions(document.Project.Services), cancellationToken);

public static SimplifierOptions GetSimplifierOptions(this IGlobalOptionService globalOptions, LanguageServices languageServices)
=> languageServices.GetRequiredService<ISimplificationService>().GetSimplifierOptions(globalOptions, fallbackOptions: null);
public static SimplifierOptions GetSimplifierOptions(this IGlobalOptionService options, LanguageServices languageServices)
=> options.GetSimplifierOptions(languageServices, fallbackOptions: null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public static ValueTask<SyntaxFormattingOptions> GetSyntaxFormattingOptionsAsync
=> document.GetSyntaxFormattingOptionsAsync(globalOptions.GetSyntaxFormattingOptions(document.Project.Services), cancellationToken);

public static SyntaxFormattingOptions GetSyntaxFormattingOptions(this IGlobalOptionService globalOptions, LanguageServices languageServices)
=> languageServices.GetRequiredService<ISyntaxFormattingService>().GetFormattingOptions(globalOptions, fallbackOptions: null);
=> globalOptions.GetSyntaxFormattingOptions(languageServices, fallbackOptions: null);
}

Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ public static Tuple<string, string, VsTextSpan> EnsureEventHandler(
throw new InvalidOperationException(ServicesVSResources.Can_t_find_where_to_insert_member);
}

var fallbackOptions = targetDocument.Project.Solution.Services.GetRequiredService<ILegacyGlobalOptionsWorkspaceService>().CleanCodeGenerationOptionsProvider;

var fallbackOptions = globalOptions.GetCleanCodeGenerationOptions(targetDocument.Project.Services);
var options = targetDocument.GetCleanCodeGenerationOptionsAsync(fallbackOptions, cancellationToken).AsTask().WaitAndGetResult_Venus(cancellationToken);

var info = codeGenerationService.GetInfo(new CodeGenerationContext(autoInsertionLocation: false), options.GenerationOptions, destinationType.SyntaxTree.Options);
Expand Down
6 changes: 3 additions & 3 deletions src/Workspaces/Core/Portable/CodeActions/CodeAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,9 @@ protected virtual async Task<Document> PostProcessChangesAsync(Document document
{
if (document.SupportsSyntaxTree)
{
// TODO: avoid ILegacyGlobalOptionsWorkspaceService https://github.com/dotnet/roslyn/issues/60777
var globalOptions = document.Project.Solution.Services.GetService<ILegacyGlobalOptionsWorkspaceService>();
var fallbackOptions = globalOptions?.CleanCodeGenerationOptionsProvider ?? CodeActionOptions.DefaultProvider;
// TODO: avoid ILegacyGlobalCodeActionOptionsWorkspaceService https://github.com/dotnet/roslyn/issues/60777
var globalOptions = document.Project.Solution.Services.GetService<ILegacyGlobalCleanCodeGenerationOptionsWorkspaceService>();
var fallbackOptions = globalOptions?.Provider ?? CodeActionOptions.DefaultProvider;

var options = await document.GetCodeCleanupOptionsAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);
return await CleanupDocumentAsync(document, options, cancellationToken).ConfigureAwait(false);
Expand Down
Loading

0 comments on commit 683a5cb

Please sign in to comment.