< Summary

Information
Class: NexusLabs.Needlr.Generators.Helpers.MermaidHelpers
Assembly: NexusLabs.Needlr.Generators
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Generators/Helpers/MermaidHelpers.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 31
Coverable lines: 31
Total lines: 101
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 24
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetMermaidNodeId(...)100%210%
GetShortTypeName(...)0%156120%
SanitizeIdentifier(...)100%210%
GetNodeShape(...)0%4260%
GetEdgeNotation(...)0%4260%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Generators/Helpers/MermaidHelpers.cs

#LineLine coverage
 1using System.Text;
 2using System.Text.RegularExpressions;
 3
 4namespace NexusLabs.Needlr.Generators.Helpers;
 5
 6/// <summary>
 7/// Helper methods for generating Mermaid diagram content.
 8/// </summary>
 9internal 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
 017        var name = typeName.Replace("global::", "")
 018                          .Replace(".", "_")
 019                          .Replace("<", "_")
 020                          .Replace(">", "_")
 021                          .Replace(",", "_")
 022                          .Replace(" ", "");
 023        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
 032        var name = fullyQualifiedTypeName.Replace("global::", "");
 33
 34        // Find the last dot that's not inside generic brackets
 035        var bracketDepth = 0;
 036        var lastDot = -1;
 037        for (var i = name.Length - 1; i >= 0; i--)
 38        {
 039            if (name[i] == '>') bracketDepth++;
 040            else if (name[i] == '<') bracketDepth--;
 041            else if (name[i] == '.' && bracketDepth == 0)
 42            {
 043                lastDot = i;
 044                break;
 45            }
 46        }
 47
 048        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    {
 056        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    {
 068        if (isDecorator) return ("[[", "]]");      // Stadium shape for decorators
 069        if (hasFactory) return ("{{", "}}");       // Hexagon shape for factory sources
 070        if (isInterceptor) return ("[[", "]]");    // Stadium shape for interceptors too
 071        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    {
 079        return edgeType switch
 080        {
 081            EdgeType.Dependency => "-->",           // Solid arrow: A depends on B
 082            EdgeType.Interface => "-.->",           // Dotted arrow: Interface to implementation
 083            EdgeType.Produces => "-.->|produces|",  // Labeled dotted: Factory produces type
 084            EdgeType.Decorates => "-->",            // Solid arrow: Decorator wraps
 085            EdgeType.Intercepts => "-.->|intercepts|", // Labeled dotted: Interceptor intercepts
 086            _ => "-->"
 087        };
 88    }
 89}
 90
 91/// <summary>
 92/// Types of edges in Mermaid dependency graphs.
 93/// </summary>
 94internal enum EdgeType
 95{
 96    Dependency,
 97    Interface,
 98    Produces,
 99    Decorates,
 100    Intercepts
 101}