-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e44ef2e
commit f6106ea
Showing
13 changed files
with
250 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
namespace Doki.Extensions; | ||
|
||
public static class DocumentationRootExtensions | ||
{ | ||
public static DocumentationObject? TryGetParent(this DocumentationRoot root, DocumentationObject child) | ||
{ | ||
ArgumentNullException.ThrowIfNull(root); | ||
ArgumentNullException.ThrowIfNull(child); | ||
|
||
return child.Parent ?? SearchParent(root, child); | ||
} | ||
|
||
private static DocumentationObject? SearchParent(DocumentationObject from, DocumentationObject to) | ||
{ | ||
if (from == to) return from; | ||
|
||
switch (from) | ||
{ | ||
case DocumentationRoot root: return SearchParentIn(root.Assemblies, to); | ||
case AssemblyDocumentation assemblyDocumentation: | ||
return SearchParentIn(assemblyDocumentation.Namespaces, to); | ||
case NamespaceDocumentation namespaceDocumentation: return SearchParentIn(namespaceDocumentation.Types, to); | ||
case TypeDocumentation typeDocumentation: | ||
{ | ||
var constructor = SearchParentIn(typeDocumentation.Constructors, to); | ||
if (constructor != null) return constructor; | ||
|
||
var method = SearchParentIn(typeDocumentation.Methods, to); | ||
if (method != null) return method; | ||
|
||
var property = SearchParentIn(typeDocumentation.Properties, to); | ||
if (property != null) return property; | ||
|
||
var field = SearchParentIn(typeDocumentation.Fields, to); | ||
if (field != null) return field; | ||
|
||
var interfaceDocumentation = SearchParentIn(typeDocumentation.Interfaces, to); | ||
if (interfaceDocumentation != null) return interfaceDocumentation; | ||
|
||
var derivedType = SearchParentIn(typeDocumentation.DerivedTypes, to); | ||
if (derivedType != null) return derivedType; | ||
|
||
var result = SearchParentInTypeDocumentationReference(typeDocumentation, to); | ||
if (result != null) return result; | ||
|
||
break; | ||
} | ||
case GenericTypeArgumentDocumentation genericTypeArgumentDocumentation: | ||
{ | ||
if (genericTypeArgumentDocumentation.Description != null) | ||
{ | ||
var description = SearchParent(genericTypeArgumentDocumentation.Description, to); | ||
if (description != null) return description; | ||
} | ||
|
||
var result = SearchParentInTypeDocumentationReference(genericTypeArgumentDocumentation, to); | ||
if (result != null) return result; | ||
|
||
break; | ||
} | ||
case TypeDocumentationReference typeDocumentationReference: | ||
{ | ||
var result = SearchParentInTypeDocumentationReference(typeDocumentationReference, to); | ||
if (result != null) return result; | ||
|
||
break; | ||
} | ||
case MemberDocumentation memberDocumentation: | ||
{ | ||
var result = SearchParentInMemberDocumentation(memberDocumentation, to); | ||
if (result != null) return result; | ||
|
||
break; | ||
} | ||
case XmlDocumentation xmlDocumentation: | ||
{ | ||
var result = SearchParentIn(xmlDocumentation.Contents, to); | ||
if (result != null) return result; | ||
|
||
break; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static DocumentationObject? SearchParentInTypeDocumentationReference( | ||
TypeDocumentationReference typeDocumentationReference, DocumentationObject to) | ||
{ | ||
if (typeDocumentationReference.BaseType != null) | ||
{ | ||
var baseType = SearchParent(typeDocumentationReference.BaseType, to); | ||
if (baseType != null) return baseType; | ||
} | ||
|
||
var genericArgument = SearchParentIn(typeDocumentationReference.GenericArguments, to); | ||
return genericArgument ?? SearchParentInMemberDocumentation(typeDocumentationReference, to); | ||
} | ||
|
||
private static DocumentationObject? SearchParentInMemberDocumentation(MemberDocumentation memberDocumentation, | ||
DocumentationObject to) | ||
{ | ||
var summary = SearchParentIn(memberDocumentation.Summaries, to); | ||
if (summary != null) return summary; | ||
|
||
var remarks = SearchParentIn(memberDocumentation.Remarks, to); | ||
if (remarks != null) return remarks; | ||
|
||
var example = SearchParentIn(memberDocumentation.Examples, to); | ||
return example; | ||
} | ||
|
||
private static DocumentationObject? SearchParentIn(IEnumerable<DocumentationObject> children, | ||
DocumentationObject to) | ||
{ | ||
return children.Select(child => SearchParent(child, to)).OfType<DocumentationObject>().FirstOrDefault(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
tests/Doki.Extensions.Tests/DocumentationRootExtensionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Xml.XPath; | ||
using Doki.TestAssembly.InheritanceChain; | ||
using Doki.Tests.Common; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
|
||
namespace Doki.Extensions.Tests; | ||
|
||
public class DocumentationRootExtensionTests | ||
{ | ||
[Fact] | ||
public async Task Test_TryGetParent_ParentPropertyIsNotNull() | ||
{ | ||
var testOutput = new DocumentationRootCapture(); | ||
|
||
var documentationGenerator = new DocumentationGenerator(); | ||
|
||
var emptyDocumentation = new XPathDocument(new StringReader("""<?xml version="1.0"?><doc></doc>""")); | ||
|
||
documentationGenerator.AddOutput(testOutput); | ||
|
||
documentationGenerator.AddAssembly(typeof(SimpleClass).Assembly, emptyDocumentation); | ||
|
||
await documentationGenerator.GenerateAsync(NullLogger.Instance); | ||
|
||
Assert.NotNull(testOutput.Root); | ||
|
||
var assemblyDocumentation = testOutput.Root.Assemblies.Single(); | ||
|
||
var namespaceDocumentation = assemblyDocumentation.Namespaces.Single(); | ||
|
||
var simpleClassDocumentation = namespaceDocumentation.Types.First(); | ||
|
||
Assert.NotNull(simpleClassDocumentation.Parent); | ||
|
||
var parent = testOutput.Root.TryGetParent(simpleClassDocumentation); | ||
|
||
Assert.NotNull(parent); | ||
Assert.Equal(namespaceDocumentation, parent); | ||
} | ||
} |
Oops, something went wrong.