Skip to content

Commit

Permalink
Cleanup GlobalOptionService
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Jun 15, 2022
1 parent 0413b65 commit 6dd76ee
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.CodeAnalysis.Editor.InlineRename
{
internal sealed class InlineRenameExperimentationOptions
{
public static readonly Option<bool> UseInlineAdornment = new(
public static readonly Option2<bool> UseInlineAdornment = new(
feature: "InlineRenameExperimentation",
name: "UseInlineAdornment",
defaultValue: false,
Expand Down
10 changes: 8 additions & 2 deletions src/EditorFeatures/Core/Logging/FunctionIdOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Concurrent;
using Microsoft.CodeAnalysis.Options;
using Roslyn.Utilities;
using System.Collections.Generic;

namespace Microsoft.CodeAnalysis.Internal.Log
{
Expand All @@ -25,13 +26,18 @@ private static Option2<bool> CreateOption(FunctionId id)
storageLocation: new LocalUserProfileStorageLocation(@"Roslyn\Internal\Performance\FunctionId\" + name));
}

private static IEnumerable<FunctionId> GetFunctionIds()
=> Enum.GetValues(typeof(FunctionId)).Cast<FunctionId>();

public static IEnumerable<IOption> GetOptions()
=> GetFunctionIds().Select(GetOption);

public static Option2<bool> GetOption(FunctionId id)
=> s_options.GetOrAdd(id, s_optionCreator);

public static Func<FunctionId, bool> CreateFunctionIsEnabledPredicate(IGlobalOptionService globalOptions)
{
var functionIds = Enum.GetValues(typeof(FunctionId)).Cast<FunctionId>();
var functionIdOptions = functionIds.ToDictionary(id => id, id => globalOptions.GetOption(GetOption(id)));
var functionIdOptions = GetFunctionIds().ToDictionary(id => id, id => globalOptions.GetOption(GetOption(id)));
return functionId => functionIdOptions[functionId];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ internal sealed class NavigationBarViewOptions
{
private const string FeatureName = "NavigationBarOptions";

public static readonly PerLanguageOption<bool> ShowNavigationBar = new(FeatureName, nameof(ShowNavigationBar), defaultValue: true);
public static readonly PerLanguageOption2<bool> ShowNavigationBar = new(FeatureName, "ShowNavigationBar", defaultValue: true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public override TWorkspaceService GetService<TWorkspaceService>()
private sealed class MockOptionService : ILegacyWorkspaceOptionService
{
public IGlobalOptionService GlobalOptions { get; } =
new GlobalOptionService(workspaceThreadingService: null, ImmutableArray<Lazy<IOptionProvider, LanguageMetadata>>.Empty, ImmutableArray<Lazy<IOptionPersisterProvider>>.Empty);
new GlobalOptionService(workspaceThreadingService: null, ImmutableArray<Lazy<IOptionPersisterProvider>>.Empty);

public void RegisterWorkspace(Workspace workspace)
{
Expand Down
4 changes: 2 additions & 2 deletions src/VisualStudio/Core/Impl/Options/AbstractOptionPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#nullable disable

using System;
using System.Linq;
using Microsoft.CodeAnalysis.Options;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Shell;
Expand All @@ -31,7 +32,7 @@ private void EnsureOptionPageCreated()
var componentModel = (IComponentModel)this.Site.GetService(typeof(SComponentModel));
var workspace = componentModel.GetService<VisualStudioWorkspace>();
s_optionService = workspace.Services.GetService<ILegacyWorkspaceOptionService>();
s_optionStore = new OptionStore(new SolutionOptionSet(s_optionService), s_optionService.GlobalOptions.GetRegisteredOptions());
s_optionStore = new OptionStore(new SolutionOptionSet(s_optionService), Enumerable.Empty<IOption>());
}

if (pageControl == null)
Expand Down Expand Up @@ -60,7 +61,6 @@ protected override void OnActivate(System.ComponentModel.CancelEventArgs e)
{
// Reset the option store to the current state of the options.
s_optionStore.SetOptions(new SolutionOptionSet(s_optionService));
s_optionStore.SetRegisteredOptions(s_optionService.GlobalOptions.GetRegisteredOptions());

s_needsToUpdateOptionStore = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,20 @@ public async Task ResetGlobalOptionsAsync(CancellationToken cancellationToken)
configurationService.Clear();

var globalOptions = await GetComponentModelServiceAsync<IGlobalOptionService>(cancellationToken);
ResetOption2(globalOptions, MetadataAsSourceOptionsStorage.NavigateToDecompiledSources);
ResetOption2(globalOptions, WorkspaceConfigurationOptionsStorage.EnableOpeningSourceGeneratedFilesInWorkspace);
ResetOption(globalOptions, MetadataAsSourceOptionsStorage.NavigateToDecompiledSources);
ResetOption(globalOptions, WorkspaceConfigurationOptionsStorage.EnableOpeningSourceGeneratedFilesInWorkspace);
ResetPerLanguageOption(globalOptions, NavigationBarViewOptions.ShowNavigationBar);
ResetPerLanguageOption2(globalOptions, VisualStudioNavigationOptions.NavigateToObjectBrowser);
ResetPerLanguageOption2(globalOptions, FeatureOnOffOptions.AddImportsOnPaste);
ResetPerLanguageOption2(globalOptions, FeatureOnOffOptions.PrettyListing);
ResetPerLanguageOption2(globalOptions, CompletionViewOptions.EnableArgumentCompletionSnippets);
ResetPerLanguageOption(globalOptions, VisualStudioNavigationOptions.NavigateToObjectBrowser);
ResetPerLanguageOption(globalOptions, FeatureOnOffOptions.AddImportsOnPaste);
ResetPerLanguageOption(globalOptions, FeatureOnOffOptions.PrettyListing);
ResetPerLanguageOption(globalOptions, CompletionViewOptions.EnableArgumentCompletionSnippets);

static void ResetOption2<T>(IGlobalOptionService globalOptions, Option2<T> option)
static void ResetOption<T>(IGlobalOptionService globalOptions, Option2<T> option)
{
globalOptions.SetGlobalOption(new OptionKey(option, language: null), option.DefaultValue);
}

static void ResetPerLanguageOption<T>(IGlobalOptionService globalOptions, PerLanguageOption<T> option)
{
globalOptions.SetGlobalOption(new OptionKey(option, LanguageNames.CSharp), option.DefaultValue);
globalOptions.SetGlobalOption(new OptionKey(option, LanguageNames.VisualBasic), option.DefaultValue);
}

static void ResetPerLanguageOption2<T>(IGlobalOptionService globalOptions, PerLanguageOption2<T> option)
static void ResetPerLanguageOption<T>(IGlobalOptionService globalOptions, PerLanguageOption2<T> option)
{
globalOptions.SetGlobalOption(new OptionKey(option, LanguageNames.CSharp), option.DefaultValue);
globalOptions.SetGlobalOption(new OptionKey(option, LanguageNames.VisualBasic), option.DefaultValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.CodeAnalysis.Editor.Shared.Options;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.NavigateTo;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Remote;
using Microsoft.CodeAnalysis.SymbolSearch;
using Microsoft.VisualStudio.LanguageServices;
Expand All @@ -18,17 +19,15 @@
namespace Roslyn.VisualStudio.DiagnosticsWindow.OptionsPages
{
[Guid(Guids.RoslynOptionPageFeatureManagerFeaturesIdString)]
internal class InternalFeaturesOnOffPage : AbstractOptionPage
internal class ForceLowMemoryModePage : AbstractOptionPage
{
protected override AbstractOptionPageControl CreateOptionPage(IServiceProvider serviceProvider, OptionStore optionStore)
{
return new InternalFeaturesOptionsControl(nameof(InternalFeatureOnOffOptions), optionStore);
}
=> new Control(optionStore);

internal class InternalFeaturesOptionsControl : InternalOptionsControl
internal sealed class Control : InternalOptionsControl
{
public InternalFeaturesOptionsControl(string featureOptionName, OptionStore optionStore)
: base(featureOptionName, optionStore)
public Control(OptionStore optionStore)
: base(Array.Empty<IOption>(), optionStore)
{
}

Expand All @@ -48,14 +47,6 @@ protected override void AddOptions(Panel panel)
lowMemoryGroup.Children.Add(new TextBlock { Text = "megabytes of extra memory in devenv.exe" });

panel.Children.Add(lowMemoryGroup);

// add OOP feature options
var oopFeatureGroup = new StackPanel();

panel.Children.Add(oopFeatureGroup);

// and add the rest of the options
base.AddOptions(panel);
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
Expand All @@ -16,12 +17,12 @@ namespace Microsoft.VisualStudio.LanguageServices.Implementation.Options
{
internal partial class InternalOptionsControl : AbstractOptionPageControl
{
private readonly string _featureOptionName;
private readonly IEnumerable<IOption> _options;

public InternalOptionsControl(string featureOptionName, OptionStore optionStore)
public InternalOptionsControl(IEnumerable<IOption> options, OptionStore optionStore)
: base(optionStore)
{
_featureOptionName = featureOptionName;
_options = options;

// options
var optionsPanel = new StackPanel();
Expand Down Expand Up @@ -108,7 +109,7 @@ public InternalOptionsControl(string featureOptionName, OptionStore optionStore)

protected virtual void AddOptions(Panel panel)
{
foreach (var option in OptionStore.GetRegisteredOptions().Where(o => o.Feature == _featureOptionName).OrderBy(o => o.Name))
foreach (var option in _options)
{
if (!option.IsPerLanguage)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.Runtime.InteropServices;
using Microsoft.CodeAnalysis.Internal.Log;
Expand All @@ -17,7 +15,7 @@ internal class PerformanceFunctionIdPage : AbstractOptionPage
{
protected override AbstractOptionPageControl CreateOptionPage(IServiceProvider serviceProvider, OptionStore optionStore)
{
return new InternalOptionsControl(nameof(FunctionIdOptions), optionStore);
return new InternalOptionsControl(FunctionIdOptions.GetOptions(), optionStore);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected override AbstractOptionPageControl CreateOptionPage(IServiceProvider s
_workspaceServices = workspace.Services;
}

return new InternalOptionsControl(nameof(LoggerOptions), optionStore);
return new InternalOptionsControl(FunctionIdOptions.GetOptions(), optionStore);
}

protected override void OnApply(PageApplyEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,9 @@
@="#0"
"Package"="{49e24138-9ee3-49e0-8ede-6b39f49303bf}"

[$RootKey$\ToolsOptionsPages\Roslyn\FeatureManager\Components]
@="#0"
"Package"="{49e24138-9ee3-49e0-8ede-6b39f49303bf}"
"Page"="{6f738951-348c-4816-9ba4-f60d92d3e98e}"

[$RootKey$\AutomationProperties\Roslyn\FeatureManager]
"Package"="{49e24138-9ee3-49e0-8ede-6b39f49303bf}"

[$RootKey$\AutomationProperties\Roslyn\FeatureManager\Components]
"Package"="{49e24138-9ee3-49e0-8ede-6b39f49303bf}"

[$RootKey$\AutomationProperties\Roslyn\FeatureManager\Components]
"Name"="Roslyn\FeatureManager.Components"

[$RootKey$\ToolsOptionsPages\Roslyn\FeatureManager]
@="#0"
"Package"="{49e24138-9ee3-49e0-8ede-6b39f49303bf}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ namespace Roslyn.VisualStudio.DiagnosticsWindow
// The option page configuration is duplicated in PackageRegistration.pkgdef.
// These attributes specify the menu structure to be used in Tools | Options. These are not
// localized because they are for internal use only.
[ProvideOptionPage(typeof(InternalFeaturesOnOffPage), @"Roslyn\FeatureManager", @"Features", categoryResourceID: 0, pageNameResourceID: 0, supportsAutomation: true, SupportsProfiles = false)]
[ProvideOptionPage(typeof(InternalComponentsOnOffPage), @"Roslyn\FeatureManager", @"Components", categoryResourceID: 0, pageNameResourceID: 0, supportsAutomation: true, SupportsProfiles = false)]
[ProvideOptionPage(typeof(ForceLowMemoryModePage), @"Roslyn\FeatureManager", @"Features", categoryResourceID: 0, pageNameResourceID: 0, supportsAutomation: true, SupportsProfiles = false)]
[ProvideOptionPage(typeof(PerformanceFunctionIdPage), @"Roslyn\Performance", @"FunctionId", categoryResourceID: 0, pageNameResourceID: 0, supportsAutomation: true, SupportsProfiles = false)]
[ProvideOptionPage(typeof(PerformanceLoggersPage), @"Roslyn\Performance", @"Loggers", categoryResourceID: 0, pageNameResourceID: 0, supportsAutomation: true, SupportsProfiles = false)]
[ProvideToolWindow(typeof(DiagnosticsWindow))]
Expand Down
12 changes: 0 additions & 12 deletions src/Workspaces/Core/Portable/Options/GlobalOptionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ namespace Microsoft.CodeAnalysis.Options
internal sealed class GlobalOptionService : IGlobalOptionService
{
private readonly IWorkspaceThreadingService? _workspaceThreadingService;
private readonly Lazy<ImmutableHashSet<IOption>> _lazyAllOptions;
private readonly ImmutableArray<Lazy<IOptionPersisterProvider>> _optionPersisterProviders;

// access is interlocked
Expand All @@ -48,13 +47,11 @@ internal sealed class GlobalOptionService : IGlobalOptionService
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
public GlobalOptionService(
[Import(AllowDefault = true)] IWorkspaceThreadingService? workspaceThreadingService,
[ImportMany] IEnumerable<Lazy<IOptionProvider, LanguageMetadata>> optionProviders,
[ImportMany] IEnumerable<Lazy<IOptionPersisterProvider>> optionPersisters)
{
_getOption = GetOption;

_workspaceThreadingService = workspaceThreadingService;
_lazyAllOptions = new Lazy<ImmutableHashSet<IOption>>(() => optionProviders.SelectMany(p => p.Value.Options).ToImmutableHashSet());
_optionPersisterProviders = optionPersisters.ToImmutableArray();
_registeredWorkspaces = ImmutableArray<Workspace>.Empty;

Expand Down Expand Up @@ -118,18 +115,9 @@ static async Task<ImmutableArray<IOptionPersister>> GetOptionPersistersAsync(
return optionKey.Option.DefaultValue;
}

public IEnumerable<IOption> GetRegisteredOptions()
=> _lazyAllOptions.Value;

public T GetOption<T>(Option<T> option)
=> OptionsHelpers.GetOption(option, _getOption);

public T GetOption<T>(Option2<T> option)
=> OptionsHelpers.GetOption(option, _getOption);

public T GetOption<T>(PerLanguageOption<T> option, string? language)
=> OptionsHelpers.GetOption(option, language, _getOption);

public T GetOption<T>(PerLanguageOption2<T> option, string? language)
=> OptionsHelpers.GetOption(option, language, _getOption);

Expand Down
15 changes: 0 additions & 15 deletions src/Workspaces/Core/Portable/Options/IGlobalOptionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,11 @@ namespace Microsoft.CodeAnalysis.Options
/// </summary>
internal interface IGlobalOptionService
{
/// <summary>
/// Gets the current value of the specific option.
/// </summary>
T GetOption<T>(Option<T> option);

/// <summary>
/// Gets the current value of the specific option.
/// </summary>
T GetOption<T>(Option2<T> option);

/// <summary>
/// Gets the current value of the specific option.
/// </summary>
T GetOption<T>(PerLanguageOption<T> option, string? languageName);

/// <summary>
/// Gets the current value of the specific option.
/// </summary>
Expand Down Expand Up @@ -74,11 +64,6 @@ internal interface IGlobalOptionService
/// </summary>
void SetGlobalOptions(ImmutableArray<OptionKey> optionKeys, ImmutableArray<object?> values);

/// <summary>
/// Returns the set of all registered options.
/// </summary>
IEnumerable<IOption> GetRegisteredOptions();

event EventHandler<OptionChangedEventArgs>? OptionChanged;

/// <summary>
Expand Down
Loading

0 comments on commit 6dd76ee

Please sign in to comment.