Skip to content

Commit

Permalink
feat: markdown support namespaces and separated member pages (#9235)
Browse files Browse the repository at this point in the history
* feat: support markdown namespaces

* test(snapshot): update snapshots for 3971cc0

* short link for class members

* test(snapshot): update snapshots for d6979a8

* support namespacelayout.nested

* support memberlayout.separatepages

* test(snapshot): update snapshots for ee26a96

* merge type members

* test(snapshot): update snapshots for 6f096a5

---------

Co-authored-by: Yufei Huang <[email protected]>
  • Loading branch information
yufeih and yufeih authored Sep 24, 2023
1 parent 5c145e9 commit f205a44
Show file tree
Hide file tree
Showing 48 changed files with 1,132 additions and 450 deletions.
2 changes: 1 addition & 1 deletion src/Docfx.Dotnet/ExtractMetadata/ExtractMetadataWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ await LoadCompilationFromProject(project.AbsolutePath) is { } compilation)
}

var projectMetadataList = new List<MetadataItem>();
var extensionMethods = assemblies.SelectMany(assembly => assembly.Item1.FindExtensionMethods()).ToArray();
var filter = new SymbolFilter(_config, _options);
var extensionMethods = assemblies.SelectMany(assembly => assembly.Item1.FindExtensionMethods(filter)).ToArray();
var allAssemblies = new HashSet<IAssemblySymbol>(assemblies.Select(a => a.Item1), SymbolEqualityComparer.Default);

foreach (var (assembly, compilation) in assemblies)
Expand Down
1,072 changes: 716 additions & 356 deletions src/Docfx.Dotnet/MarkdownFormatter.cs

Large diffs are not rendered by default.

11 changes: 2 additions & 9 deletions src/Docfx.Dotnet/SymbolFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,9 @@ internal static partial class SymbolFormatter
private static readonly SymbolDisplayFormat s_methodQualifiedNameFormat = s_qualifiedNameFormat
.WithParameterOptions(SymbolDisplayParameterOptions.IncludeType | SymbolDisplayParameterOptions.IncludeParamsRefOut);

private static readonly SymbolDisplayFormat s_linkItemNameWithTypeFormat = new(
memberOptions: SymbolDisplayMemberOptions.IncludeContainingType,
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypes);

private static readonly SymbolDisplayFormat s_linkItemQualifiedNameFormat = s_linkItemNameWithTypeFormat
.WithTypeQualificationStyle(SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces);

public static string GetName(ISymbol symbol, SyntaxLanguage language)
public static string GetName(ISymbol symbol, SyntaxLanguage language, bool nullableReferenceType = true, bool overload = false)
{
return GetNameParts(symbol, language).ToDisplayString();
return GetNameParts(symbol, language, nullableReferenceType, overload).ToDisplayString();
}

public static ImmutableArray<SymbolDisplayPart> GetNameParts(
Expand Down
44 changes: 38 additions & 6 deletions src/Docfx.Dotnet/SymbolHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,34 +82,66 @@ public static bool IsOperator(IMethodSymbol method)
return method.MethodKind is MethodKind.BuiltinOperator or MethodKind.UserDefinedOperator or MethodKind.Conversion;
}

public static IEnumerable<IMethodSymbol> FindExtensionMethods(this IAssemblySymbol assembly)
public static IEnumerable<IMethodSymbol> FindExtensionMethods(this IAssemblySymbol assembly, SymbolFilter filter)
{
if (!assembly.MightContainExtensionMethods)
return Array.Empty<IMethodSymbol>();

return
from ns in assembly.GetAllNamespaces()
from ns in assembly.GetAllNamespaces(filter)
from type in ns.GetTypeMembers()
where type.MightContainExtensionMethods
from member in type.GetMembers()
where member.Kind is SymbolKind.Method && ((IMethodSymbol)member).IsExtensionMethod
where filter.IncludeApi(member)
select (IMethodSymbol)member;
}

public static IEnumerable<INamespaceSymbol> GetAllNamespaces(this IAssemblySymbol assembly)
public static IEnumerable<INamespaceSymbol> GetAllNamespaces(this IAssemblySymbol assembly, SymbolFilter filter)
{
var stack = new Stack<INamespaceSymbol>();
stack.Push(assembly.GlobalNamespace);
while (stack.TryPop(out var ns))
while (stack.TryPop(out var item))
{
yield return ns;
foreach (var child in ns.GetNamespaceMembers())
if (!filter.IncludeApi(item))
continue;

yield return item;
foreach (var child in item.GetNamespaceMembers())
{
stack.Push(child);
}
}
}

public static IEnumerable<INamedTypeSymbol> GetAllTypes(this IAssemblySymbol assembly, SymbolFilter filter)
{
var stack = new Stack<INamespaceOrTypeSymbol>();
stack.Push(assembly.GlobalNamespace);

while (stack.TryPop(out var item))
{
if (!filter.IncludeApi(item))
continue;

if (item is INamedTypeSymbol type)
{
yield return type;

foreach (var child in type.GetTypeMembers())
stack.Push(child);
}
else if (item is INamespaceSymbol ns)
{
foreach (var child in ns.GetNamespaceMembers())
stack.Push(child);

foreach (var child in ns.GetTypeMembers())
stack.Push(child);
}
}
}

public static IEnumerable<ISymbol> GetInheritedMembers(this INamedTypeSymbol symbol, SymbolFilter filter)
{
for (var type = symbol.BaseType; type != null; type = type.BaseType)
Expand Down
4 changes: 2 additions & 2 deletions src/Docfx.Dotnet/SymbolUrlResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ internal static partial class SymbolUrlResolver
"!" => null,
"N" or "T" => $"{uid.Replace('`', '-')}{ext}",
"M" or "F" or "P" or "E" => memberLayout is MemberLayout.SeparatePages && !symbol.IsEnumMember()
? $"{VisitorHelper.GetId(symbol).Replace('`', '-')}{ext}"
: $"{VisitorHelper.GetId(symbol.ContainingType).Replace('`', '-')}{ext}#{Regex.Replace(uid, @"/\W/", "_")}",
? $"{VisitorHelper.FileNameId(VisitorHelper.GetOverloadId(symbol))}{ext}#{Regex.Replace(uid, @"/\W/", "_")}"
: $"{VisitorHelper.FileNameId(VisitorHelper.GetId(symbol.ContainingType))}{ext}#{Regex.Replace(uid, @"/\W/", "_")}",
_ => throw new NotSupportedException($"Unknown comment ID format '{type}'"),
};
}
Expand Down
5 changes: 5 additions & 0 deletions src/Docfx.Dotnet/Visitors/VisitorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ internal static class VisitorHelper
public static string GlobalNamespaceId { get; set; }
private static readonly Regex GenericMethodPostFix = new(@"``\d+$", RegexOptions.Compiled);

public static string FileNameId(string id)
{
return id.Replace('`', '-').Replace('#', '-').Replace("*", "");
}

public static string GetId(ISymbol symbol)
{
if (symbol == null)
Expand Down
2 changes: 1 addition & 1 deletion test/Docfx.Dotnet.Tests/GenerateMetadataFromCSUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class GenerateMetadataFromCSUnitTest
private static MetadataItem Verify(string code, ExtractMetadataConfig config = null)
{
var compilation = CompilationHelper.CreateCompilationFromCSharpCode(code, "test.dll");
var extensionMethods = compilation.Assembly.FindExtensionMethods().ToArray();
var extensionMethods = compilation.Assembly.FindExtensionMethods(new(new(), new())).ToArray();
return compilation.Assembly.GenerateMetadataItem(compilation, config, extensionMethods: extensionMethods);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Namespace BuildFromAssembly

## Classes

[Class1](BuildFromAssembly.Class1.md)

This is a test class.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Namespace BuildFromCSharpSourceCode

## Classes

[CSharp](BuildFromCSharpSourceCode.CSharp.md)

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Interface IIssue8948
# Interface Class1.IIssue8948

__Namespace:__ [BuildFromProject](BuildFromProject.md)
__Assembly:__ BuildFromProject.dll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Class Issue8665
# Class Class1.Issue8665

__Namespace:__ [BuildFromProject](BuildFromProject.md)
__Assembly:__ BuildFromProject.dll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Class Issue8696Attribute
# Class Class1.Issue8696Attribute

__Namespace:__ [BuildFromProject](BuildFromProject.md)
__Assembly:__ BuildFromProject.dll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Class Issue8948
# Class Class1.Issue8948

__Namespace:__ [BuildFromProject](BuildFromProject.md)
__Assembly:__ BuildFromProject.dll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Class Test
# Class Class1.Test<T>

__Namespace:__ [BuildFromProject](BuildFromProject.md)
__Assembly:__ BuildFromProject.dll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Class Class1
# Class Inheritdoc.Issue6366.Class1<T>

__Namespace:__ [BuildFromProject](BuildFromProject.md)
__Assembly:__ BuildFromProject.dll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Class Class2
# Class Inheritdoc.Issue6366.Class2

__Namespace:__ [BuildFromProject](BuildFromProject.md)
__Assembly:__ BuildFromProject.dll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Class Issue6366
# Class Inheritdoc.Issue6366

__Namespace:__ [BuildFromProject](BuildFromProject.md)
__Assembly:__ BuildFromProject.dll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Class Issue7035
# Class Inheritdoc.Issue7035

__Namespace:__ [BuildFromProject](BuildFromProject.md)
__Assembly:__ BuildFromProject.dll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Class Issue7484
# Class Inheritdoc.Issue7484

__Namespace:__ [BuildFromProject](BuildFromProject.md)
__Assembly:__ BuildFromProject.dll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Class Issue8101
# Class Inheritdoc.Issue8101

__Namespace:__ [BuildFromProject](BuildFromProject.md)
__Assembly:__ BuildFromProject.dll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Struct Issue8129
# Struct Inheritdoc.Issue8129

__Namespace:__ [BuildFromProject](BuildFromProject.md)
__Assembly:__ BuildFromProject.dll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Class A

__Namespace:__ [BuildFromProject](BuildFromProject.md).[Issue8540](BuildFromProject.Issue8540.md).[A](BuildFromProject.Issue8540.A.md)
__Namespace:__ [BuildFromProject.Issue8540.A](BuildFromProject.Issue8540.A.md)
__Assembly:__ BuildFromProject.dll

```csharp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Namespace BuildFromProject.Issue8540.A

## Classes

[A](BuildFromProject.Issue8540.A.A.md)

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Class B

__Namespace:__ [BuildFromProject](BuildFromProject.md).[Issue8540](BuildFromProject.Issue8540.md).[B](BuildFromProject.Issue8540.B.md)
__Namespace:__ [BuildFromProject.Issue8540.B](BuildFromProject.Issue8540.B.md)
__Assembly:__ BuildFromProject.dll

```csharp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Namespace BuildFromProject.Issue8540.B

## Classes

[B](BuildFromProject.Issue8540.B.B.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Namespace BuildFromProject.Issue8540

## Namespaces

[BuildFromProject.Issue8540.A](BuildFromProject.Issue8540.A.md)

[BuildFromProject.Issue8540.B](BuildFromProject.Issue8540.B.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Namespace BuildFromProject

## Namespaces

[BuildFromProject.Issue8540](BuildFromProject.Issue8540.md)

## Classes

[Class1](BuildFromProject.Class1.md)

[Inheritdoc](BuildFromProject.Inheritdoc.md).[Issue6366](BuildFromProject.Inheritdoc.Issue6366.md).[Class1](BuildFromProject.Inheritdoc.Issue6366.Class1-1.md)<T>

[Inheritdoc](BuildFromProject.Inheritdoc.md).[Issue6366](BuildFromProject.Inheritdoc.Issue6366.md).[Class2](BuildFromProject.Inheritdoc.Issue6366.Class2.md)

[Inheritdoc](BuildFromProject.Inheritdoc.md)

[Inheritdoc](BuildFromProject.Inheritdoc.md).[Issue6366](BuildFromProject.Inheritdoc.Issue6366.md)

[Inheritdoc](BuildFromProject.Inheritdoc.md).[Issue7035](BuildFromProject.Inheritdoc.Issue7035.md)

[Inheritdoc](BuildFromProject.Inheritdoc.md).[Issue7484](BuildFromProject.Inheritdoc.Issue7484.md)

This is a test class to have something for DocFX to document.

[Inheritdoc](BuildFromProject.Inheritdoc.md).[Issue8101](BuildFromProject.Inheritdoc.Issue8101.md)

[Class1](BuildFromProject.Class1.md).[Issue8665](BuildFromProject.Class1.Issue8665.md)

[Class1](BuildFromProject.Class1.md).[Issue8696Attribute](BuildFromProject.Class1.Issue8696Attribute.md)

[Class1](BuildFromProject.Class1.md).[Issue8948](BuildFromProject.Class1.Issue8948.md)

[Class1](BuildFromProject.Class1.md).[Test](BuildFromProject.Class1.Test-1.md)<T>

## Structs

[Inheritdoc](BuildFromProject.Inheritdoc.md).[Issue8129](BuildFromProject.Inheritdoc.Issue8129.md)

## Interfaces

[IInheritdoc](BuildFromProject.IInheritdoc.md)

[Class1](BuildFromProject.Class1.md).[IIssue8948](BuildFromProject.Class1.IIssue8948.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Namespace BuildFromVBSourceCode

## Classes

[BaseClass1](BuildFromVBSourceCode.BaseClass1.md)

This is the BaseClass

[Class1](BuildFromVBSourceCode.Class1.md)

This is summary from vb class...

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Class Cat
# Class Cat<T, K>

__Namespace:__ [CatLibrary](CatLibrary.md)
__Assembly:__ CatLibrary.dll
Expand Down Expand Up @@ -264,8 +264,6 @@ Hint whether this cat has cheat mode.

This is an argument exception

## Events

### ownEat

Eat event of this cat
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Class CatException
# Class CatException<T>

__Namespace:__ [CatLibrary](CatLibrary.md)
__Assembly:__ CatLibrary.dll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Class Complex
# Class Complex<T, J>

__Namespace:__ [CatLibrary](CatLibrary.md)
__Assembly:__ CatLibrary.dll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Enum ColorType
# Enum ContainersRefType.ColorType

__Namespace:__ [CatLibrary](CatLibrary.md).[Core](CatLibrary.Core.md)
__Namespace:__ [CatLibrary.Core](CatLibrary.Core.md)
__Assembly:__ CatLibrary.Core.dll

Enumeration ColorType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Class ContainersRefTypeChild
# Class ContainersRefType.ContainersRefTypeChild

__Namespace:__ [CatLibrary](CatLibrary.md).[Core](CatLibrary.Core.md)
__Namespace:__ [CatLibrary.Core](CatLibrary.Core.md)
__Assembly:__ CatLibrary.Core.dll

```csharp
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Interface ContainersRefTypeChildInterface
# Interface ContainersRefType.ContainersRefTypeChildInterface

__Namespace:__ [CatLibrary](CatLibrary.md).[Core](CatLibrary.Core.md)
__Namespace:__ [CatLibrary.Core](CatLibrary.Core.md)
__Assembly:__ CatLibrary.Core.dll

```csharp
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Delegate ContainersRefTypeDelegate
# Delegate ContainersRefType.ContainersRefTypeDelegate

__Namespace:__ [CatLibrary](CatLibrary.md).[Core](CatLibrary.Core.md)
__Namespace:__ [CatLibrary.Core](CatLibrary.Core.md)
__Assembly:__ CatLibrary.Core.dll

Delegate ContainersRefTypeDelegate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Struct ContainersRefType

__Namespace:__ [CatLibrary](CatLibrary.md).[Core](CatLibrary.Core.md)
__Namespace:__ [CatLibrary.Core](CatLibrary.Core.md)
__Assembly:__ CatLibrary.Core.dll

Struct ContainersRefType
Expand Down Expand Up @@ -79,8 +79,6 @@ public static int ContainersRefTypeNonRefMethod(params object[] parmsArray)

[int](https://learn.microsoft.com/dotnet/api/system.int32)
## Events

### ContainersRefTypeEventHandler

```csharp
Expand Down
Loading

0 comments on commit f205a44

Please sign in to comment.