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

Allow code actions to retrieve options for any language #60697

Merged
merged 1 commit into from
Apr 13, 2022
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 @@ -468,7 +468,7 @@ void Method()
Assert.Equal(2, diagnostics.Where(d => d.Id == "CS0219").Count());

var options = CodeActionOptions.Default;
var allFixes = (await fixService.GetFixesAsync(document, span, options, CancellationToken.None))
var allFixes = (await fixService.GetFixesAsync(document, span, _ => options, CancellationToken.None))
.SelectMany(fixCollection => fixCollection.Fixes);

var cs0219Fixes = allFixes.Where(fix => fix.PrimaryDiagnostic.Id == "CS0219").ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ private protected static async Task AssertCodeCleanupResult(string expected, str
var enabledDiagnostics = codeCleanupService.GetAllDiagnostics();

var newDoc = await codeCleanupService.CleanupAsync(
document, enabledDiagnostics, new ProgressTracker(), options, formattingOptions, CancellationToken.None);
document, enabledDiagnostics, new ProgressTracker(), _ => options, formattingOptions, CancellationToken.None);

var actual = await newDoc.GetTextAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private async IAsyncEnumerable<SuggestedActionSet> GetCodeFixesAndRefactoringsAs
var workspace = document.Project.Solution.Workspace;
var supportsFeatureService = workspace.Services.GetRequiredService<ITextBufferSupportsFeatureService>();

var options = GlobalOptions.GetCodeActionOptions(document.Project.Language);
var options = GlobalOptions.GetCodeActionOptionsProvider();

var fixesTask = GetCodeFixesAsync(
state, supportsFeatureService, requestedActionCategories, workspace, document, range,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public bool TryGetTelemetryId(out Guid telemetryId)
Func<string, IDisposable?> addOperationScope =
description => operationContext?.AddScope(allowCancellation: true, string.Format(EditorFeaturesResources.Gathering_Suggestions_0, description));

var options = GlobalOptions.GetBlockingCodeActionOptions(document.Project.Language);
var options = GlobalOptions.GetBlockingCodeActionOptionsProvider();

// We convert the code fixes and refactorings to UnifiedSuggestedActionSets instead of
// SuggestedActionSets so that we can share logic between local Roslyn and LSP.
Expand Down Expand Up @@ -266,7 +266,7 @@ protected static Task<ImmutableArray<UnifiedSuggestedActionSet>> GetCodeFixesAsy
SnapshotSpan range,
Func<string, IDisposable?> addOperationScope,
CodeActionRequestPriority priority,
CodeActionOptions options,
CodeActionOptionsProvider options,
CancellationToken cancellationToken)
{
if (state.Target.Owner._codeFixService == null ||
Expand Down Expand Up @@ -306,7 +306,7 @@ protected static Task<ImmutableArray<UnifiedSuggestedActionSet>> GetRefactorings
TextSpan? selection,
Func<string, IDisposable?> addOperationScope,
CodeActionRequestPriority priority,
CodeActionOptions options,
CodeActionOptionsProvider options,
CancellationToken cancellationToken)
{
if (!selection.HasValue)
Expand Down Expand Up @@ -414,7 +414,7 @@ await InvokeBelowInputPriorityAsync(() =>
ReferenceCountedDisposable<State> state,
Document document,
SnapshotSpan range,
CodeActionOptions options,
CodeActionOptionsProvider options,
CancellationToken cancellationToken)
{
foreach (var order in Orderings)
Expand Down Expand Up @@ -458,7 +458,7 @@ await InvokeBelowInputPriorityAsync(() =>
private async Task<string?> TryGetRefactoringSuggestedActionCategoryAsync(
Document document,
TextSpan? selection,
CodeActionOptions options,
CodeActionOptionsProvider options,
CancellationToken cancellationToken)
{
using var state = _state.TryAddReference();
Expand Down Expand Up @@ -630,7 +630,7 @@ private void OnSuggestedActionsChanged(Workspace currentWorkspace, DocumentId? c
if (document == null)
return null;

var options = GlobalOptions.GetCodeActionOptions(document.Project.Language);
var options = GlobalOptions.GetCodeActionOptionsProvider();

using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
var linkedToken = linkedTokenSource.Token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,11 @@ internal static CodeActionOptionsProvider GetCodeActionOptionsProvider(this IGlo
var cache = ImmutableDictionary<string, CodeActionOptions>.Empty;
return language => ImmutableInterlocked.GetOrAdd(ref cache, language, (language, options) => GetCodeActionOptions(options, language), globalOptions);
}

internal static CodeActionOptionsProvider GetBlockingCodeActionOptionsProvider(this IGlobalOptionService globalOptions)
{
var cache = ImmutableDictionary<string, CodeActionOptions>.Empty;
return language => ImmutableInterlocked.GetOrAdd(ref cache, language, (language, options) => GetBlockingCodeActionOptions(options, language), globalOptions);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static async Task<VSInternalCodeAction[]> GetVSCodeActionsAsync(
CodeActionParams request,
CodeActionsCache codeActionsCache,
Document document,
CodeActionOptions options,
CodeActionOptionsProvider options,
ICodeFixService codeFixService,
ICodeRefactoringService codeRefactoringService,
CancellationToken cancellationToken)
Expand Down Expand Up @@ -186,7 +186,7 @@ public static async Task<ImmutableArray<CodeAction>> GetCodeActionsAsync(
CodeActionsCache codeActionsCache,
Document document,
LSP.Range selection,
CodeActionOptions options,
CodeActionOptionsProvider options,
ICodeFixService codeFixService,
ICodeRefactoringService codeRefactoringService,
CancellationToken cancellationToken)
Expand Down Expand Up @@ -242,7 +242,7 @@ private static CodeAction GetNestedActionsFromActionSet(IUnifiedSuggestedAction

private static async ValueTask<ImmutableArray<UnifiedSuggestedActionSet>> GetActionSetsAsync(
Document document,
CodeActionOptions options,
CodeActionOptionsProvider options,
ICodeFixService codeFixService,
ICodeRefactoringService codeRefactoringService,
LSP.Range selection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public CodeActionResolveHandler(
var data = ((JToken)codeAction.Data!).ToObject<CodeActionResolveData>();
Assumes.Present(data);

var options = _globalOptions.GetCodeActionOptions(document.Project.Language);
var options = _globalOptions.GetCodeActionOptionsProvider();

var codeActions = await CodeActionHelpers.GetCodeActionsAsync(
_codeActionsCache,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public CodeActionsHandler(
var document = context.Document;
Contract.ThrowIfNull(document);

var options = _globalOptions.GetCodeActionOptions(document.Project.Language);
var options = _globalOptions.GetCodeActionOptionsProvider();

var codeActions = await CodeActionHelpers.GetVSCodeActionsAsync(
request, _codeActionsCache, document, options, _codeFixService, _codeRefactoringService, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public override async Task<object> HandleRequestAsync(LSP.ExecuteCommandParams r
var runRequest = ((JToken)request.Arguments.Single()).ToObject<CodeActionResolveData>();
Assumes.Present(runRequest);

var options = _globalOptions.GetCodeActionOptions(document.Project.Language);
var options = _globalOptions.GetCodeActionOptionsProvider();

var codeActions = await CodeActionHelpers.GetCodeActionsAsync(
_codeActionsCache, document, runRequest.Range, options, _codeFixService, _codeRefactoringService, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal async Task<CodeRefactoring> GetCodeRefactoringAsync(
var span = documentsWithSelections.Single().SelectedSpans.Single();
var actions = ArrayBuilder<(CodeAction, TextSpan?)>.GetInstance();
var document = workspace.CurrentSolution.GetDocument(documentsWithSelections.Single().Id);
var context = new CodeRefactoringContext(document, span, (a, t) => actions.Add((a, t)), parameters.codeActionOptions, CancellationToken.None);
var context = new CodeRefactoringContext(document, span, (a, t) => actions.Add((a, t)), _ => parameters.codeActionOptions, CancellationToken.None);
await provider.ComputeRefactoringsAsync(context);

var result = actions.Count > 0 ? new CodeRefactoring(provider, actions.ToImmutable()) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected override AnalyzerOptions GetAnalyzerOptions(Project project)
=> new WorkspaceAnalyzerOptions(base.GetAnalyzerOptions(project), project.Solution, _sharedState.IdeAnalyzerOptions);

protected override CodeFixContext CreateCodeFixContext(Document document, TextSpan span, ImmutableArray<Diagnostic> diagnostics, Action<CodeAction, ImmutableArray<Diagnostic>> registerCodeFix, CancellationToken cancellationToken)
=> new(document, span, diagnostics, registerCodeFix, _sharedState.CodeActionOptions, cancellationToken);
=> new(document, span, diagnostics, registerCodeFix, _ => _sharedState.CodeActionOptions, cancellationToken);

protected override FixAllContext CreateFixAllContext(
Document? document,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ protected static Document GetDocumentAndAnnotatedSpan(TestWorkspace workspace, o
diagnostic.Location.SourceSpan,
ImmutableArray.Create(diagnostic),
(a, d) => fixes.Add(new CodeFix(document.Project, a, d)),
options,
_ => options,
CancellationToken.None);

await fixer.RegisterCodeFixesAsync(context);
Expand Down
18 changes: 9 additions & 9 deletions src/EditorFeatures/Test/CodeFixes/CodeFixServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public async Task TestGetFirstDiagnosticWithFixAsync()
var project = workspace.CurrentSolution.Projects.Single().AddAnalyzerReference(reference);
var document = project.Documents.Single();
var unused = await fixService.GetMostSevereFixAsync(
document, TextSpan.FromBounds(0, 0), CodeActionRequestPriority.None, CodeActionOptions.Default, CancellationToken.None);
document, TextSpan.FromBounds(0, 0), CodeActionRequestPriority.None, _ => CodeActionOptions.Default, CancellationToken.None);

var fixer1 = (MockFixer)fixers.Single().Value;
var fixer2 = (MockFixer)reference.Fixer!;
Expand Down Expand Up @@ -91,7 +91,7 @@ public async Task TestGetFixesAsyncWithDuplicateDiagnostics()
// Verify that we do not crash when computing fixes.
var options = CodeActionOptions.Default;

_ = await tuple.codeFixService.GetFixesAsync(document, TextSpan.FromBounds(0, 0), options, CancellationToken.None);
_ = await tuple.codeFixService.GetFixesAsync(document, TextSpan.FromBounds(0, 0), _ => options, CancellationToken.None);

// Verify that code fix is invoked with both the diagnostics in the context,
// i.e. duplicate diagnostics are not silently discarded by the CodeFixService.
Expand All @@ -118,7 +118,7 @@ public async Task TestGetFixesAsyncHasNoDuplicateConfigurationActions()

// Verify registered configuration code actions do not have duplicates.
var options = CodeActionOptions.Default;
var fixCollections = await tuple.codeFixService.GetFixesAsync(document, TextSpan.FromBounds(0, 0), options, CancellationToken.None);
var fixCollections = await tuple.codeFixService.GetFixesAsync(document, TextSpan.FromBounds(0, 0), _ => options, CancellationToken.None);
var codeActions = fixCollections.SelectMany(c => c.Fixes.Select(f => f.Action)).ToImmutableArray();
Assert.Equal(7, codeActions.Length);
var uniqueTitles = new HashSet<string>();
Expand Down Expand Up @@ -152,14 +152,14 @@ public async Task TestGetFixesAsyncForFixableAndNonFixableAnalyzersAsync()

// Verify only analyzerWithFix is executed when GetFixesAsync is invoked with 'CodeActionRequestPriority.Normal'.
_ = await tuple.codeFixService.GetFixesAsync(document, TextSpan.FromBounds(0, 0),
priority: CodeActionRequestPriority.Normal, options,
priority: CodeActionRequestPriority.Normal, _ => options,
addOperationScope: _ => null, cancellationToken: CancellationToken.None);
Assert.True(analyzerWithFix.ReceivedCallback);
Assert.False(analyzerWithoutFix.ReceivedCallback);

// Verify both analyzerWithFix and analyzerWithoutFix are executed when GetFixesAsync is invoked with 'CodeActionRequestPriority.Lowest'.
_ = await tuple.codeFixService.GetFixesAsync(document, TextSpan.FromBounds(0, 0),
priority: CodeActionRequestPriority.Lowest, options,
priority: CodeActionRequestPriority.Lowest, _ => options,
addOperationScope: _ => null, cancellationToken: CancellationToken.None);
Assert.True(analyzerWithFix.ReceivedCallback);
Assert.True(analyzerWithoutFix.ReceivedCallback);
Expand Down Expand Up @@ -190,7 +190,7 @@ public async Task TestGetFixesAsyncForDocumentDiagnosticAnalyzerAsync()
var options = CodeActionOptions.Default;

_ = await tuple.codeFixService.GetFixesAsync(document, TextSpan.FromBounds(0, 0),
priority: CodeActionRequestPriority.Normal, options,
priority: CodeActionRequestPriority.Normal, _ => options,
addOperationScope: _ => null, cancellationToken: CancellationToken.None);
Assert.True(documentDiagnosticAnalyzer.ReceivedCallback);
}
Expand Down Expand Up @@ -275,7 +275,7 @@ private static async Task<ImmutableArray<CodeFixCollection>> GetAddedFixesAsync(
var project = workspace.CurrentSolution.Projects.Single().AddAnalyzerReference(reference);
document = project.Documents.Single();
var options = CodeActionOptions.Default;
var fixes = await tuple.codeFixService.GetFixesAsync(document, TextSpan.FromBounds(0, 0), options, CancellationToken.None);
var fixes = await tuple.codeFixService.GetFixesAsync(document, TextSpan.FromBounds(0, 0), _ => options, CancellationToken.None);

if (exception)
{
Expand All @@ -300,7 +300,7 @@ private static async Task GetFirstDiagnosticWithFixWithExceptionValidationAsync(

GetDocumentAndExtensionManager(tuple.analyzerService, workspace, out var document, out var extensionManager);
var unused = await tuple.codeFixService.GetMostSevereFixAsync(
document, TextSpan.FromBounds(0, 0), CodeActionRequestPriority.None, CodeActionOptions.Default, CancellationToken.None);
document, TextSpan.FromBounds(0, 0), CodeActionRequestPriority.None, _ => CodeActionOptions.Default, CancellationToken.None);
Assert.True(extensionManager.IsDisabled(codefix));
Assert.False(extensionManager.IsIgnored(codefix));
Assert.True(errorReported);
Expand Down Expand Up @@ -692,7 +692,7 @@ private static async Task<ImmutableArray<CodeFixCollection>> GetNuGetAndVsixCode
var document = project.Documents.Single();
var options = CodeActionOptions.Default;

return await fixService.GetFixesAsync(document, TextSpan.FromBounds(0, 0), options, CancellationToken.None);
return await fixService.GetFixesAsync(document, TextSpan.FromBounds(0, 0), _ => options, CancellationToken.None);
}

private sealed class NuGetCodeFixProvider : AbstractNuGetOrVsixCodeFixProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task TestProjectRefactoringAsync()
var project = workspace.CurrentSolution.Projects.Single().AddAnalyzerReference(reference);
var document = project.Documents.Single();
var options = CodeActionOptions.Default;
var refactorings = await refactoringService.GetRefactoringsAsync(document, TextSpan.FromBounds(0, 0), options, CancellationToken.None);
var refactorings = await refactoringService.GetRefactoringsAsync(document, TextSpan.FromBounds(0, 0), _ => options, CancellationToken.None);

var stubRefactoringAction = refactorings.Single(refactoring => refactoring.CodeActions.FirstOrDefault().action?.Title == nameof(StubRefactoring));
Assert.True(stubRefactoringAction is object);
Expand All @@ -69,7 +69,7 @@ private static async Task VerifyRefactoringDisabledAsync<T>()
var document = project.Documents.Single();
var extensionManager = (EditorLayerExtensionManager.ExtensionManager)document.Project.Solution.Workspace.Services.GetRequiredService<IExtensionManager>();
var options = CodeActionOptions.Default;
var result = await refactoringService.GetRefactoringsAsync(document, TextSpan.FromBounds(0, 0), options, CancellationToken.None);
var result = await refactoringService.GetRefactoringsAsync(document, TextSpan.FromBounds(0, 0), _ => options, CancellationToken.None);
Assert.True(extensionManager.IsDisabled(codeRefactoring));
Assert.False(extensionManager.IsIgnored(codeRefactoring));

Expand Down
10 changes: 5 additions & 5 deletions src/EditorFeatures/Test2/CodeFixes/CodeFixServiceTests.vb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Namespace Microsoft.CodeAnalysis.Editor.Implementation.CodeFixes.UnitTests
Dim fixes = Await codefixService.GetFixesAsync(
document,
(Await document.GetSyntaxRootAsync()).FullSpan,
options,
Function(language) options,
CancellationToken.None)

Assert.Equal(0, fixes.Count())
Expand All @@ -93,7 +93,7 @@ Namespace Microsoft.CodeAnalysis.Editor.Implementation.CodeFixes.UnitTests
fixes = Await codefixService.GetFixesAsync(
document,
(Await document.GetSyntaxRootAsync()).FullSpan,
options,
Function(language) options,
CancellationToken.None)
Assert.Equal(1, fixes.Count())

Expand All @@ -103,7 +103,7 @@ Namespace Microsoft.CodeAnalysis.Editor.Implementation.CodeFixes.UnitTests
fixes = Await codefixService.GetFixesAsync(
document,
(Await document.GetSyntaxRootAsync()).FullSpan,
options,
Function(language) options,
CancellationToken.None)

Assert.Equal(0, fixes.Count())
Expand Down Expand Up @@ -154,7 +154,7 @@ Namespace Microsoft.CodeAnalysis.Editor.Implementation.CodeFixes.UnitTests
Dim fixes = Await codefixService.GetFixesAsync(
document,
(Await document.GetSyntaxRootAsync()).FullSpan,
options,
Function(language) options,
CancellationToken.None)

Assert.Equal(0, fixes.Count())
Expand All @@ -170,7 +170,7 @@ Namespace Microsoft.CodeAnalysis.Editor.Implementation.CodeFixes.UnitTests
fixes = Await codefixService.GetFixesAsync(
document,
(Await document.GetSyntaxRootAsync()).FullSpan,
options,
Function(language) options,
CancellationToken.None)

Assert.Equal(0, fixes.Count())
Expand Down
Loading