Skip to content

Commit fb31739

Browse files
Further cleanup of the code action api (5) (#80215)
2 parents 265c765 + 2f32052 commit fb31739

File tree

15 files changed

+64
-154
lines changed

15 files changed

+64
-154
lines changed

src/EditorFeatures/CSharpTest/CodeActions/PreviewExceptionTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ private static async Task ActionSets(EditorTestWorkspace workspace, CodeRefactor
8787
Assert.False(extensionManager.IsIgnored(provider));
8888
}
8989

90-
private static CodeRefactoringSuggestedAction CreateRefactoringSuggestedAction(EditorTestWorkspace workspace, CodeRefactoringProvider provider, out EditorLayerExtensionManager.ExtensionManager extensionManager)
90+
private static SuggestedActionWithNestedFlavors CreateRefactoringSuggestedAction(EditorTestWorkspace workspace, CodeRefactoringProvider provider, out EditorLayerExtensionManager.ExtensionManager extensionManager)
9191
{
9292
var codeActions = new List<CodeAction>();
9393
RefactoringSetup(workspace, provider, codeActions, out extensionManager, out var textBuffer, out var document);
94-
var suggestedAction = new CodeRefactoringSuggestedAction(
94+
var suggestedAction = new SuggestedActionWithNestedFlavors(
9595
workspace.ExportProvider.GetExportedValue<IThreadingContext>(),
9696
workspace.ExportProvider.GetExportedValue<SuggestedActionsSourceProvider>(),
97-
document, textBuffer, provider, codeActions.First(), fixAllFlavors: null);
97+
document, textBuffer, provider, codeActions.First(), fixAllFlavors: null, diagnostic: null);
9898
return suggestedAction;
9999
}
100100

src/EditorFeatures/Core/Suggestions/PreviewChanges/PreviewChangesCodeAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Microsoft.CodeAnalysis.Editor.Implementation.Suggestions;
1313

14-
internal partial class SuggestedActionWithNestedFlavors
14+
internal sealed partial class SuggestedActionWithNestedFlavors
1515
{
1616
private sealed class PreviewChangesCodeAction(
1717
CodeAction originalCodeAction,

src/EditorFeatures/Core/Suggestions/RefineUsingCopilot/RefineUsingCopilotCodeAction.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
using Microsoft.CodeAnalysis.CodeActions;
1212
using Microsoft.CodeAnalysis.Collections;
1313
using Microsoft.CodeAnalysis.Copilot;
14-
using Microsoft.CodeAnalysis.Diagnostics;
1514
using Microsoft.CodeAnalysis.Shared.Extensions;
16-
using Roslyn.Utilities;
1715

1816
namespace Microsoft.CodeAnalysis.Editor.Implementation.Suggestions;
1917

@@ -26,7 +24,7 @@ internal partial class SuggestedActionWithNestedFlavors
2624
private sealed class RefineUsingCopilotCodeAction(
2725
Solution originalSolution,
2826
CodeAction originalCodeAction,
29-
DiagnosticData? primaryDiagnostic,
27+
Diagnostic? primaryDiagnostic,
3028
ICopilotCodeAnalysisService copilotCodeAnalysisService) : CodeAction
3129
{
3230
public override string Title => EditorFeaturesResources.Refine_using_Copilot;
@@ -73,12 +71,8 @@ protected override async Task<ImmutableArray<CodeActionOperation>> ComputeOperat
7371
var oldDocument = changeSummary.OldSolution.GetRequiredDocument(changedDocumentId);
7472
var newDocument = changeSummary.NewSolution.GetRequiredDocument(changedDocumentId);
7573

76-
var convertedPrimaryDiagnostic = primaryDiagnostic != null
77-
? await primaryDiagnostic.ToDiagnosticAsync(oldDocument.Project, cancellationToken).ConfigureAwait(false)
78-
: null;
79-
8074
cancellationToken.ThrowIfCancellationRequested();
81-
return [new OpenRefinementSessionOperation(oldDocument, newDocument, convertedPrimaryDiagnostic, copilotCodeAnalysisService)];
75+
return [new OpenRefinementSessionOperation(oldDocument, newDocument, primaryDiagnostic, copilotCodeAnalysisService)];
8276
}
8377

8478
/// <summary>

src/EditorFeatures/Core/Suggestions/SuggestedActions/CodeFixSuggestedAction.cs

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/EditorFeatures/Core/Suggestions/SuggestedActions/CodeRefactoringSuggestedAction.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/EditorFeatures/Core/Suggestions/SuggestedActions/AbstractFixAllSuggestedAction.cs renamed to src/EditorFeatures/Core/Suggestions/SuggestedActions/RefactorOrFixAllSuggestedAction.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
using System.Threading;
77
using System.Threading.Tasks;
88
using Microsoft.CodeAnalysis.CodeActions;
9+
using Microsoft.CodeAnalysis.CodeFixes;
910
using Microsoft.CodeAnalysis.CodeFixesAndRefactorings;
11+
using Microsoft.CodeAnalysis.CodeRefactorings;
1012
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
1113
using Microsoft.CodeAnalysis.Internal.Log;
1214
using Microsoft.VisualStudio.Language.Intellisense;
@@ -24,28 +26,28 @@ internal sealed class RefactorOrFixAllSuggestedAction(
2426
ITextBuffer subjectBuffer,
2527
IRefactorOrFixAllState fixAllState,
2628
CodeAction originalCodeAction,
27-
string? diagnosticTelemetryId,
28-
AbstractFixAllCodeAction fixAllCodeAction)
29+
string? diagnosticTelemetryId)
2930
: SuggestedAction(threadingContext,
3031
sourceProvider,
3132
originalSolution,
3233
subjectBuffer,
3334
fixAllState.FixAllProvider,
34-
fixAllCodeAction),
35+
fixAllState switch
36+
{
37+
FixAllState state => new FixAllCodeAction(state),
38+
RefactorAllState state => new RefactorAllCodeAction(state),
39+
_ => throw ExceptionUtilities.UnexpectedValue(fixAllState)
40+
}),
3541
ITelemetryDiagnosticID<string?>
3642
{
37-
public CodeAction OriginalCodeAction { get; } = originalCodeAction;
38-
39-
public IRefactorOrFixAllState FixAllState { get; } = fixAllState;
40-
4143
public string? GetDiagnosticID() => diagnosticTelemetryId;
4244

4345
public override bool TryGetTelemetryId(out Guid telemetryId)
4446
{
4547
// We get the telemetry id for the original code action we are fixing,
4648
// not the special 'FixAllCodeAction'. that is the .CodeAction this
4749
// SuggestedAction is pointing at.
48-
telemetryId = OriginalCodeAction.GetTelemetryId(FixAllState.Scope);
50+
telemetryId = originalCodeAction.GetTelemetryId(fixAllState.Scope);
4951
return true;
5052
}
5153

@@ -54,15 +56,15 @@ protected override async Task InnerInvokeAsync(
5456
{
5557
await this.ThreadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
5658

57-
var fixAllKind = FixAllState.FixAllKind;
59+
var fixAllKind = fixAllState.FixAllKind;
5860
var functionId = fixAllKind switch
5961
{
6062
FixAllKind.CodeFix => FunctionId.CodeFixes_FixAllOccurrencesSession,
6163
FixAllKind.Refactoring => FunctionId.Refactoring_FixAllOccurrencesSession,
6264
_ => throw ExceptionUtilities.UnexpectedValue(fixAllKind)
6365
};
6466

65-
using (Logger.LogBlock(functionId, FixAllLogger.CreateCorrelationLogMessage(FixAllState.CorrelationId), cancellationToken))
67+
using (Logger.LogBlock(functionId, FixAllLogger.CreateCorrelationLogMessage(fixAllState.CorrelationId), cancellationToken))
6668
{
6769
await base.InnerInvokeAsync(progress, cancellationToken).ConfigureAwait(false);
6870
}
File renamed without changes.

src/EditorFeatures/Core/Suggestions/SuggestedActionWithNestedFlavors.cs renamed to src/EditorFeatures/Core/Suggestions/SuggestedActions/SuggestedActionWithNestedFlavors.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Threading;
88
using System.Threading.Tasks;
99
using Microsoft.CodeAnalysis.CodeActions;
10+
using Microsoft.CodeAnalysis.CodeFixes;
1011
using Microsoft.CodeAnalysis.Copilot;
1112
using Microsoft.CodeAnalysis.Diagnostics;
1213
using Microsoft.CodeAnalysis.Editor.Host;
@@ -17,34 +18,37 @@
1718
using Microsoft.CodeAnalysis.Shared.Extensions;
1819
using Microsoft.CodeAnalysis.Text;
1920
using Microsoft.VisualStudio.Language.Intellisense;
21+
using Microsoft.VisualStudio.PlatformUI;
2022
using Microsoft.VisualStudio.Text;
2123

2224
namespace Microsoft.CodeAnalysis.Editor.Implementation.Suggestions;
2325

2426
/// <summary>
25-
/// Base type for all SuggestedActions that have 'flavors'. 'Flavors' are child actions that
26-
/// are presented as simple links, not as menu-items, in the light-bulb. Examples of 'flavors'
27-
/// include 'preview changes' (for refactorings and fixes) and 'fix all in document, project, solution'
28-
/// (for refactorings and fixes).
27+
/// Type for all SuggestedActions that have 'flavors'. 'Flavors' are child actions that are presented as simple links,
28+
/// not as menu-items, in the light-bulb. Examples of 'flavors' include 'preview changes' (for refactorings and fixes)
29+
/// and 'fix all in document, project, solution' (for refactorings and fixes).
2930
///
30-
/// Because all derivations support 'preview changes', we bake that logic into this base type.
31+
/// Supports 'preview changes' for all changes.
3132
/// </summary>
32-
internal abstract partial class SuggestedActionWithNestedFlavors(
33+
internal sealed partial class SuggestedActionWithNestedFlavors(
3334
IThreadingContext threadingContext,
3435
SuggestedActionsSourceProvider sourceProvider,
3536
TextDocument originalDocument,
3637
ITextBuffer subjectBuffer,
3738
object provider,
3839
CodeAction codeAction,
39-
SuggestedActionSet? fixAllFlavors)
40+
SuggestedActionSet? fixAllFlavors,
41+
Diagnostic? diagnostic)
4042
: SuggestedAction(threadingContext,
4143
sourceProvider,
4244
originalDocument.Project.Solution,
4345
subjectBuffer,
4446
provider,
45-
codeAction), ISuggestedActionWithFlavors
47+
codeAction), ISuggestedActionWithFlavors, ITelemetryDiagnosticID<string?>
4648
{
4749
private readonly SuggestedActionSet? _fixAllFlavors = fixAllFlavors;
50+
private readonly Diagnostic? _diagnostic = diagnostic;
51+
4852
private ImmutableArray<SuggestedActionSet> _nestedFlavors;
4953

5054
public TextDocument OriginalDocument { get; } = originalDocument;
@@ -139,7 +143,7 @@ await copilotService.IsAvailableAsync(cancellationToken).ConfigureAwait(false) i
139143

140144
return CreateTrivialAction(
141145
this, new RefineUsingCopilotCodeAction(
142-
this.OriginalSolution, this.CodeAction, this.GetDiagnostic(), copilotService));
146+
this.OriginalSolution, this.CodeAction, _diagnostic, copilotService));
143147
}
144148
}
145149

@@ -193,8 +197,10 @@ await copilotService.IsAvailableAsync(cancellationToken).ConfigureAwait(false) i
193197
// GetPreviewPane() needs to run on the UI thread.
194198
this.ThreadingContext.ThrowIfNotOnUIThread();
195199

196-
return previewPaneService.GetPreviewPane(GetDiagnostic(), previewContents!);
200+
var diagnosticData = _diagnostic is null ? null : CodeFix.GetDiagnosticData(this.OriginalDocument.Project, _diagnostic);
201+
return previewPaneService.GetPreviewPane(diagnosticData, previewContents!);
197202
}
198203

199-
protected virtual DiagnosticData? GetDiagnostic() => null;
204+
public string? GetDiagnosticID()
205+
=> _diagnostic?.GetTelemetryDiagnosticID();
200206
}

src/EditorFeatures/Core/Suggestions/SuggestedActionsSource_Async.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -287,25 +287,20 @@ async Task<ImmutableArray<UnifiedSuggestedActionSet>> GetRefactoringsAsync()
287287
ISuggestedAction ConvertToSuggestedAction(UnifiedSuggestedAction unifiedSuggestedAction)
288288
=> unifiedSuggestedAction switch
289289
{
290-
UnifiedCodeFixSuggestedAction codeFixAction => new CodeFixSuggestedAction(
290+
UnifiedCodeFixSuggestedAction codeFixAction => new SuggestedActionWithNestedFlavors(
291291
_threadingContext, owner, originalDocument, subjectBuffer,
292-
codeFixAction.CodeFix, codeFixAction.Provider, codeFixAction.OriginalCodeAction,
293-
ConvertToSuggestedActionSet(codeFixAction.FixAllFlavors, originalDocument)),
294-
UnifiedCodeRefactoringSuggestedAction codeRefactoringAction => new CodeRefactoringSuggestedAction(
292+
codeFixAction.Provider, codeFixAction.OriginalCodeAction,
293+
ConvertToSuggestedActionSet(codeFixAction.FixAllFlavors, originalDocument),
294+
codeFixAction.CodeFix.PrimaryDiagnostic),
295+
UnifiedCodeRefactoringSuggestedAction codeRefactoringAction => new SuggestedActionWithNestedFlavors(
295296
_threadingContext, owner, originalDocument, subjectBuffer,
296297
codeRefactoringAction.Provider, codeRefactoringAction.OriginalCodeAction,
297-
ConvertToSuggestedActionSet(codeRefactoringAction.FixAllFlavors, originalDocument)),
298-
UnifiedFixAllCodeFixSuggestedAction fixAllAction
298+
ConvertToSuggestedActionSet(codeRefactoringAction.FixAllFlavors, originalDocument),
299+
diagnostic: null),
300+
UnifiedRefactorOrFixAllSuggestedAction refactorOrFixAllAction
299301
=> new RefactorOrFixAllSuggestedAction(
300302
_threadingContext, owner, originalSolution, subjectBuffer,
301-
fixAllAction.FixAllState, fixAllAction.OriginalCodeAction, fixAllAction.TelemetryId,
302-
new FixAllCodeAction(fixAllAction.FixAllState)),
303-
UnifiedRefactorAllCodeRefactoringSuggestedAction fixAllCodeRefactoringAction
304-
=> new RefactorOrFixAllSuggestedAction(
305-
_threadingContext, owner, originalSolution, subjectBuffer,
306-
fixAllCodeRefactoringAction.FixAllState, fixAllCodeRefactoringAction.OriginalCodeAction,
307-
diagnosticTelemetryId: null,
308-
new RefactorAllCodeRefactoringCodeAction(fixAllCodeRefactoringAction.FixAllState)),
303+
refactorOrFixAllAction.FixAllState, refactorOrFixAllAction.OriginalCodeAction, refactorOrFixAllAction.TelemetryId),
309304
UnifiedSuggestedActionWithNestedActions nestedAction => new SuggestedActionWithNestedActions(
310305
_threadingContext, owner, originalSolution, subjectBuffer,
311306
nestedAction.Provider, nestedAction.OriginalCodeAction,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Microsoft.CodeAnalysis.CodeRefactorings;
1111
/// <summary>
1212
/// Fix all code action for a code action registered by a <see cref="CodeRefactoringProvider"/>.
1313
/// </summary>
14-
internal sealed class RefactorAllCodeRefactoringCodeAction(IRefactorOrFixAllState fixAllState)
14+
internal sealed class RefactorAllCodeAction(IRefactorOrFixAllState fixAllState)
1515
: AbstractFixAllCodeAction(fixAllState, showPreviewChangesDialog: true)
1616
{
1717
protected override IRefactorOrFixAllContext CreateFixAllContext(IRefactorOrFixAllState fixAllState, IProgress<CodeAnalysisProgress> progressTracker, CancellationToken cancellationToken)

0 commit comments

Comments
 (0)