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

Propagate CodeActionOptions through fix all #59575

Merged
merged 7 commits into from
Feb 19, 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
4 changes: 3 additions & 1 deletion eng/config/BannedSymbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ M:Microsoft.CodeAnalysis.Completion.CompletionProvider.GetDescriptionAsync(Micro
M:Microsoft.CodeAnalysis.Completion.CompletionService.GetCompletionsAsync(Microsoft.CodeAnalysis.Document,System.Int32,Microsoft.CodeAnalysis.Completion.CompletionTrigger,System.Collections.Immutable.ImmutableHashSet{System.String},Microsoft.CodeAnalysis.Options.OptionSet,System.Threading.CancellationToken); Use internal overload instead
M:Microsoft.CodeAnalysis.Completion.CompletionService.GetDescriptionAsync(Microsoft.CodeAnalysis.Document,Microsoft.CodeAnalysis.Completion.CompletionItem,System.Threading.CancellationToken); Use internal overload instead
M:Microsoft.CodeAnalysis.Completion.CompletionService.GetRules; Use internal overload instead
M:Microsoft.CodeAnalysis.QuickInfo.QuickInfoService.GetQuickInfoAsync(Microsoft.CodeAnalysis.Document,System.Int32,System.Threading.CancellationToken); Use internal overload instead
M:Microsoft.CodeAnalysis.QuickInfo.QuickInfoService.GetQuickInfoAsync(Microsoft.CodeAnalysis.Document,System.Int32,System.Threading.CancellationToken); Use internal overload instead
M:Microsoft.CodeAnalysis.CodeFixes.FixAllContext.#ctor(Microsoft.CodeAnalysis.Document,Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider,Microsoft.CodeAnalysis.CodeFixes.FixAllScope,System.String,System.Collections.Generic.IEnumerable{System.String},Microsoft.CodeAnalysis.CodeFixes.FixAllContext.DiagnosticProvider,System.Threading.CancellationToken); Use internal overload instead
M:Microsoft.CodeAnalysis.CodeFixes.FixAllContext.#ctor(Microsoft.CodeAnalysis.Project,Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider,Microsoft.CodeAnalysis.CodeFixes.FixAllScope,System.String,System.Collections.Generic.IEnumerable{System.String},Microsoft.CodeAnalysis.CodeFixes.FixAllContext.DiagnosticProvider,System.Threading.CancellationToken); Use internal overload instead
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,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, isBlocking: false);
var options = GlobalOptions.GetCodeActionOptions(document.Project.Language);

var fixesTask = GetCodeFixesAsync(
state, supportsFeatureService, requestedActionCategories, workspace, document, range,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
#nullable disable

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Editor.Host;
using Microsoft.CodeAnalysis.Extensions;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Roslyn.Utilities;

Expand All @@ -22,25 +24,28 @@ namespace Microsoft.CodeAnalysis.Editor.Implementation.Suggestions
/// Service to compute and apply <see cref="FixMultipleCodeAction"/> code fixes.
/// </summary>
[ExportWorkspaceService(typeof(IFixMultipleOccurrencesService), ServiceLayer.Host), Shared]
internal class FixMultipleOccurrencesService : IFixMultipleOccurrencesService
internal sealed class FixMultipleOccurrencesService : IFixMultipleOccurrencesService
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public FixMultipleOccurrencesService(IAsynchronousOperationListenerProvider listenerProvider)
=> listenerProvider.GetListener(FeatureAttribute.LightBulb);
{
listenerProvider.GetListener(FeatureAttribute.LightBulb);
tmat marked this conversation as resolved.
Show resolved Hide resolved
}

public Solution GetFix(
ImmutableDictionary<Document, ImmutableArray<Diagnostic>> diagnosticsToFix,
Workspace workspace,
CodeFixProvider fixProvider,
FixAllProvider fixAllProvider,
CodeActionOptionsProvider optionsProvider,
string equivalenceKey,
string waitDialogTitle,
string waitDialogMessage,
CancellationToken cancellationToken)
{
var fixMultipleState = FixAllState.Create(
fixAllProvider, diagnosticsToFix, fixProvider, equivalenceKey);
fixAllProvider, diagnosticsToFix, fixProvider, equivalenceKey, optionsProvider);

return GetFixedSolution(
fixMultipleState, workspace, waitDialogTitle,
Expand All @@ -52,13 +57,14 @@ public Solution GetFix(
Workspace workspace,
CodeFixProvider fixProvider,
FixAllProvider fixAllProvider,
CodeActionOptionsProvider optionsProvider,
string equivalenceKey,
string waitDialogTitle,
string waitDialogMessage,
CancellationToken cancellationToken)
{
var fixMultipleState = FixAllState.Create(
fixAllProvider, diagnosticsToFix, fixProvider, equivalenceKey);
fixAllProvider, diagnosticsToFix, fixProvider, equivalenceKey, optionsProvider);

return GetFixedSolution(
fixMultipleState, workspace, waitDialogTitle,
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.GetCodeActionOptions(document.Project.Language, isBlocking: true);
var options = GlobalOptions.GetBlockingCodeActionOptions(document.Project.Language);

// 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 @@ -414,13 +414,14 @@ await InvokeBelowInputPriorityAsync(() =>
ReferenceCountedDisposable<State> state,
Document document,
SnapshotSpan range,
CodeActionOptions options,
CancellationToken cancellationToken)
{
if (state.Target.Owner._codeFixService != null &&
state.Target.SubjectBuffer.SupportsCodeFixes())
{
var result = await state.Target.Owner._codeFixService.GetMostSevereFixableDiagnosticAsync(
document, range.Span.ToTextSpan(), cancellationToken).ConfigureAwait(false);
document, range.Span.ToTextSpan(), options, cancellationToken).ConfigureAwait(false);

if (result.HasFix)
{
Expand Down Expand Up @@ -614,12 +615,12 @@ private void OnSuggestedActionsChanged(Workspace currentWorkspace, DocumentId? c
if (document == null)
return null;

var options = GlobalOptions.GetCodeActionOptions(document.Project.Language, isBlocking: false);
var options = GlobalOptions.GetCodeActionOptions(document.Project.Language);

using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
var linkedToken = linkedTokenSource.Token;

var errorTask = Task.Run(() => GetFixLevelAsync(state, document, range, linkedToken), linkedToken);
var errorTask = Task.Run(() => GetFixLevelAsync(state, document, range, options, linkedToken), linkedToken);

var selection = await GetSpanAsync(state, range, linkedToken).ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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.Collections.Immutable;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.SymbolSearch;
Expand All @@ -10,10 +11,22 @@ namespace Microsoft.CodeAnalysis.CodeActions
{
internal static class CodeActionOptionsStorage
{
internal static CodeActionOptions GetCodeActionOptions(this IGlobalOptionService globalOptions, string language, bool isBlocking)
internal static CodeActionOptions GetCodeActionOptions(this IGlobalOptionService globalOptions, string language)
=> GetCodeActionOptions(globalOptions, language, isBlocking: false);

internal static CodeActionOptions GetBlockingCodeActionOptions(this IGlobalOptionService globalOptions, string language)
=> GetCodeActionOptions(globalOptions, language, isBlocking: true);

private static CodeActionOptions GetCodeActionOptions(this IGlobalOptionService globalOptions, string language, bool isBlocking)
=> new(
SearchOptions: globalOptions.GetSymbolSearchOptions(language),
HideAdvancedMembers: globalOptions.GetOption(CompletionOptionsStorage.HideAdvancedMembers, language),
IsBlocking: isBlocking);

internal static CodeActionOptionsProvider GetCodeActionOptionsProvider(this IGlobalOptionService globalOptions)
{
var cache = ImmutableDictionary<string, CodeActionOptions>.Empty;
return language => ImmutableInterlocked.GetOrAdd(ref cache, language, (language, options) => GetCodeActionOptions(options, language), globalOptions);
}
}
}
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, isBlocking: false);
var options = _globalOptions.GetCodeActionOptions(document.Project.Language);

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, isBlocking: false);
var options = _globalOptions.GetCodeActionOptions(document.Project.Language);

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, isBlocking: false);
var options = _globalOptions.GetCodeActionOptions(document.Project.Language);

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 @@ -219,7 +219,7 @@ protected static Document GetDocumentAndAnnotatedSpan(TestWorkspace workspace, o

var fixAllState = GetFixAllState(
fixAllProvider, diagnostics, fixer, testDriver, document,
scope.Value, equivalenceKey);
scope.Value, equivalenceKey, _ => options);
var fixAllContext = new FixAllContext(fixAllState, new ProgressTracker(), CancellationToken.None);
var fixAllFix = await fixAllProvider.GetFixAsync(fixAllContext);

Expand All @@ -235,24 +235,25 @@ private static FixAllState GetFixAllState(
TestDiagnosticAnalyzerDriver testDriver,
Document document,
FixAllScope scope,
string equivalenceKey)
string equivalenceKey,
CodeActionOptionsProvider optionsProvider)
{
Assert.NotEmpty(diagnostics);

if (scope == FixAllScope.Custom)
{
// Bulk fixing diagnostics in selected scope.
var diagnosticsToFix = ImmutableDictionary.CreateRange(SpecializedCollections.SingletonEnumerable(KeyValuePairUtil.Create(document, diagnostics.ToImmutableArray())));
return FixAllState.Create(fixAllProvider, diagnosticsToFix, fixer, equivalenceKey);
return FixAllState.Create(fixAllProvider, diagnosticsToFix, fixer, equivalenceKey, optionsProvider);
}

var diagnostic = diagnostics.First();
var diagnosticIds = ImmutableHashSet.Create(diagnostic.Id);
var fixAllDiagnosticProvider = new FixAllDiagnosticProvider(testDriver, diagnosticIds);

return diagnostic.Location.IsInSource
? new FixAllState(fixAllProvider, document, fixer, scope, equivalenceKey, diagnosticIds, fixAllDiagnosticProvider)
: new FixAllState(fixAllProvider, document.Project, fixer, scope, equivalenceKey, diagnosticIds, fixAllDiagnosticProvider);
? new FixAllState(fixAllProvider, document, document.Project, fixer, scope, equivalenceKey, diagnosticIds, fixAllDiagnosticProvider, optionsProvider)
: new FixAllState(fixAllProvider, document: null, document.Project, fixer, scope, equivalenceKey, diagnosticIds, fixAllDiagnosticProvider, optionsProvider);
}

private protected Task TestActionCountInAllFixesAsync(
Expand Down
4 changes: 2 additions & 2 deletions src/EditorFeatures/Test/CodeFixes/CodeFixServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async Task TestGetFirstDiagnosticWithFixAsync()
var reference = new MockAnalyzerReference();
var project = workspace.CurrentSolution.Projects.Single().AddAnalyzerReference(reference);
var document = project.Documents.Single();
var unused = await fixService.GetMostSevereFixableDiagnosticAsync(document, TextSpan.FromBounds(0, 0), cancellationToken: CancellationToken.None);
var unused = await fixService.GetMostSevereFixableDiagnosticAsync(document, TextSpan.FromBounds(0, 0), CodeActionOptions.Default, CancellationToken.None);

var fixer1 = (MockFixer)fixers.Single().Value;
var fixer2 = (MockFixer)reference.Fixer!;
Expand Down Expand Up @@ -297,7 +297,7 @@ private static async Task GetFirstDiagnosticWithFixWithExceptionValidationAsync(
errorReportingService.OnError = message => errorReported = true;

GetDocumentAndExtensionManager(tuple.analyzerService, workspace, out var document, out var extensionManager);
var unused = await tuple.codeFixService.GetMostSevereFixableDiagnosticAsync(document, TextSpan.FromBounds(0, 0), cancellationToken: CancellationToken.None);
var unused = await tuple.codeFixService.GetMostSevereFixableDiagnosticAsync(document, TextSpan.FromBounds(0, 0), CodeActionOptions.Default, CancellationToken.None);
Assert.True(extensionManager.IsDisabled(codefix));
Assert.False(extensionManager.IsIgnored(codefix));
Assert.True(errorReported);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

Imports System.Collections.Immutable
Imports System.Threading
Imports Microsoft.CodeAnalysis.CodeActions
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.Host
Imports Microsoft.CodeAnalysis.SyncNamespaces
Expand Down Expand Up @@ -39,7 +40,7 @@ namespace Test.Namespace.App
Dim document = project.Documents.Single()

Dim syncService = project.GetLanguageService(Of ISyncNamespacesService)()
Dim newSolution = Await syncService.SyncNamespacesAsync(ImmutableArray.Create(project), CancellationToken.None)
Dim newSolution = Await syncService.SyncNamespacesAsync(ImmutableArray.Create(project), CodeActionOptions.Default, CancellationToken.None)

Dim solutionChanges = workspace.CurrentSolution.GetChanges(newSolution)

Expand Down Expand Up @@ -75,7 +76,7 @@ namespace Test
Dim document = project.Documents.Single()

Dim syncService = project.GetLanguageService(Of ISyncNamespacesService)()
Dim newSolution = Await syncService.SyncNamespacesAsync(projects, CancellationToken.None)
Dim newSolution = Await syncService.SyncNamespacesAsync(projects, CodeActionOptions.Default, CancellationToken.None)

Dim solutionChanges = workspace.CurrentSolution.GetChanges(newSolution)
Dim projectChanges = solutionChanges.GetProjectChanges().Single()
Expand Down Expand Up @@ -132,7 +133,7 @@ namespace Test2.Namespace.App
Dim project = projects(0)

Dim syncService = project.GetLanguageService(Of ISyncNamespacesService)()
Dim newSolution = Await syncService.SyncNamespacesAsync(projects, CancellationToken.None)
Dim newSolution = Await syncService.SyncNamespacesAsync(projects, CodeActionOptions.Default, CancellationToken.None)

Dim solutionChanges = workspace.CurrentSolution.GetChanges(newSolution)

Expand Down Expand Up @@ -186,7 +187,7 @@ namespace Test2.Namespace.App
Dim document = project.Documents.Single()

Dim syncService = project.GetLanguageService(Of ISyncNamespacesService)()
Dim newSolution = Await syncService.SyncNamespacesAsync(projects, CancellationToken.None)
Dim newSolution = Await syncService.SyncNamespacesAsync(projects, CodeActionOptions.Default, CancellationToken.None)

Dim solutionChanges = workspace.CurrentSolution.GetChanges(newSolution)
Dim projectChanges = solutionChanges.GetProjectChanges().Single()
Expand Down Expand Up @@ -251,7 +252,7 @@ namespace Test2.Namespace
Dim document2 = project2.Documents.Single()

Dim syncService = project.GetLanguageService(Of ISyncNamespacesService)()
Dim newSolution = Await syncService.SyncNamespacesAsync(projects, CancellationToken.None)
Dim newSolution = Await syncService.SyncNamespacesAsync(projects, CodeActionOptions.Default, CancellationToken.None)

Dim solutionChanges = workspace.CurrentSolution.GetChanges(newSolution)
Dim projectChanges = solutionChanges.GetProjectChanges().ToImmutableArray()
Expand Down
Loading