Skip to content

feat: sequence diagram — parse and render rect blocks - #170

Merged
jongalloway merged 5 commits into
mainfrom
copilot/add-parse-render-rect-blocks
Apr 17, 2026
Merged

jongalloway merged 5 commits into
mainfrom
copilot/add-parse-render-rect-blocks

Conversation

Copilot AI commented Apr 17, 2026 •

Copy link
Copy Markdown
Contributor

Mermaid's rect rgb(...)...end / rect rgba(...)...end syntax was silently ignored. This adds full parse → layout → render support for coloured background bands in sequence diagrams.

Changes

Model

  • Group: Added Metadata dictionary (Dictionary<string, object>) matching the existing Node/Edge pattern, enabling parsers to annotate groups without subclassing.

Parser (MermaidSequenceParser)

  • Parses rect rgb(R,G,B) and rect rgba(R,G,B,A) opening lines and end closing lines using a stack (handles nesting).
  • Each block maps to a Group with FillColor set and sequence:rectGroup=true / sequence:rectStartIndex / sequence:rectEndIndex stored in metadata.
  • Blocks enclosing zero messages are dropped.

Layout (DefaultLayoutEngine.Sequence)

  • After per-message sequence:messageY positions are finalised, rect-band groups receive X=0, Width=canvasWidth, and Y-bounds computed so adjacent bands share a midpoint boundary (firstMsgY − halfRow → lastMsgY + rowH − halfRow) with no overlap.

Renderer (SvgStructureWriter)

  • AppendGroup detects sequence:rectGroup and delegates to a new AppendSequenceRectBand helper: a plain <rect> spanning the full canvas width.
  • rgb() colors get fill-opacity="0.2"; rgba() colors use the embedded alpha directly.

Example input

sequenceDiagram
    participant A as Alice
    participant B as Bob
    A->>B: Initialise
    rect rgb(0,128,255)
        A->>B: Query
        B-->>A: Result
    end
    rect rgba(255,128,0,0.25)
        A->>B: Notify
        B-->>A: Ack
    end

Tests

  • 9 new parser unit tests covering rgb/rgba parsing, metadata, index bounds, empty blocks, and message count.
  • New snapshot fixture mermaid-sequence-rect with generated SVG baseline.

Copilot AI and others added 2 commits April 17, 2026 01:21
- Add `Metadata` dictionary to `Group` model
- Parse `rect rgb(R,G,B)` / `rect rgba(R,G,B,A)` and `end` in MermaidSequenceParser
- Layout: compute Group X/Y/Width/Height for sequence rect bands; adjacent bands meet exactly
- Render sequence rect bands as semi-transparent colored full-width rectangles in SvgStructureWriter
- Add 9 unit tests for rect block parsing in MermaidSequenceParserTests
- Add snapshot fixture mermaid-sequence-rect with expected SVG baseline

Agent-Logs-Url: https://github.com/jongalloway/DiagramForge/sessions/105be76e-e00b-4f8a-ae9d-01ea5b8ccb4e

Co-authored-by: jongalloway <68539+jongalloway@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for parsing and rendering rect blocks in sequence diagrams feat: sequence diagram — parse and render rect blocks Apr 17, 2026
Copilot AI requested a review from jongalloway April 17, 2026 01:26
@jongalloway
jongalloway marked this pull request as ready for review April 17, 2026 03:05
@jongalloway
jongalloway requested a review from Copilot April 17, 2026 03:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds end-to-end support for Mermaid sequence-diagram rect rgb(...) ... end / rect rgba(...) ... end background bands by extending the semantic model (Group metadata), parsing rect blocks into groups, laying them out after message positions are known, and rendering them as plain SVG <rect> elements.

Changes:

  • Extend Group with a Metadata dictionary so parsers/layout can tag group instances without subclassing.
  • Parse rect rgb(...) / rect rgba(...) blocks in MermaidSequenceParser into groups annotated with sequence:rect* metadata.
  • Lay out and render rect-band groups in the sequence layout engine and SVG writer; add parser unit tests and an E2E snapshot fixture/baseline.
Show a summary per file
File Description
tests/DiagramForge.Tests/Parsers/Mermaid/MermaidSequenceParserTests.cs Adds unit tests validating rect parsing, metadata, and interaction with participant/message parsing.
tests/DiagramForge.E2ETests/Fixtures/mermaid-sequence-rect.input New E2E input fixture covering both rgb() and rgba() rect bands.
tests/DiagramForge.E2ETests/Fixtures/mermaid-sequence-rect.expected.svg New snapshot baseline verifying SVG output includes the background rect bands.
src/DiagramForge/Rendering/SvgStructureWriter.cs Detects rect-band groups and renders them as simple semi-transparent <rect> elements.
src/DiagramForge/Parsers/Mermaid/MermaidSequenceParser.cs Implements rect block parsing via a stack and attaches rect metadata to groups.
src/DiagramForge/Models/Group.cs Adds Group.Metadata to match Node/Edge metadata patterns.
src/DiagramForge/Layout/DefaultLayoutEngine.Sequence.cs Computes rect-band group bounds from per-message Y positions and canvas width.

Copilot's findings

  • Files reviewed: 6/7 changed files
  • Comments generated: 1

Comment on lines +102 to +106
var groupId = $"sequence:rect:{rectIndex++}";
var group = new Group(groupId, string.Empty);
group.FillColor = normalizedColor;
group.Metadata["sequence:rectGroup"] = true;
openRects.Push((group, messageIndex));

Copilot AI Apr 17, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SvgRenderer renders groups in a deterministic order using ThenBy(g => g.Id). With IDs like sequence:rect:0..10, lexicographic sorting will place ...:10 before ...:2, which can change z-order for overlapping/nested rect bands once there are >9 blocks. Consider zero-padding the numeric suffix (e.g., sequence:rect:0001) or storing a separate numeric metadata key for ordering.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in cdfff2a by changing the group ID format to use D4 zero-padding (e.g., sequence:rect:0000, sequence:rect:0001, ..., sequence:rect:9999). This ensures ThenBy(g => g.Id, StringComparer.Ordinal) in SvgRenderer produces correct z-order for all realistic rect-block counts.

@jongalloway

Copy link
Copy Markdown
Owner

@copilot resolve the merge conflicts in this pull request

…der-rect-blocks

# Conflicts:
#	tests/DiagramForge.Tests/Parsers/Mermaid/MermaidSequenceParserTests.cs

Co-authored-by: jongalloway <68539+jongalloway@users.noreply.github.com>

Copilot AI commented Apr 17, 2026

Copy link
Copy Markdown
Contributor Author

@copilot resolve the merge conflicts in this pull request

Resolved in c517327. The conflict was in MermaidSequenceParserTests.cs where our rect block tests overlapped with the autonumber tests added in main — both test sections are now included.

@jongalloway
jongalloway merged commit c5532fd into main Apr 17, 2026
5 checks passed
@jongalloway
jongalloway deleted the copilot/add-parse-render-rect-blocks branch April 17, 2026 04:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sequence diagram: parse and render rect blocks

3 participants