Skip to content

Commit

Permalink
Remove fallback options for SimplifierOptions (#74222)
Browse files Browse the repository at this point in the history
* SimplifierOptions

* Fix tests
  • Loading branch information
tmat authored Jul 2, 2024
1 parent 70be0c7 commit 8814b23
Show file tree
Hide file tree
Showing 34 changed files with 112 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected override async Task FixAllAsync(
#if CODE_STYLE
var options = CSharpSimplifierOptions.Default;
#else
var options = (CSharpSimplifierOptions)await document.GetSimplifierOptionsAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);
var options = (CSharpSimplifierOptions)await document.GetSimplifierOptionsAsync(cancellationToken).ConfigureAwait(false);
#endif

// Bulk apply these, except at the expression level. One fix at the expression level may prevent another fix
Expand Down
2 changes: 1 addition & 1 deletion src/Analyzers/Core/Analyzers/AnalyzerOptionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public AnalyzerOptionsProvider(IOptionsReader options, string language, Analyzer
public CodeStyleOption2<bool> PreferPredefinedTypeKeywordInDeclaration => GetOption(CodeStyleOptions2.PreferIntrinsicPredefinedTypeKeywordInDeclaration, FallbackSimplifierOptions.PreferPredefinedTypeKeywordInDeclaration);

public SimplifierOptions GetSimplifierOptions(ISimplification simplification)
=> simplification.GetSimplifierOptions(_options, _fallbackOptions.CleanupOptions?.SimplifierOptions);
=> simplification.GetSimplifierOptions(_options);

// SyntaxFormattingOptions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public EventHookupTestState(XElement workspaceElement, OptionsCollection options

_testSessionHookupMutex = new Mutex(false);
_commandHandler.TESTSessionHookupMutex = _testSessionHookupMutex;
options?.SetGlobalOptions(Workspace.GlobalOptions);

if (options != null)
{
Workspace.SetAnalyzerFallbackOptions(options);
}
}

public static EventHookupTestState CreateTestState(string markup, OptionsCollection options = null)
Expand Down
5 changes: 4 additions & 1 deletion src/EditorFeatures/CSharpTest/Intents/IntentTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ internal static async Task<ImmutableArray<IntentSource>> GetIntentsAsync(
OptionsCollection? options = null,
string? intentData = null)
{
options?.SetGlobalOptions(workspace.GlobalOptions);
if (options != null)
{
workspace.SetAnalyzerFallbackOptions(options);
}

var intentSource = workspace.ExportProvider.GetExportedValue<IIntentSourceProvider>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.NavigationBar
Return document
End If

Dim simplifierOptions = Await newDocument.GetSimplifierOptionsAsync(globalOptions, cancellationToken).ConfigureAwait(False)
Dim simplifierOptions = Await newDocument.GetSimplifierOptionsAsync(cancellationToken).ConfigureAwait(False)
Dim formattingOptions = Await newDocument.GetSyntaxFormattingOptionsAsync(globalOptions, cancellationToken).ConfigureAwait(False)

newDocument = Await Simplifier.ReduceAsync(newDocument, Simplifier.Annotation, simplifierOptions, cancellationToken).ConfigureAwait(False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
return;
}

var simplifierOptions = (CSharpSimplifierOptions)await document.GetSimplifierOptionsAsync(context.Options, cancellationToken).ConfigureAwait(false);
var simplifierOptions = (CSharpSimplifierOptions)await document.GetSimplifierOptionsAsync(cancellationToken).ConfigureAwait(false);
var typeStyle = AnalyzeTypeName(declaredType, semanticModel, simplifierOptions, cancellationToken);
if (typeStyle.IsStylePreferred && typeStyle.Notification.Severity != ReportDiagnostic.Suppress)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected override string ToDisplayString(IParameterSymbol parameter, SymbolDisp

protected override async ValueTask<bool> PrefersThrowExpressionAsync(Document document, SimplifierOptionsProvider fallbackOptions, CancellationToken cancellationToken)
{
var options = (CSharpSimplifierOptions)await document.GetSimplifierOptionsAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);
var options = (CSharpSimplifierOptions)await document.GetSimplifierOptionsAsync(cancellationToken).ConfigureAwait(false);
return options.PreferThrowExpression.Value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected override async Task<ExpressionStatementSyntax> CreateTupleDeconstructi
CancellationToken cancellationToken)
{
var semanticFacts = document.GetRequiredLanguageService<ISemanticFactsService>();
var simplifierOptions = (CSharpSimplifierOptions)await document.GetSimplifierOptionsAsync(optionsProvider, cancellationToken).ConfigureAwait(false);
var simplifierOptions = (CSharpSimplifierOptions)await document.GetSimplifierOptionsAsync(cancellationToken).ConfigureAwait(false);
var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);

var tupleUnderlyingType = tupleType.TupleUnderlyingType ?? tupleType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private async Task<Solution> EncapsulateFieldAsync(
new SyntaxAnnotation(),
document);

var simplifierOptions = await document.GetSimplifierOptionsAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);
var simplifierOptions = await document.GetSimplifierOptionsAsync(cancellationToken).ConfigureAwait(false);

var documentWithProperty = await AddPropertyAsync(
document, document.Project.Solution, field, generatedProperty, fallbackOptions, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected override async Task<ImmutableArray<CodeAction>> GetRefactoringsForAllP
// Great. The list has parameters that need null checks. Offer to add null checks for all.
return [CodeAction.Create(
FeaturesResources.Add_null_checks_for_all_parameters,
c => UpdateDocumentForRefactoringAsync(document, blockStatementOpt, listOfParametersOrdinals, parameterSpan, fallbackOptions, c),
c => UpdateDocumentForRefactoringAsync(document, blockStatementOpt, listOfParametersOrdinals, parameterSpan, c),
nameof(FeaturesResources.Add_null_checks_for_all_parameters))];
}

Expand All @@ -96,7 +96,7 @@ protected override async Task<ImmutableArray<CodeAction>> GetRefactoringsForSing
if (!ParameterValidForNullCheck(document, parameter, semanticModel, blockStatementOpt, cancellationToken))
return [];

var simplifierOptions = (TSimplifierOptions)await document.GetSimplifierOptionsAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);
var simplifierOptions = (TSimplifierOptions)await document.GetSimplifierOptionsAsync(cancellationToken).ConfigureAwait(false);

// Great. There was no null check. Offer to add one.
using var result = TemporaryArray<CodeAction>.Empty;
Expand Down Expand Up @@ -128,7 +128,6 @@ private async Task<Document> UpdateDocumentForRefactoringAsync(
IBlockOperation? blockStatementOpt,
List<int> listOfParametersOrdinals,
TextSpan parameterSpan,
CleanCodeGenerationOptionsProvider fallbackOptions,
CancellationToken cancellationToken)
{
TSimplifierOptions? lazySimplifierOptions = null;
Expand Down Expand Up @@ -156,7 +155,7 @@ private async Task<Document> UpdateDocumentForRefactoringAsync(
if (!CanOfferRefactoring(functionDeclaration, semanticModel, syntaxFacts, cancellationToken, out blockStatementOpt))
continue;

lazySimplifierOptions ??= (TSimplifierOptions)await document.GetSimplifierOptionsAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);
lazySimplifierOptions ??= (TSimplifierOptions)await document.GetSimplifierOptionsAsync(cancellationToken).ConfigureAwait(false);

// If parameter is a string, default check would be IsNullOrEmpty. This is because IsNullOrEmpty is more
// commonly used in this regard according to telemetry and UX testing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal AbstractIntroduceVariableCodeAction(
protected override async Task<Document> GetChangedDocumentAsync(CancellationToken cancellationToken)
{
var changedDocument = await GetChangedDocumentCoreAsync(cancellationToken).ConfigureAwait(false);
var simplifierOptions = await changedDocument.GetSimplifierOptionsAsync(Options.SimplifierOptions, cancellationToken).ConfigureAwait(false);
var simplifierOptions = await changedDocument.GetSimplifierOptionsAsync(cancellationToken).ConfigureAwait(false);
return await Simplifier.ReduceAsync(changedDocument, simplifierOptions, cancellationToken).ConfigureAwait(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)

var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var options = (TSimplifierOptions)await document.GetSimplifierOptionsAsync(context.Options, cancellationToken).ConfigureAwait(false);
var options = (TSimplifierOptions)await document.GetSimplifierOptionsAsync(cancellationToken).ConfigureAwait(false);

var (node, diagnosticId) = GetNodeToSimplify(
root, model, span, options, cancellationToken);
Expand All @@ -104,7 +104,7 @@ protected override async Task FixAllAsync(
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var simplifierOptions = (TSimplifierOptions)await document.GetSimplifierOptionsAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);
var simplifierOptions = (TSimplifierOptions)await document.GetSimplifierOptionsAsync(cancellationToken).ConfigureAwait(false);

foreach (var diagnostic in diagnostics)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private async Task<Document> CleanupDocumentAsync(
if (document.SupportsSyntaxTree)
{
var addImportPlacementOptions = await document.GetAddImportPlacementOptionsAsync(fallbackOptions: null, cancellationToken).ConfigureAwait(false);
var simplifierOptions = await document.GetSimplifierOptionsAsync(fallbackOptions: null, cancellationToken).ConfigureAwait(false);
var simplifierOptions = await document.GetSimplifierOptionsAsync(cancellationToken).ConfigureAwait(false);
var syntaxFormattingOptions = await document.GetSyntaxFormattingOptionsAsync(fallbackOptions: null, cancellationToken).ConfigureAwait(false);

document = await ImportAdder.AddImportsFromSymbolAnnotationAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public SimplifyMethodHandler()
// Call to the Simplifier and pass back the edits.
var configOptions = await originalDocument.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false);
var simplificationService = originalDocument.Project.Services.GetRequiredService<ISimplificationService>();
var options = simplificationService.GetSimplifierOptions(configOptions, simplificationService.DefaultOptions);
var options = simplificationService.GetSimplifierOptions(configOptions);
var newDocument = await Simplifier.ReduceAsync(annotatedDocument, options, cancellationToken).ConfigureAwait(false);
var changes = await newDocument.GetTextChangesAsync(originalDocument, cancellationToken).ConfigureAwait(false);
return changes.Select(change => ProtocolConversions.TextChangeToTextEdit(change, originalSourceText)).ToArray();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(VSInternalInlineCompleti

// Use the formatting options specified by the client to format the snippet.
var formattingOptions = await ProtocolConversions.GetFormattingOptionsAsync(request.Options, document, _globalOptions, cancellationToken).ConfigureAwait(false);
var simplifierOptions = await document.GetSimplifierOptionsAsync(_globalOptions, cancellationToken).ConfigureAwait(false);
var simplifierOptions = await document.GetSimplifierOptionsAsync(cancellationToken).ConfigureAwait(false);

var formattedLspSnippet = await GetFormattedLspSnippetAsync(parsedSnippet, wordOnLeft.Value, document, sourceText, formattingOptions, simplifierOptions, cancellationToken).ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public async Task TestGetSimplifierOptionsOnTypeScriptDocument()

await using var testLspServer = await CreateTsTestLspServerAsync(workspaceXml);
var document = testLspServer.GetCurrentSolution().Projects.Single().Documents.Single();
var simplifierOptions = testLspServer.TestWorkspace.GlobalOptions.GetSimplifierOptions(document.Project.Services, fallbackOptions: null);
var simplifierOptions = testLspServer.TestWorkspace.GlobalOptions.GetSimplifierOptions(document.Project.Services);
Assert.Same(SimplifierOptions.CommonDefaults, simplifierOptions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected override int FieldChanged(string field, out int requeryFunction)
return (VSConstants.S_OK, snippetFunctionService.SwitchDefaultCaseForm, hasCurrentValue);
}

var simplifierOptions = await document.GetSimplifierOptionsAsync(snippetExpansionClient.EditorOptionsService.GlobalOptions, cancellationToken).ConfigureAwait(false);
var simplifierOptions = await document.GetSimplifierOptionsAsync(cancellationToken).ConfigureAwait(false);

var value = await snippetFunctionService.GetSwitchExpansionAsync(document, caseGenerationSpan.Value, switchExpressionSpan.Value, simplifierOptions, cancellationToken).ConfigureAwait(false);
if (value == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public SnippetFunctionSimpleTypeName(
if (!TryGetFieldSpan(out var fieldSpan))
return (VSConstants.E_FAIL, value, hasDefaultValue);

var simplifierOptions = await document.GetSimplifierOptionsAsync(snippetExpansionClient.EditorOptionsService.GlobalOptions, cancellationToken).ConfigureAwait(false);
var simplifierOptions = await document.GetSimplifierOptionsAsync(cancellationToken).ConfigureAwait(false);

var simplifiedTypeName = await SnippetFunctionService.GetSimplifiedTypeNameAsync(document, fieldSpan.Value, _fullyQualifiedName, simplifierOptions, cancellationToken).ConfigureAwait(false);
if (string.IsNullOrEmpty(simplifiedTypeName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ private SyntaxNode InsertNode(
{
document = _threadingContext.JoinableTaskFactory.Run(async () =>
{
var simplifierOptions = await document.GetSimplifierOptionsAsync(_editorOptionsService.GlobalOptions, cancellationToken).ConfigureAwait(false);
var simplifierOptions = await document.GetSimplifierOptionsAsync(cancellationToken).ConfigureAwait(false);
return await Simplifier.ReduceAsync(document, annotation, simplifierOptions, cancellationToken).ConfigureAwait(false);
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/VisualStudio/Core/Impl/CodeModel/FileCodeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ int IVBFileCodeModelEvents.EndEdit()
// perform expensive operations at once
var newDocument = State.ThreadingContext.JoinableTaskFactory.Run(async () =>
{
var simplifierOptions = await _batchDocument.GetSimplifierOptionsAsync(GlobalOptions, CancellationToken.None).ConfigureAwait(false);
var simplifierOptions = await _batchDocument.GetSimplifierOptionsAsync(CancellationToken.None).ConfigureAwait(false);
return await Simplifier.ReduceAsync(_batchDocument, Simplifier.Annotation, simplifierOptions, CancellationToken.None).ConfigureAwait(false);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ internal partial class CSharpSimplificationService()
public override SimplifierOptions DefaultOptions
=> CSharpSimplifierOptions.Default;

public override SimplifierOptions GetSimplifierOptions(IOptionsReader options, SimplifierOptions? fallbackOptions)
=> new CSharpSimplifierOptions(options, (CSharpSimplifierOptions?)fallbackOptions);
public override SimplifierOptions GetSimplifierOptions(IOptionsReader options)
=> new CSharpSimplifierOptions(options, fallbackOptions: null);

public override SyntaxNode Expand(SyntaxNode node, SemanticModel semanticModel, SyntaxAnnotation? annotationForReplacedAliasIdentifier, Func<SyntaxNode, bool>? expandInsideNode, bool expandParameter, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ ValueTask<SyntaxFormattingOptions> OptionsProvider<SyntaxFormattingOptions>.GetO
=> ValueTaskFactory.FromResult(options.GetSyntaxFormattingOptions(languageServices, fallbackOptions: null));

ValueTask<SimplifierOptions> OptionsProvider<SimplifierOptions>.GetOptionsAsync(LanguageServices languageServices, CancellationToken cancellationToken)
=> ValueTaskFactory.FromResult(options.GetSimplifierOptions(languageServices, fallbackOptions: null));
=> ValueTaskFactory.FromResult(options.GetSimplifierOptions(languageServices));

ValueTask<AddImportPlacementOptions> OptionsProvider<AddImportPlacementOptions>.GetOptionsAsync(LanguageServices languageServices, CancellationToken cancellationToken)
=> ValueTaskFactory.FromResult(options.GetAddImportPlacementOptions(languageServices, allowInHiddenRegions: null, fallbackOptions: null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected AbstractSimplificationService(ImmutableArray<AbstractReducer> reducers
protected abstract void AddImportDeclarations(TCompilationUnitSyntax root, ArrayBuilder<SyntaxNode> importDeclarations);

public abstract SimplifierOptions DefaultOptions { get; }
public abstract SimplifierOptions GetSimplifierOptions(IOptionsReader options, SimplifierOptions? fallbackOptions);
public abstract SimplifierOptions GetSimplifierOptions(IOptionsReader options);

protected virtual SyntaxNode TransformReducedNode(SyntaxNode reducedNode, SyntaxNode originalNode)
=> reducedNode;
Expand Down
Loading

0 comments on commit 8814b23

Please sign in to comment.