Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't provide CodeActionWithDialog if option service is null #67951

Merged
merged 1 commit into from
Apr 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@ private async Task HandleNonSelectionAsync(
GetExistingMemberInfo(
containingType, out var hasEquals, out var hasGetHashCode);

var globalOptions = document.Project.Solution.Services.GetService<ILegacyGlobalOptionsWorkspaceService>();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The core of the change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Is it possible to leverage #66798 instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I don't know this service exists

Copy link
Member Author

@Cosifne Cosifne Apr 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see it provides reading these values

private static readonly PerLanguageOption2<bool> s_generateOperators = new(

It seems like it has a fall back of CodeCleanUpOptions, but the values used here are not part of it, like
https://sourceroslyn.io/#Microsoft.CodeAnalysis.Workspaces/Options/ILegacyGlobalOptionsWorkspaceService.cs,ebbb686a0c6361c3,references

if (globalOptions == null)
return;

var actions = await CreateActionsAsync(
document, typeDeclaration, containingType, viableMembers, fallbackOptions,
hasEquals, hasGetHashCode, withDialog: true, cancellationToken).ConfigureAwait(false);
hasEquals, hasGetHashCode, withDialog: true, globalOptions, cancellationToken).ConfigureAwait(false);

context.RegisterRefactorings(actions, textSpan);
}
Expand Down Expand Up @@ -190,7 +194,7 @@ public async Task<ImmutableArray<CodeAction>> GenerateEqualsAndGetHashCodeFromMe

return await CreateActionsAsync(
document, typeDeclaration, info.ContainingType, info.SelectedMembers, fallbackOptions,
hasEquals, hasGetHashCode, withDialog: false, cancellationToken).ConfigureAwait(false);
hasEquals, hasGetHashCode, withDialog: false, globalOptions: null, cancellationToken).ConfigureAwait(false);
}
}

Expand All @@ -201,7 +205,7 @@ public async Task<ImmutableArray<CodeAction>> GenerateEqualsAndGetHashCodeFromMe
private async Task<ImmutableArray<CodeAction>> CreateActionsAsync(
Document document, SyntaxNode typeDeclaration, INamedTypeSymbol containingType, ImmutableArray<ISymbol> selectedMembers,
CleanCodeGenerationOptionsProvider fallbackOptions,
bool hasEquals, bool hasGetHashCode, bool withDialog, CancellationToken cancellationToken)
bool hasEquals, bool hasGetHashCode, bool withDialog, ILegacyGlobalOptionsWorkspaceService? globalOptions, CancellationToken cancellationToken)
{
using var _ = ArrayBuilder<Task<CodeAction>>.GetInstance(out var tasks);

Expand All @@ -215,42 +219,50 @@ private async Task<ImmutableArray<CodeAction>> CreateActionsAsync(
// the user would need to bother just generating that member without also
// generating 'Equals' as well.
tasks.Add(CreateCodeActionAsync(
document, typeDeclaration, containingType, selectedMembers, fallbackOptions,
document, typeDeclaration, containingType, selectedMembers, fallbackOptions, globalOptions,
generateEquals: true, generateGetHashCode: false, withDialog, cancellationToken));
tasks.Add(CreateCodeActionAsync(
document, typeDeclaration, containingType, selectedMembers, fallbackOptions,
document, typeDeclaration, containingType, selectedMembers, fallbackOptions, globalOptions,
generateEquals: true, generateGetHashCode: true, withDialog, cancellationToken));
}
else if (!hasEquals)
{
tasks.Add(CreateCodeActionAsync(
document, typeDeclaration, containingType, selectedMembers, fallbackOptions,
document, typeDeclaration, containingType, selectedMembers, fallbackOptions, globalOptions,
generateEquals: true, generateGetHashCode: false, withDialog, cancellationToken));
}
else if (!hasGetHashCode)
{
tasks.Add(CreateCodeActionAsync(
document, typeDeclaration, containingType, selectedMembers, fallbackOptions,
document, typeDeclaration, containingType, selectedMembers, fallbackOptions, globalOptions,
generateEquals: false, generateGetHashCode: true, withDialog, cancellationToken));
}

var codeActions = await Task.WhenAll(tasks).ConfigureAwait(false);
return codeActions.ToImmutableArray();

}

private Task<CodeAction> CreateCodeActionAsync(
Document document, SyntaxNode typeDeclaration, INamedTypeSymbol containingType, ImmutableArray<ISymbol> members,
CleanCodeGenerationOptionsProvider fallbackOptions,
CleanCodeGenerationOptionsProvider fallbackOptions, ILegacyGlobalOptionsWorkspaceService? globalOptions,
bool generateEquals, bool generateGetHashCode, bool withDialog, CancellationToken cancellationToken)
{
return withDialog
? CreateCodeActionWithDialogAsync(document, typeDeclaration, containingType, members, fallbackOptions, generateEquals, generateGetHashCode, cancellationToken)
: CreateCodeActionWithoutDialogAsync(document, typeDeclaration, containingType, members, fallbackOptions, generateEquals, generateGetHashCode, cancellationToken);
if (withDialog)
{
// We can't create dialog code action if globalOptions is null
Contract.ThrowIfNull(globalOptions);
return CreateCodeActionWithDialogAsync(document, typeDeclaration, containingType, members, fallbackOptions, globalOptions, generateEquals, generateGetHashCode, cancellationToken);
}
else
{
return CreateCodeActionWithoutDialogAsync(document, typeDeclaration, containingType, members, fallbackOptions, generateEquals, generateGetHashCode, cancellationToken);
}
}

private async Task<CodeAction> CreateCodeActionWithDialogAsync(
Document document, SyntaxNode typeDeclaration, INamedTypeSymbol containingType, ImmutableArray<ISymbol> members,
CleanCodeGenerationOptionsProvider fallbackOptions,
CleanCodeGenerationOptionsProvider fallbackOptions, ILegacyGlobalOptionsWorkspaceService globalOptions,
bool generateEquals, bool generateGetHashCode, CancellationToken cancellationToken)
{
var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
Expand All @@ -259,7 +271,6 @@ private async Task<CodeAction> CreateCodeActionWithDialogAsync(

if (CanImplementIEquatable(semanticModel, containingType, out var equatableTypeOpt))
{
var globalOptions = document.Project.Solution.Services.GetRequiredService<ILegacyGlobalOptionsWorkspaceService>();
var value = globalOptions.GetGenerateEqualsAndGetHashCodeFromMembersImplementIEquatable(document.Project.Language);

var displayName = equatableTypeOpt.ToDisplayString(new SymbolDisplayFormat(
Expand All @@ -274,7 +285,6 @@ private async Task<CodeAction> CreateCodeActionWithDialogAsync(

if (!HasOperators(containingType))
{
var globalOptions = document.Project.Solution.Services.GetRequiredService<ILegacyGlobalOptionsWorkspaceService>();
var value = globalOptions.GetGenerateEqualsAndGetHashCodeFromMembersGenerateOperators(document.Project.Language);

pickMembersOptions.Add(new PickMembersOption(
Expand Down