提交 475c6258 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #14573 from CyrusNajmabadi/codeActionRefactoring

Remove strange inheritance hierarchies.
......@@ -608,7 +608,6 @@
<Compile Include="Implementation\Suggestions\SuggestedAction.CaretPositionRestorer.cs" />
<Compile Include="Implementation\Suggestions\SuggestedAction.cs" />
<Compile Include="Implementation\Suggestions\SuggestedActionsSourceProvider.cs" />
<Compile Include="Implementation\Suggestions\SuggestedActionWithFlavors.cs" />
<Compile Include="Implementation\Suggestions\SuppressionSuggestedAction.cs" />
<Compile Include="Implementation\TextBufferAssociatedViewService.cs" />
<Compile Include="Implementation\TextDiffing\EditorTextDifferencingService.cs" />
......@@ -635,6 +634,7 @@
<Compile Include="Shared\Tagging\EventSources\TaggerEventSources.EditorFormatMapChangedEventSource.cs" />
<Compile Include="Shared\Tagging\EventSources\TaggerEventSources.ViewSpanChangedEventSource.cs" />
<Compile Include="Shared\Utilities\SynchronizationContextTaskScheduler.cs" />
<Compile Include="Implementation\Suggestions\SuggestedActionWithPreview.cs" />
<Compile Include="Tagging\AbstractAsynchronousTaggerProvider.Tagger.cs" />
<Compile Include="Tagging\AbstractAsynchronousTaggerProvider.TagSource.cs" />
<Compile Include="Tagging\AbstractAsynchronousTaggerProvider.TagSource_ReferenceCounting.cs" />
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using Microsoft.CodeAnalysis.CodeActions;
......@@ -17,7 +15,7 @@ namespace Microsoft.CodeAnalysis.Editor.Implementation.Suggestions
/// <summary>
/// Represents light bulb menu item for code fixes.
/// </summary>
internal class CodeFixSuggestedAction : SuggestedActionWithFlavors, ITelemetryDiagnosticID<string>
internal sealed class CodeFixSuggestedAction : SuggestedActionWithPreview, ISuggestedActionWithFlavors, ITelemetryDiagnosticID<string>
{
private readonly CodeFix _fix;
private readonly SuggestedActionSet _fixAllSuggestedActionSet;
......@@ -38,47 +36,7 @@ internal class CodeFixSuggestedAction : SuggestedActionWithFlavors, ITelemetryDi
_fixAllSuggestedActionSet = fixAllSuggestedActionSet;
}
/// <summary>
/// If the provided fix all context is non-null and the context's code action Id matches the given code action's Id then,
/// returns the set of fix all occurrences actions associated with the code action.
/// </summary>
internal static SuggestedActionSet GetFixAllSuggestedActionSet(
CodeAction action,
int actionCount,
FixAllState fixAllState,
IEnumerable<FixAllScope> supportedScopes,
Diagnostic firstDiagnostic,
Workspace workspace,
ITextBuffer subjectBuffer,
ICodeActionEditHandlerService editHandler,
IWaitIndicator waitIndicator,
IAsynchronousOperationListener operationListener)
{
if (fixAllState == null)
{
return null;
}
if (actionCount > 1 && action.EquivalenceKey == null)
{
return null;
}
var fixAllSuggestedActions = ArrayBuilder<FixAllSuggestedAction>.GetInstance();
foreach (var scope in supportedScopes)
{
var fixAllStateForScope = fixAllState.WithScopeAndEquivalenceKey(scope, action.EquivalenceKey);
var fixAllAction = new FixAllCodeAction(fixAllStateForScope, showPreviewChangesDialog: true);
var fixAllSuggestedAction = new FixAllSuggestedAction(
workspace, subjectBuffer, editHandler, waitIndicator, fixAllAction,
fixAllStateForScope.FixAllProvider, firstDiagnostic, operationListener);
fixAllSuggestedActions.Add(fixAllSuggestedAction);
}
return new SuggestedActionSet(
fixAllSuggestedActions.ToImmutableAndFree(),
title: EditorFeaturesResources.Fix_all_occurrences_in);
}
protected override SuggestedActionSet GetAdditionalActionSet() => _fixAllSuggestedActionSet;
public string GetDiagnosticID()
{
......@@ -98,10 +56,5 @@ protected override DiagnosticData GetDiagnostic()
{
return _fix.GetPrimaryDiagnosticData();
}
protected override SuggestedActionSet GetFixAllSuggestedActionSet()
{
return _fixAllSuggestedActionSet;
}
}
}
}
\ No newline at end of file
......@@ -11,7 +11,7 @@ namespace Microsoft.CodeAnalysis.Editor.Implementation.Suggestions
/// <summary>
/// Represents light bulb menu item for code refactorings.
/// </summary>
internal class CodeRefactoringSuggestedAction : SuggestedActionWithFlavors
internal sealed class CodeRefactoringSuggestedAction : SuggestedActionWithPreview
{
public CodeRefactoringSuggestedAction(
Workspace workspace,
......@@ -25,4 +25,4 @@ internal class CodeRefactoringSuggestedAction : SuggestedActionWithFlavors
{
}
}
}
}
\ No newline at end of file
......@@ -387,4 +387,4 @@ public override int GetHashCode()
#endregion
}
}
}
\ No newline at end of file
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
......@@ -14,41 +14,30 @@
namespace Microsoft.CodeAnalysis.Editor.Implementation.Suggestions
{
/// <summary>
/// Base class for light bulb menu items for code fixes and code refactorings.
/// </summary>
internal class SuggestedActionWithFlavors : SuggestedAction, ISuggestedActionWithFlavors
internal class SuggestedActionWithPreview : SuggestedAction
{
protected SuggestedActionWithFlavors(
Workspace workspace,
ITextBuffer subjectBuffer,
ICodeActionEditHandlerService editHandler,
IWaitIndicator waitIndicator,
CodeAction codeAction,
object provider,
IAsynchronousOperationListener operationListener) : base(workspace, subjectBuffer, editHandler, waitIndicator, codeAction, provider, operationListener)
{
}
private ImmutableArray<SuggestedActionSet> _actionSets;
public override bool HasActionSets
public SuggestedActionWithPreview(
Workspace workspace, ITextBuffer subjectBuffer, ICodeActionEditHandlerService editHandler,
IWaitIndicator waitIndicator, CodeAction codeAction, object provider,
IAsynchronousOperationListener operationListener)
: base(workspace, subjectBuffer, editHandler, waitIndicator, codeAction,
provider, operationListener, actionSets: null)
{
get
{
// HasActionSets is called synchronously on the UI thread. In order to avoid blocking the UI thread,
// we need to provide a 'quick' answer here as opposed to the 'right' answer. Providing the 'right'
// answer is expensive (because we will need to call CodeAction.GetPreviewOperationsAsync() (to
// compute whether or not we should display the flavored action for 'Preview Changes') which in turn
// will involve computing the changed solution for the ApplyChangesOperation for the fix / refactoring
// So we always return 'true' here (so that platform will call GetActionSetsAsync() below). Platform
// guarantees that nothing bad will happen if we return 'true' here and later return 'null' / empty
// collection from within GetPreviewAsync().
return true;
}
}
private ImmutableArray<SuggestedActionSet> _actionSets;
public async override Task<IEnumerable<SuggestedActionSet>> GetActionSetsAsync(CancellationToken cancellationToken)
// HasActionSets is called synchronously on the UI thread. In order to avoid blocking the UI thread,
// we need to provide a 'quick' answer here as opposed to the 'right' answer. Providing the 'right'
// answer is expensive (because we will need to call CodeAction.GetPreviewOperationsAsync() (to
// compute whether or not we should display the flavored action for 'Preview Changes') which in turn
// will involve computing the changed solution for the ApplyChangesOperation for the fix / refactoring
// So we always return 'true' here (so that platform will call GetActionSetsAsync() below). Platform
// guarantees that nothing bad will happen if we return 'true' here and later return 'null' / empty
// collection from within GetPreviewAsync().
public override bool HasActionSets => true;
public async sealed override Task<IEnumerable<SuggestedActionSet>> GetActionSetsAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
......@@ -70,12 +59,10 @@ public async override Task<IEnumerable<SuggestedActionSet>> GetActionSetsAsync(C
builder.Add(previewChangesSuggestedActionSet);
}
cancellationToken.ThrowIfCancellationRequested();
var fixAllSuggestedActionSet = GetFixAllSuggestedActionSet();
if (fixAllSuggestedActionSet != null)
var additionalSet = this.GetAdditionalActionSet();
if (additionalSet != null)
{
builder.Add(fixAllSuggestedActionSet);
builder.Add(additionalSet);
}
return builder.ToImmutableAndFree();
......@@ -87,7 +74,7 @@ public async override Task<IEnumerable<SuggestedActionSet>> GetActionSetsAsync(C
return _actionSets;
}
private async Task<SuggestedActionSet> GetPreviewChangesSuggestedActionSetAsync(CancellationToken cancellationToken)
protected async Task<SuggestedActionSet> GetPreviewChangesSuggestedActionSetAsync(CancellationToken cancellationToken)
{
var previewResult = await GetPreviewResultAsync(cancellationToken).ConfigureAwait(true);
if (previewResult == null)
......@@ -107,11 +94,6 @@ private async Task<SuggestedActionSet> GetPreviewChangesSuggestedActionSetAsync(
return new SuggestedActionSet(ImmutableArray.Create(previewSuggestedAction));
}
protected virtual SuggestedActionSet GetFixAllSuggestedActionSet()
{
// Only code fixes support fix all occurrences at the moment. So only
// CodeFixSuggestedAction provides a non-null-returning implementation for this method.
return null;
}
protected virtual SuggestedActionSet GetAdditionalActionSet() => null;
}
}
}
\ No newline at end of file
......@@ -349,7 +349,7 @@ private IEnumerable<SuggestedActionSet> OrganizeFixes(Workspace workspace, IEnum
var fixCount = fixes.Length;
Func<CodeAction, SuggestedActionSet> getFixAllSuggestedActionSet =
codeAction => CodeFixSuggestedAction.GetFixAllSuggestedActionSet(
codeAction => GetFixAllSuggestedActionSet(
codeAction, fixCount, fixCollection.FixAllState,
fixCollection.SupportedScopes, fixCollection.FirstDiagnostic,
workspace, _subjectBuffer, _owner._editHandler,
......@@ -434,6 +434,48 @@ private IEnumerable<SuggestedActionSet> OrganizeFixes(Workspace workspace, IEnum
map[groupKey].Add(suggestedAction);
}
/// <summary>
/// If the provided fix all context is non-null and the context's code action Id matches the given code action's Id then,
/// returns the set of fix all occurrences actions associated with the code action.
/// </summary>
internal static SuggestedActionSet GetFixAllSuggestedActionSet(
CodeAction action,
int actionCount,
FixAllState fixAllState,
IEnumerable<FixAllScope> supportedScopes,
Diagnostic firstDiagnostic,
Workspace workspace,
ITextBuffer subjectBuffer,
ICodeActionEditHandlerService editHandler,
IWaitIndicator waitIndicator,
IAsynchronousOperationListener operationListener)
{
if (fixAllState == null)
{
return null;
}
if (actionCount > 1 && action.EquivalenceKey == null)
{
return null;
}
var fixAllSuggestedActions = ArrayBuilder<FixAllSuggestedAction>.GetInstance();
foreach (var scope in supportedScopes)
{
var fixAllStateForScope = fixAllState.WithScopeAndEquivalenceKey(scope, action.EquivalenceKey);
var fixAllAction = new FixAllCodeAction(fixAllStateForScope, showPreviewChangesDialog: true);
var fixAllSuggestedAction = new FixAllSuggestedAction(
workspace, subjectBuffer, editHandler, waitIndicator, fixAllAction,
fixAllStateForScope.FixAllProvider, firstDiagnostic, operationListener);
fixAllSuggestedActions.Add(fixAllSuggestedAction);
}
return new SuggestedActionSet(
fixAllSuggestedActions.ToImmutableAndFree(),
title: EditorFeaturesResources.Fix_all_occurrences_in);
}
/// <summary>
/// Return prioritized set of fix groups such that fix group for suppression always show up at the bottom of the list.
/// </summary>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册