| | | 1 | | using System.Linq; |
| | | 2 | | using System.Text; |
| | | 3 | | using System.Xml; |
| | | 4 | | using System.Xml.Linq; |
| | | 5 | | |
| | | 6 | | using Microsoft.CodeAnalysis; |
| | | 7 | | |
| | | 8 | | namespace NexusLabs.Needlr.Generators; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Extracts normalized semantic content from Roslyn XML documentation comments. |
| | | 12 | | /// </summary> |
| | | 13 | | internal static class DocumentationCommentHelper |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Gets the semantic content of a named <c>param</c> element. |
| | | 17 | | /// </summary> |
| | | 18 | | internal static string? GetParameterDocumentation( |
| | | 19 | | ISymbol symbol, |
| | | 20 | | string parameterName) |
| | | 21 | | { |
| | 29 | 22 | | var document = TryParseDocumentation(symbol); |
| | 29 | 23 | | var parameter = document? |
| | 29 | 24 | | .Descendants("param") |
| | 29 | 25 | | .FirstOrDefault(element => |
| | 42 | 26 | | element.Attribute("name")?.Value == parameterName); |
| | 29 | 27 | | return parameter is null |
| | 29 | 28 | | ? null |
| | 29 | 29 | | : GetNormalizedElementContent(parameter); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets the semantic content of a symbol's <c>summary</c> element. |
| | | 34 | | /// </summary> |
| | | 35 | | internal static string? GetSummaryDocumentation(ISymbol symbol) |
| | | 36 | | { |
| | 24 | 37 | | var summary = TryParseDocumentation(symbol)? |
| | 24 | 38 | | .Descendants("summary") |
| | 24 | 39 | | .FirstOrDefault(); |
| | 24 | 40 | | return summary is null |
| | 24 | 41 | | ? null |
| | 24 | 42 | | : GetNormalizedElementContent(summary); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | private static XDocument? TryParseDocumentation(ISymbol symbol) |
| | | 46 | | { |
| | 53 | 47 | | var xml = symbol.GetDocumentationCommentXml(); |
| | 53 | 48 | | if (string.IsNullOrWhiteSpace(xml)) |
| | 45 | 49 | | return null; |
| | | 50 | | |
| | | 51 | | try |
| | | 52 | | { |
| | 8 | 53 | | return XDocument.Parse(xml); |
| | | 54 | | } |
| | 0 | 55 | | catch (XmlException) |
| | | 56 | | { |
| | 0 | 57 | | return null; |
| | | 58 | | } |
| | 8 | 59 | | } |
| | | 60 | | |
| | | 61 | | private static string? GetNormalizedElementContent(XElement element) |
| | | 62 | | { |
| | 12 | 63 | | foreach (var text in element.DescendantNodesAndSelf().OfType<XText>()) |
| | | 64 | | { |
| | 3 | 65 | | text.Value = NormalizeWhitespace(text.Value); |
| | | 66 | | } |
| | | 67 | | |
| | 3 | 68 | | var content = string.Concat( |
| | 3 | 69 | | element.Nodes() |
| | 3 | 70 | | .Select(node => node.ToString(SaveOptions.DisableFormatting))) |
| | 3 | 71 | | .Trim(); |
| | 3 | 72 | | return content.Length == 0 ? null : content; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | private static string NormalizeWhitespace(string value) |
| | | 76 | | { |
| | 3 | 77 | | var builder = new StringBuilder(value.Length); |
| | 3 | 78 | | var previousWasWhitespace = false; |
| | | 79 | | |
| | 138 | 80 | | foreach (var character in value) |
| | | 81 | | { |
| | 66 | 82 | | if (char.IsWhiteSpace(character)) |
| | | 83 | | { |
| | 7 | 84 | | if (!previousWasWhitespace) |
| | 7 | 85 | | builder.Append(' '); |
| | | 86 | | |
| | 7 | 87 | | previousWasWhitespace = true; |
| | 7 | 88 | | continue; |
| | | 89 | | } |
| | | 90 | | |
| | 59 | 91 | | builder.Append(character); |
| | 59 | 92 | | previousWasWhitespace = false; |
| | | 93 | | } |
| | | 94 | | |
| | 3 | 95 | | return builder.ToString(); |
| | | 96 | | } |
| | | 97 | | } |