Skip to content

Commit f147df6

Browse files
committed
improve handling of generated explicit interface implementation docs
1 parent 9bf5f9c commit f147df6

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

src/InheritDoc/InheritDocProcessor.cs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ static XDocument loadDoc(string path)
7878
var doc = loadDoc(docPath);
7979
var docMembers = doc.Root.Element(DocElementNames.Members);
8080
int beforeCount = docMembers.Descendants(DocElementNames.InheritDoc).Count();
81-
int trimCount = 0;
8281

8382
if (beforeCount == 0 && trimLevel == ApiLevel.None)
8483
return (0, 0, 0);
@@ -100,39 +99,33 @@ static XDocument loadDoc(string path)
10099
var refDocs = getRefDocs(refPaths, addPaths, refCref, logger);
101100
logger.Write(ILogger.Severity.Diag, "External ref docs found: " + refDocs.Root.Elements(DocElementNames.Member).Count().ToString());
102101

103-
if (trimLevel > ApiLevel.None)
104-
beforeCount = docMembers.Descendants(DocElementNames.InheritDoc).Count(dm => !dm.Ancestors(DocElementNames.Member).Any(m => m.HasAttribute(DocAttributeNames._trimmed)));
102+
beforeCount = docMembers.Descendants(DocElementNames.InheritDoc).Count(dm => !dm.Ancestors(DocElementNames.Member).Any(m => m.HasAttribute(DocAttributeNames._trimmed) || m.HasAttribute(DocAttributeNames._gencode)));
105103

106104
var mem = default(XElement);
107105
while ((mem = docMembers.Elements(DocElementNames.Member).FirstOrDefault(isInheritDocCandidate)) is not null)
108106
replaceInheritDoc(docPath, mem, docMap, docMembers, refDocs, logger);
109107

110-
foreach (var md in docMembers.Elements(DocElementNames.Member))
111-
{
112-
if (md.HasAttribute(DocAttributeNames._visited))
113-
md.SetAttributeValue(DocAttributeNames._visited, null);
114-
115-
if (md.HasAttribute(DocAttributeNames._gencode))
116-
md.SetAttributeValue(DocAttributeNames._gencode, null);
117-
}
108+
var totrim = docMembers.Elements(DocElementNames.Member).Where(m => m.HasAttribute(DocAttributeNames._trimmed) || (m.HasAttribute(DocAttributeNames._gencode) && !m.Elements().Any(e => e.Name != DocElementNames.InheritDoc))).ToList();
109+
int trimCount = totrim.Count;
118110

119-
if (trimLevel > ApiLevel.None)
111+
foreach (var md in totrim)
120112
{
121-
var totrim = docMembers.Elements(DocElementNames.Member).Where(m => m.HasAttribute(DocAttributeNames._trimmed)).ToList();
122-
trimCount = totrim.Count();
113+
string reason = md.HasAttribute(DocAttributeNames._gencode) && !md.Elements().Any(e => e.Name != DocElementNames.InheritDoc) ? "generated" : trimLevel == ApiLevel.Private ? "private" : "non-public";
114+
logger.Write(ILogger.Severity.Diag, "Trimming " + reason + " doc: " + (string)md.Attribute(DocAttributeNames.Name));
123115

124-
foreach (var md in totrim)
125-
{
126-
logger.Write(ILogger.Severity.Diag, "Trimming " + (trimLevel == ApiLevel.Private ? "private" : "non-public") + " doc: " + (string)md.Attribute(DocAttributeNames.Name));
116+
if (md.PreviousNode.IsWhiteSpace())
117+
md.PreviousNode.Remove();
127118

128-
if (md.PreviousNode.IsWhiteSpace())
129-
md.PreviousNode.Remove();
130-
131-
md.Remove();
132-
}
119+
md.Remove();
133120
}
134121

135-
int afterCount = docMembers.Descendants(DocElementNames.InheritDoc).Count();
122+
int afterCount = docMembers.Descendants(DocElementNames.InheritDoc).Count(dm => !dm.Ancestors(DocElementNames.Member).Any(m => m.HasAttribute(DocAttributeNames._gencode)));
123+
124+
foreach (var md in docMembers.Elements(DocElementNames.Member).Where(m => m.HasAttribute(DocAttributeNames._visited) || m.HasAttribute(DocAttributeNames._gencode)))
125+
{
126+
md.SetAttributeValue(DocAttributeNames._visited, null);
127+
md.SetAttributeValue(DocAttributeNames._gencode, null);
128+
}
136129

137130
using var writer = XmlWriter.Create(outPath, new XmlWriterSettings { Encoding = new UTF8Encoding(false), IndentChars = " " });
138131
doc.Save(writer);
@@ -181,9 +174,7 @@ private static IDictionary<string, IEnumerable<DocMatch>> generateDocMap(IList<T
181174

182175
foreach (var bt in t.GetBaseCandidates())
183176
{
184-
var rbt = bt.Resolve();
185-
string cref = rbt.GetDocID();
186-
177+
string cref = bt.Resolve().GetDocID();
187178
if (dml.Count == 0 || crefs.Contains(cref))
188179
dml.Add(new DocMatch(cref, t, bt));
189180

@@ -205,14 +196,20 @@ private static IDictionary<string, IEnumerable<DocMatch>> generateDocMap(IList<T
205196
// If no docs for public explicit interface implementation, inject them
206197
// including the whitespace they would have had if they had been there.
207198
if (idx == 0 && (om?.DeclaringType.GetApiLevel() ?? ApiLevel.None) > trimLevel && t.GetApiLevel() > trimLevel && !findDocsByID(docMembers, memID).Any())
199+
{
200+
if (docMembers.LastNode.IsWhiteSpace())
201+
docMembers.LastNode.Remove();
202+
208203
docMembers.Add(
209-
new XText(" "),
204+
new XText("\n "),
210205
new XElement(DocElementNames.Member,
211206
new XAttribute(DocAttributeNames.Name, memID),
207+
new XAttribute(DocAttributeNames._gencode, true),
212208
new XText("\n "), new XElement(DocElementNames.InheritDoc),
213209
new XText("\n ")),
214210
new XText("\n ")
215211
);
212+
}
216213

217214
var methDocs = findDocsByID(docMembers, memID);
218215
if ((om?.DeclaringType.GetApiLevel() ?? m.GetApiLevel()) <= trimLevel)
@@ -234,7 +231,7 @@ private static IDictionary<string, IEnumerable<DocMatch>> generateDocMap(IList<T
234231
var crefs = methDocs.Descendants(DocElementNames.InheritDoc).Select(i => (string)i.Attribute(DocAttributeNames.Cref)).Where(c => !string.IsNullOrWhiteSpace(c)).ToHashSet();
235232
var dml = new List<DocMatch>();
236233

237-
var bases = om is not null ? (new[] { om }).Concat(m.GetBaseCandidates()) : m.GetBaseCandidates();
234+
var bases = (om is not null ? (new[] { om }) : [ ]).Concat(m.GetBaseCandidates());
238235
foreach (var (bm, cref) in bases.SelectMany(bm => bm.GetDocID().Select(d => (bm, d))))
239236
{
240237
if (dml.Count == 0 || crefs.Contains(cref))

0 commit comments

Comments
 (0)