Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Nov 26, 2024
1 parent 4046a7a commit 5f206da
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,32 +95,25 @@ public async Task<Document> ImplementInterfaceAsync(
return await generator.ImplementInterfaceAsync(cancellationToken).ConfigureAwait(false);
}

public async Task<ISymbol> ExplicitlyImplementSingleInterfaceMemberAsync(
public async Task<ISymbol> ImplementInterfaceMemberAsync(
Document document,
IImplementInterfaceInfo info,
ISymbol member,
ISymbol interfaceMember,
ImplementTypeOptions options,
ImplementInterfaceConfiguration configuration,
CancellationToken cancellationToken)
{
var configuration = new ImplementInterfaceConfiguration()
{
Abstractly = false,
Explicitly = true,
OnlyRemaining = false,
ImplementDisposePattern = false,
ThroughMember = null,
};
var generator = new ImplementInterfaceGenerator(
this, document, info, options, configuration);
var implementedMembers = await generator.GenerateExplicitlyImplementedMembersAsync(member, options.PropertyGenerationBehavior, cancellationToken).ConfigureAwait(false);
var implementedMembers = await generator.GenerateExplicitlyImplementedMembersAsync(interfaceMember, options.PropertyGenerationBehavior, cancellationToken).ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();

var singleImplemented = implementedMembers[0];
Contract.ThrowIfNull(singleImplemented);

// Since non-indexer properties are the only symbols that get their implementing accessor symbols returned,
// we have to process the created symbols and reduce to the single property wherein the accessors are contained
if (member is IPropertySymbol { IsIndexer: false })
if (interfaceMember is IPropertySymbol { IsIndexer: false })
{
IPropertySymbol? commonContainer = null;
foreach (var implementedMember in implementedMembers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// 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.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
Expand Down Expand Up @@ -32,10 +31,16 @@ Task<Document> ImplementInterfaceAsync(
ImplementInterfaceConfiguration configuration,
CancellationToken cancellationToken);

Task<ISymbol> ExplicitlyImplementSingleInterfaceMemberAsync(
/// <summary>
/// Produces the symbol that implements that provided <paramref name="interfaceMember"/> within the corresponding
/// <see cref="IImplementInterfaceInfo.ClassOrStructType"/>, based on the provided <paramref name="options"/> and
/// <paramref name="configuration"/>.
/// </summary>
Task<ISymbol> ImplementInterfaceMemberAsync(
Document document,
IImplementInterfaceInfo info,
ISymbol member,
ISymbol interfaceMember,
ImplementTypeOptions options,
ImplementInterfaceConfiguration configuration,
CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ protected override async Task<ISymbol> GenerateMemberAsync(
Contract.ThrowIfNull(state);

var options = await newDocument.GetImplementTypeOptionsAsync(cancellationToken).ConfigureAwait(false);
var implementedSymbol = await implementInterfaceService.ExplicitlyImplementSingleInterfaceMemberAsync(
newDocument, state, member, options, cancellationToken).ConfigureAwait(false);
var configuration = new ImplementInterfaceConfiguration()
{
Explicitly = true,
};

return implementedSymbol;
return await implementInterfaceService.ImplementInterfaceMemberAsync(
newDocument, state, member, options, configuration, cancellationToken).ConfigureAwait(false);
}

private static async Task<SyntaxNode?> GetInterfaceNodeInCompletionAsync(
Expand All @@ -79,31 +82,24 @@ protected override async Task<ISymbol> GenerateMemberAsync(
var node = syntaxTree.FindNode(item.Span, findInTrivia: false, getInnermostNodeForTie: true, cancellationToken);
var primaryTypeDeclaration = node.GetAncestor<BaseTypeDeclarationSyntax>();
Contract.ThrowIfNull(primaryTypeDeclaration, "Expected a BaseTypeDeclarationSyntax to contain the implemented interface member");

var interfaceNode = NodeInDeclaration(primaryTypeDeclaration);
if (interfaceNode != null)
{
return interfaceNode;
}

// If we have not found the target interface in this declaration, it is possible we have other partial declarations
// whose base lists contain our interface
var declaringSyntaxReferences = baseMemberInterfaceType.DeclaringSyntaxReferences;
foreach (var declaring in declaringSyntaxReferences)
foreach (var syntaxReference in declaringSyntaxReferences)
{
var declaringSyntax = await declaring.GetSyntaxAsync(cancellationToken).ConfigureAwait(false);
if (declaringSyntax is null)
{
continue;
}
var declaringSyntax = await syntaxReference.GetSyntaxAsync(cancellationToken).ConfigureAwait(false);

// We have already evaluated the primary type declaration
if (declaringSyntax == primaryTypeDeclaration)
continue;

if (declaringSyntax is not BaseTypeDeclarationSyntax baseTypeDeclarationSyntax)
{
continue;
}

interfaceNode = NodeInDeclaration(baseTypeDeclarationSyntax);
if (interfaceNode != null)
Expand Down

0 comments on commit 5f206da

Please sign in to comment.