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

Options Refactoring - Round 3 #57254

Merged
merged 17 commits into from
Nov 1, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Fading;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Utilities;
using Roslyn.Utilities;
Expand Down Expand Up @@ -41,8 +40,11 @@ protected override void InitializeWorker(AnalysisContext context)

private void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
{
var fadeCode = context.GetOption(FadingOptions.FadeOutUnreachableCode, LanguageNames.CSharp);

#if CODE_STYLE
var fadeCode = true;
#else
var fadeCode = context.GetOption(Fading.FadingOptions.FadeOutUnreachableCode, LanguageNames.CSharp);
#endif
var semanticModel = context.SemanticModel;
var cancellationToken = context.CancellationToken;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Threading;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Fading;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
Expand Down Expand Up @@ -165,7 +164,11 @@ private void AnalyzeSemanticModel(SemanticModelAnalysisContext context)

static bool ShouldFade(AnalyzerOptions options, SyntaxTree tree, string language, CancellationToken cancellationToken)
{
return options.GetOption(FadingOptions.FadeOutUnusedImports, language, tree, cancellationToken);
#if CODE_STYLE
return true;
#else
return options.GetOption(Fading.FadingOptions.FadeOutUnusedImports, language, tree, cancellationToken);
#endif
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.CodeAnalysis.Editor.Implementation.AddImports;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
using Microsoft.VisualStudio.Commanding;
using Microsoft.VisualStudio.Utilities;

Expand All @@ -28,7 +29,8 @@ internal class CSharpAddImportsPasteCommandHandler : AbstractAddImportsPasteComm
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpAddImportsPasteCommandHandler(IThreadingContext threadingContext) : base(threadingContext)
public CSharpAddImportsPasteCommandHandler(IThreadingContext threadingContext, IGlobalOptionService globalOptions)
: base(threadingContext, globalOptions)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Commanding;
Expand Down Expand Up @@ -55,8 +56,9 @@ internal partial class AutomaticLineEnderCommandHandler : AbstractAutomaticLineE
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public AutomaticLineEnderCommandHandler(
ITextUndoHistoryRegistry undoRegistry,
IEditorOperationsFactoryService editorOperations)
: base(undoRegistry, editorOperations)
IEditorOperationsFactoryService editorOperations,
IGlobalOptionService globalOptions)
: base(undoRegistry, editorOperations, globalOptions)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.CodeAnalysis.Editor.Shared.Options;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Text.Shared.Extensions;
Expand All @@ -33,18 +34,21 @@ internal sealed class BlockCommentEditingCommandHandler : ICommandHandler<Return
{
private readonly ITextUndoHistoryRegistry _undoHistoryRegistry;
private readonly IEditorOperationsFactoryService _editorOperationsFactoryService;
private readonly IGlobalOptionService _globalOptions;

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public BlockCommentEditingCommandHandler(
ITextUndoHistoryRegistry undoHistoryRegistry,
IEditorOperationsFactoryService editorOperationsFactoryService)
IEditorOperationsFactoryService editorOperationsFactoryService,
IGlobalOptionService globalOptions)
{
Contract.ThrowIfNull(undoHistoryRegistry);
Contract.ThrowIfNull(editorOperationsFactoryService);

_undoHistoryRegistry = undoHistoryRegistry;
_editorOperationsFactoryService = editorOperationsFactoryService;
_globalOptions = globalOptions;
}

public string DisplayName => EditorFeaturesResources.Block_Comment_Editing;
Expand All @@ -57,7 +61,7 @@ public bool ExecuteCommand(ReturnKeyCommandArgs args, CommandExecutionContext co

private bool TryHandleReturnKey(ITextBuffer subjectBuffer, ITextView textView)
{
if (!subjectBuffer.GetFeatureOnOffOption(FeatureOnOffOptions.AutoInsertBlockCommentStartString))
if (!_globalOptions.GetOption(FeatureOnOffOptions.AutoInsertBlockCommentStartString, LanguageNames.CSharp))
return false;

var caretPosition = textView.GetCaretPoint(subjectBuffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Options;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text.Shared.Extensions;
using Microsoft.VisualStudio.Commanding;
using Microsoft.VisualStudio.Text.Editor.Commanding.Commands;
Expand All @@ -22,10 +23,13 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.BlockCommentEditing
[Order(After = nameof(BlockCommentEditingCommandHandler))]
internal sealed class CloseBlockCommentCommandHandler : ICommandHandler<TypeCharCommandArgs>
{
private readonly IGlobalOptionService _globalOptions;

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CloseBlockCommentCommandHandler()
public CloseBlockCommentCommandHandler(IGlobalOptionService globalOptions)
{
_globalOptions = globalOptions;
}

public string DisplayName => EditorFeaturesResources.Block_Comment_Editing;
Expand All @@ -49,7 +53,7 @@ public bool ExecuteCommand(TypeCharCommandArgs args, CommandExecutionContext exe
if (line.End == position &&
line.IsEmptyOrWhitespace(0, line.Length - 2))
{
if (args.SubjectBuffer.GetFeatureOnOffOption(FeatureOnOffOptions.AutoInsertBlockCommentStartString) &&
if (_globalOptions.GetOption(FeatureOnOffOptions.AutoInsertBlockCommentStartString, LanguageNames.CSharp) &&
BlockCommentEditingCommandHandler.IsCaretInsideBlockCommentSyntax(caret.Value, out _, out _))
{
args.SubjectBuffer.Replace(new VisualStudio.Text.Span(position - 1, 1), "/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public StringLiteralBraceMatcher()
{
}

public async Task<BraceMatchingResult?> FindBracesAsync(Document document, int position, CancellationToken cancellationToken)
public async Task<BraceMatchingResult?> FindBracesAsync(Document document, int position, BraceMatchingOptions options, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var token = root.FindToken(position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Text.Shared.Extensions;
Expand All @@ -42,15 +43,20 @@ internal sealed class CompleteStatementCommandHandler : IChainedCommandHandler<T
{
private readonly ITextUndoHistoryRegistry _textUndoHistoryRegistry;
private readonly IEditorOperationsFactoryService _editorOperationsFactoryService;
private readonly IGlobalOptionService _globalOptions;

public CommandState GetCommandState(TypeCharCommandArgs args, Func<CommandState> nextCommandHandler) => nextCommandHandler();

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CompleteStatementCommandHandler(ITextUndoHistoryRegistry textUndoHistoryRegistry, IEditorOperationsFactoryService editorOperationsFactoryService)
public CompleteStatementCommandHandler(
ITextUndoHistoryRegistry textUndoHistoryRegistry,
IEditorOperationsFactoryService editorOperationsFactoryService,
IGlobalOptionService globalOptions)
{
_textUndoHistoryRegistry = textUndoHistoryRegistry;
_editorOperationsFactoryService = editorOperationsFactoryService;
_globalOptions = globalOptions;
}

public string DisplayName => CSharpEditorResources.Complete_statement_on_semicolon;
Expand All @@ -76,7 +82,7 @@ public void ExecuteCommand(TypeCharCommandArgs args, Action nextCommandHandler,
transaction.Complete();
}

private static bool BeforeExecuteCommand(bool speculative, TypeCharCommandArgs args, CommandExecutionContext executionContext)
private bool BeforeExecuteCommand(bool speculative, TypeCharCommandArgs args, CommandExecutionContext executionContext)
{
if (args.TypedChar != ';' || !args.TextView.Selection.IsEmpty)
{
Expand All @@ -89,7 +95,7 @@ private static bool BeforeExecuteCommand(bool speculative, TypeCharCommandArgs a
return false;
}

if (!args.SubjectBuffer.GetFeatureOnOffOption(FeatureOnOffOptions.AutomaticallyCompleteStatementOnSemicolon))
if (!_globalOptions.GetOption(FeatureOnOffOptions.AutomaticallyCompleteStatementOnSemicolon))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.VisualStudio.Commanding;
using Microsoft.VisualStudio.Utilities;
Expand Down Expand Up @@ -41,6 +42,7 @@ internal partial class EventHookupCommandHandler : ForegroundThreadAffinitizedOb
{
private readonly IInlineRenameService _inlineRenameService;
private readonly IAsynchronousOperationListener _asyncListener;
private readonly IGlobalOptionService _globalOptions;

internal readonly EventHookupSessionManager EventHookupSessionManager;

Expand All @@ -52,12 +54,14 @@ internal partial class EventHookupCommandHandler : ForegroundThreadAffinitizedOb
public EventHookupCommandHandler(
IThreadingContext threadingContext,
IInlineRenameService inlineRenameService,
IAsynchronousOperationListenerProvider listenerProvider,
EventHookupSessionManager eventHookupSessionManager)
EventHookupSessionManager eventHookupSessionManager,
IGlobalOptionService globalOptions,
IAsynchronousOperationListenerProvider listenerProvider)
: base(threadingContext)
{
_inlineRenameService = inlineRenameService;
_asyncListener = listenerProvider.GetListener(FeatureAttribute.EventHookup);
_globalOptions = globalOptions;

this.EventHookupSessionManager = eventHookupSessionManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal partial class EventHookupCommandHandler : IChainedCommandHandler<TabKey
public void ExecuteCommand(TabKeyCommandArgs args, Action nextHandler, CommandExecutionContext context)
{
AssertIsForeground();
if (!args.SubjectBuffer.GetFeatureOnOffOption(InternalFeatureOnOffOptions.EventHookup))
if (!_globalOptions.GetOption(InternalFeatureOnOffOptions.EventHookup))
{
nextHandler();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void ExecuteCommand(TypeCharCommandArgs args, Action nextHandler, Command
AssertIsForeground();
nextHandler();

if (!args.SubjectBuffer.GetFeatureOnOffOption(InternalFeatureOnOffOptions.EventHookup))
if (!_globalOptions.GetOption(InternalFeatureOnOffOptions.EventHookup))
{
EventHookupSessionManager.CancelAndDismissExistingSessions();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,20 @@

namespace Microsoft.CodeAnalysis.Editor.CSharp.SplitStringLiteral
{
internal class SplitStringLiteralOptions
{
public static PerLanguageOption2<bool> Enabled =
new(nameof(SplitStringLiteralOptions), nameof(Enabled), defaultValue: true,
storageLocation: new RoamingProfileStorageLocation("TextEditor.%LANGUAGE%.Specific.SplitStringLiterals"));
}

[ExportOptionProvider(LanguageNames.CSharp), Shared]
internal class SplitStringLiteralOptionsProvider : IOptionProvider
[ExportGlobalOptionProvider(LanguageNames.CSharp), Shared]
internal sealed class SplitStringLiteralOptions : IOptionProvider
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public SplitStringLiteralOptionsProvider()
public SplitStringLiteralOptions()
{
}

public ImmutableArray<IOption> Options { get; } = ImmutableArray.Create<IOption>(
SplitStringLiteralOptions.Enabled);
ImmutableArray<IOption> IOptionProvider.Options { get; } = ImmutableArray.Create<IOption>(
Enabled);

public static PerLanguageOption2<bool> Enabled =
new(nameof(SplitStringLiteralOptions), nameof(Enabled), defaultValue: true,
storageLocation: new RoamingProfileStorageLocation("TextEditor.%LANGUAGE%.Specific.SplitStringLiterals"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis.Editor.Shared.Options;
using Microsoft.CodeAnalysis.Editor.UnitTests;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.VisualStudio.Commanding;
using Microsoft.VisualStudio.Text;
Expand Down Expand Up @@ -282,8 +283,8 @@ public void NotClosedAfterAsteriskSpaceWithOptionOff()
";
Verify(code, expected, workspace =>
{
workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(
workspace.CurrentSolution.Options.WithChangedOption(FeatureOnOffOptions.AutoInsertBlockCommentStartString, LanguageNames.CSharp, false)));
var globalOptions = workspace.GetService<IGlobalOptionService>();
globalOptions.SetGlobalOption(new OptionKey(FeatureOnOffOptions.AutoInsertBlockCommentStartString, LanguageNames.CSharp), false);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2608,13 +2608,13 @@ public async Task TestCreateWithBufferNotInWorkspace()
WpfTestRunner.RequireWpfFact($"Creates an {nameof(IWpfTextView)} explicitly with an unrelated buffer");
using var disposableView = workspace.ExportProvider.GetExportedValue<ITextEditorFactoryService>().CreateDisposableTextView(extraBuffer);
var listenerProvider = workspace.ExportProvider.GetExportedValue<IAsynchronousOperationListenerProvider>();
var globalOptionsService = workspace.ExportProvider.GetExportedValue<IGlobalOptionService>();
var globalOptions = workspace.ExportProvider.GetExportedValue<IGlobalOptionService>();

var provider = new SemanticClassificationViewTaggerProvider(
workspace.ExportProvider.GetExportedValue<IThreadingContext>(),
workspace.ExportProvider.GetExportedValue<ClassificationTypeMap>(),
listenerProvider,
globalOptionsService);
workspace.GetService<IThreadingContext>(),
workspace.GetService<ClassificationTypeMap>(),
globalOptions,
listenerProvider);

using var tagger = (IDisposable)provider.CreateTagger<IClassificationTag>(disposableView.TextView, extraBuffer);
using (var edit = extraBuffer.CreateEdit())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Text.Shared.Extensions;
using Microsoft.VisualStudio.Text;
Expand Down Expand Up @@ -42,8 +43,9 @@ public async Task TestTagsChangedForPortionThatChanged()

var tagComputer = new SyntacticClassificationTaggerProvider.TagComputer(
new SyntacticClassificationTaggerProvider(
workspace.ExportProvider.GetExportedValue<IThreadingContext>(),
workspace.GetService<IThreadingContext>(),
typeMap: null,
workspace.GetService<IGlobalOptionService>(),
AsynchronousOperationListenerProvider.NullProvider),
subjectBuffer,
AsynchronousOperationListenerProvider.NullListener,
Expand Down Expand Up @@ -99,8 +101,9 @@ public async Task TestTagsChangedAfterDelete()

var tagComputer = new SyntacticClassificationTaggerProvider.TagComputer(
new SyntacticClassificationTaggerProvider(
workspace.ExportProvider.GetExportedValue<IThreadingContext>(),
workspace.GetService<IThreadingContext>(),
typeMap,
workspace.GetService<IGlobalOptionService>(),
AsynchronousOperationListenerProvider.NullProvider),
subjectBuffer,
AsynchronousOperationListenerProvider.NullListener,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.Editor.Shared.Options;
using Microsoft.CodeAnalysis.Editor.UnitTests.CompleteStatement;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.VisualStudio.Commanding;
using Roslyn.Test.Utilities;
Expand Down Expand Up @@ -4180,7 +4181,8 @@ public int XValue
Verify(code, expected, ExecuteTest,
setOptionsOpt: workspace =>
{
workspace.SetOptions(workspace.Options.WithChangedOption(FeatureOnOffOptions.AutomaticallyCompleteStatementOnSemicolon, false));
var globalOptions = workspace.GetService<IGlobalOptionService>();
globalOptions.SetGlobalOption(new OptionKey(FeatureOnOffOptions.AutomaticallyCompleteStatementOnSemicolon), false);
});
}
protected override TestWorkspace CreateTestWorkspace(string code)
Expand Down
Loading