From ad23230dd22244c1e66542f36879c3eed89ab93e Mon Sep 17 00:00:00 2001 From: Catarina Gamboa Date: Wed, 18 Nov 2026 19:14:24 +0000 Subject: [PATCH 1/3] State machine: green arrows where the call is allowed, red crossed arrow from the actual state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In focus mode every arrow of the called method was red, including those from the states where the call is allowed, and the actual state had no arrow for the call at all, since the spec has none. Now the transitions of the called method are green, arrow and label, with their guards (an unmet guard keeps its red text), and each possible state that rejects the call gets a red self-loop "✕ method": the attempted call. When the spec already has a transition of the method from such a state, that one is drawn as the rejected call instead. No red loop when the state of the receiver is unknown. The loop is never dimmed, collapsed or counted, as it is no spec transition. The legend gains the two arrows, each only when on the diagram. Same colouring with Show all. Client version 0.1.4 for the study build. Co-Authored-By: Claude Fable 5.1 --- client/package.json | 2 +- client/src/webview/diagram-focus.spec.ts | 47 +++++++++++----- client/src/webview/diagram.ts | 72 +++++++++++++++--------- client/src/webview/script.spec.ts | 4 +- client/src/webview/styles.ts | 61 +++++++++++++++++--- client/src/webview/views/fsm/fsm.spec.ts | 11 ++-- client/src/webview/views/fsm/fsm.ts | 17 ++++-- 7 files changed, 155 insertions(+), 59 deletions(-) diff --git a/client/package.json b/client/package.json index 50bcba3..476b434 100644 --- a/client/package.json +++ b/client/package.json @@ -2,7 +2,7 @@ "name": "liquid-java", "displayName": "LiquidJava", "description": "Extending Java with Liquid Types", - "version": "0.1.3", + "version": "0.1.4", "publisher": "AlcidesFonseca", "repository": { "type": "git", diff --git a/client/src/webview/diagram-focus.spec.ts b/client/src/webview/diagram-focus.spec.ts index baada7d..cf0af78 100644 --- a/client/src/webview/diagram-focus.spec.ts +++ b/client/src/webview/diagram-focus.spec.ts @@ -62,7 +62,9 @@ function lines(diagram: string): string[] { } const dimmedTransitions = (diagram: string) => lines(diagram).filter(line => line.startsWith('%% lj-dimmed-transitions:')); -const calledMethod = (label: string) => `${label}`; +const selfLoops = (diagram: string) => lines(diagram).filter(line => line.startsWith('%% lj-self-loops:')); +const allowedCall = (label: string) => `${label}`; +const rejectedCall = (label: string) => `✕ ${label}`; const dimmedMethod = (label: string) => `${label}`; const collapsedMethods = (labels: string[], count: string) => `${count}${labels.map(label => `${label}`).join('')}`; @@ -81,7 +83,7 @@ describe('state error focus', () => { expect(getRelatedStates(getStateErrorFocus(datagramSocket())!)).toEqual(['bound', 'closed', 'unbound', 'connected']); }); - it('marks valid and invalid states and colours every edge of the called method', () => { + it('marks valid and invalid states, the allowed transitions of the called method, and the rejected call', () => { for (const focusOnError of [true, false]) { const diagram = lines(createMermaidDiagram(datagramSocket(), 'TB', false, focusOnError)); @@ -90,11 +92,14 @@ describe('state error focus', () => { 'state "✕ closed" as closed', 'class bound ljStateValid', 'class closed ljStateInvalid', + // the spec has no receive from closed, so the rejected call is a loop on it, drawn round like the others + `closed --> closed : ${rejectedCall('receive')}`, + '%% lj-self-loops: edge5 edge6 edge7 edge11 edge13', ])); const send = focusOnError ? dimmedMethod('send') : 'send'; // a single dimmed method keeps its name for (const state of ['unbound', 'bound', 'connected']) { - expect(diagram).toContain(`${state} --> ${state} : ${calledMethod('receive')}
${send}`); + expect(diagram).toContain(`${state} --> ${state} : ${allowedCall('receive')}
${send}`); } // accepted states that are not possible get only a class, without a mark in their name expect(diagram.filter(line => /^(state|class) .*\b(unbound|connected)\b/.test(line))) @@ -157,7 +162,7 @@ describe('state error focus', () => { ); expect(lines(createMermaidDiagram(sm, 'TB', false, true))).toEqual(expect.arrayContaining([ - `bound --> bound : ${calledMethod('receive')}
${collapsedMethods(['send', 'getPort', 'getOption'], '+3 more')}`, + `bound --> bound : ${allowedCall('receive')}
${collapsedMethods(['send', 'getPort', 'getOption'], '+3 more')}`, `unbound --> bound : ${collapsedMethods(['bind', 'bindAll'], '2 methods')}`, ])); }); @@ -184,7 +189,16 @@ describe('state error focus', () => { sm.transitions.push({ from: 'bound', to: 'bound', label: 'receiveAll' }); expect(lines(createMermaidDiagram(sm, 'TB', false, true))) - .toContain(`bound --> bound : ${calledMethod('receive')}
${collapsedMethods(['send', 'receiveAll'], '+2 more')}`); + .toContain(`bound --> bound : ${allowedCall('receive')}
${collapsedMethods(['send', 'receiveAll'], '+2 more')}`); + }); + + it('draws a transition of the called method from a rejecting state as the rejected call, without a loop', () => { + const sm = datagramSocket(); + sm.transitions.push({ from: 'closed', to: 'closed', label: 'receive', fromCondition: 'retries(this) > 0' }); + + const diagram = createMermaidDiagram(sm, 'TB', true, true); + expect(lines(diagram)).toContain(`closed --> closed : [retries(this) > 0] ${rejectedCall('receive')}`); + expect(diagram.match(/closed --> closed/g)).toHaveLength(1); }); it('does not focus without accepted states', () => { @@ -207,7 +221,7 @@ describe('state error focus', () => { expect(focus).toMatchObject({ unknownState: true, validStates: [], invalidStates: [] }); const diagram = createMermaidDiagram(sm, 'TB', false, true); expect(lines(diagram)).toContain('class unbound,bound,connected ljStateAccepted'); - expect(diagram).not.toMatch(/ljStateValid|ljStateInvalid|ljStateDimmed|" as /); + expect(diagram).not.toMatch(/ljStateValid|ljStateInvalid|ljStateDimmed|" as |lj-call-rejected/); // only the failed method stays bright expect(lines(diagram)).toEqual(expect.arrayContaining([`unbound --> bound : ${dimmedMethod('bind')}`, `broken --> broken : ${dimmedMethod('repair')}`])); }); @@ -219,13 +233,15 @@ describe('state error focus', () => { 'state "✕ childrenAllowed" as childrenAllowed', 'class childrenAllowed ljStateInvalid', 'class maybeHasChildren ljStateAccepted', - `maybeHasChildren --> maybeHasChildren : ${calledMethod('getFirstChild')}`, + `maybeHasChildren --> maybeHasChildren : ${allowedCall('getFirstChild')}`, `childrenAllowed --> maybeHasChildren : ${dimmedMethod('add')}`, `maybeHasChildren --> childrenAllowed : ${dimmedMethod('removeAllChildren')}`, + `childrenAllowed --> childrenAllowed : ${rejectedCall('getFirstChild')}`, ])); expect(diagram.join('\n')).not.toMatch(/maybeHasChildren" as|ljStateValid|ljStateDimmed/); - // the constructor is dimmed although it leads to a possible state + // the constructor is dimmed although it leads to a possible state, the rejected call never is expect(dimmedTransitions(diagram.join('\n'))).toEqual(['%% lj-dimmed-transitions: edge0 edge1 edge2']); + expect(selfLoops(diagram.join('\n'))).toEqual(['%% lj-self-loops: edge3 edge4']); }); it('only highlights the possible states when the accepted states are unknown', () => { @@ -234,7 +250,7 @@ describe('state error focus', () => { expect(getStateErrorFocus(sm)).toBeNull(); const diagram = createMermaidDiagram(sm, 'TB', false, true); expect(diagram).toBe(createMermaidDiagram(sm, 'TB', false, false)); - expect(diagram).toContain(`maybeHasChildren --> maybeHasChildren : ${calledMethod('getFirstChild')}`); + expect(diagram).toContain(`maybeHasChildren --> maybeHasChildren : ${allowedCall('getFirstChild')}`); expect(diagram).toContain('class childrenAllowed ljStateErrorActual'); expect(diagram).not.toMatch(/ljStateValid|ljStateInvalid|ljStateAccepted|ljStateDimmed|" as /); }); @@ -432,19 +448,22 @@ describe('a state error on several state sets', () => { const sms = door(); const [doorState, lockState] = sms.map(sm => lines(createMermaidDiagram(sm, 'TB', true, false, sms))); - expect(lockState).toContain(`unlocked --> unlocked : ${guard('doorState(this) == closed')} ${calledMethod('open')}`); - expect(doorState).toContain(`closed --> opened : ${guard(unmet('lockState(this) == unlocked'))} open`); + expect(lockState).toContain(`unlocked --> unlocked : ${guard('doorState(this) == closed')} ${allowedCall('open')}`); + expect(lockState).toContain(`locked --> locked : ${rejectedCall('open')}`); + // the call is allowed from closed, only its condition on the other state set is unmet + expect(doorState).toContain(`closed --> opened : ${guard(unmet('lockState(this) == unlocked'))} ${allowedCall('open')}`); expect(doorState).toContain(`closed --> closed : ${guard('lockState(this) != locked')} lock`); + expect(doorState.join('\n')).not.toContain('lj-call-rejected'); // the unmet condition stays when conditions are collapsed expect(lines(createMermaidDiagram(sms[0], 'TB', false, false, sms))) - .toEqual(expect.arrayContaining([`closed --> opened : ${guard(unmet('lockState(this) == unlocked'))} open`, 'closed --> closed : lock'])); + .toEqual(expect.arrayContaining([`closed --> opened : ${guard(unmet('lockState(this) == unlocked'))} ${allowedCall('open')}`, 'closed --> closed : lock'])); // only the clauses the actual state does not meet are marked sms[0].transitions[0].fromCondition = 'lockState(this) == unlocked && (size(this) > 0 || ready(this))'; expect(lines(createMermaidDiagram(sms[0], 'TB', true, false, sms))) - .toContain(`closed --> opened : ${guard(`${unmet('lockState(this) == unlocked')} && (size(this) > 0 || ready(this))`)} open`); + .toContain(`closed --> opened : ${guard(`${unmet('lockState(this) == unlocked')} && (size(this) > 0 || ready(this))`)} ${allowedCall('open')}`); // without a condition on the state set in a wrong state, the call stays marked sms[0].transitions[0].fromCondition = 'size(this) > 0'; - expect(lines(createMermaidDiagram(sms[0], 'TB', false, false, sms))).toContain(`closed --> opened : ${calledMethod('open')}`); + expect(lines(createMermaidDiagram(sms[0], 'TB', false, false, sms))).toContain(`closed --> opened : ${allowedCall('open')}`); }); it('draws a transition that keeps its state as a round loop', async () => { diff --git a/client/src/webview/diagram.ts b/client/src/webview/diagram.ts index e3c61cd..2243882 100644 --- a/client/src/webview/diagram.ts +++ b/client/src/webview/diagram.ts @@ -111,18 +111,22 @@ export function createMermaidDiagram( // group transitions by from/to states and merge labels const transitionMap = new Map(); + // the possible states that reject the call and have a transition of the called method, drawn as the rejected call + const rejectedFrom = new Set(); sm.transitions.forEach(transition => { const key = `${transition.from}|${transition.to}`; const isCalledMethod = transition.label === sm.errorContext?.calledMethod; // in focus mode, only the failed method stays bright const dimmed = collapse && !isCalledMethod; const unmet = hasUnmetCondition(transition, sm, stateSets, rejectingStateSets); + const rejected = isCalledMethod && !!errorFocus?.invalidStates.includes(transition.from); + if (rejected) rejectedFrom.add(transition.from); const label = getTransitionLabel( transition.label, getFromConditionLabel(transition.fromCondition, stateSets, unmet ? rejectingStateSets : null, showConditions), transition.toCondition, showConditions, - isCalledMethod && !unmet, + rejected ? 'rejected' : isCalledMethod ? 'allowed' : null, ); if (!transitionMap.has(key)) transitionMap.set(key, { bright: [], dimmed: [] }); const group = transitionMap.get(key)!; @@ -137,6 +141,13 @@ export function createMermaidDiagram( const [from, to] = key.split('|'); addTransition(` ${from} --> ${to} : ${getCollapsedLabel(bright, dimmed, 'methods')}`, bright.length === 0, from === to); }); + // the spec has no transition of the call from a state that rejects it, so the rejected call is drawn + // as a loop on each such state; it is no spec transition, so it is never dimmed or collapsed + if (errorFocus?.calledMethod) { + const rejectedCall = getCallLabel(escapeMermaidLabel(errorFocus.calledMethod), 'rejected'); + errorFocus.invalidStates.filter(state => !rejectedFrom.has(state)) + .forEach(state => addTransition(` ${state} --> ${state} : ${rejectedCall}`, false, true)); + } if (selfLoops.length > 0) lines.push(`${SELF_LOOPS_COMMENT} ${selfLoops.join(' ')}`); if (!errorFocus) { @@ -185,26 +196,31 @@ function getCollapsedLabel(bright: string[], dimmed: CollapsibleLabel[], noun: ' * Gets the label of a transition: the condition it needs before its method, and the one it sets after, * e.g. [lockState(this) == unlocked] open [doorState(this) == opened] * @param fromCondition the condition before, from getFromConditionLabel - * @param isFailedCall whether to mark the method as the rejected call + * @param call whether the method is the called one, allowed from the transition's state or rejected there */ function getTransitionLabel( label: string, fromCondition: string | null, toCondition?: string | null, showConditions = false, - isFailedCall = false, + call: CallKind | null = null, ): string { const escapedLabel = escapeMermaidLabel(label); - const methodLabel = isFailedCall - ? `${escapedLabel}` - : escapedLabel; return [ fromCondition, - methodLabel, + call ? getCallLabel(escapedLabel, call) : escapedLabel, showConditions ? getConditionLabel('post', toCondition) : '', ].filter(Boolean).join(' '); } +// how a transition of the called method is drawn: green where the spec allows the call, red and crossed where it rejects it +type CallKind = 'allowed' | 'rejected'; + +// the called method, coloured once rendered with its transition +function getCallLabel(escapedMethod: string, call: CallKind): string { + return `${call === 'rejected' ? `${INVALID_STATE_ICON} ` : ''}${escapedMethod}`; +} + /** * Whether a transition of the rejected call, from a possible state, needs a state of a state set whose state rejects it */ @@ -272,7 +288,7 @@ export async function renderMermaidDiagram(document: any, window: any) { const dimmedTransitions = Array.from(mermaidElements, (element: any) => getListedTransitions(element.textContent, DIMMED_TRANSITIONS_COMMENT)); const selfLoops = Array.from(mermaidElements, (element: any) => getListedTransitions(element.textContent, SELF_LOOPS_COMMENT)); await mermaid.run({ nodes: mermaidElements }); - highlightErrorTransitions(document); + colourCallTransitions(document); mermaidElements.forEach((element: any, index: number) => { markTransitions(element, dimmedTransitions[index], 'lj-dimmed'); roundSelfLoops(element, selfLoops[index]); @@ -288,17 +304,20 @@ export async function renderMermaidDiagram(document: any, window: any) { } } -function highlightErrorTransitions(document: any) { - document.querySelectorAll('.lj-state-error-method').forEach((methodLabel: any) => { - const label = methodLabel.closest('g.label[data-id]'); - const edgeId = label?.getAttribute('data-id'); - const svg = label?.ownerSVGElement; - if (!edgeId || !svg) return; - - const transition = svg.querySelector(`path.transition[data-id="${edgeId}"]`); - if (!transition || transition.classList.contains('lj-state-error-transition')) return; - transition.classList.add('lj-state-error-transition'); - highlightTransitionMarker(svg, transition, edgeId); +// colours the line and arrowhead of each transition of the called method like its label +function colourCallTransitions(document: any) { + (['allowed', 'rejected'] as CallKind[]).forEach(call => { + document.querySelectorAll(`.lj-call-${call}`).forEach((methodLabel: any) => { + const label = methodLabel.closest('g.label[data-id]'); + const edgeId = label?.getAttribute('data-id'); + const svg = label?.ownerSVGElement; + if (!edgeId || !svg) return; + + const transition = svg.querySelector(`path.transition[data-id="${edgeId}"]`); + if (!transition || transition.classList.contains(`lj-call-${call}-transition`)) return; + transition.classList.add(`lj-call-${call}-transition`); + colourTransitionMarker(svg, transition, edgeId, call); + }); }); } @@ -454,7 +473,8 @@ function getLinePoints(svg: Element): Point[] { }); } -function highlightTransitionMarker(svg: any, transition: any, edgeId: string) { +// the arrowhead is shared by every transition, so the coloured one gets a copy of its own +function colourTransitionMarker(svg: any, transition: any, edgeId: string, call: CallKind) { const markerReference = transition.getAttribute('marker-end'); const markerId = markerReference?.match(/^url\(#(.+)\)$/)?.[1]; if (!markerId) return; @@ -462,12 +482,12 @@ function highlightTransitionMarker(svg: any, transition: any, edgeId: string) { const marker = svg.querySelector(`#${markerId}`); if (!marker?.parentNode) return; - const highlightedMarker = marker.cloneNode(true); - const highlightedMarkerId = `${markerId}-lj-error-${edgeId}`; - highlightedMarker.setAttribute('id', highlightedMarkerId); - highlightedMarker.classList.add('lj-state-error-marker'); - marker.parentNode.appendChild(highlightedMarker); - transition.setAttribute('marker-end', `url(#${highlightedMarkerId})`); + const colouredMarker = marker.cloneNode(true); + const colouredMarkerId = `${markerId}-lj-${call}-${edgeId}`; + colouredMarker.setAttribute('id', colouredMarkerId); + colouredMarker.classList.add(`lj-call-${call}-marker`); + marker.parentNode.appendChild(colouredMarker); + transition.setAttribute('marker-end', `url(#${colouredMarkerId})`); } /** diff --git a/client/src/webview/script.spec.ts b/client/src/webview/script.spec.ts index d81c849..9d0b22d 100644 --- a/client/src/webview/script.spec.ts +++ b/client/src/webview/script.spec.ts @@ -310,7 +310,7 @@ describe('webview script', () => { const copied: string = writeText.mock.calls[0][0]; expect(copied.match(/stateDiagram-v2/g)).toHaveLength(2); expect(copied).toContain('Idle --> Busy'); - expect(copied).not.toMatch(/ljState|lj-dimmed|lj-state-error-method/); + expect(copied).not.toMatch(/ljState|lj-dimmed|lj-call-/); }); it('copies the Mermaid source without the error overlay', async () => { @@ -330,7 +330,7 @@ describe('webview script', () => { expect(root.querySelector('.mermaid')?.textContent).toContain('ljStateDimmed'); expect(writeText.mock.calls[1][0]).toBe(plain); - expect(plain).not.toMatch(/ljState|lj-dimmed|lj-state-error-method/); + expect(plain).not.toMatch(/ljState|lj-dimmed|lj-call-/); }); it('opens another state error focused after showing the full diagram of the previous one', () => { diff --git a/client/src/webview/styles.ts b/client/src/webview/styles.ts index 41cef31..554625f 100644 --- a/client/src/webview/styles.ts +++ b/client/src/webview/styles.ts @@ -1195,19 +1195,29 @@ export function getStyles(): string { .diagram-container .mermaid svg .ljStateErrorActual .label-container { stroke: var(--vscode-errorForeground) !important; } - .diagram-container .mermaid .edgeLabel .lj-state-error-method, + .diagram-container .mermaid .edgeLabel .lj-call-allowed { + color: var(--lj-state-valid) !important; + } + .diagram-container .mermaid .edgeLabel .lj-call-rejected, .diagram-container .mermaid .edgeLabel .lj-unmet-condition { - color: var(--vscode-errorForeground) !important; + color: var(--lj-state-invalid) !important; } .diagram-container .mermaid svg path.lj-self-loop { stroke-width: 1.5px !important; } - .diagram-container .mermaid svg path.lj-state-error-transition { - stroke: var(--vscode-errorForeground) !important; + .diagram-container .mermaid svg path.lj-call-allowed-transition, + .diagram-container .mermaid svg marker.lj-call-allowed-marker path { + stroke: var(--lj-state-valid) !important; } - .diagram-container .mermaid svg marker.lj-state-error-marker path { - fill: var(--vscode-errorForeground) !important; - stroke: var(--vscode-errorForeground) !important; + .diagram-container .mermaid svg marker.lj-call-allowed-marker path { + fill: var(--lj-state-valid) !important; + } + .diagram-container .mermaid svg path.lj-call-rejected-transition, + .diagram-container .mermaid svg marker.lj-call-rejected-marker path { + stroke: var(--lj-state-invalid) !important; + } + .diagram-container .mermaid svg marker.lj-call-rejected-marker path { + fill: var(--lj-state-invalid) !important; } .diagram-container .mermaid svg .ljStateInvalid .label-container { fill: color-mix(in srgb, var(--lj-state-invalid) 10%, var(--vscode-editor-background)) !important; @@ -1362,5 +1372,42 @@ export function getStyles(): string { border: 1px dashed; border-color: var(--lj-state-accepted); } + /* the arrows: a line with its head, and a cross before the rejected one, as on the diagram */ + .diagram-error-legend-allowed, + .diagram-error-legend-rejected { + position: relative; + width: 1.1rem; + height: 2px; + border-radius: 0; + background: var(--lj-call-colour); + } + .diagram-error-legend-allowed::after, + .diagram-error-legend-rejected::after { + content: ''; + position: absolute; + right: -1px; + top: -3px; + border-left: 6px solid var(--lj-call-colour); + border-top: 4px solid transparent; + border-bottom: 4px solid transparent; + } + .diagram-error-legend-allowed { + --lj-call-colour: var(--lj-state-valid); + } + .diagram-error-legend-rejected { + --lj-call-colour: var(--lj-state-invalid); + margin-left: 0.9rem; + } + .diagram-error-legend-rejected::before { + content: '✕'; + position: absolute; + right: 100%; + top: 50%; + margin-right: 0.15rem; + transform: translateY(-50%); + font-size: 0.75rem; + line-height: 1; + color: var(--lj-call-colour); + } `; } diff --git a/client/src/webview/views/fsm/fsm.spec.ts b/client/src/webview/views/fsm/fsm.spec.ts index 1d4110c..6bdbae5 100644 --- a/client/src/webview/views/fsm/fsm.spec.ts +++ b/client/src/webview/views/fsm/fsm.spec.ts @@ -169,12 +169,13 @@ describe('state machine view', () => { }); it.each([ - ['invalid, valid, and other accepted states', ['bound', 'closed'], ['\u2715 Actual state, not expected', '\u2713 Actual state, expected', 'Expected state']], - ['only an invalid and other accepted states', ['closed'], ['\u2715 Actual state, not expected', 'Expected state']], - ['an unknown receiver state', ['unbound', 'bound', 'connected', 'closed'], ['Expected state']], - ])('explains the state marks in a legend with %s', (_, possibleStates, items) => { + ['invalid, valid, and other accepted states', ['bound', 'closed'], ['\u2715 Actual state, not expected', '\u2713 Actual state, expected', 'Expected state', 'Where receive() is allowed', 'The call, not allowed here']], + ['only an invalid and other accepted states', ['closed'], ['\u2715 Actual state, not expected', 'Expected state', 'Where receive() is allowed', 'The call, not allowed here']], + ['an unknown receiver state', ['unbound', 'bound', 'connected', 'closed'], ['Expected state', 'Where receive() is allowed']], + ])('explains the state marks and the arrows of the call in a legend with %s', (_, possibleStates, items) => { renderStateMachineView(document.body, [machine({ states: ['unbound', 'bound', 'connected', 'closed'], + transitions: [{ from: 'bound', to: 'bound', label: 'receive' }], errorContext: socketError(possibleStates), })], [diagram], 'TB', false, true); @@ -367,7 +368,7 @@ describe('state machine view', () => { .toBe('Invalid method call param.write() \u00B7 expected state tilingSet and compressionExplicit \u00B7 actual state startTiling and compressionExplicit'); expect(document.querySelector('.diagram-state-set-dimmed')).toBeNull(); expect(Array.from(document.querySelectorAll('.diagram-error-legend-item'), node => node.textContent)) - .toEqual(['\u2715 Actual state, not expected', '\u2713 Actual state, expected', 'Expected state']); + .toEqual(['\u2715 Actual state, not expected', '\u2713 Actual state, expected', 'Expected state', 'The call, not allowed here']); }); it('names the state sets in the summary, without those in which every state allows the call', () => { diff --git a/client/src/webview/views/fsm/fsm.ts b/client/src/webview/views/fsm/fsm.ts index 1f06454..b841fac 100644 --- a/client/src/webview/views/fsm/fsm.ts +++ b/client/src/webview/views/fsm/fsm.ts @@ -57,7 +57,7 @@ function renderStateMachineViewHtml(sms: LJStateMachine[], diagrams: string[], o ${errorFocuses.length > 0 - ? renderStateErrorSummary(getStateErrorSummary(sms), stateErrors) + renderStateErrorLegend(errorFocuses, focusOnError) + ? renderStateErrorSummary(getStateErrorSummary(sms), stateErrors) + renderStateErrorLegend(sms, errorFocuses, focusOnError) : isStateErrorShown(stateErrors) ? renderStateErrorSummary(getUnmarkedStateErrorSummary(sms, stateErrors), stateErrors) : ''} ${showConditions && hasConditionExpansions ? /*html*/`
@@ -168,14 +168,23 @@ function renderStateErrorStepper({ index, count }: StateErrorNavigation): string `; } -// explains the state marks with the same outlines, fills, and icons as the diagram, next to the toggle that collapses the rest -function renderStateErrorLegend(focuses: StateErrorFocus[], focusOnError: boolean): string { +// explains the state marks and the arrows of the call with the same outlines, fills, colours, and icons as the diagram, +// next to the toggle that collapses the rest +function renderStateErrorLegend(sms: LJStateMachine[], focuses: StateErrorFocus[], focusOnError: boolean): string { + const hasWrongState = focuses.some(focus => focus.invalidStates.length > 0); + // the diagram draws the transitions of the called method from the states that do not reject it as the allowed call + const hasAllowedCall = sms.some(sm => { + const focus = getStateErrorFocus(sm); + return !!focus && sm.transitions.some(transition => transition.label === focus.calledMethod && !focus.invalidStates.includes(transition.from)); + }); const items = [ - focuses.some(focus => focus.invalidStates.length > 0) ? renderLegendItem('invalid', `${INVALID_STATE_ICON} Actual state, not expected`) : '', + hasWrongState ? renderLegendItem('invalid', `${INVALID_STATE_ICON} Actual state, not expected`) : '', focuses.some(focus => focus.validStates.length > 0) ? renderLegendItem('valid', `${VALID_STATE_ICON} Actual state, expected`) : '', focuses.some(focus => focus.acceptedStates.some(state => !focus.validStates.includes(state))) ? renderLegendItem('accepted', 'Expected state') : '', + hasAllowedCall ? renderLegendItem('allowed', `Where ${escapeHtml(focuses[0].calledMethod ?? '')}() is allowed`) : '', + hasWrongState ? renderLegendItem('rejected', 'The call, not allowed here') : '', ].filter(Boolean); return /*html*/`
From 0989cb4192a65ffd30ed60fbecb71bda861d5d09 Mon Sep 17 00:00:00 2001 From: Catarina Gamboa Date: Wed, 18 Nov 2026 19:23:48 +0000 Subject: [PATCH 2/3] Put the rejected call on the loop its state already has ELK nests a second self-loop around the first one and its label, and the rounding then drew it as a wide ellipse across the diagram (A1_01, top to bottom). No state had two loops before, since the methods between the same states share an edge, so the rejected call now shares the state's loop too, as its first line, red; a state without a loop gets a new one. Co-Authored-By: Claude Fable 5.1 --- client/src/webview/diagram-focus.spec.ts | 10 ++++++++++ client/src/webview/diagram.ts | 19 ++++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/client/src/webview/diagram-focus.spec.ts b/client/src/webview/diagram-focus.spec.ts index cf0af78..4984747 100644 --- a/client/src/webview/diagram-focus.spec.ts +++ b/client/src/webview/diagram-focus.spec.ts @@ -201,6 +201,16 @@ describe('state error focus', () => { expect(diagram.match(/closed --> closed/g)).toHaveLength(1); }); + it('puts the rejected call on the loop a rejecting state already has, first and never dimmed', () => { + const sm = datagramSocket(); + sm.transitions.push({ from: 'closed', to: 'closed', label: 'close' }); + + expect(lines(createMermaidDiagram(sm, 'TB', false, true))).toContain(`closed --> closed : ${rejectedCall('receive')}
${dimmedMethod('close')}`); + const shown = createMermaidDiagram(sm, 'TB', false, false); + expect(lines(shown)).toContain(`closed --> closed : ${rejectedCall('receive')}
close`); + expect(shown.match(/closed --> closed/g)).toHaveLength(1); + }); + it('does not focus without accepted states', () => { const sm = datagramSocket({ acceptedStates: [], validStates: [], invalidStates: [] }); diff --git a/client/src/webview/diagram.ts b/client/src/webview/diagram.ts index 2243882..aa7c741 100644 --- a/client/src/webview/diagram.ts +++ b/client/src/webview/diagram.ts @@ -136,18 +136,23 @@ export function createMermaidDiagram( else if (!group.dimmed.some(method => method.name === name)) group.dimmed.push({ name, label }); }); + // the spec has no transition of the call from a state that rejects it, so the rejected call is drawn as a loop + // on each such state, on the loop the state already has since the layout nests a second one around the first; + // it is no spec transition, so it is never dimmed, collapsed, or counted + if (errorFocus?.calledMethod) { + const rejectedCall = getCallLabel(escapeMermaidLabel(errorFocus.calledMethod), 'rejected'); + errorFocus.invalidStates.filter(state => !rejectedFrom.has(state)).forEach(state => { + const key = `${state}|${state}`; + if (!transitionMap.has(key)) transitionMap.set(key, { bright: [], dimmed: [] }); + transitionMap.get(key)!.bright.unshift(rejectedCall); + }); + } + // add transitions, dimming those whose methods are all dimmed transitionMap.forEach(({ bright, dimmed }, key) => { const [from, to] = key.split('|'); addTransition(` ${from} --> ${to} : ${getCollapsedLabel(bright, dimmed, 'methods')}`, bright.length === 0, from === to); }); - // the spec has no transition of the call from a state that rejects it, so the rejected call is drawn - // as a loop on each such state; it is no spec transition, so it is never dimmed or collapsed - if (errorFocus?.calledMethod) { - const rejectedCall = getCallLabel(escapeMermaidLabel(errorFocus.calledMethod), 'rejected'); - errorFocus.invalidStates.filter(state => !rejectedFrom.has(state)) - .forEach(state => addTransition(` ${state} --> ${state} : ${rejectedCall}`, false, true)); - } if (selfLoops.length > 0) lines.push(`${SELF_LOOPS_COMMENT} ${selfLoops.join(' ')}`); if (!errorFocus) { From 303e50727f087151ce38e6c46f46111a9b5be633 Mon Sep 17 00:00:00 2001 From: Catarina Gamboa Date: Wed, 18 Nov 2026 19:26:47 +0000 Subject: [PATCH 3/3] Colour the called method only when the accepted states are known Without them nothing says where the call is allowed, and the legend is not shown, so a green label had no explanation. The method keeps its plain label in that case; the possible states stay outlined. Co-Authored-By: Claude Fable 5.1 --- client/src/webview/diagram-focus.spec.ts | 4 +++- client/src/webview/diagram.ts | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/client/src/webview/diagram-focus.spec.ts b/client/src/webview/diagram-focus.spec.ts index 4984747..98a8708 100644 --- a/client/src/webview/diagram-focus.spec.ts +++ b/client/src/webview/diagram-focus.spec.ts @@ -260,7 +260,9 @@ describe('state error focus', () => { expect(getStateErrorFocus(sm)).toBeNull(); const diagram = createMermaidDiagram(sm, 'TB', false, true); expect(diagram).toBe(createMermaidDiagram(sm, 'TB', false, false)); - expect(diagram).toContain(`maybeHasChildren --> maybeHasChildren : ${allowedCall('getFirstChild')}`); + // nothing says where the call is allowed, so the called method is not coloured + expect(lines(diagram)).toContain('maybeHasChildren --> maybeHasChildren : getFirstChild'); + expect(diagram).not.toContain('lj-call'); expect(diagram).toContain('class childrenAllowed ljStateErrorActual'); expect(diagram).not.toMatch(/ljStateValid|ljStateInvalid|ljStateAccepted|ljStateDimmed|" as /); }); diff --git a/client/src/webview/diagram.ts b/client/src/webview/diagram.ts index aa7c741..a2672f7 100644 --- a/client/src/webview/diagram.ts +++ b/client/src/webview/diagram.ts @@ -121,12 +121,13 @@ export function createMermaidDiagram( const unmet = hasUnmetCondition(transition, sm, stateSets, rejectingStateSets); const rejected = isCalledMethod && !!errorFocus?.invalidStates.includes(transition.from); if (rejected) rejectedFrom.add(transition.from); + // without the accepted states, nothing says where the call is allowed, and there is no legend to explain a colour const label = getTransitionLabel( transition.label, getFromConditionLabel(transition.fromCondition, stateSets, unmet ? rejectingStateSets : null, showConditions), transition.toCondition, showConditions, - rejected ? 'rejected' : isCalledMethod ? 'allowed' : null, + rejected ? 'rejected' : isCalledMethod && errorFocus ? 'allowed' : null, ); if (!transitionMap.has(key)) transitionMap.set(key, { bright: [], dimmed: [] }); const group = transitionMap.get(key)!;