Skip to content

Show the collection name/type in the Foreach visualization#1558

Open
dan-niles wants to merge 1 commit intowso2:release/bi-1.8.xfrom
dan-niles:add-description-to-foreach-node
Open

Show the collection name/type in the Foreach visualization#1558
dan-niles wants to merge 1 commit intowso2:release/bi-1.8.xfrom
dan-niles:add-description-to-foreach-node

Conversation

@dan-niles
Copy link
Contributor

@dan-niles dan-niles commented Feb 25, 2026

Purpose

Fixes wso2/product-ballerina-integrator#902

This PR updates the FOREACH node widget to show the collection name and type in the flow diagram.

image

Summary by CodeRabbit

  • UI/UX Improvements
    • Enhanced While node header to display conditions and descriptions.
    • Menu button now dynamically appears on hover, reducing visual clutter and improving interface clarity.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 25, 2026

📝 Walkthrough

Walkthrough

This change restructures the WhileNode widget component to extract and display collection information from FOREACH statements. It adds description rendering logic that formats collection type and variable names, implements hover-based menu button visibility, and reorganizes the header layout to accommodate the new description display alongside the title.

Changes

Cohort / File(s) Summary
WhileNode Widget Enhancement
workspaces/ballerina/bi-diagram/src/components/nodes/WhileNode/WhileNodeWidget.tsx
Added description extraction and rendering for FOREACH nodes to display collection type and variable information. Restructured header layout with hover-driven menu button visibility. Replaced static positioning with flexbox layout for menu button and integrated renderDescription function to format and display collection details conditionally.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A foreach now shows what we iterate,
The collection's name, no need to wait,
Hover reveals the menu's delight,
Layout refined, the diagram's just right!

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description provides the essential information (purpose, linked issue, and visual evidence) but lacks most required template sections including Goals, Approach, test coverage, security verification, and documentation/training details. Complete the PR template by adding Goals, Approach (with technical implementation details), Automation tests (with code coverage metrics), Security checks confirmation, Documentation/Training links, and other relevant sections.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Show the collection name/type in the Foreach visualization' clearly and accurately summarizes the main change—enhancing the Foreach node widget to display collection information in the diagram.
Linked Issues check ✅ Passed The code changes directly address the linked issue #902 by implementing logic to extract and display collection name/type information in the Foreach node widget through description rendering.
Out of Scope Changes check ✅ Passed All changes are scoped to the WhileNode/WhileNodeWidget component and focused on displaying collection information for iteration visualization, which directly aligns with the linked issue requirements.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dan-niles dan-niles marked this pull request as ready for review February 25, 2026 07:06
@dan-niles dan-niles force-pushed the add-description-to-foreach-node branch from 6aefe52 to 4b64015 Compare February 25, 2026 07:09
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
workspaces/ballerina/bi-diagram/src/components/nodes/WhileNode/WhileNodeWidget.tsx (1)

45-46: ⚠️ Potential issue | 🟡 Minor

Duplicate CSS property: color is declared twice.

Line 45 and 46 both set color: ${ThemeColors.ON_SURFACE}. The second declaration shadows the first. Remove the duplicate.

Proposed fix
         color: ${ThemeColors.ON_SURFACE};
-        color: ${ThemeColors.ON_SURFACE};
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@workspaces/ballerina/bi-diagram/src/components/nodes/WhileNode/WhileNodeWidget.tsx`
around lines 45 - 46, In WhileNodeWidget (component WhileNodeWidget), remove the
duplicate CSS declaration by deleting the redundant "color:
${ThemeColors.ON_SURFACE};" so the style block only declares color once; locate
the duplicate inside the styled block where ThemeColors.ON_SURFACE is applied
and remove the extra line to avoid shadowing.
🧹 Nitpick comments (3)
.vscode/settings.json (1)

2-2: Unrelated change: enabling formatOnSave can produce noisy diffs.

This is orthogonal to the PR objective (Foreach description). Enabling it repo-wide may cause large formatting-only diffs from contributors whose local formatter config differs. Consider splitting this into a separate PR or ensuring all contributors share identical Prettier/formatter settings.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.vscode/settings.json at line 2, Remove the unrelated change enabling
"editor.formatOnSave" from this PR (it causes noisy formatting diffs); either
revert that line from the current diff or move the change into a separate,
focused PR that also adds/updates a shared formatter configuration (e.g.,
.prettierrc or .editorconfig) and contributor guidance so everyone shares
identical formatter settings before enabling format-on-save.
workspaces/ballerina/bi-diagram/src/components/nodes/WhileNode/WhileNodeWidget.tsx (2)

284-292: Description extraction logic looks correct; minor note on the regex.

The regex /^foreach\s+(.+?)\s*\{/ won't match if sourceCode has a newline between the iterable expression and the opening brace. This is likely fine for typical Ballerina source representations, but if multiline source is possible, adding the s flag (dotAll) would make it resilient.

Suggested tweak
-            const match = model.node.codedata.sourceCode.match(/^foreach\s+(.+?)\s*\{/);
+            const match = model.node.codedata.sourceCode.match(/^foreach\s+(.+?)\s*\{/s);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@workspaces/ballerina/bi-diagram/src/components/nodes/WhileNode/WhileNodeWidget.tsx`
around lines 284 - 292, The description extraction in WhileNodeWidget (const
description in WhileNodeWidget.tsx) uses the regex /^foreach\s+(.+?)\s*\{/ which
fails if model.node.codedata.sourceCode contains a newline before the opening
brace; update the pattern to allow newlines (either add the dotAll flag, e.g.
use /pattern/s, or replace .+? with [\s\S]+? ) when matching the iterable
expression so match?.[1] correctly captures multiline expressions from
model.node.codedata.sourceCode.

354-357: Empty style={{}} wrapper and missing optional chaining on metadata.

Two small items:

  1. Line 355: <div style={{}}> — the empty style object is a no-op. If this div is just a grouping wrapper, a <> fragment (or removing it) would be cleaner. If styling is planned, a TODO would help.

  2. Line 357: model.node.metadata.label — consider model.node.metadata?.label for defensive access in case metadata is ever undefined.

Proposed fix
-                    <div style={{ display: "flex", flexDirection: "row", alignItems: "center", justifyContent: "space-between", width: "100%" }}>
-                        <div style={{}}>
-                            <div style={{ display: "flex", flexDirection: "row", alignItems: "center", gap: "4px" }}>
-                                <NodeStyles.Title>{model.node.metadata.label || model.node.codedata.node}</NodeStyles.Title>
+                    <div style={{ display: "flex", flexDirection: "row", alignItems: "center", justifyContent: "space-between", width: "100%" }}>
+                        <div>
+                            <div style={{ display: "flex", flexDirection: "row", alignItems: "center", gap: "4px" }}>
+                                <NodeStyles.Title>{model.node.metadata?.label || model.node.codedata.node}</NodeStyles.Title>
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@workspaces/ballerina/bi-diagram/src/components/nodes/WhileNode/WhileNodeWidget.tsx`
around lines 354 - 357, Remove the no-op wrapper div (the <div style={{}}>
around the title) — replace it with a React fragment or remove it entirely so
you don’t keep an empty style prop, and if you intend to keep a wrapper add a
TODO comment for future styles; also make the metadata access defensive by using
optional chaining when rendering the title (change references like
model.node.metadata.label to model.node.metadata?.label ||
model.node.codedata.node) so NodeStyles.Title (and any other uses of
model.node.metadata) won’t throw if metadata is undefined.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In
`@workspaces/ballerina/bi-diagram/src/components/nodes/WhileNode/WhileNodeWidget.tsx`:
- Around line 45-46: In WhileNodeWidget (component WhileNodeWidget), remove the
duplicate CSS declaration by deleting the redundant "color:
${ThemeColors.ON_SURFACE};" so the style block only declares color once; locate
the duplicate inside the styled block where ThemeColors.ON_SURFACE is applied
and remove the extra line to avoid shadowing.

---

Nitpick comments:
In @.vscode/settings.json:
- Line 2: Remove the unrelated change enabling "editor.formatOnSave" from this
PR (it causes noisy formatting diffs); either revert that line from the current
diff or move the change into a separate, focused PR that also adds/updates a
shared formatter configuration (e.g., .prettierrc or .editorconfig) and
contributor guidance so everyone shares identical formatter settings before
enabling format-on-save.

In
`@workspaces/ballerina/bi-diagram/src/components/nodes/WhileNode/WhileNodeWidget.tsx`:
- Around line 284-292: The description extraction in WhileNodeWidget (const
description in WhileNodeWidget.tsx) uses the regex /^foreach\s+(.+?)\s*\{/ which
fails if model.node.codedata.sourceCode contains a newline before the opening
brace; update the pattern to allow newlines (either add the dotAll flag, e.g.
use /pattern/s, or replace .+? with [\s\S]+? ) when matching the iterable
expression so match?.[1] correctly captures multiline expressions from
model.node.codedata.sourceCode.
- Around line 354-357: Remove the no-op wrapper div (the <div style={{}}> around
the title) — replace it with a React fragment or remove it entirely so you don’t
keep an empty style prop, and if you intend to keep a wrapper add a TODO comment
for future styles; also make the metadata access defensive by using optional
chaining when rendering the title (change references like
model.node.metadata.label to model.node.metadata?.label ||
model.node.codedata.node) so NodeStyles.Title (and any other uses of
model.node.metadata) won’t throw if metadata is undefined.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a3a33f4 and 6aefe52.

📒 Files selected for processing (2)
  • .vscode/settings.json
  • workspaces/ballerina/bi-diagram/src/components/nodes/WhileNode/WhileNodeWidget.tsx

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
workspaces/ballerina/bi-diagram/src/components/nodes/WhileNode/WhileNodeWidget.tsx (1)

284-315: Extract FOREACH description parsing to a utility function for maintainability and testability.

The regex parsing at lines 288–299 is coupled to the widget render path, making it difficult to test and maintain. While the line 288 regex is robust enough, line 299's pattern /^(\S+)\s+(\S+)\s+(in)\s+(.+)$/ cannot handle Ballerina's destructuring binding patterns (e.g., foreach [string, int] [x, y] in pairs), which limits the widget's ability to display complex foreach statements correctly. Moving this extraction logic to a dedicated utility function in src/utils/ with comprehensive test coverage would improve both maintainability and handle edge cases more gracefully.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@workspaces/ballerina/bi-diagram/src/components/nodes/WhileNode/WhileNodeWidget.tsx`
around lines 284 - 315, Extract the FOREACH parsing logic from WhileNodeWidget:
move the regex/parse block currently inside the description IIFE and
renderDescription() into a new utility function (e.g.,
parseForeachDescription(sourceOrCondition: string): { type?: string; varName?:
string; iterable?: string; full?: string } | null) placed in src/utils/, then
call that function from WhileNodeWidget's description computation and
renderDescription; update renderDescription to use the returned structured
object (falling back to raw description) rather than the inline regex, and add
unit tests for parseForeachDescription to cover simple and destructuring
patterns like "foreach [string,int] [x,y] in pairs" so the component can render
complex foreach statements correctly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In
`@workspaces/ballerina/bi-diagram/src/components/nodes/WhileNode/WhileNodeWidget.tsx`:
- Around line 284-315: Extract the FOREACH parsing logic from WhileNodeWidget:
move the regex/parse block currently inside the description IIFE and
renderDescription() into a new utility function (e.g.,
parseForeachDescription(sourceOrCondition: string): { type?: string; varName?:
string; iterable?: string; full?: string } | null) placed in src/utils/, then
call that function from WhileNodeWidget's description computation and
renderDescription; update renderDescription to use the returned structured
object (falling back to raw description) rather than the inline regex, and add
unit tests for parseForeachDescription to cover simple and destructuring
patterns like "foreach [string,int] [x,y] in pairs" so the component can render
complex foreach statements correctly.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6aefe52 and 4b64015.

📒 Files selected for processing (1)
  • workspaces/ballerina/bi-diagram/src/components/nodes/WhileNode/WhileNodeWidget.tsx

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.

2 participants