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

Fixes #57: Improves inheritdoc so that developer documentation is properly referenced on the autogenerated interfaces #63

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 10 additions & 11 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 MethodSignatureDisplayFormat =
new(
Expand Down Expand Up @@ -67,6 +68,7 @@ private static string GetNameSpace(ISymbol typeSymbol)
{
return typeSymbol.ContainingNamespace.ToDisplayString();
}

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

return string.IsNullOrWhiteSpace(customNs)
Expand All @@ -92,10 +94,7 @@ InterfaceBuilder codeGenerator
.GroupBy(x => x.ToDisplayString(MethodSignatureDisplayFormat))
.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 +116,7 @@ private static void AddMethod(InterfaceBuilder codeGenerator, IMethodSymbol meth
codeGenerator.AddMethodToInterface(
name,
returnType.ToDisplayString(TypeDisplayFormat),
InheritDoc,
InheritDoc(method),
paramResult,
typedArgs
);
Expand Down Expand Up @@ -197,7 +196,7 @@ InterfaceBuilder codeGenerator
codeGenerator.AddEventToInterface(
name,
type.ToDisplayString(TypeDisplayFormat),
InheritDoc
InheritDoc(evt)
);
});
}
Expand Down Expand Up @@ -226,7 +225,7 @@ private static string GetMethodOptionalValue(IParameterSymbol x)
null when x.Type.IsValueType
=> $" = default({x.Type.ToDisplayString(TypeDisplayFormat)})",
null => " = null",
_ => $" = {x.ExplicitDefaultValue}"
_ => $" = {x.ExplicitDefaultValue}",
};
}

Expand Down Expand Up @@ -285,7 +284,7 @@ InterfaceBuilder interfaceGenerator
hasGet,
hasSet,
isRef,
InheritDoc
InheritDoc(prop)
);
});
}
Expand All @@ -300,7 +299,7 @@ private static PropertySetKind GetSetKind(IMethodSymbol? setMethodSymbol)
_
=> setMethodSymbol is { DeclaredAccessibility: Accessibility.Public }
? PropertySetKind.Always
: PropertySetKind.NoSet
: PropertySetKind.NoSet,
};
}

Expand Down Expand Up @@ -328,7 +327,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