Skip to content

Commit

Permalink
Make helepr
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Nov 26, 2024
1 parent 532de1d commit f10eaf5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,28 @@ internal sealed class State(
SyntaxNode contextNode,
INamedTypeSymbol classOrStructType,
ImmutableArray<INamedTypeSymbol> interfaceTypes,
SemanticModel model) : IImplementInterfaceInfo
SemanticModel model)
{
public SyntaxNode ContextNode { get; } = contextNode;
public INamedTypeSymbol ClassOrStructType { get; } = classOrStructType;
public ImmutableArray<INamedTypeSymbol> InterfaceTypes { get; } = interfaceTypes;
public ImplementInterfaceInfo Info { get; private set; } = new()
{
ClassOrStructType = classOrStructType,
ContextNode = contextNode,
InterfaceTypes = interfaceTypes,
};

public SyntaxNode ContextNode => Info.ContextNode;
public INamedTypeSymbol ClassOrStructType => Info.ClassOrStructType;
public ImmutableArray<INamedTypeSymbol> InterfaceTypes => Info.InterfaceTypes;
public SemanticModel Model { get; } = model;

public readonly Document Document = document;

// The members that are not implemented at all.
public ImmutableArray<(INamedTypeSymbol type, ImmutableArray<ISymbol> members)> MembersWithoutExplicitOrImplicitImplementationWhichCanBeImplicitlyImplemented { get; private set; } = [];
public ImmutableArray<(INamedTypeSymbol type, ImmutableArray<ISymbol> members)> MembersWithoutExplicitOrImplicitImplementation { get; private set; } = [];
public ImmutableArray<(INamedTypeSymbol type, ImmutableArray<ISymbol> members)> MembersWithoutExplicitOrImplicitImplementationWhichCanBeImplicitlyImplemented => Info.MembersWithoutExplicitOrImplicitImplementationWhichCanBeImplicitlyImplemented;
public ImmutableArray<(INamedTypeSymbol type, ImmutableArray<ISymbol> members)> MembersWithoutExplicitOrImplicitImplementation => Info.MembersWithoutExplicitOrImplicitImplementation;

// The members that have no explicit implementation.
public ImmutableArray<(INamedTypeSymbol type, ImmutableArray<ISymbol> members)> MembersWithoutExplicitImplementation { get; private set; } = [];
public ImmutableArray<(INamedTypeSymbol type, ImmutableArray<ISymbol> members)> MembersWithoutExplicitImplementation => Info.MembersWithoutExplicitImplementation;

public static State? Generate(
AbstractImplementInterfaceService service,
Expand All @@ -54,14 +61,23 @@ internal sealed class State(

if (service.CanImplementImplicitly)
{
state.MembersWithoutExplicitOrImplicitImplementationWhichCanBeImplicitlyImplemented = state.ClassOrStructType.GetAllUnimplementedMembers(
interfaceTypes, includeMembersRequiringExplicitImplementation: false, cancellationToken);
state.Info = state.Info with
{
MembersWithoutExplicitOrImplicitImplementationWhichCanBeImplicitlyImplemented = state.ClassOrStructType.GetAllUnimplementedMembers(
interfaceTypes, includeMembersRequiringExplicitImplementation: false, cancellationToken)
};

state.MembersWithoutExplicitOrImplicitImplementation = state.ClassOrStructType.GetAllUnimplementedMembers(
interfaceTypes, includeMembersRequiringExplicitImplementation: true, cancellationToken);
state.Info = state.Info with
{
MembersWithoutExplicitOrImplicitImplementation = state.ClassOrStructType.GetAllUnimplementedMembers(
interfaceTypes, includeMembersRequiringExplicitImplementation: true, cancellationToken)
};

state.MembersWithoutExplicitImplementation = state.ClassOrStructType.GetAllUnimplementedExplicitMembers(
interfaceTypes, cancellationToken);
state.Info = state.Info with
{
MembersWithoutExplicitImplementation = state.ClassOrStructType.GetAllUnimplementedExplicitMembers(
interfaceTypes, cancellationToken)
};

var allMembersImplemented = state.MembersWithoutExplicitOrImplicitImplementationWhichCanBeImplicitlyImplemented.Length == 0;
var allMembersImplementedExplicitly = state.MembersWithoutExplicitImplementation.Length == 0;
Expand All @@ -71,8 +87,11 @@ internal sealed class State(
else
{
// We put the members in this bucket so that the code fix title is "Implement Interface"
state.MembersWithoutExplicitOrImplicitImplementationWhichCanBeImplicitlyImplemented = state.ClassOrStructType.GetAllUnimplementedExplicitMembers(
interfaceTypes, cancellationToken);
state.Info = state.Info with
{
MembersWithoutExplicitOrImplicitImplementationWhichCanBeImplicitlyImplemented = state.ClassOrStructType.GetAllUnimplementedExplicitMembers(
interfaceTypes, cancellationToken)
};

var allMembersImplemented = state.MembersWithoutExplicitOrImplicitImplementationWhichCanBeImplicitlyImplemented.Length == 0;
return !allMembersImplemented ? state : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@

namespace Microsoft.CodeAnalysis.ImplementInterface;

internal interface IImplementInterfaceInfo
internal sealed record ImplementInterfaceInfo
{
/// <summary>
/// The class or struct that is implementing the interface.
/// </summary>
INamedTypeSymbol ClassOrStructType { get; }
public required INamedTypeSymbol ClassOrStructType { get; init; }

/// <summary>
/// The specific declaration node for <see cref="ClassOrStructType"/> that the interface implementations should be
/// added to.
/// </summary>
SyntaxNode ContextNode { get; }
public required SyntaxNode ContextNode { get; init; }

/// <summary>
/// Set of interfaces to implement. Normally a single interface (when a user invokes the code action on a single
/// entry in the interface-list for a type). However, it may be multiple in the VB case where a user presses
/// 'enter' at the end of the interfaces list, where we'll implement all the missing members for all listed interfaces.
/// </summary>
ImmutableArray<INamedTypeSymbol> InterfaceTypes { get; }
public ImmutableArray<INamedTypeSymbol> InterfaceTypes { get; init; }

ImmutableArray<(INamedTypeSymbol type, ImmutableArray<ISymbol> members)> MembersWithoutExplicitOrImplicitImplementationWhichCanBeImplicitlyImplemented { get; }
ImmutableArray<(INamedTypeSymbol type, ImmutableArray<ISymbol> members)> MembersWithoutExplicitOrImplicitImplementation { get; }
ImmutableArray<(INamedTypeSymbol type, ImmutableArray<ISymbol> members)> MembersWithoutExplicitImplementation { get; }
public ImmutableArray<(INamedTypeSymbol type, ImmutableArray<ISymbol> members)> MembersWithoutExplicitOrImplicitImplementationWhichCanBeImplicitlyImplemented { get; init; }
public ImmutableArray<(INamedTypeSymbol type, ImmutableArray<ISymbol> members)> MembersWithoutExplicitOrImplicitImplementation { get; init; }
public ImmutableArray<(INamedTypeSymbol type, ImmutableArray<ISymbol> members)> MembersWithoutExplicitImplementation { get; init; }
}

0 comments on commit f10eaf5

Please sign in to comment.