-
Notifications
You must be signed in to change notification settings - Fork 868
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic support for top-level inheritdoc elements. (#1178)
* Basic support for top-level inheritdoc elements. Does not support inheritdoc elements nested inside other elements. Does not support any attributes on inheritdoc elements. Fixes #247. * Address code review feedback. Specifically, fix spelling mistakes and remove and unused field.
- Loading branch information
1 parent
82eadcb
commit a2ba6d5
Showing
11 changed files
with
271 additions
and
0 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 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
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
125 changes: 125 additions & 0 deletions
125
src/Microsoft.DocAsCode.Metadata.ManagedReference/Resolvers/CopyInheritedDocumentation.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,125 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Microsoft.DocAsCode.Metadata.ManagedReference | ||
{ | ||
using Microsoft.DocAsCode.Common; | ||
using Microsoft.DocAsCode.DataContracts.ManagedReference; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
|
||
/// <summary> | ||
/// Copies doc comments to items marked with 'inheritdoc' from interfaces and base classes. | ||
/// </summary> | ||
public class CopyInherited : IResolverPipeline | ||
{ | ||
public void Run(MetadataModel yaml, ResolverContext context) | ||
{ | ||
TreeIterator.Preorder(yaml.TocYamlViewModel, null, | ||
s => s.IsInvalid ? null : s.Items, | ||
(current, parent) => | ||
{ | ||
if (current.IsInheritDoc) | ||
InheritDoc(current, context); | ||
return true; | ||
}); | ||
} | ||
|
||
static void InheritDoc(MetadataItem dest, ResolverContext context) | ||
{ | ||
dest.IsInheritDoc = false; | ||
|
||
switch (dest.Type) | ||
{ | ||
case MemberType.Constructor: | ||
if (dest.Parent == null || dest.Syntax == null || dest.Syntax.Parameters == null) | ||
return; | ||
Debug.Assert(dest.Parent.Type == MemberType.Class); | ||
|
||
//try to find the base class | ||
if (dest.Parent.Inheritance?.Count == 0) | ||
return; | ||
MetadataItem baseClass; | ||
if (!context.Members.TryGetValue(dest.Parent.Inheritance[dest.Parent.Inheritance.Count - 1], out baseClass)) | ||
return; | ||
if (baseClass.Items == null) | ||
return; | ||
|
||
//look a constructor in the base class which has a matching signature | ||
foreach (var ctor in baseClass.Items) | ||
{ | ||
if (ctor.Type != MemberType.Constructor) | ||
continue; | ||
if (ctor.Syntax == null || ctor.Syntax.Parameters == null) | ||
continue; | ||
if (ctor.Syntax.Parameters.Count != dest.Syntax.Parameters.Count) | ||
continue; | ||
|
||
bool parametersMatch = true; | ||
for (int ndx = 0; ndx < dest.Syntax.Parameters.Count; ndx++) | ||
{ | ||
var myParam = dest.Syntax.Parameters[ndx]; | ||
var baseParam = ctor.Syntax.Parameters[ndx]; | ||
if (myParam.Name != baseParam.Name) | ||
parametersMatch = false; | ||
if (myParam.Type != baseParam.Type) | ||
parametersMatch = false; | ||
} | ||
|
||
if (parametersMatch) | ||
{ | ||
Copy(dest, ctor, context); | ||
return; | ||
} | ||
} | ||
break; | ||
|
||
case MemberType.Method: | ||
case MemberType.Property: | ||
case MemberType.Event: | ||
Copy(dest, dest.Overridden, context); | ||
if (dest.Implements != null) | ||
{ | ||
foreach (var item in dest.Implements) | ||
{ | ||
Copy(dest, item, context); | ||
} | ||
} | ||
break; | ||
|
||
case MemberType.Class: | ||
if (dest.Inheritance.Count != 0) | ||
{ | ||
Copy(dest, dest.Inheritance[dest.Inheritance.Count - 1], context); | ||
} | ||
if (dest.Implements != null) | ||
{ | ||
foreach (var item in dest.Implements) | ||
{ | ||
Copy(dest, item, context); | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
|
||
static void Copy(MetadataItem dest, string srcName, ResolverContext context) | ||
{ | ||
MetadataItem src; | ||
if (string.IsNullOrEmpty(srcName) || !context.Members.TryGetValue(srcName, out src)) | ||
return; | ||
|
||
Copy(dest, src, context); | ||
} | ||
|
||
static void Copy(MetadataItem dest, MetadataItem src, ResolverContext context) | ||
{ | ||
if (src.IsInheritDoc) | ||
{ | ||
InheritDoc(src, context); | ||
} | ||
|
||
dest.CopyInheritedData(src); | ||
} | ||
} | ||
} |
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
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