Skip to content

Commit 671d5d9

Browse files
Handle langword for see elements (#61069)
* Handle langword for see elements Handle `<see langword="" />` references in XML documentation as inline code. Resolves #61042. * Remove redundant using Added back by merge/rebase but not needed. * Apply suggestions from code review Co-authored-by: Safia Abdalla <[email protected]> --------- Co-authored-by: Safia Abdalla <[email protected]>
1 parent 0796e70 commit 671d5d9

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

src/OpenApi/gen/XmlComments/XmlComment.cs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ private XmlComment(Compilation compilation, string xml)
3535
// Transform triple slash comment
3636
var doc = XDocument.Parse(xml, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
3737

38-
ResolveCrefLink(compilation, doc, $"//{DocumentationCommentXmlNames.SeeAlsoElementName}[@cref]");
39-
ResolveCrefLink(compilation, doc, $"//{DocumentationCommentXmlNames.SeeElementName}[@cref]");
38+
ResolveCrefLink(compilation, doc, DocumentationCommentXmlNames.SeeAlsoElementName);
39+
ResolveCrefLink(compilation, doc, DocumentationCommentXmlNames.SeeElementName);
40+
41+
ResolveLangKeyword(doc, DocumentationCommentXmlNames.SeeElementName);
42+
4043
// Resolve <list> and <item> tags into bullets
4144
ResolveListTags(doc);
4245
// Resolve <code> tags into code blocks
@@ -171,18 +174,20 @@ private static void ResolveParaTags(XDocument document)
171174
/// </summary>
172175
/// <param name="compilation">The compilation to resolve type symbol declarations from.</param>
173176
/// <param name="node">The target node to process crefs in.</param>
174-
/// <param name="nodeSelector">The node type to process crefs for, can be `see` or `seealso`.</param>
175-
private static void ResolveCrefLink(Compilation compilation, XNode node, string nodeSelector)
177+
/// <param name="elementName">The node type to process crefs for, can be `see` or `seealso`.</param>
178+
private static void ResolveCrefLink(Compilation compilation, XNode node, string elementName)
176179
{
177-
if (node == null || string.IsNullOrEmpty(nodeSelector))
180+
if (node == null || string.IsNullOrEmpty(elementName))
178181
{
179182
return;
180183
}
181184

182-
var nodes = node.XPathSelectElements(nodeSelector + "[@cref]").ToArray();
185+
var attributeName = DocumentationCommentXmlNames.CrefAttributeName;
186+
187+
var nodes = node.XPathSelectElements($"//{elementName}[@{attributeName}]").ToArray();
183188
foreach (var item in nodes)
184189
{
185-
var cref = item.Attribute(DocumentationCommentXmlNames.CrefAttributeName).Value;
190+
var cref = item.Attribute(attributeName).Value;
186191
if (string.IsNullOrEmpty(cref))
187192
{
188193
continue;
@@ -197,6 +202,33 @@ private static void ResolveCrefLink(Compilation compilation, XNode node, string
197202
}
198203
}
199204

205+
/// <summary>
206+
/// Resolves the links in the XML documentation into language keywords.
207+
/// </summary>
208+
/// <param name="node">The target node to process crefs in.</param>
209+
/// <param name="elementName">The node type to process langwords for, can be `see` or `seealso`.</param>
210+
private static void ResolveLangKeyword(XNode node, string elementName)
211+
{
212+
if (node == null || string.IsNullOrEmpty(elementName))
213+
{
214+
return;
215+
}
216+
217+
var attributeName = DocumentationCommentXmlNames.LangwordAttributeName;
218+
219+
var nodes = node.XPathSelectElements($"//{elementName}[@{attributeName}]").ToArray();
220+
foreach (var item in nodes)
221+
{
222+
var langword = item.Attribute(attributeName).Value;
223+
if (string.IsNullOrEmpty(langword))
224+
{
225+
continue;
226+
}
227+
228+
item.ReplaceWith(new XText($"`{langword}`"));
229+
}
230+
}
231+
200232
private static IEnumerable<string?> GetMultipleExampleNodes(XPathNavigator navigator, string selector)
201233
{
202234
var iterator = navigator.Select(selector);

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/CompletenessTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,40 @@ public static T GetGenericValue<T>(T para)
437437
return para;
438438
}
439439
}
440+
441+
/// <summary>
442+
/// A class that implements the <see cref="IDisposable"/> interface.
443+
/// </summary>
444+
public class DisposableType : IDisposable
445+
{
446+
/// <summary>
447+
/// Finalizes an instance of the <see cref="DisposableType"/> class.
448+
/// </summary>
449+
~DisposableType()
450+
{
451+
Dispose(false);
452+
}
453+
454+
/// <inheritdoc />
455+
public void Dispose()
456+
{
457+
Dispose(true);
458+
GC.SuppressFinalize(this);
459+
}
460+
461+
/// <summary>
462+
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
463+
/// </summary>
464+
/// <param name="disposing">
465+
/// <see langword="true" /> to release both managed and unmanaged resources;
466+
/// <see langword="false" /> to release only unmanaged resources.
467+
/// <see langword="null" /> to indicate a no-op.
468+
/// </param>
469+
protected virtual void Dispose(bool disposing)
470+
{
471+
// No-op
472+
}
473+
}
440474
""";
441475
var generator = new XmlCommentGenerator();
442476
await SnapshotTestHelper.Verify(source, generator, out var compilation);

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/CompletenessTests.SupportsAllXmlTagsOnSchemas#OpenApiXmlCommentSupport.generated.verified.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ type as a cref attribute.
148148
generic types to open generics for use in
149149
typeof expressions.", null, null, null, null, false, null, null, null));
150150
cache.Add(@"T:ParamsAndParamRefs", new XmlComment(@"This shows examples of typeparamref and typeparam tags", null, null, null, null, false, null, null, null));
151-
cache.Add(@"P:ExampleClass.Label", new XmlComment(null, null, @" The string? ExampleClass.Label is a <see langword=""string"" />
151+
cache.Add(@"T:DisposableType", new XmlComment(@"A class that implements the IDisposable interface.", null, null, null, null, false, null, null, null));
152+
cache.Add(@"P:ExampleClass.Label", new XmlComment(null, null, @" The string? ExampleClass.Label is a `string`
152153
that you use for a label.
153154
Note that there isn't a way to provide a ""cref"" to
154155
each accessor, only to the property itself.", null, @"The `Label` property represents a label
@@ -191,6 +192,7 @@ that implement this interface when the
191192
method as a cref attribute.
192193
The parameter and return value are both of an arbitrary type,
193194
T", null, null, false, null, null, null));
195+
cache.Add(@"M:DisposableType.Dispose", new XmlComment(null, null, null, null, null, false, null, null, null));
194196

195197
return cache;
196198
}

0 commit comments

Comments
 (0)