Skip to content

Commit

Permalink
Move reading HideAdvancedMembers option up (#58747)
Browse files Browse the repository at this point in the history
* Move reading HideAdvancedMembers option up

* CodeActionOptions
  • Loading branch information
tmat authored Jan 20, 2022
1 parent 305a91c commit 52323cb
Show file tree
Hide file tree
Showing 41 changed files with 321 additions and 207 deletions.
7 changes: 4 additions & 3 deletions src/EditorFeatures/CSharpTest/AddUsing/AddUsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
Expand Down Expand Up @@ -6503,7 +6504,7 @@ static void Main(string[] args)
[WorkItem(1266354, "https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1266354")]
public async Task TestAddUsingsEditorBrowsableAdvancedDifferentProjectOptionOff(TestHost testHost)
{
const string InitialWorkspace = @"
var initialWorkspace = @"
<Workspace>
<Project Language=""Visual Basic"" AssemblyName=""lib"" CommonReferences=""true"">
<Document FilePath=""lib.vb"">
Expand All @@ -6529,8 +6530,8 @@ static void Main(string[] args)
</Project>
</Workspace>";

await TestMissingAsync(InitialWorkspace, new TestParameters(
options: Option(CompletionOptions.Metadata.HideAdvancedMembers, true),
await TestMissingAsync(initialWorkspace, new TestParameters(
codeActionOptions: CodeActionOptions.Default with { HideAdvancedMembers = true },
testHost: testHost));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,8 @@ void Method()
var diagnostics = await diagnosticService.GetDiagnosticsForSpanAsync(document, span);
Assert.Equal(2, diagnostics.Where(d => d.Id == "CS0219").Count());

var allFixes = (await fixService.GetFixesAsync(document, span, cancellationToken: CancellationToken.None))
var options = CodeActionOptions.Default;
var allFixes = (await fixService.GetFixesAsync(document, span, options, CancellationToken.None))
.SelectMany(fixCollection => fixCollection.Fixes);

var cs0219Fixes = allFixes.Where(fix => fix.PrimaryDiagnostic.Id == "CS0219").ToArray();
Expand Down
5 changes: 4 additions & 1 deletion src/EditorFeatures/CSharpTest/Formatting/CodeCleanupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.AddImports;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeCleanup;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
Expand Down Expand Up @@ -561,6 +562,8 @@ private protected static async Task AssertCodeCleanupResult(string expected, str
{
using var workspace = TestWorkspace.CreateCSharp(code, composition: EditorTestCompositions.EditorFeaturesWpf);

var options = CodeActionOptions.Default;

var solution = workspace.CurrentSolution
.WithOptions(workspace.Options
.WithChangedOption(GenerationOptions.PlaceSystemNamespaceFirst, LanguageNames.CSharp, systemUsingsFirst)
Expand All @@ -586,7 +589,7 @@ private protected static async Task AssertCodeCleanupResult(string expected, str
var enabledDiagnostics = codeCleanupService.GetAllDiagnostics();

var newDoc = await codeCleanupService.CleanupAsync(
document, enabledDiagnostics, new ProgressTracker(), CancellationToken.None);
document, enabledDiagnostics, new ProgressTracker(), options, CancellationToken.None);

var actual = await newDoc.GetTextAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1618,7 +1618,7 @@ static void Main(string[] args)
[WorkItem(54544, "https://github.com/dotnet/roslyn/issues/54544")]
public async Task TestAddUsingsEditorBrowsableAdvancedDifferentProjectOptionOff()
{
const string InitialWorkspace = @"
var initialWorkspace = @"
<Workspace>
<Project Language=""Visual Basic"" AssemblyName=""lib"" CommonReferences=""true"">
<Document FilePath=""lib.vb"">
Expand All @@ -1644,8 +1644,8 @@ static void Main(string[] args)
</Project>
</Workspace>";

await TestMissingAsync(InitialWorkspace, new TestParameters(
options: Option(CompletionOptions.Metadata.HideAdvancedMembers, true)));
await TestMissingAsync(initialWorkspace, new TestParameters(
codeActionOptions: CodeActionOptions.Default with { HideAdvancedMembers = true }));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,14 @@ private async IAsyncEnumerable<SuggestedActionSet> GetCodeFixesAndRefactoringsAs
var workspace = document.Project.Solution.Workspace;
var supportsFeatureService = workspace.Services.GetRequiredService<ITextBufferSupportsFeatureService>();

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

var fixesTask = GetCodeFixesAsync(
state, supportsFeatureService, requestedActionCategories, workspace, document, range,
addOperationScope, priority, isBlocking: false, cancellationToken);
addOperationScope, priority, options, cancellationToken);
var refactoringsTask = GetRefactoringsAsync(
state, supportsFeatureService, requestedActionCategories, GlobalOptions, workspace, document, selection,
addOperationScope, priority, isBlocking: false, cancellationToken);
addOperationScope, priority, options, cancellationToken);

await Task.WhenAll(fixesTask, refactoringsTask).ConfigureAwait(false);

Expand Down
22 changes: 14 additions & 8 deletions src/EditorFeatures/Core.Wpf/Suggestions/SuggestedActionsSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,18 @@ public bool TryGetTelemetryId(out Guid telemetryId)
Func<string, IDisposable?> addOperationScope =
description => operationContext?.AddScope(allowCancellation: true, string.Format(EditorFeaturesResources.Gathering_Suggestions_0, description));

var options = CodeActionOptionsFactory.GetCodeActionOptions(document.Project, isBlocking: true);

// We convert the code fixes and refactorings to UnifiedSuggestedActionSets instead of
// SuggestedActionSets so that we can share logic between local Roslyn and LSP.
var fixesTask = GetCodeFixesAsync(
state, supportsFeatureService, requestedActionCategories, workspace, document, range,
addOperationScope, CodeActionRequestPriority.None,
isBlocking: true, cancellationToken);
options, cancellationToken);

var refactoringsTask = GetRefactoringsAsync(
state, supportsFeatureService, requestedActionCategories, GlobalOptions, workspace, document, selection,
addOperationScope, CodeActionRequestPriority.None, isBlocking: true, cancellationToken);
addOperationScope, CodeActionRequestPriority.None, options, cancellationToken);

Task.WhenAll(fixesTask, refactoringsTask).WaitAndGetResult(cancellationToken);

Expand Down Expand Up @@ -263,7 +266,7 @@ protected static Task<ImmutableArray<UnifiedSuggestedActionSet>> GetCodeFixesAsy
SnapshotSpan range,
Func<string, IDisposable?> addOperationScope,
CodeActionRequestPriority priority,
bool isBlocking,
CodeActionOptions options,
CancellationToken cancellationToken)
{
if (state.Target.Owner._codeFixService == null ||
Expand All @@ -275,7 +278,7 @@ protected static Task<ImmutableArray<UnifiedSuggestedActionSet>> GetCodeFixesAsy

return UnifiedSuggestedActionsSource.GetFilterAndOrderCodeFixesAsync(
workspace, state.Target.Owner._codeFixService, document, range.Span.ToTextSpan(),
priority, isBlocking, addOperationScope, cancellationToken).AsTask();
priority, options, addOperationScope, cancellationToken).AsTask();
}

private static string GetFixCategory(DiagnosticSeverity severity)
Expand Down Expand Up @@ -303,7 +306,7 @@ protected static Task<ImmutableArray<UnifiedSuggestedActionSet>> GetRefactorings
TextSpan? selection,
Func<string, IDisposable?> addOperationScope,
CodeActionRequestPriority priority,
bool isBlocking,
CodeActionOptions options,
CancellationToken cancellationToken)
{
if (!selection.HasValue)
Expand Down Expand Up @@ -332,7 +335,7 @@ protected static Task<ImmutableArray<UnifiedSuggestedActionSet>> GetRefactorings
var filterOutsideSelection = !requestedActionCategories.Contains(PredefinedSuggestedActionCategoryNames.Refactoring);

return UnifiedSuggestedActionsSource.GetFilterAndOrderCodeRefactoringsAsync(
workspace, state.Target.Owner._codeRefactoringService, document, selection.Value, priority, isBlocking,
workspace, state.Target.Owner._codeRefactoringService, document, selection.Value, priority, options,
addOperationScope, filterOutsideSelection, cancellationToken);
}

Expand Down Expand Up @@ -439,6 +442,7 @@ await InvokeBelowInputPriorityAsync(() =>
private async Task<string?> TryGetRefactoringSuggestedActionCategoryAsync(
Document document,
TextSpan? selection,
CodeActionOptions options,
CancellationToken cancellationToken)
{
using var state = _state.TryAddReference();
Expand All @@ -457,7 +461,7 @@ await InvokeBelowInputPriorityAsync(() =>
state.Target.SubjectBuffer.SupportsRefactorings())
{
if (await state.Target.Owner._codeRefactoringService.HasRefactoringsAsync(
document, selection.Value, cancellationToken).ConfigureAwait(false))
document, selection.Value, options, cancellationToken).ConfigureAwait(false))
{
return PredefinedSuggestedActionCategoryNames.Refactoring;
}
Expand Down Expand Up @@ -610,6 +614,8 @@ private void OnSuggestedActionsChanged(Workspace currentWorkspace, DocumentId? c
if (document == null)
return null;

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

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

Expand All @@ -621,7 +627,7 @@ private void OnSuggestedActionsChanged(Workspace currentWorkspace, DocumentId? c
if (selection != null)
{
refactoringTask = Task.Run(
() => TryGetRefactoringSuggestedActionCategoryAsync(document, selection, linkedToken), linkedToken);
() => TryGetRefactoringSuggestedActionCategoryAsync(document, selection, options, linkedToken), linkedToken);
}

// If we happen to get the result of the error task before the refactoring task,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Diagnostics;
using Microsoft.CodeAnalysis.AddMissingImports;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Options;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
Expand Down Expand Up @@ -114,6 +115,9 @@ private void ExecuteCommandWorker(
return;
}

var options = new AddMissingImportsOptions(
HideAdvancedMembers: document.Project.Solution.Options.GetOption(CompletionOptions.Metadata.HideAdvancedMembers, document.Project.Language));

using var _ = executionContext.OperationContext.AddScope(allowCancellation: true, DialogText);
var cancellationToken = executionContext.OperationContext.UserCancellationToken;

Expand All @@ -123,7 +127,7 @@ private void ExecuteCommandWorker(

var addMissingImportsService = document.GetRequiredLanguageService<IAddMissingImportsFeatureService>();
#pragma warning disable VSTHRD102 // Implement internal logic asynchronously
var updatedDocument = _threadingContext.JoinableTaskFactory.Run(() => addMissingImportsService.AddMissingImportsAsync(document, textSpan, cancellationToken));
var updatedDocument = _threadingContext.JoinableTaskFactory.Run(() => addMissingImportsService.AddMissingImportsAsync(document, textSpan, options, cancellationToken));
#pragma warning restore VSTHRD102 // Implement internal logic asynchronously
if (updatedDocument is null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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 Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.SymbolSearch;

namespace Microsoft.CodeAnalysis.CodeActions
{
internal static class CodeActionOptionsFactory
{
internal static CodeActionOptions GetCodeActionOptions(Project project, bool isBlocking)
{
var options = project.Solution.Options;
var language = project.Language;

return new(
IsBlocking: isBlocking,
SearchReferenceAssemblies: options.GetOption(SymbolSearchOptions.SuggestForTypesInReferenceAssemblies, language),
HideAdvancedMembers: options.GetOption(CompletionOptions.Metadata.HideAdvancedMembers, language));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,15 @@ private static async ValueTask<ImmutableArray<UnifiedSuggestedActionSet>> GetAct
{
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var textSpan = ProtocolConversions.RangeToTextSpan(selection, text);
var options = CodeActionOptionsFactory.GetCodeActionOptions(document.Project, isBlocking: false);

var codeFixes = await UnifiedSuggestedActionsSource.GetFilterAndOrderCodeFixesAsync(
document.Project.Solution.Workspace, codeFixService, document, textSpan,
CodeActionRequestPriority.None,
isBlocking: false, addOperationScope: _ => null, cancellationToken).ConfigureAwait(false);
options, addOperationScope: _ => null, cancellationToken).ConfigureAwait(false);

var codeRefactorings = await UnifiedSuggestedActionsSource.GetFilterAndOrderCodeRefactoringsAsync(
document.Project.Solution.Workspace, codeRefactoringService, document, textSpan, CodeActionRequestPriority.None, isBlocking: false,
document.Project.Solution.Workspace, codeRefactoringService, document, textSpan, CodeActionRequestPriority.None, options,
addOperationScope: _ => null, filterOutsideSelection: false, cancellationToken).ConfigureAwait(false);

var actionSets = UnifiedSuggestedActionsSource.FilterAndOrderActionSets(
Expand Down
Loading

0 comments on commit 52323cb

Please sign in to comment.