|
8 | 8 | using System.Collections.Immutable; |
9 | 9 | using System.Composition; |
10 | 10 | using System.Threading; |
11 | | -using Microsoft.CodeAnalysis.CodeActions; |
| 11 | +using System.Threading.Tasks; |
12 | 12 | using Microsoft.CodeAnalysis.CodeFixes; |
13 | 13 | using Microsoft.CodeAnalysis.Extensions; |
14 | 14 | using Microsoft.CodeAnalysis.Host.Mef; |
15 | | -using Microsoft.CodeAnalysis.Shared.TestHooks; |
16 | | -using Roslyn.Utilities; |
17 | 15 |
|
18 | | -namespace Microsoft.CodeAnalysis.Editor.Implementation.Suggestions |
| 16 | +namespace Microsoft.CodeAnalysis.Editor.Implementation.Suggestions; |
| 17 | + |
| 18 | +/// <summary> |
| 19 | +/// Service to compute and apply <see cref="FixMultipleCodeAction"/> code fixes. |
| 20 | +/// </summary> |
| 21 | +[ExportWorkspaceService(typeof(IFixMultipleOccurrencesService), ServiceLayer.Host), Shared] |
| 22 | +[method: ImportingConstructor] |
| 23 | +[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] |
| 24 | +internal sealed class FixMultipleOccurrencesService() : IFixMultipleOccurrencesService |
19 | 25 | { |
20 | | - /// <summary> |
21 | | - /// Service to compute and apply <see cref="FixMultipleCodeAction"/> code fixes. |
22 | | - /// </summary> |
23 | | - [ExportWorkspaceService(typeof(IFixMultipleOccurrencesService), ServiceLayer.Host), Shared] |
24 | | - internal sealed class FixMultipleOccurrencesService : IFixMultipleOccurrencesService |
| 26 | + public Task<Solution> GetFixAsync( |
| 27 | + ImmutableDictionary<Document, ImmutableArray<Diagnostic>> diagnosticsToFix, |
| 28 | + Workspace workspace, |
| 29 | + CodeFixProvider fixProvider, |
| 30 | + FixAllProvider fixAllProvider, |
| 31 | + string equivalenceKey, |
| 32 | + string waitDialogTitle, |
| 33 | + string waitDialogMessage, |
| 34 | + IProgress<CodeAnalysisProgress> progress, |
| 35 | + CancellationToken cancellationToken) |
25 | 36 | { |
26 | | - [ImportingConstructor] |
27 | | - [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] |
28 | | - public FixMultipleOccurrencesService(IAsynchronousOperationListenerProvider listenerProvider) |
29 | | - { |
30 | | - listenerProvider.GetListener(FeatureAttribute.LightBulb); |
31 | | - } |
| 37 | + var fixMultipleState = FixAllState.Create( |
| 38 | + fixAllProvider, diagnosticsToFix, fixProvider, equivalenceKey); |
32 | 39 |
|
33 | | - public Solution GetFix( |
34 | | - ImmutableDictionary<Document, ImmutableArray<Diagnostic>> diagnosticsToFix, |
35 | | - Workspace workspace, |
36 | | - CodeFixProvider fixProvider, |
37 | | - FixAllProvider fixAllProvider, |
38 | | - string equivalenceKey, |
39 | | - string waitDialogTitle, |
40 | | - string waitDialogMessage, |
41 | | - IProgress<CodeAnalysisProgress> progress, |
42 | | - CancellationToken cancellationToken) |
43 | | - { |
44 | | - var fixMultipleState = FixAllState.Create( |
45 | | - fixAllProvider, diagnosticsToFix, fixProvider, equivalenceKey); |
| 40 | + return GetFixedSolutionAsync( |
| 41 | + fixMultipleState, workspace, waitDialogTitle, waitDialogMessage, progress, cancellationToken); |
| 42 | + } |
46 | 43 |
|
47 | | - return GetFixedSolution( |
48 | | - fixMultipleState, workspace, waitDialogTitle, waitDialogMessage, progress, cancellationToken); |
49 | | - } |
| 44 | + public Task<Solution> GetFixAsync( |
| 45 | + ImmutableDictionary<Project, ImmutableArray<Diagnostic>> diagnosticsToFix, |
| 46 | + Workspace workspace, |
| 47 | + CodeFixProvider fixProvider, |
| 48 | + FixAllProvider fixAllProvider, |
| 49 | + string equivalenceKey, |
| 50 | + string waitDialogTitle, |
| 51 | + string waitDialogMessage, |
| 52 | + IProgress<CodeAnalysisProgress> progress, |
| 53 | + CancellationToken cancellationToken) |
| 54 | + { |
| 55 | + var fixMultipleState = FixAllState.Create( |
| 56 | + fixAllProvider, diagnosticsToFix, fixProvider, equivalenceKey); |
50 | 57 |
|
51 | | - public Solution GetFix( |
52 | | - ImmutableDictionary<Project, ImmutableArray<Diagnostic>> diagnosticsToFix, |
53 | | - Workspace workspace, |
54 | | - CodeFixProvider fixProvider, |
55 | | - FixAllProvider fixAllProvider, |
56 | | - string equivalenceKey, |
57 | | - string waitDialogTitle, |
58 | | - string waitDialogMessage, |
59 | | - IProgress<CodeAnalysisProgress> progress, |
60 | | - CancellationToken cancellationToken) |
61 | | - { |
62 | | - var fixMultipleState = FixAllState.Create( |
63 | | - fixAllProvider, diagnosticsToFix, fixProvider, equivalenceKey); |
| 58 | + return GetFixedSolutionAsync( |
| 59 | + fixMultipleState, workspace, waitDialogTitle, waitDialogMessage, progress, cancellationToken); |
| 60 | + } |
64 | 61 |
|
65 | | - return GetFixedSolution( |
66 | | - fixMultipleState, workspace, waitDialogTitle, waitDialogMessage, progress, cancellationToken); |
67 | | - } |
| 62 | + private static async Task<Solution> GetFixedSolutionAsync( |
| 63 | + FixAllState fixAllState, |
| 64 | + Workspace workspace, |
| 65 | + string title, |
| 66 | + string waitDialogMessage, |
| 67 | + IProgress<CodeAnalysisProgress> progress, |
| 68 | + CancellationToken cancellationToken) |
| 69 | + { |
| 70 | + var fixMultipleCodeAction = new FixMultipleCodeAction( |
| 71 | + fixAllState, title, waitDialogMessage); |
68 | 72 |
|
69 | | - private static Solution GetFixedSolution( |
70 | | - FixAllState fixAllState, |
71 | | - Workspace workspace, |
72 | | - string title, |
73 | | - string waitDialogMessage, |
74 | | - IProgress<CodeAnalysisProgress> progress, |
75 | | - CancellationToken cancellationToken) |
| 73 | + Solution newSolution = null; |
| 74 | + var extensionManager = workspace.Services.GetService<IExtensionManager>(); |
| 75 | + await extensionManager.PerformActionAsync(fixAllState.FixAllProvider, async () => |
76 | 76 | { |
77 | | - var fixMultipleCodeAction = new FixMultipleCodeAction( |
78 | | - fixAllState, title, waitDialogMessage); |
79 | | - |
80 | | - Solution newSolution = null; |
81 | | - var extensionManager = workspace.Services.GetService<IExtensionManager>(); |
82 | | - extensionManager.PerformAction(fixAllState.FixAllProvider, () => |
83 | | - { |
84 | | - // We don't need to post process changes here as the inner code action created for Fix multiple code fix already executes. |
85 | | - newSolution = fixMultipleCodeAction.GetChangedSolutionInternalAsync( |
86 | | - fixAllState.Solution, progress, postProcessChanges: false, cancellationToken).WaitAndGetResult(cancellationToken); |
87 | | - }); |
| 77 | + // We don't need to post process changes here as the inner code action created for Fix multiple code fix already executes. |
| 78 | + newSolution = await fixMultipleCodeAction.GetChangedSolutionInternalAsync( |
| 79 | + fixAllState.Solution, progress, postProcessChanges: false, cancellationToken).ConfigureAwait(false); |
| 80 | + }).ConfigureAwait(false); |
88 | 81 |
|
89 | | - return newSolution; |
90 | | - } |
| 82 | + return newSolution; |
91 | 83 | } |
92 | 84 | } |
0 commit comments