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

Query the public members only once #61

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
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 @@ -25,11 +25,11 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<EnableNETAnalyzers>True</EnableNETAnalyzers>
<AnalysisLevel>latest-Recommended</AnalysisLevel>
<Version>5.0.1</Version>
<Version>5.0.2</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>1701;1702;NU5128</NoWarn>
<PackageReleaseNotes>Use ForAttributeWithMetadataName to improve performance</PackageReleaseNotes>
<PackageReleaseNotes>Query members only once. Small optimization.</PackageReleaseNotes>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DebugGenerator|AnyCPU'">
Expand Down
44 changes: 17 additions & 27 deletions AutomaticInterface/AutomaticInterface/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,17 @@ is not ClassDeclarationSyntax classSyntax

interfaceGenerator.AddClassDocumentation(GetDocumentationForClass(classSyntax));
interfaceGenerator.AddGeneric(GetGeneric(classSyntax));
AddPropertiesToInterface(typeSymbol, interfaceGenerator);
AddMethodsToInterface(typeSymbol, interfaceGenerator);
AddEventsToInterface(typeSymbol, interfaceGenerator);

var members = typeSymbol
.GetAllMembers()
.Where(x => x.DeclaredAccessibility == Accessibility.Public)
.Where(x => !x.IsStatic)
.Where(x => !HasIgnoreAttribute(x))
.ToList();

AddPropertiesToInterface(members, interfaceGenerator);
AddMethodsToInterface(members, interfaceGenerator);
AddEventsToInterface(members, interfaceGenerator);

var generatedCode = interfaceGenerator.Build();

Expand All @@ -74,19 +82,12 @@ private static string GetNameSpace(ISymbol typeSymbol)
: customNs!;
}

private static void AddMethodsToInterface(
ITypeSymbol classSymbol,
InterfaceBuilder codeGenerator
)
private static void AddMethodsToInterface(List<ISymbol> members, InterfaceBuilder codeGenerator)
{
classSymbol
.GetAllMembers()
members
.Where(x => x.Kind == SymbolKind.Method)
.OfType<IMethodSymbol>()
.Where(x => x.DeclaredAccessibility == Accessibility.Public)
.Where(x => x.MethodKind == MethodKind.Ordinary)
.Where(x => !x.IsStatic)
.Where(x => !HasIgnoreAttribute(x))
.Where(x => x.ContainingType.Name != nameof(Object))
.Where(x => !HasIgnoreAttribute(x))
.GroupBy(x => x.ToDisplayString(MethodSignatureDisplayFormat))
Expand Down Expand Up @@ -172,18 +173,11 @@ private static bool IsNullable(ITypeSymbol typeSymbol)
return false;
}

private static void AddEventsToInterface(
ITypeSymbol classSymbol,
InterfaceBuilder codeGenerator
)
private static void AddEventsToInterface(List<ISymbol> members, InterfaceBuilder codeGenerator)
{
classSymbol
.GetAllMembers()
members
.Where(x => x.Kind == SymbolKind.Event)
.OfType<IEventSymbol>()
.Where(x => x.DeclaredAccessibility == Accessibility.Public)
.Where(x => !x.IsStatic)
.Where(x => !HasIgnoreAttribute(x))
.GroupBy(x => x.ToDisplayString(MethodSignatureDisplayFormat))
.Select(g => g.First())
.ToList()
Expand Down Expand Up @@ -253,18 +247,14 @@ private static string GetRefKind(IParameterSymbol x)
}

private static void AddPropertiesToInterface(
ITypeSymbol classSymbol,
List<ISymbol> members,
InterfaceBuilder interfaceGenerator
)
{
classSymbol
.GetAllMembers()
members
.Where(x => x.Kind == SymbolKind.Property)
.OfType<IPropertySymbol>()
.Where(x => x.DeclaredAccessibility == Accessibility.Public)
.Where(x => !x.IsStatic)
.Where(x => !x.IsIndexer)
.Where(x => !HasIgnoreAttribute(x))
.GroupBy(x => x.Name)
.Select(g => g.First())
.ToList()
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,14 @@ Should be simply a build and run Tests

## Changelog

### 5.0.0
### 5.0.2

- Query members only once. Small optimization. Thanks, @crwsolutions!

### 5.0.1

- Sync DemoClass and actual generated code with README. Thanks, @crwsolutions!
- Removed manual attribute in TestNuget project. Thanks, @crwsolutions!
- Removed manual attribute in TestNuget project. Thanks, @crwsolutions!

### 5.0.0

Expand Down
Loading