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..98a8708 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,26 @@ 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('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', () => { @@ -207,7 +231,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 +243,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 +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 : ${calledMethod('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 /); }); @@ -432,19 +460,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..a2672f7 100644 --- a/client/src/webview/diagram.ts +++ b/client/src/webview/diagram.ts @@ -111,18 +111,23 @@ 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); + // 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, - isCalledMethod && !unmet, + rejected ? 'rejected' : isCalledMethod && errorFocus ? 'allowed' : null, ); if (!transitionMap.has(key)) transitionMap.set(key, { bright: [], dimmed: [] }); const group = transitionMap.get(key)!; @@ -132,6 +137,18 @@ 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('|'); @@ -185,26 +202,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 +294,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 +310,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 +479,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 +488,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*/`