| | | 1 | | using System.Text; |
| | | 2 | | using System.Text.RegularExpressions; |
| | | 3 | | |
| | | 4 | | namespace NexusLabs.Needlr.Generators.Helpers; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Helper methods for generating Mermaid diagram content. |
| | | 8 | | /// </summary> |
| | | 9 | | internal static class MermaidHelpers |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Creates a valid Mermaid node ID from a fully qualified type name. |
| | | 13 | | /// </summary> |
| | | 14 | | public static string GetMermaidNodeId(string typeName) |
| | | 15 | | { |
| | | 16 | | // Remove global:: prefix and sanitize for Mermaid |
| | 0 | 17 | | var name = typeName.Replace("global::", "") |
| | 0 | 18 | | .Replace(".", "_") |
| | 0 | 19 | | .Replace("<", "_") |
| | 0 | 20 | | .Replace(">", "_") |
| | 0 | 21 | | .Replace(",", "_") |
| | 0 | 22 | | .Replace(" ", ""); |
| | 0 | 23 | | return name; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the short type name without namespace (e.g., "MyService" from "global::MyApp.MyService"). |
| | | 28 | | /// </summary> |
| | | 29 | | public static string GetShortTypeName(string fullyQualifiedTypeName) |
| | | 30 | | { |
| | | 31 | | // Remove global:: prefix |
| | 0 | 32 | | var name = fullyQualifiedTypeName.Replace("global::", ""); |
| | | 33 | | |
| | | 34 | | // Find the last dot that's not inside generic brackets |
| | 0 | 35 | | var bracketDepth = 0; |
| | 0 | 36 | | var lastDot = -1; |
| | 0 | 37 | | for (var i = name.Length - 1; i >= 0; i--) |
| | | 38 | | { |
| | 0 | 39 | | if (name[i] == '>') bracketDepth++; |
| | 0 | 40 | | else if (name[i] == '<') bracketDepth--; |
| | 0 | 41 | | else if (name[i] == '.' && bracketDepth == 0) |
| | | 42 | | { |
| | 0 | 43 | | lastDot = i; |
| | 0 | 44 | | break; |
| | | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | return lastDot >= 0 ? name.Substring(lastDot + 1) : name; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Sanitizes a string for use as a Mermaid subgraph identifier. |
| | | 53 | | /// </summary> |
| | | 54 | | public static string SanitizeIdentifier(string name) |
| | | 55 | | { |
| | 0 | 56 | | return Regex.Replace(name, @"[^a-zA-Z0-9_]", "_"); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets the Mermaid shape notation for a type based on its characteristics. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="isDecorator">True if this is a decorator type (uses stadium shape).</param> |
| | | 63 | | /// <param name="hasFactory">True if this has [GenerateFactory] (uses hexagon shape).</param> |
| | | 64 | | /// <param name="isInterceptor">True if this is an interceptor (uses subroutine shape).</param> |
| | | 65 | | /// <returns>Tuple of (startShape, endShape) for Mermaid notation.</returns> |
| | | 66 | | public static (string Start, string End) GetNodeShape(bool isDecorator, bool hasFactory, bool isInterceptor) |
| | | 67 | | { |
| | 0 | 68 | | if (isDecorator) return ("[[", "]]"); // Stadium shape for decorators |
| | 0 | 69 | | if (hasFactory) return ("{{", "}}"); // Hexagon shape for factory sources |
| | 0 | 70 | | if (isInterceptor) return ("[[", "]]"); // Stadium shape for interceptors too |
| | 0 | 71 | | return ("[", "]"); // Rectangle for normal types |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Gets the Mermaid edge notation for different relationship types. |
| | | 76 | | /// </summary> |
| | | 77 | | public static string GetEdgeNotation(EdgeType edgeType) |
| | | 78 | | { |
| | 0 | 79 | | return edgeType switch |
| | 0 | 80 | | { |
| | 0 | 81 | | EdgeType.Dependency => "-->", // Solid arrow: A depends on B |
| | 0 | 82 | | EdgeType.Interface => "-.->", // Dotted arrow: Interface to implementation |
| | 0 | 83 | | EdgeType.Produces => "-.->|produces|", // Labeled dotted: Factory produces type |
| | 0 | 84 | | EdgeType.Decorates => "-->", // Solid arrow: Decorator wraps |
| | 0 | 85 | | EdgeType.Intercepts => "-.->|intercepts|", // Labeled dotted: Interceptor intercepts |
| | 0 | 86 | | _ => "-->" |
| | 0 | 87 | | }; |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Types of edges in Mermaid dependency graphs. |
| | | 93 | | /// </summary> |
| | | 94 | | internal enum EdgeType |
| | | 95 | | { |
| | | 96 | | Dependency, |
| | | 97 | | Interface, |
| | | 98 | | Produces, |
| | | 99 | | Decorates, |
| | | 100 | | Intercepts |
| | | 101 | | } |