feat(f7.1): sequence participant/message diff + rect emission (#22)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -55,9 +55,49 @@ export function parseSequence(source: string): SeqDiagram {
|
||||
return { header: header || "sequenceDiagram", participants: declared, statements };
|
||||
}
|
||||
|
||||
// Replaced in Task 5 with the real diff/emit.
|
||||
export function diffSequence(_beforeSrc: string, currentSrc: string): string {
|
||||
void diffArrays;
|
||||
void CW_COLORS;
|
||||
return currentSrc;
|
||||
function hexToRgb(hex: string): string {
|
||||
const h = hex.replace("#", "");
|
||||
const n = parseInt(h, 16);
|
||||
return `${(n >> 16) & 255}, ${(n >> 8) & 255}, ${n & 255}`;
|
||||
}
|
||||
|
||||
function rectWrap(stmt: string, hex: string): string[] {
|
||||
return [` rect rgb(${hexToRgb(hex)})`, ` ${stmt}`, ` end`];
|
||||
}
|
||||
|
||||
export function diffSequence(beforeSrc: string, currentSrc: string): string {
|
||||
const before = parseSequence(beforeSrc);
|
||||
const current = parseSequence(currentSrc);
|
||||
|
||||
const out: string[] = [current.header];
|
||||
|
||||
// Participants: keep current declarations, then re-declare removed ones (ghosts).
|
||||
for (const p of current.participants) out.push(` participant ${p}`);
|
||||
const curSet = new Set(current.participants);
|
||||
for (const p of before.participants) {
|
||||
if (!curSet.has(p)) out.push(` participant ${p}`);
|
||||
}
|
||||
|
||||
// Message stream diff via LCS over statements; pair adjacent removed+added as changed.
|
||||
const parts = diffArrays(before.statements, current.statements);
|
||||
for (let n = 0; n < parts.length; n++) {
|
||||
const ch = parts[n];
|
||||
if (!ch.added && !ch.removed) {
|
||||
for (const s of ch.value) out.push(` ${s}`);
|
||||
continue;
|
||||
}
|
||||
if (ch.removed) {
|
||||
const next = parts[n + 1];
|
||||
const addVals = next?.added ? next.value : [];
|
||||
const paired = Math.min(ch.value.length, addVals.length);
|
||||
for (let k = 0; k < paired; k++) out.push(...rectWrap(addVals[k], CW_COLORS.changed));
|
||||
for (let k = paired; k < ch.value.length; k++) out.push(...rectWrap(ch.value[k], CW_COLORS.removed));
|
||||
for (let k = paired; k < addVals.length; k++) out.push(...rectWrap(addVals[k], CW_COLORS.added));
|
||||
if (next?.added) n++;
|
||||
continue;
|
||||
}
|
||||
// lone added run
|
||||
for (const s of ch.value) out.push(...rectWrap(s, CW_COLORS.added));
|
||||
}
|
||||
return out.join("\n");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user