Skip to content

Commit

Permalink
merged
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianSauer committed Nov 29, 2024
2 parents 2920be4 + 434f310 commit 9d66cad
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 89 deletions.
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.3</Version>
<Version>5.1.0</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>1701;1702;NU5128</NoWarn>
<PackageReleaseNotes>Query members only once. Small optimization.</PackageReleaseNotes>
<PackageReleaseNotes>mproves inheritdoc so that developer documentation is properly referenced on the autogenerated interfaces</PackageReleaseNotes>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DebugGenerator|AnyCPU'">
Expand Down
41 changes: 22 additions & 19 deletions AutomaticInterface/AutomaticInterface/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace AutomaticInterface;

public static class Builder
{
private const string InheritDoc = "/// <inheritdoc />"; // we use inherit doc because that should be able to fetch documentation from base classes.
private static string InheritDoc(ISymbol source) =>
$"/// <inheritdoc cref=\"{source.ToDisplayString().Replace('<', '{').Replace('>', '}')}\" />"; // we use inherit doc because that should be able to fetch documentation from base classes.

private static readonly SymbolDisplayFormat FullyQualifiedDisplayFormat =
new(
Expand Down Expand Up @@ -62,15 +63,19 @@ private static string GetNameSpace(ISymbol typeSymbol)
{
var generationAttribute = typeSymbol
.GetAttributes()
.FirstOrDefault(x =>
x.AttributeClass != null
&& x.AttributeClass.Name.Contains(AutomaticInterfaceGenerator.DefaultAttributeName)
.FirstOrDefault(
x =>
x.AttributeClass != null
&& x.AttributeClass
.Name
.Contains(AutomaticInterfaceGenerator.DefaultAttributeName)
);

if (generationAttribute == null)
{
return typeSymbol.ContainingNamespace.ToDisplayString();
}

var customNs = generationAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();

return string.IsNullOrWhiteSpace(customNs)
Expand All @@ -89,10 +94,7 @@ private static void AddMethodsToInterface(List<ISymbol> members, InterfaceBuilde
.GroupBy(x => x.ToDisplayString(FullyQualifiedDisplayFormat))
.Select(g => g.First())
.ToList()
.ForEach(method =>
{
AddMethod(codeGenerator, method);
});
.ForEach(method => AddMethod(codeGenerator, method));
}

private static void AddMethod(InterfaceBuilder codeGenerator, IMethodSymbol method)
Expand All @@ -117,7 +119,7 @@ private static void AddMethod(InterfaceBuilder codeGenerator, IMethodSymbol meth
codeGenerator.AddMethodToInterface(
name,
returnType.ToDisplayString(FullyQualifiedDisplayFormat),
InheritDoc,
InheritDoc(method),
paramResult,
typedArgs
);
Expand Down Expand Up @@ -190,7 +192,7 @@ private static void AddEventsToInterface(List<ISymbol> members, InterfaceBuilder
codeGenerator.AddEventToInterface(
name,
type.ToDisplayString(FullyQualifiedDisplayFormat),
InheritDoc
InheritDoc(evt)
);
});
}
Expand Down Expand Up @@ -219,7 +221,7 @@ private static string GetMethodOptionalValue(IParameterSymbol x)
null when x.Type.IsValueType
=> $" = default({x.Type.ToDisplayString(FullyQualifiedDisplayFormat)})",
null => " = null",
_ => $" = {x.ExplicitDefaultValue}"
_ => $" = {x.ExplicitDefaultValue}",
};
}

Expand Down Expand Up @@ -274,7 +276,7 @@ InterfaceBuilder interfaceGenerator
hasGet,
hasSet,
isRef,
InheritDoc
InheritDoc(prop)
);
});
}
Expand All @@ -289,18 +291,19 @@ private static PropertySetKind GetSetKind(IMethodSymbol? setMethodSymbol)
_
=> setMethodSymbol is { DeclaredAccessibility: Accessibility.Public }
? PropertySetKind.Always
: PropertySetKind.NoSet
: PropertySetKind.NoSet,
};
}

private static bool HasIgnoreAttribute(ISymbol x)
{
return x.GetAttributes()
.Any(a =>
a.AttributeClass != null
&& a.AttributeClass.Name.Contains(
AutomaticInterfaceGenerator.IgnoreAutomaticInterfaceAttributeName
)
.Any(
a =>
a.AttributeClass != null
&& a.AttributeClass
.Name
.Contains(AutomaticInterfaceGenerator.IgnoreAutomaticInterfaceAttributeName)
);
}

Expand All @@ -317,7 +320,7 @@ private static string GetDocumentationForClass(CSharpSyntaxNode classSyntax)
SyntaxKind.DocumentationCommentExteriorTrivia,
SyntaxKind.EndOfDocumentationCommentToken,
SyntaxKind.MultiLineDocumentationCommentTrivia,
SyntaxKind.SingleLineDocumentationCommentTrivia
SyntaxKind.SingleLineDocumentationCommentTrivia,
];

var trivia = classSyntax
Expand Down
2 changes: 1 addition & 1 deletion AutomaticInterface/AutomaticInterface/InterfaceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private static string GetSet(PropertySetKind propSetKind)
PropertySetKind.NoSet => string.Empty,
PropertySetKind.Always => "set; ",
PropertySetKind.Init => "init; ",
_ => throw new ArgumentOutOfRangeException(nameof(propSetKind), propSetKind, null)
_ => throw new ArgumentOutOfRangeException(nameof(propSetKind), propSetKind, null),
};
}

Expand Down
11 changes: 11 additions & 0 deletions AutomaticInterface/AutomaticInterfaceExample/DemoClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ private string BMethod(string x, string y) // ignored because not public
return x + y;
}

/// <summary>
/// CMethod allows operations with multiple generic type parameters and string inputs.
/// </summary>
/// <typeparam name="T">The first generic type parameter which must be a class.</typeparam>
/// <typeparam name="T1">The second generic type parameter which must be a structure.</typeparam>
/// <typeparam name="T2">The third generic type parameter.</typeparam>
/// <typeparam name="T3">The fourth generic type parameter which must be derived from DemoClass.</typeparam>
/// <typeparam name="T4">The fifth generic type parameter which must implement IDemoClass.</typeparam>
/// <param name="x">The optional first string input parameter.</param>
/// <param name="y">The second string input parameter.</param>
/// <return>Returns a string result.</return>
public string CMethod<T, T1, T2, T3, T4>(string? x, string y) // included
where T : class
where T1 : struct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ namespace AutomaticInterfaceExample
)]
public class DemoClass2 : IDemoClass2
{
/// <summary>
/// This is a test method
/// </summary>
public void Test() { }
}
}
3 changes: 3 additions & 0 deletions AutomaticInterface/AutomaticInterfaceExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ static void Main(string[] args)

IDemoClass demoInterface = demo;
Console.WriteLine(demoInterface.AMethod("A", "B"));
Console.WriteLine(
demoInterface.CMethod<string, int, uint, DemoClass, DemoClass>("A", "B")
);
Console.WriteLine("Hello World!");
}
}
Expand Down
22 changes: 11 additions & 11 deletions AutomaticInterface/Tests/GeneratorTests.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.Hello" />
string Hello { get; }
}
Expand Down Expand Up @@ -85,7 +85,7 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.Hello" />
string Hello { get; }
}
Expand Down Expand Up @@ -139,7 +139,7 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.NullableProperty" />
string? NullableProperty { get; set; }
}
Expand Down Expand Up @@ -195,7 +195,7 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.NullableProperty" />
global::System.Threading.Tasks.Task<string?> NullableProperty { get; set; }
}
Expand Down Expand Up @@ -243,7 +243,7 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface ISecondClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.SecondClass.AProperty" />
int AProperty { get; set; }
}
Expand Down Expand Up @@ -288,7 +288,7 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.SomeProperty" />
string SomeProperty { get; set; }
}
Expand Down Expand Up @@ -334,7 +334,7 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.SomeProperty" />
string SomeProperty { get; set; }
}
Expand Down Expand Up @@ -377,7 +377,7 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.Hello" />
string Hello { get; set; }
}
Expand Down Expand Up @@ -421,7 +421,7 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.Hello" />
string Hello { set; }
}
Expand Down Expand Up @@ -465,7 +465,7 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.Hello" />
string Hello { get; }
}
Expand Down Expand Up @@ -508,7 +508,7 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.Hello" />
string Hello { get; init; }
}
Expand Down
16 changes: 8 additions & 8 deletions AutomaticInterface/Tests/GeneratorTests.TypeResolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.Hello" />
string Hello { get; set; }
}
Expand Down Expand Up @@ -77,7 +77,7 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.Hello" />
string Hello { get; set; }
}
Expand Down Expand Up @@ -120,7 +120,7 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.Hello" />
global::System.Threading.Tasks.Task Hello { get; set; }
}
Expand Down Expand Up @@ -169,10 +169,10 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IModelManager
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.ModelManager.GetModel1()" />
global::AutomaticInterfaceExample.Models1.Model GetModel1();
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.ModelManager.GetModel2()" />
global::AutomaticInterfaceExample.Models2.Model GetModel2();
}
Expand Down Expand Up @@ -222,7 +222,7 @@ namespace RootNamespace.ModelManager
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IModelManager
{
/// <inheritdoc />
/// <inheritdoc cref="RootNamespace.ModelManager.ModelManager.GetModel()" />
global::RootNamespace.Models.Model GetModel();
}
Expand Down Expand Up @@ -268,7 +268,7 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.GetTask()" />
global::System.Threading.Tasks.Task GetTask();
}
Expand Down Expand Up @@ -314,7 +314,7 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.GetClass()" />
global::GlobalNamespace.AClass GetClass();
}
Expand Down
Loading

0 comments on commit 9d66cad

Please sign in to comment.