forked from dotnet/roslyn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dotnet#53117 from CyrusNajmabadi/asyncLightbulb
Add support for async lightbulb model.
- Loading branch information
Showing
62 changed files
with
705 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/EditorFeatures/Core.Wpf/Suggestions/AsyncSuggestedActionsSource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// 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; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.Editor.Shared; | ||
using Microsoft.CodeAnalysis.Editor.Shared.Utilities; | ||
using Microsoft.CodeAnalysis.Host; | ||
using Microsoft.CodeAnalysis.Internal.Log; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Microsoft.CodeAnalysis.UnifiedSuggestions; | ||
using Microsoft.VisualStudio.Language.Intellisense; | ||
using Microsoft.VisualStudio.Text; | ||
using Microsoft.VisualStudio.Text.Editor; | ||
using Roslyn.Utilities; | ||
|
||
namespace Microsoft.CodeAnalysis.Editor.Implementation.Suggestions | ||
{ | ||
internal partial class SuggestedActionsSourceProvider | ||
{ | ||
private partial class AsyncSuggestedActionsSource : SuggestedActionsSource, ISuggestedActionsSourceExperimental | ||
{ | ||
public AsyncSuggestedActionsSource( | ||
IThreadingContext threadingContext, | ||
SuggestedActionsSourceProvider owner, | ||
ITextView textView, | ||
ITextBuffer textBuffer, | ||
ISuggestedActionCategoryRegistryService suggestedActionCategoryRegistry) | ||
: base(threadingContext, owner, textView, textBuffer, suggestedActionCategoryRegistry) | ||
{ | ||
} | ||
|
||
public async IAsyncEnumerable<SuggestedActionSet> GetSuggestedActionsAsync( | ||
ISuggestedActionCategorySet requestedActionCategories, | ||
SnapshotSpan range, | ||
[EnumeratorCancellation] CancellationToken cancellationToken) | ||
{ | ||
AssertIsForeground(); | ||
|
||
using var state = SourceState.TryAddReference(); | ||
if (state is null) | ||
yield break; | ||
|
||
var workspace = state.Target.Workspace; | ||
if (workspace is null) | ||
yield break; | ||
|
||
var selection = TryGetCodeRefactoringSelection(state, range); | ||
await workspace.Services.GetRequiredService<IWorkspaceStatusService>().WaitUntilFullyLoadedAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
using (Logger.LogBlock(FunctionId.SuggestedActions_GetSuggestedActionsAsync, cancellationToken)) | ||
{ | ||
var document = range.Snapshot.GetOpenDocumentInCurrentContextWithChanges(); | ||
if (document is null) | ||
yield break; | ||
|
||
// Compute and return the high pri set of fixes and refactorings first so the user | ||
// can act on them immediately without waiting on the regular set. | ||
var highPriSet = GetCodeFixesAndRefactoringsAsync( | ||
state, requestedActionCategories, document, range, selection, _ => null, | ||
CodeActionRequestPriority.High, cancellationToken).WithCancellation(cancellationToken).ConfigureAwait(false); | ||
await foreach (var set in highPriSet) | ||
yield return set; | ||
|
||
var lowPriSet = GetCodeFixesAndRefactoringsAsync( | ||
state, requestedActionCategories, document, range, selection, _ => null, | ||
CodeActionRequestPriority.Normal, cancellationToken).WithCancellation(cancellationToken).ConfigureAwait(false); | ||
await foreach (var set in lowPriSet) | ||
yield return set; | ||
} | ||
} | ||
|
||
private async IAsyncEnumerable<SuggestedActionSet> GetCodeFixesAndRefactoringsAsync( | ||
ReferenceCountedDisposable<State> state, | ||
ISuggestedActionCategorySet requestedActionCategories, | ||
Document document, | ||
SnapshotSpan range, | ||
TextSpan? selection, | ||
Func<string, IDisposable?> addOperationScope, | ||
CodeActionRequestPriority priority, | ||
[EnumeratorCancellation] CancellationToken cancellationToken) | ||
{ | ||
var workspace = document.Project.Solution.Workspace; | ||
var supportsFeatureService = workspace.Services.GetRequiredService<ITextBufferSupportsFeatureService>(); | ||
|
||
var fixesTask = GetCodeFixesAsync( | ||
state, supportsFeatureService, requestedActionCategories, workspace, document, range, | ||
addOperationScope, priority, isBlocking: false, cancellationToken); | ||
var refactoringsTask = GetRefactoringsAsync( | ||
state, supportsFeatureService, requestedActionCategories, workspace, document, selection, | ||
addOperationScope, priority, isBlocking: false, cancellationToken); | ||
|
||
if (priority == CodeActionRequestPriority.High) | ||
{ | ||
// in a high pri scenario, return data as soon as possible so that the user can interact with them. | ||
// this is especially important for state-machine oriented refactorings (like rename) where the user | ||
// should always have access to them effectively synchronously. | ||
var firstTask = await Task.WhenAny(fixesTask, refactoringsTask).ConfigureAwait(false); | ||
var secondTask = firstTask == fixesTask ? refactoringsTask : fixesTask; | ||
|
||
var orderedTasks = new[] { firstTask, secondTask }; | ||
foreach (var task in orderedTasks) | ||
{ | ||
if (task == fixesTask) | ||
{ | ||
var fixes = await fixesTask.ConfigureAwait(false); | ||
foreach (var set in ConvertToSuggestedActionSets(state, selection, fixes, ImmutableArray<UnifiedSuggestedActionSet>.Empty)) | ||
yield return set; | ||
} | ||
else | ||
{ | ||
Contract.ThrowIfFalse(task == refactoringsTask); | ||
|
||
var refactorings = await refactoringsTask.ConfigureAwait(false); | ||
foreach (var set in ConvertToSuggestedActionSets(state, selection, ImmutableArray<UnifiedSuggestedActionSet>.Empty, refactorings)) | ||
yield return set; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
var actionsArray = await Task.WhenAll(fixesTask, refactoringsTask).ConfigureAwait(false); | ||
foreach (var set in ConvertToSuggestedActionSets(state, selection, fixes: actionsArray[0], refactorings: actionsArray[1])) | ||
yield return set; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.