From 00ac35050bf15777e23726c368b1bfb2ac03221b Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Thu, 19 Nov 2026 22:15:22 -0800 Subject: [PATCH 1/6] feat(diff): render code diffs in diff embeds with Pierre diffs Code diffs in ```diff fences now render through @pierre/diffs: syntax highlighting by file type, word-level changes, and per-file headers for multi-file git diffs, themed with Sim's surface and badge tokens. Our parser still validates the fence and resolves sim: sources; its hunks are written back out as a well-formed patch, so agent diffs without hunk line numbers keep working with line numbers hidden and hunk headings shown. DiffView keeps only the side-by-side document comparison, which Pierre does not cover. --- apps/sim/components/diff/diff-embed.tsx | 5 +- apps/sim/components/diff/diff-view.tsx | 122 ++-------------------- apps/sim/components/diff/patch-view.tsx | 130 ++++++++++++++++++++++++ apps/sim/package.json | 1 + bun.lock | 15 ++- 5 files changed, 154 insertions(+), 119 deletions(-) create mode 100644 apps/sim/components/diff/patch-view.tsx diff --git a/apps/sim/components/diff/diff-embed.tsx b/apps/sim/components/diff/diff-embed.tsx index 7553780b77a..feac1b485ac 100644 --- a/apps/sim/components/diff/diff-embed.tsx +++ b/apps/sim/components/diff/diff-embed.tsx @@ -7,6 +7,7 @@ import { getErrorMessage } from '@sim/utils/errors' import Link from 'next/link' import { useParams } from 'next/navigation' import { DiffView } from '@/components/diff/diff-view' +import { PatchView } from '@/components/diff/patch-view' import { type DiffSource, parseUnifiedDiff, sameSource, type UnifiedDiff } from '@/lib/diff/unified' import { workspaceResourcePath } from '@/lib/resources' import { useDocumentQuery } from '@/hooks/queries/kb/knowledge' @@ -189,7 +190,7 @@ function DocumentComparison({ diff, oldHeading, newHeading, wrapLines }: Documen - + ) } @@ -201,7 +202,7 @@ function CodeDiff({ diff, heading, wrapLines }: CodeDiffProps) { - + ) } diff --git a/apps/sim/components/diff/diff-view.tsx b/apps/sim/components/diff/diff-view.tsx index 8c3b593b2f7..05523102a8f 100644 --- a/apps/sim/components/diff/diff-view.tsx +++ b/apps/sim/components/diff/diff-view.tsx @@ -17,8 +17,6 @@ const DEL_ROW = 'relative bg-[color-mix(in_srgb,var(--badge-error-bg)_18%,transparent)] before:absolute before:inset-y-0 before:left-0 before:w-[2px] before:bg-[var(--badge-error-text)]' const ADD_WORD = 'rounded-sm bg-[color-mix(in_srgb,var(--badge-success-bg)_70%,transparent)]' const DEL_WORD = 'rounded-sm bg-[color-mix(in_srgb,var(--badge-error-bg)_70%,transparent)]' -const GUTTER = - 'select-none py-0.5 pr-2 pl-2 text-right text-[var(--text-muted)] text-caption tabular-nums' const MARKER = 'select-none py-0.5 text-center text-caption' interface Segment { @@ -37,13 +35,11 @@ type ProseItem = interface DiffViewProps { hunks: DiffHunk[] wrapLines?: boolean - prose?: boolean } interface LineTextProps { line: DiffLine segments: Segment[] | undefined - prose: boolean } interface HighlightedTextProps { @@ -59,11 +55,7 @@ interface CollapsedRunProps { row?: number } -interface DiffCellProps extends Omit { - wrapLines: boolean -} -interface UnifiedRowProps extends Omit { - numbered: boolean +interface DiffCellProps extends LineTextProps { wrapLines: boolean } interface ProseColumnProps { @@ -79,12 +71,6 @@ interface HunkHeadingProps { hunk: DiffHunk row?: number } -interface HunkRowsProps { - hunk: DiffHunk - index: number - numbered: boolean - wrapLines: boolean -} /** Pairs removed and added lines within each change block for bounded word-level highlighting. */ function wordSegments(lines: DiffLine[]): Map { @@ -140,15 +126,8 @@ function HighlightedText({ text, offset, segments, type }: HighlightedTextProps) return <>{children} } -function LineText({ line, segments, prose }: LineTextProps) { +function LineText({ line, segments }: LineTextProps) { const changeLabel = line.type === 'add' ? 'Added: ' : line.type === 'del' ? 'Removed: ' : null - if (!prose) - return ( - <> - {changeLabel} - - - ) const prefix = /^#{1,6}\s+/.exec(line.text)?.[0] ?? '' const text = line.text.slice(prefix.length) let offset = prefix.length @@ -290,40 +269,12 @@ function DiffCell({ line, segments, wrapLines }: DiffCellProps) { wrapLines ? 'whitespace-pre-wrap break-words' : 'whitespace-pre' )} > - + ) } -function UnifiedRow({ line, segments, numbered, wrapLines }: UnifiedRowProps) { - return ( -
- {numbered && ( - - {line.oldLine ?? ''} - - )} - {numbered && ( - - {line.newLine ?? ''} - - )} - - {marker(line)} - - - - -
- ) -} - function ProseColumn({ items, side, wrapLines }: ProseColumnProps) { const [scrollElement, setScrollElement] = useState(null) const scrollEdges = useScrollEdges(scrollElement, { axis: 'x' }) @@ -426,68 +377,7 @@ function ProseRows({ hunks, wrapLines }: ProseRowsProps) { ) } -function HunkRows({ hunk, index, numbered, wrapLines }: HunkRowsProps) { - const [expanded, setExpanded] = useState>(() => new Set()) - const segments = wordSegments(hunk.lines) - const items = visibleItems(hunk.lines, (start) => expanded.has(start)) - return ( - <> - {(hunk.file || hunk.heading || index > 0) && } - {items.map((item) => - 'collapsed' in item ? ( - setExpanded((current) => new Set(current).add(item.key))} - /> - ) : ( - - ) - )} - - ) -} - -/** Unified file edits and aligned prose comparisons share highlights and collapsible context. */ -export function DiffView({ hunks, wrapLines = true, prose = false }: DiffViewProps) { - const [scrollElement, setScrollElement] = useState(null) - const scrollEdges = useScrollEdges(scrollElement, { axis: 'x' }) - if (prose) return - const numbered = hunks.some((hunk) => - hunk.lines.some((line) => line.oldLine !== undefined || line.newLine !== undefined) - ) - const columns = numbered - ? 'grid-cols-[minmax(3.5ch,auto)_minmax(3.5ch,auto)_24px_minmax(0,1fr)]' - : 'grid-cols-[24px_minmax(0,1fr)]' - return ( -
-
- {hunks.map((hunk, index) => ( - - ))} -
-
- ) +/** Two documents side by side, aligned row for row, with word highlights and collapsible context. */ +export function DiffView({ hunks, wrapLines = true }: DiffViewProps) { + return } diff --git a/apps/sim/components/diff/patch-view.tsx b/apps/sim/components/diff/patch-view.tsx new file mode 100644 index 00000000000..b3d631c7176 --- /dev/null +++ b/apps/sim/components/diff/patch-view.tsx @@ -0,0 +1,130 @@ +'use client' + +import { type CSSProperties, useMemo } from 'react' +import { type FileDiffOptions, parsePatchFiles } from '@pierre/diffs' +import { FileDiff } from '@pierre/diffs/react' +import { useTheme } from 'next-themes' +import type { DiffHunk, UnifiedDiff } from '@/lib/diff/unified' + +interface PatchViewProps { + diff: UnifiedDiff + /** Names the file when the diff carries no path, so its language is still inferred. */ + fallbackName: string + wrapLines: boolean +} + +/** + * Maps Sim's tokens onto Pierre's custom properties. Custom properties inherit through the + * component's shadow root, so this is the one place the diff takes Sim's palette and fonts. + */ +const SIM_DIFF_THEME = { + '--diffs-font-family': 'var(--font-mono)', + '--diffs-header-font-family': 'var(--font-season)', + '--diffs-font-size': '13px', + '--diffs-line-height': '22px', + '--diffs-bg-context-override': 'var(--surface-2)', + '--diffs-bg-context-gutter-override': 'var(--surface-2)', + '--diffs-bg-separator-override': 'var(--surface-3)', + '--diffs-bg-buffer-override': 'var(--surface-3)', + '--diffs-addition-color-override': 'var(--badge-success-text)', + '--diffs-deletion-color-override': 'var(--badge-error-text)', + '--diffs-bg-addition-override': + 'color-mix(in srgb, var(--badge-success-bg) 18%, var(--surface-2))', + '--diffs-bg-addition-number-override': + 'color-mix(in srgb, var(--badge-success-bg) 18%, var(--surface-2))', + '--diffs-bg-addition-emphasis-override': + 'color-mix(in srgb, var(--badge-success-bg) 70%, transparent)', + '--diffs-bg-deletion-override': 'color-mix(in srgb, var(--badge-error-bg) 18%, var(--surface-2))', + '--diffs-bg-deletion-number-override': + 'color-mix(in srgb, var(--badge-error-bg) 18%, var(--surface-2))', + '--diffs-bg-deletion-emphasis-override': + 'color-mix(in srgb, var(--badge-error-bg) 70%, transparent)', + '--diffs-fg-number-override': 'var(--text-muted)', +} as CSSProperties + +function hunkRange(start: number, count: number): string { + return `${count === 0 ? Math.max(start - 1, 0) : start},${count}` +} + +/** + * Pierre takes its base background from the syntax theme and sets it inline, so only an + * `!important` rule inside its shadow root puts the diff on Sim's card surface. + */ +const SIM_SURFACE_CSS = + ':host, pre { --diffs-light-bg: var(--surface-2) !important; --diffs-dark-bg: var(--surface-2) !important; }' + +/** + * Writes one file's hunks back out as a well-formed patch. Agent-written diffs often omit hunk + * line numbers, which Pierre requires, so a hunk without them starts one line past the hunk + * before it: the one-line gap is what makes Pierre draw the separator carrying its heading. The + * caller hides line numbers in that case, so the invented ones never show. + */ +function serializeFile(name: string, hunks: DiffHunk[]): string { + const rows = [`diff --git a/${name} b/${name}`, `--- a/${name}`, `+++ b/${name}`] + let nextOld = 1 + let nextNew = 1 + for (const hunk of hunks) { + const oldStart = hunk.lines.find((line) => line.oldLine !== undefined)?.oldLine ?? nextOld + 1 + const newStart = hunk.lines.find((line) => line.newLine !== undefined)?.newLine ?? nextNew + 1 + const oldCount = hunk.lines.filter((line) => line.type !== 'add').length + const newCount = hunk.lines.filter((line) => line.type !== 'del').length + rows.push( + `@@ -${hunkRange(oldStart, oldCount)} +${hunkRange(newStart, newCount)} @@${hunk.heading ? ` ${hunk.heading}` : ''}` + ) + for (const line of hunk.lines) + rows.push(`${line.type === 'add' ? '+' : line.type === 'del' ? '-' : ' '}${line.text}`) + nextOld = oldStart + oldCount + nextNew = newStart + newCount + } + return rows.join('\n') +} + +function toPatch(diff: UnifiedDiff, fallbackName: string): string { + const files = new Map() + for (const hunk of diff.hunks) { + const name = hunk.file ?? diff.path ?? fallbackName + files.set(name, [...(files.get(name) ?? []), hunk]) + } + return [...files].map(([name, hunks]) => serializeFile(name, hunks)).join('\n') +} + +/** + * A code diff rendered by Pierre: syntax highlighted by file type, with word-level changes and + * collapsible context. A multi-file git diff renders one titled section per file; a single file + * relies on the card header the embed already draws. + */ +export function PatchView({ diff, fallbackName, wrapLines }: PatchViewProps) { + const { forcedTheme, resolvedTheme } = useTheme() + const numbered = diff.hunks.some((hunk) => + hunk.lines.some((line) => line.oldLine !== undefined || line.newLine !== undefined) + ) + const files = useMemo(() => { + const parsed = parsePatchFiles(toPatch(diff, fallbackName)).flatMap((patch) => patch.files) + if (numbered) return parsed + return parsed.map((file) => ({ + ...file, + hunks: file.hunks.map((hunk, index) => ({ + ...hunk, + hunkSpecs: hunk.hunkContext || (index > 0 ? '⋯' : undefined), + })), + })) + }, [diff, fallbackName, numbered]) + const options: FileDiffOptions = { + diffStyle: 'unified', + diffIndicators: 'bars', + lineDiffType: 'word', + overflow: wrapLines ? 'wrap' : 'scroll', + themeType: (forcedTheme ?? resolvedTheme) === 'dark' ? 'dark' : 'light', + disableLineNumbers: !numbered, + disableFileHeader: files.length === 1, + hunkSeparators: 'metadata', + unsafeCSS: SIM_SURFACE_CSS, + } + return ( +
+ {files.map((file, index) => ( + + ))} +
+ ) +} diff --git a/apps/sim/package.json b/apps/sim/package.json index 88dbe16f6ba..388727e403e 100644 --- a/apps/sim/package.json +++ b/apps/sim/package.json @@ -139,6 +139,7 @@ "@opentelemetry/sdk-node": "0.223.0", "@opentelemetry/sdk-trace": "2.12.0", "@opentelemetry/semantic-conventions": "^1.44.0", + "@pierre/diffs": "1.6.4", "@radix-ui/react-avatar": "1.3.1", "@radix-ui/react-checkbox": "1.4.1", "@radix-ui/react-collapsible": "1.2.1", diff --git a/bun.lock b/bun.lock index 47bb817a57f..3ad519e8ab4 100644 --- a/bun.lock +++ b/bun.lock @@ -283,6 +283,7 @@ "@opentelemetry/sdk-node": "0.223.0", "@opentelemetry/sdk-trace": "2.12.0", "@opentelemetry/semantic-conventions": "^1.44.0", + "@pierre/diffs": "1.6.4", "@radix-ui/react-avatar": "1.3.1", "@radix-ui/react-checkbox": "1.4.1", "@radix-ui/react-collapsible": "1.2.1", @@ -1855,6 +1856,14 @@ "@peculiar/webcrypto": ["@peculiar/webcrypto@1.7.1", "", { "dependencies": { "@peculiar/asn1-schema": "^2.7.0", "@peculiar/json-schema": "^1.1.12", "@peculiar/utils": "^2.0.2", "tslib": "^2.8.1", "webcrypto-core": "^1.9.2" } }, "sha512-ODOov0sGMJMf3jPonOkgGqPknTsu+DdQ7kD++gz8aI+aFMOMHFbWAA2taqXXVTdP+OTOQR/znGvSpmkeI0WTYQ=="], + "@pierre/diffs": ["@pierre/diffs@1.6.4", "", { "dependencies": { "@pierre/highlights": "0.4.0", "@pierre/theme": "2.0.0", "@pierre/theming": "1.0.2", "diff": "9.0.0", "hast-util-to-html": "9.0.5", "lru_map": "0.4.1", "shiki": "^3.0.0 || ^4.0.0" }, "peerDependencies": { "react": "^18.3.1 || ^19.0.0", "react-dom": "^18.3.1 || ^19.0.0" }, "optionalPeers": ["react", "react-dom"] }, "sha512-RsKoruCOp50qaDM3a7BQan5K77xMkGWT1NF01fiYP1PfT3v4CvIjwytNk5WjZG0BesBkMOP2UbrBIDigB1k0ug=="], + + "@pierre/highlights": ["@pierre/highlights@0.4.0", "", {}, "sha512-2a38H7ep3KweGi1fGQpJk+Krod2OMpDIXagKPlqBd1Y9P460T7zQmc66Wm2JAH1SpIG93wCyyhZVrkc5dnAJVQ=="], + + "@pierre/theme": ["@pierre/theme@2.0.0", "", {}, "sha512-yNDd9GYLQl1mEUJR8AneJ5e4ohLIHQd/wZLWr4fagt78vS2RwwZNW530vVgHqXFAyFVcFlRmGUD5ramXH46OXw=="], + + "@pierre/theming": ["@pierre/theming@1.0.2", "", { "peerDependencies": { "@pierre/theme": "^1.1.0 || ^2.0.0", "@shikijs/themes": "^3.0.0 || ^4.0.0", "react": "^18.3.1 || ^19.0.0", "react-dom": "^18.3.1 || ^19.0.0", "shiki": "^3.0.0 || ^4.0.0" }, "optionalPeers": ["@pierre/theme", "@shikijs/themes", "react", "react-dom", "shiki"] }, "sha512-RmXLLqtx31UGcR8kZyO5hGAQ1EXz6loT1Cu0BFAGsbnBboIxp2BlomkLfWyvdT4WZXnBGyu25onL1gKpFQuSUw=="], + "@pinojs/redact": ["@pinojs/redact@0.4.0", "", {}, "sha512-k2ENnmBugE/rzQfEcdWHcCY+/FM3VLzH9cYEsbdsoqrvzAKRhUZeRNhAZvB8OitQJ1TBed3yqWtdjzS6wJKBwg=="], "@playwright/test": ["@playwright/test@1.64.1", "", { "dependencies": { "playwright": "1.64.1" }, "bin": { "playwright": "cli.js" } }, "sha512-IaYGml/nSOiZWROfPtuIL7neuu9qDOZ3TNvNljUIrhDMxiGTgPjzj/g9p9RxpS2tSepPDRY+UwAaEeiKtUvIAg=="], @@ -3357,7 +3366,7 @@ "hast-util-to-estree": ["hast-util-to-estree@3.1.3", "", { "dependencies": { "@types/estree": "^1.0.0", "@types/estree-jsx": "^1.0.0", "@types/hast": "^3.0.0", "comma-separated-tokens": "^2.0.0", "devlop": "^1.0.0", "estree-util-attach-comments": "^3.0.0", "estree-util-is-identifier-name": "^3.0.0", "hast-util-whitespace": "^3.0.0", "mdast-util-mdx-expression": "^2.0.0", "mdast-util-mdx-jsx": "^3.0.0", "mdast-util-mdxjs-esm": "^2.0.0", "property-information": "^7.0.0", "space-separated-tokens": "^2.0.0", "style-to-js": "^1.0.0", "unist-util-position": "^5.0.0", "zwitch": "^2.0.0" } }, "sha512-48+B/rJWAp0jamNbAAf9M7Uf//UVqAoMmgXhBdxTDJLGKY+LRnZ99qcG+Qjl5HfMpYNzS5v4EAwVEF34LeAj7w=="], - "hast-util-to-html": ["hast-util-to-html@9.0.6", "", { "dependencies": { "@types/hast": "^3.0.0", "@types/unist": "^3.0.0", "ccount": "^2.0.0", "comma-separated-tokens": "^2.0.0", "hast-util-whitespace": "^3.0.0", "html-void-elements": "^3.0.0", "mdast-util-to-hast": "^13.0.0", "property-information": "^7.0.0", "space-separated-tokens": "^2.0.0", "stringify-entities": "^4.0.0", "zwitch": "^2.0.4" } }, "sha512-1SZUxCN3TtJAE7e2BQN2MBHsoZ1gnmi9GoCP5Di59CSWptIezSAW09yDZyVbanb78V9eCwrKqdQsjtfVTxYHWA=="], + "hast-util-to-html": ["hast-util-to-html@9.0.5", "", { "dependencies": { "@types/hast": "^3.0.0", "@types/unist": "^3.0.0", "ccount": "^2.0.0", "comma-separated-tokens": "^2.0.0", "hast-util-whitespace": "^3.0.0", "html-void-elements": "^3.0.0", "mdast-util-to-hast": "^13.0.0", "property-information": "^7.0.0", "space-separated-tokens": "^2.0.0", "stringify-entities": "^4.0.0", "zwitch": "^2.0.4" } }, "sha512-OguPdidb+fbHQSU4Q4ZiLKnzWo8Wwsf5bZfbvu7//a9oTYoqD/fWpe96NuHkoS9h0ccGOTe0C4NGXdtS0iObOw=="], "hast-util-to-jsx-runtime": ["hast-util-to-jsx-runtime@2.3.7", "", { "dependencies": { "@types/estree": "^1.0.0", "@types/hast": "^3.0.0", "@types/unist": "^3.0.0", "comma-separated-tokens": "^2.0.0", "devlop": "^1.0.0", "estree-util-is-identifier-name": "^3.0.0", "hast-util-whitespace": "^3.0.0", "mdast-util-mdx-expression": "^2.0.0", "mdast-util-mdx-jsx": "^3.0.0", "mdast-util-mdxjs-esm": "^2.0.0", "property-information": "^7.0.0", "space-separated-tokens": "^2.0.0", "style-to-object": "^2.0.0", "unist-util-position": "^5.0.0", "vfile-message": "^4.0.0" } }, "sha512-F7DNV2NMcTxA4GqPVHnXzX+mpoPHAIY9rJAVH3CZ1FdBZOcBUEjUuvMkMpHVs/sicEtznh9tMGj5WUR9S4E2rg=="], @@ -3671,6 +3680,8 @@ "lru.min": ["lru.min@1.1.4", "", {}, "sha512-DqC6n3QQ77zdFpCMASA1a3Jlb64Hv2N2DciFGkO/4L9+q/IpIAuRlKOvCXabtRW6cQf8usbmM6BE/TOPysCdIA=="], + "lru_map": ["lru_map@0.4.1", "", {}, "sha512-I+lBvqMMFfqaV8CJCISjI3wbjmwVu/VyOoU7+qtu9d7ioW5klMgsTTiUOUp+DJvfTTzKXoPbyC6YfgkNcyPSOg=="], + "lz-string": ["lz-string@1.5.0", "", { "bin": { "lz-string": "bin/bin.js" } }, "sha512-h5bgJWpxJNswbU7qCrV0tIKQCaS3blPDrqKWx+QxzuzL1zGUzij9XCWLrSLsJPu5t+eWA/ycetzYAO5IOMcWAQ=="], "magic-string": ["magic-string@1.4.3", "", { "dependencies": { "@jridgewell/sourcemap-codec": "^1.6.0" } }, "sha512-z12OxmaPGE0F4xlpGdjuAUckipLpQlTAuAmyGhNoD7ODpYUzvDfvtxUbjM/jwznyP178oju/KDdyi220wNJ0RQ=="], @@ -5049,6 +5060,8 @@ "@redis/client/cluster-key-slot": ["cluster-key-slot@1.1.2", "", {}, "sha512-RMr0FhtfXemyinomL4hrWcYJxmX6deFdCxpJzhDttxgO1+bcCnkk+9drydLVDmAMG7NE6aN/fl4F7ucU/90gAA=="], + "@shikijs/core/hast-util-to-html": ["hast-util-to-html@9.0.6", "", { "dependencies": { "@types/hast": "^3.0.0", "@types/unist": "^3.0.0", "ccount": "^2.0.0", "comma-separated-tokens": "^2.0.0", "hast-util-whitespace": "^3.0.0", "html-void-elements": "^3.0.0", "mdast-util-to-hast": "^13.0.0", "property-information": "^7.0.0", "space-separated-tokens": "^2.0.0", "stringify-entities": "^4.0.0", "zwitch": "^2.0.4" } }, "sha512-1SZUxCN3TtJAE7e2BQN2MBHsoZ1gnmi9GoCP5Di59CSWptIezSAW09yDZyVbanb78V9eCwrKqdQsjtfVTxYHWA=="], + "@shuding/opentype.js/fflate": ["fflate@0.7.5", "", {}, "sha512-QieYf//cis6ywHNi5qW1+PXPQ4bC+XVJAtS4AXIML8P76GroEiOxm/oQtn1f02UkJY1+KsXMJcC+R2v/Eg4G3g=="], "@smithy/core/tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="], From fe7976260cd6f8f6fd62575f6f5fcc20faa569fb Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Thu, 19 Nov 2026 22:35:05 -0800 Subject: [PATCH 2/6] feat(diff): one diff renderer and one syntax palette across Sim Workflow comparison field diffs now render through Pierre too, on the full before and after text: syntax highlighted by field (code language, JSON, prose), word-level changes, and expandable folds. The hand-rolled line engine in text-diff-lines.ts and its test are gone; the size guard, the one-sided cap and the expand-all control stay. Both diff surfaces render through SimFileDiff, which owns the theme, the +/- markers and Sim's change colors. Diffs color code like Code does: the Prism palette in code.css is now --code-token-* variables, and the Shiki scope mapping docs already maintained moved into emcn (syntax-theme.ts) so docs and the app share it. Docs output is byte-identical and Prism code blocks are pixel-identical. Embed polish: hunk separators show only the heading, diffs without line numbers keep their first heading, and the patch conversion moved to lib/diff/patch.ts with tests. --- apps/docs/lib/shiki-theme.ts | 168 +------------ apps/docs/scripts/compile-content.ts | 18 +- .../change-list/field-change-row.tsx | 28 ++- .../change-list/text-diff-lines.test.ts | 175 ------------- .../components/change-list/text-diff-lines.ts | 173 ------------- .../components/change-list/text-diff.tsx | 229 ++++++++---------- .../w/components/workflow-diff/utils.ts | 2 +- apps/sim/components/diff/patch-view.tsx | 127 ++-------- apps/sim/components/diff/sim-file-diff.tsx | 74 ++++++ apps/sim/lib/diff/patch.test.ts | 108 +++++++++ apps/sim/lib/diff/patch.ts | 56 +++++ packages/emcn/package.json | 4 + packages/emcn/src/components/code/code.css | 133 ++++------ .../emcn/src/components/code/syntax-theme.ts | 198 +++++++++++++++ 14 files changed, 649 insertions(+), 844 deletions(-) delete mode 100644 apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff-lines.test.ts delete mode 100644 apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff-lines.ts create mode 100644 apps/sim/components/diff/sim-file-diff.tsx create mode 100644 apps/sim/lib/diff/patch.test.ts create mode 100644 apps/sim/lib/diff/patch.ts create mode 100644 packages/emcn/src/components/code/syntax-theme.ts diff --git a/apps/docs/lib/shiki-theme.ts b/apps/docs/lib/shiki-theme.ts index a7cd7004a61..e44bbea6696 100644 --- a/apps/docs/lib/shiki-theme.ts +++ b/apps/docs/lib/shiki-theme.ts @@ -1,35 +1,13 @@ -import type { ThemeRegistrationRaw } from 'shiki' - /** - * Sim's syntax theme for docs code blocks. - * - * The platform highlights code with PrismJS through `@sim/emcn`'s `Code` component, whose - * token colors live in `packages/emcn/src/components/code/code.css`. Docs highlight with - * Shiki, which is TextMate-scoped rather than Prism-tokenized, so parity has to be - * expressed as a scope-to-color mapping rather than a shared stylesheet. The palette follows - * `code.css` in light mode, with darker hues for readable contrast. Dark mode consumes the + * Sim's syntax theme for docs code blocks. The scope mapping is shared with the app in + * `@sim/emcn/syntax-theme`; docs supply their own palettes. Light mode + * follows `code.css` with darker hues for readable contrast, and dark mode consumes the * docs semantic tokens so authored MDX and generated examples share the reading palette. - * - * The mapping was derived by highlighting the same snippets through both engines and - * comparing output. Two places where the grammars genuinely disagree, and the choice made: - * - * - Prism only colors a shell command when it recognises the binary, so `npm` highlights - * but `sim` does not. TextMate scopes the first word of a statement as the command - * regardless, so `sim` highlights too. Shiki's reading is the correct one and is kept. - * - TextMate scopes every bare shell argument as `string.unquoted.argument`, which would - * paint most of a command line in the string color. Prism leaves those plain, so the - * scope is pinned back to the base foreground and only genuinely quoted strings carry - * the string color. - * - * Bare `variable` is deliberately unmapped. TextMate scopes declared JavaScript - * identifiers as `variable.other.*`, and coloring those would make JS and TS far busier - * than the platform editor, which leaves them at the base foreground. Only the things - * Prism actually calls out — object and config keys, shell flags and shell expansions — - * are mapped to the variable color. */ +import { buildSyntaxTheme, type SyntaxColors } from '@sim/emcn/syntax-theme' /** Light-mode platform hues adjusted to meet WCAG AA text contrast on the code surface. */ -const LIGHT = { +const LIGHT: SyntaxColors = { /** `--text-primary` — the platform's base code foreground. */ foreground: '#1a1a1a', /** `--surface-5` — the fill `chipFieldSurfaceClass` puts under a code surface. */ @@ -43,10 +21,10 @@ const LIGHT = { function: '#854d0e', regex: '#be123c', deleted: '#b91c1c', -} as const +} /** Dark syntax roles resolve through the shared docs theme, including portal surfaces. */ -const DARK = { +const DARK: SyntaxColors = { foreground: 'var(--docs-text)', background: 'var(--docs-surface)', comment: 'var(--docs-green)', @@ -58,138 +36,10 @@ const DARK = { function: 'var(--docs-orange)', regex: 'var(--docs-red)', deleted: 'var(--docs-red)', -} as const - -type TokenColors = typeof LIGHT | typeof DARK - -/** - * Builds a Shiki theme from one palette. Scope order matters: TextMate resolves a token by - * longest matching scope, so the narrower entries below (`string.unquoted.argument`, - * `keyword.operator`) intentionally follow and override the broader ones. - */ -function buildTheme(name: string, type: 'light' | 'dark', c: TokenColors): ThemeRegistrationRaw { - return { - name, - type, - settings: [ - { settings: { background: c.background, foreground: c.foreground } }, - - /** Prism `comment` / `block-comment` / `prolog` / `doctype` / `cdata`. */ - { - scope: ['comment', 'punctuation.definition.comment', 'string.comment'], - settings: { foreground: c.comment }, - }, - - /** Prism `keyword` / `atrule` — plus TextMate's storage scopes, which Prism folds into keywords. */ - { - scope: [ - 'keyword', - 'keyword.control', - 'keyword.other', - 'storage', - 'storage.type', - 'storage.modifier', - 'meta.import keyword', - 'meta.export keyword', - ], - settings: { foreground: c.keyword }, - }, - - /** Prism `function` and `class-name`. */ - { - scope: [ - 'entity.name.function', - 'entity.name.command', - 'support.function', - 'meta.function-call.generic', - 'entity.name.class', - 'entity.name.type', - 'support.class', - 'support.type', - ], - settings: { foreground: c.function }, - }, - - /** - * Prism `property` / `attr-name` / `variable` — object and config keys, markup - * attributes, shell flags and shell expansions. Bare `variable` is excluded on - * purpose; see the module docblock. - */ - { - scope: [ - 'support.type.property-name', - 'meta.object-literal.key', - 'entity.name.tag.yaml', - 'entity.other.attribute-name', - 'constant.other.option', - 'variable.other.normal', - 'variable.other.special', - 'punctuation.definition.variable', - ], - settings: { foreground: c.variable }, - }, - - /** Prism `number` / `boolean` / `constant` / `tag`. */ - { - scope: [ - 'constant.numeric', - 'constant.language', - 'constant.character', - 'support.constant', - 'entity.name.tag', - 'variable.language', - ], - settings: { foreground: c.constant }, - }, - - /** Prism `string` / `char` / `inserted`. */ - { - scope: [ - 'string', - 'string.quoted', - 'string.template', - 'punctuation.definition.string', - 'markup.inserted', - ], - settings: { foreground: c.string }, - }, - - /** - * Bare shell arguments. TextMate calls these strings; Prism leaves them plain, and - * plain is what keeps a command line readable. Declared after `string` so it wins. - */ - { scope: ['string.unquoted.argument'], settings: { foreground: c.foreground } }, - - /** Prism `punctuation` / `operator` / `entity` / `symbol`. */ - { - scope: [ - 'punctuation', - 'meta.brace', - 'keyword.operator', - 'punctuation.separator', - 'punctuation.terminator', - 'punctuation.accessor', - ], - settings: { foreground: c.punctuation }, - }, - - /** Prism `regex` / `important`. */ - { - scope: ['string.regexp', 'constant.regexp', 'keyword.other.important'], - settings: { foreground: c.regex }, - }, - - /** Prism `deleted`, plus TextMate's invalid scopes. */ - { - scope: ['markup.deleted', 'invalid', 'invalid.illegal'], - settings: { foreground: c.deleted }, - }, - ], - } } -const simShikiLight = buildTheme('sim-light', 'light', LIGHT) -const simShikiDark = buildTheme('sim-dark', 'dark', DARK) +const simShikiLight = buildSyntaxTheme('sim-light', 'light', LIGHT) +const simShikiDark = buildSyntaxTheme('sim-dark', 'dark', DARK) /** Shared syntax palette for build-time MDX and generated API examples. */ export const simShikiOptions = { diff --git a/apps/docs/scripts/compile-content.ts b/apps/docs/scripts/compile-content.ts index 1aa1b7b7cfd..502111deab7 100644 --- a/apps/docs/scripts/compile-content.ts +++ b/apps/docs/scripts/compile-content.ts @@ -18,6 +18,11 @@ import { buildSearchCatalog, buildSearchIndex } from '@/lib/search/build' const logger = createLogger('DocsContent') const DOCS_ROOT = path.resolve(import.meta.dirname, '..') +/** The Shiki scope mapping docs highlight with; it lives in emcn, outside the docs tree. */ +const SYNTAX_THEME_FILE = path.resolve( + DOCS_ROOT, + '../../packages/emcn/src/components/code/syntax-theme.ts' +) const CONTENT_ROOT = path.join(DOCS_ROOT, 'content/docs') const OUTPUT_ROOT = path.join(DOCS_ROOT, '.content') const MAX_CONTENT_BYTES = 64 * 1024 * 1024 @@ -197,6 +202,7 @@ async function generate() { path.join(DOCS_ROOT, 'lib/content/markdown.ts'), path.join(DOCS_ROOT, 'lib/content/output.ts'), path.join(DOCS_ROOT, 'lib/shiki-theme.ts'), + SYNTAX_THEME_FILE, path.join(DOCS_ROOT, 'components/workflow-preview/examples.ts'), path.join(DOCS_ROOT, 'components/workflow-preview/block-display-workflows.ts'), path.join(DOCS_ROOT, 'package.json'), @@ -449,6 +455,12 @@ if (process.argv.includes('--watch')) { } } } + const scheduleRefresh = () => { + clearTimeout(timer) + timer = setTimeout(() => { + void refresh() + }, 150) + } watch(DOCS_ROOT, { recursive: true }, (_, filename) => { const changed = filename?.toString().replaceAll('\\', '/') ?? '' if ( @@ -459,11 +471,9 @@ if (process.argv.includes('--watch')) { !['scripts/compile-content.ts', 'lib/shiki-theme.ts', ...OPENAPI_SPEC_FILES].includes(changed) ) return - clearTimeout(timer) - timer = setTimeout(() => { - void refresh() - }, 150) + scheduleRefresh() }) + watch(SYNTAX_THEME_FILE, scheduleRefresh) logger.info('Watching documentation source') await refresh() } else await generate() diff --git a/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/field-change-row.tsx b/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/field-change-row.tsx index ffac8e097c9..024d748ba12 100644 --- a/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/field-change-row.tsx +++ b/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/field-change-row.tsx @@ -2,6 +2,7 @@ import { ChipModalField, ChipTag, cn } from '@sim/emcn' import { ArrowRight } from '@sim/emcn/icons' +import { isValidCodeLanguage } from '@/lib/execution/languages' import { formatValueForDisplay, resolveFieldLabel } from '@/lib/workflows/comparison/resolve-values' import { StructuredValueDiff } from '@/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/structured-value-diff' import { @@ -20,9 +21,30 @@ import { isBlankValue, isSentenceLike, toDiffText, + type ValueKind, } from '@/app/workspace/[workspaceId]/w/components/workflow-diff/utils' +import type { SubBlockConfig } from '@/blocks/types' import { formatParameterLabel } from '@/tools/params' +/** + * The syntax a text diff highlights as: JSON for structured values, a code field's language + * (the block's own `language` setting first, as the editor does), and plain text for prose. + */ +function diffLanguage( + config: SubBlockConfig | undefined, + kind: ValueKind, + values: Record +): string { + if (kind === 'json') return 'json' + if (config?.type !== 'code') return 'text' + const selected = values.language + const language = + typeof selected === 'string' && isValidCodeLanguage(selected) + ? selected + : (config.language ?? 'javascript') + return language === 'shell' ? 'bash' : language +} + interface FieldChangeRowProps { blockType: string scope: 'block' | 'subblock' | 'container' @@ -117,7 +139,11 @@ export function FieldChangeRow({ /> )} {textual && !sameDisplayValue && !emptyTextChange && ( - + )} {scalar && !sameDisplayValue && diff --git a/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff-lines.test.ts b/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff-lines.test.ts deleted file mode 100644 index 72849a14fc1..00000000000 --- a/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff-lines.test.ts +++ /dev/null @@ -1,175 +0,0 @@ -/** - * @vitest-environment node - */ -import { describe, expect, it } from 'vitest' -import { - buildDiffRows, - capOneSided, - type DiffLine, - foldRows, - MAX_DIFF_LINES, - markWordChanges, - ONE_SIDED_VISIBLE_LINES, - splitLines, - toLines, -} from '@/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff-lines' - -const context = (count: number): DiffLine[] => - Array.from({ length: count }, (_, index) => ({ kind: 'context', text: `ctx ${index}` })) - -describe('splitLines and toLines', () => { - it('drops only the trailing newline and keeps interior blank lines', () => { - expect(splitLines('')).toEqual(['']) - expect(splitLines('a\n')).toEqual(['a']) - expect(splitLines('a\n\nb\n')).toEqual(['a', '', 'b']) - }) - - it('flattens a line diff into kinds', () => { - expect(toLines('a\nb\n', 'a\nc\n')).toEqual([ - { kind: 'context', text: 'a' }, - { kind: 'removed', text: 'b' }, - { kind: 'added', text: 'c' }, - ]) - }) -}) - -describe('foldRows', () => { - it('keeps two lines of context on each side of a change and folds the rest', () => { - const rows = foldRows([...context(6), { kind: 'added', text: 'x' }, ...context(6)]) - - expect(rows.map((row) => row.type)).toEqual([ - 'fold', - 'line', - 'line', - 'line', - 'line', - 'line', - 'fold', - ]) - expect(rows[0]).toMatchObject({ type: 'fold', lines: context(6).slice(0, 4) }) - expect(rows[6]).toMatchObject({ type: 'fold', lines: context(6).slice(2) }) - }) - - it('never folds a run too short to be worth it', () => { - const rows = foldRows([...context(3), { kind: 'removed', text: 'x' }, ...context(5)]) - expect(rows.every((row) => row.type === 'line')).toBe(true) - }) -}) - -describe('markWordChanges', () => { - it('marks the words that differ when most of a line pair survived', () => { - const [removed, added] = markWordChanges([ - { kind: 'removed', text: 'reply warmly in one sentence' }, - { kind: 'added', text: 'reply warmly in two sentences' }, - ]) - - /* Whitespace is its own token, so the unchanged space between them stays unmarked. */ - expect(removed.parts?.filter((part) => part.changed).map((part) => part.value)).toEqual([ - 'one', - 'sentence', - ]) - expect(added.parts?.filter((part) => part.changed).map((part) => part.value)).toEqual([ - 'two', - 'sentences', - ]) - }) - - it('keeps an indentation-only edit visible', () => { - const [removed, added] = markWordChanges([ - { kind: 'removed', text: ' return result' }, - { kind: 'added', text: ' return result' }, - ]) - - expect(removed.parts?.map((part) => part.value).join('')).toBe(' return result') - expect(added.parts?.map((part) => part.value).join('')).toBe(' return result') - expect(added.parts?.some((part) => part.changed)).toBe(true) - }) - - it('leaves a rewrite and an oversized pair as plain lines', () => { - const rewrite = markWordChanges([ - { kind: 'removed', text: 'alpha beta gamma' }, - { kind: 'added', text: 'one two three four' }, - ]) - expect(rewrite.every((line) => line.parts === undefined)).toBe(true) - - const huge = markWordChanges([ - { kind: 'removed', text: 'a '.repeat(3000) }, - { kind: 'added', text: `${'a '.repeat(2999)}b` }, - ]) - expect(huge.every((line) => line.parts === undefined)).toBe(true) - }) - - it('pairs only as many lines as both runs have', () => { - const lines = markWordChanges([ - { kind: 'removed', text: 'keep this one' }, - { kind: 'removed', text: 'keep this two' }, - { kind: 'added', text: 'keep this three' }, - ]) - expect(lines[0].parts).toBeDefined() - expect(lines[1].parts).toBeUndefined() - expect(lines[2].parts).toBeDefined() - }) -}) - -describe('capOneSided', () => { - it('tucks a long one-sided body behind one tail row and folds mixed bodies normally', () => { - const added = Array.from({ length: ONE_SIDED_VISIBLE_LINES + 3 }, (_, index) => ({ - kind: 'added' as const, - text: `line ${index}`, - })) - const rows = capOneSided(added) - expect(rows).toHaveLength(ONE_SIDED_VISIBLE_LINES + 1) - expect(rows[ONE_SIDED_VISIBLE_LINES]).toMatchObject({ type: 'tail' }) - expect(rows[ONE_SIDED_VISIBLE_LINES]).toHaveProperty('lines.length', 3) - - expect( - capOneSided(added.slice(0, ONE_SIDED_VISIBLE_LINES)).every((row) => row.type === 'line') - ).toBe(true) - expect( - capOneSided([...added, { kind: 'context', text: 'c' }]).some((row) => row.type === 'tail') - ).toBe(false) - }) -}) - -describe('buildDiffRows', () => { - it('renders a brand new body as added lines only', () => { - expect(buildDiffRows('', 'return 1\n')).toEqual([ - { type: 'line', line: { kind: 'added', text: 'return 1' } }, - ]) - }) - - it('never caps two identical long bodies as if they were new', () => { - const body = Array.from({ length: ONE_SIDED_VISIBLE_LINES + 5 }, (_, i) => `l${i}`).join('\n') - const rows = buildDiffRows(body, body) - expect(rows.some((row) => row.type === 'tail')).toBe(false) - expect(rows.every((row) => row.type === 'line' || row.type === 'fold')).toBe(true) - expect(buildDiffRows('', '')).toEqual([]) - }) - - it('summarizes bodies too long to diff inline', () => { - const lines = Array.from({ length: MAX_DIFF_LINES }, (_, i) => `a${i}`).join('\n') - expect(buildDiffRows(lines, `${lines}\nb`)).toEqual([ - { type: 'oversized', oldLines: MAX_DIFF_LINES, newLines: MAX_DIFF_LINES + 1 }, - ]) - }) - it('gives up on word marks for a heavily rewritten pair instead of diffing it word by word', () => { - const before = Array.from({ length: 150 }, (_, i) => `alpha${i}`).join(' ') - const after = Array.from({ length: 150 }, (_, i) => `beta${i}`).join(' ') - const lines = markWordChanges(toLines(before, after)) - - expect(lines.map((line) => line.kind)).toEqual(['removed', 'added']) - expect(lines.every((line) => line.parts === undefined)).toBe(true) - }) - it('reads CRLF and bare CR endings as line breaks', () => { - const rows = buildDiffRows('a\r\nb\rc', 'a\nb\nd') - - expect( - rows.map((row) => (row.type === 'line' ? [row.line.kind, row.line.text] : row.type)) - ).toEqual([ - ['context', 'a'], - ['context', 'b'], - ['removed', 'c'], - ['added', 'd'], - ]) - }) -}) diff --git a/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff-lines.ts b/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff-lines.ts deleted file mode 100644 index e8fa856aa36..00000000000 --- a/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff-lines.ts +++ /dev/null @@ -1,173 +0,0 @@ -import { diffLines, diffWordsWithSpace } from 'diff' - -/** Lines of context kept around each change before the rest folds away. */ -export const CONTEXT_LINES = 2 -/** An unchanged run shorter than this is never worth folding. */ -export const MIN_FOLD_LINES = 4 -/** - * A body that is entirely one side (a new block's code, a removed block's - * prompt) has no unchanged run to fold, so it shows this many lines and - * tucks the rest behind one expander instead of swallowing the card. - */ -export const ONE_SIDED_VISIBLE_LINES = 12 -/** Past this share of changed characters a line pair is a rewrite, not an edit, so no word marks. */ -const MAX_WORD_HIGHLIGHT_RATIO = 0.6 -/** - * Word diffing is quadratic in the worst case; a pair of lines longer than - * this together is shown as plain removed and added lines instead of - * stalling the pane on a pathological prompt. - */ -const MAX_WORD_DIFF_CHARS = 4000 -/** - * Word marks only help when a line pair differs by a few words, so the word - * diff gives up past this many edits. That caps each pair's work at a small - * multiple of its length, so many long, heavily rewritten line pairs cannot - * add up to a stall the way a per-line size limit alone would allow. - */ -const MAX_WORD_EDITS = 64 -/** - * Line diffing is quadratic in the worst case too; two bodies with more lines - * than this between them are summarized instead of diffed so opening a - * comparison never hangs the tab. - */ -export const MAX_DIFF_LINES = 2000 - -export interface DiffLine { - kind: 'added' | 'removed' | 'context' - text: string - /** Word-level parts when this line pairs with its counterpart on the other side */ - parts?: Array<{ value: string; changed: boolean }> -} - -export type DiffRow = - | { type: 'line'; line: DiffLine } - | { type: 'fold'; lines: DiffLine[] } - | { type: 'tail'; lines: DiffLine[] } - | { type: 'oversized'; oldLines: number; newLines: number } - -/** - * Pairs each run of removed lines with the run of added lines that follows it, - * line by line, and marks the words that differ within each pair. A one-word - * edit inside a long sentence then reads as that word, not as the whole line - * leaving and coming back. Whitespace counts as a word so an indentation-only - * edit still shows. - */ -export function markWordChanges(lines: DiffLine[]): DiffLine[] { - const out = [...lines] - let index = 0 - while (index < out.length) { - if (out[index].kind !== 'removed') { - index += 1 - continue - } - let removedEnd = index - while (removedEnd < out.length && out[removedEnd].kind === 'removed') removedEnd += 1 - let addedEnd = removedEnd - while (addedEnd < out.length && out[addedEnd].kind === 'added') addedEnd += 1 - const pairs = Math.min(removedEnd - index, addedEnd - removedEnd) - for (let offset = 0; offset < pairs; offset += 1) { - const removed = out[index + offset] - const added = out[removedEnd + offset] - const total = removed.text.length + added.text.length - if (total === 0 || total > MAX_WORD_DIFF_CHARS) continue - const words = diffWordsWithSpace(removed.text, added.text, { - maxEditLength: MAX_WORD_EDITS, - }) - if (!words) continue - const changedChars = words - .filter((part) => part.added || part.removed) - .reduce((sum, part) => sum + part.value.length, 0) - if (changedChars / total > MAX_WORD_HIGHLIGHT_RATIO) continue - out[index + offset] = { - ...removed, - parts: words - .filter((part) => !part.added) - .map((part) => ({ value: part.value, changed: Boolean(part.removed) })), - } - out[removedEnd + offset] = { - ...added, - parts: words - .filter((part) => !part.removed) - .map((part) => ({ value: part.value, changed: Boolean(part.added) })), - } - } - index = addedEnd - } - return out -} - -function normalizeLineEndings(value: string): string { - return value.replace(/\r\n?/g, '\n') -} - -export function splitLines(value: string): string[] { - const lines = value.split('\n') - if (lines.length > 1 && lines[lines.length - 1] === '') lines.pop() - return lines -} - -export function toLines(oldText: string, newText: string): DiffLine[] { - const out: DiffLine[] = [] - for (const part of diffLines(oldText, newText)) { - const kind: DiffLine['kind'] = part.added ? 'added' : part.removed ? 'removed' : 'context' - for (const text of splitLines(part.value)) out.push({ kind, text }) - } - return out -} - -/** Folds long unchanged runs, keeping a little context on either side of each change. */ -export function foldRows(lines: DiffLine[]): DiffRow[] { - const rows: DiffRow[] = [] - let run: DiffLine[] = [] - const flush = (isTail: boolean) => { - if (run.length === 0) return - const head = rows.length === 0 ? 0 : CONTEXT_LINES - const tail = isTail ? 0 : CONTEXT_LINES - if (run.length - head - tail >= MIN_FOLD_LINES) { - for (const line of run.slice(0, head)) rows.push({ type: 'line', line }) - rows.push({ type: 'fold', lines: run.slice(head, run.length - tail) }) - for (const line of run.slice(run.length - tail)) rows.push({ type: 'line', line }) - } else { - for (const line of run) rows.push({ type: 'line', line }) - } - run = [] - } - for (const line of lines) { - if (line.kind === 'context') { - run.push(line) - } else { - flush(false) - rows.push({ type: 'line', line }) - } - } - flush(true) - return rows -} - -/** Caps a body that is all added or all removed at a visible head plus one expander for the rest. */ -export function capOneSided(lines: DiffLine[]): DiffRow[] { - const oneSided = - lines.length > 0 && - lines[0].kind !== 'context' && - lines.every((line) => line.kind === lines[0].kind) - if (!oneSided || lines.length <= ONE_SIDED_VISIBLE_LINES) return foldRows(lines) - return [ - ...lines.slice(0, ONE_SIDED_VISIBLE_LINES).map((line): DiffRow => ({ type: 'line', line })), - { type: 'tail', lines: lines.slice(ONE_SIDED_VISIBLE_LINES) }, - ] -} - -/** - * The rows a text diff renders for two bodies: line diff, word marks, folds and - * the one-sided cap, or a single summary row when the bodies are too long to - * diff inline. - */ -export function buildDiffRows(rawOld: string, rawNew: string): DiffRow[] { - /* Stored text may carry CRLF or bare CR endings; diff them as the lines they display as. */ - const oldText = normalizeLineEndings(rawOld) - const newText = normalizeLineEndings(rawNew) - const oldLines = splitLines(oldText).length - const newLines = splitLines(newText).length - if (oldLines + newLines > MAX_DIFF_LINES) return [{ type: 'oversized', oldLines, newLines }] - return capOneSided(markWordChanges(toLines(oldText, newText))) -} diff --git a/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff.tsx b/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff.tsx index 67cad80fe97..dcdba5dab5e 100644 --- a/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff.tsx +++ b/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff.tsx @@ -1,163 +1,130 @@ 'use client' import { useMemo, useState } from 'react' -import { Button, Code, cn } from '@sim/emcn' +import { parseDiffFromFile } from '@pierre/diffs' +import { Button, cn } from '@sim/emcn' import { diffWordsWithSpace } from 'diff' -import { - buildDiffRows, - type DiffLine, -} from '@/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff-lines' +import { SimFileDiff } from '@/components/diff/sim-file-diff' -/** Folds beyond this many in one field get a single "expand all" control. */ -const MANY_FOLDS = 3 - -const LINE_CLASS: Record = { - added: 'bg-[var(--badge-success-bg)] text-[var(--text-primary)]', - removed: 'bg-[var(--badge-error-bg)] text-[var(--text-primary)]', - context: 'text-[var(--text-secondary)]', -} - -const GUTTER_CLASS: Record = { - added: 'text-[var(--badge-success-text)]', - removed: 'text-[var(--text-error)]', - context: 'text-[var(--text-muted)]', -} +/** + * Line diffing is quadratic in the worst case; two bodies with more lines than this between + * them are summarized instead of diffed, so opening a comparison never hangs the tab. + */ +const MAX_DIFF_LINES = 2000 +/** Unchanged lines kept around each change; the rest fold behind Pierre's expanders. */ +const CONTEXT_LINES = 2 +/** + * A body that is entirely one side (a new block's code, a removed block's prompt) has no + * unchanged run to fold, so it shows this many lines and tucks the rest behind one expander. + */ +const ONE_SIDED_VISIBLE_LINES = 12 +/** Past this many changed regions, one control expands or collapses them all. */ +const MANY_REGIONS = 3 -const GUTTER_SIGN: Record = { - added: '+', - removed: '−', - context: ' ', +/** + * A field value as the lines it displays as: CRLF and bare CR become LF, and the text ends in + * a newline, so a missing final newline is never reported as a change of its own. + */ +function asLines(text: string): string { + const normalized = text.replace(/\r\n?/g, '\n') + return normalized === '' || normalized.endsWith('\n') ? normalized : `${normalized}\n` } -interface FoldRowProps { - count: number - /** What the hidden lines are: unchanged context, or the rest of a one-sided body */ - kind: 'unchanged' | 'more' - onExpand: () => void +function lineCount(text: string): number { + return text === '' ? 0 : text.replace(/\n$/, '').split('\n').length } -function FoldRow({ count, kind, onExpand }: FoldRowProps) { - return ( - - ) +function firstLines(text: string, count: number): string { + return `${text.split('\n').slice(0, count).join('\n')}\n` } interface TextDiffProps { oldText: string newText: string + /** Syntax for highlighting, such as `javascript` or `json`; `text` for prose. */ + language: string } /** - * A unified line diff with a sign gutter. Unchanged runs fold to a count and - * expand on click, so a long prompt shows only the hunks that moved. + * A unified line diff of two bodies of text, highlighted in the field's language. Unchanged + * runs fold to a count and expand in place, so a long prompt shows only the hunks that moved. */ -export function TextDiff({ oldText, newText }: TextDiffProps) { - const rows = useMemo(() => buildDiffRows(oldText, newText), [oldText, newText]) - const [expanded, setExpanded] = useState>(() => new Set()) - const [expandedAll, setExpandedAll] = useState(false) - // Fold indexes belong to one pair of bodies; a new comparison starts folded again. - const [foldsFor, setFoldsFor] = useState({ oldText, newText }) - if (foldsFor.oldText !== oldText || foldsFor.newText !== newText) { - setFoldsFor({ oldText, newText }) - setExpanded(new Set()) - setExpandedAll(false) +export function TextDiff({ oldText, newText, language }: TextDiffProps) { + const before = asLines(oldText) + const after = asLines(newText) + const oldLines = lineCount(before) + const newLines = lineCount(after) + const oneSidedLines = before === '' ? newLines : after === '' ? oldLines : 0 + const [showRest, setShowRest] = useState(false) + const [expandAll, setExpandAll] = useState(false) + /* Expansion belongs to one pair of bodies; a new comparison starts folded again. */ + const [expandedFor, setExpandedFor] = useState({ before, after }) + if (expandedFor.before !== before || expandedFor.after !== after) { + setExpandedFor({ before, after }) + setShowRest(false) + setExpandAll(false) } - const foldCount = rows.filter((row) => row.type === 'fold').length + const capped = oneSidedLines > ONE_SIDED_VISIBLE_LINES && !showRest + const oversized = oldLines + newLines > MAX_DIFF_LINES + const fileDiff = useMemo(() => { + if (oversized) return null + /* An empty side is absent, not one blank line, so a new or removed body is all one side. */ + const side = (name: string, text: string) => + text === '' + ? null + : { + name, + contents: capped ? firstLines(text, ONE_SIDED_VISIBLE_LINES) : text, + lang: language, + } + return parseDiffFromFile(side('before', before), side('after', after), { + context: CONTEXT_LINES, + }) + }, [before, after, language, capped, oversized]) + + if (!fileDiff) + return ( +

+ Too long to compare line by line: {oldLines} lines before, {newLines} lines after. +

+ ) + const regions = fileDiff.hunks.length return ( - - {foldCount > MANY_FOLDS && ( +
+ {regions > MANY_REGIONS && ( + )} + + {capped && ( + )} - {rows.map((row, index) => { - if (row.type === 'oversized') { - return ( -
- Too long to compare line by line: {row.oldLines} lines before, {row.newLines} lines - after. -
- ) - } - if (row.type === 'fold' || row.type === 'tail') { - if (!expandedAll && !expanded.has(index)) { - return ( - setExpanded((prev) => new Set(prev).add(index))} - /> - ) - } - return row.lines.map((line, lineIndex) => ( - - )) - } - return - })} - - ) -} - -interface DiffLineRowProps { - line: DiffLine -} - -function DiffLineRow({ line }: DiffLineRowProps) { - return ( -
- - {GUTTER_SIGN[line.kind]} - - - {line.parts - ? line.parts.map((part, index) => ( - - {part.value} - - )) - : line.text || ' '} -
) } diff --git a/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/utils.ts b/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/utils.ts index 85fa733a235..80244a7cf37 100644 --- a/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/utils.ts +++ b/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/utils.ts @@ -24,7 +24,7 @@ import type { SubBlockConfig } from '@/blocks/types' import type { BlockState, WorkflowState } from '@/stores/workflows/workflow/types' /** How a changed value should be rendered in the change list. */ -type ValueKind = 'text' | 'scalar' | 'json' | 'secret' | 'toggle' | 'structured' +export type ValueKind = 'text' | 'scalar' | 'json' | 'secret' | 'toggle' | 'structured' const INLINE_TEXT_MAX_LENGTH = 60 diff --git a/apps/sim/components/diff/patch-view.tsx b/apps/sim/components/diff/patch-view.tsx index b3d631c7176..8db561ef7a0 100644 --- a/apps/sim/components/diff/patch-view.tsx +++ b/apps/sim/components/diff/patch-view.tsx @@ -1,10 +1,9 @@ 'use client' -import { type CSSProperties, useMemo } from 'react' -import { type FileDiffOptions, parsePatchFiles } from '@pierre/diffs' -import { FileDiff } from '@pierre/diffs/react' -import { useTheme } from 'next-themes' -import type { DiffHunk, UnifiedDiff } from '@/lib/diff/unified' +import { useMemo } from 'react' +import { SimFileDiff } from '@/components/diff/sim-file-diff' +import { toFileDiffs } from '@/lib/diff/patch' +import type { UnifiedDiff } from '@/lib/diff/unified' interface PatchViewProps { diff: UnifiedDiff @@ -14,116 +13,28 @@ interface PatchViewProps { } /** - * Maps Sim's tokens onto Pierre's custom properties. Custom properties inherit through the - * component's shadow root, so this is the one place the diff takes Sim's palette and fonts. - */ -const SIM_DIFF_THEME = { - '--diffs-font-family': 'var(--font-mono)', - '--diffs-header-font-family': 'var(--font-season)', - '--diffs-font-size': '13px', - '--diffs-line-height': '22px', - '--diffs-bg-context-override': 'var(--surface-2)', - '--diffs-bg-context-gutter-override': 'var(--surface-2)', - '--diffs-bg-separator-override': 'var(--surface-3)', - '--diffs-bg-buffer-override': 'var(--surface-3)', - '--diffs-addition-color-override': 'var(--badge-success-text)', - '--diffs-deletion-color-override': 'var(--badge-error-text)', - '--diffs-bg-addition-override': - 'color-mix(in srgb, var(--badge-success-bg) 18%, var(--surface-2))', - '--diffs-bg-addition-number-override': - 'color-mix(in srgb, var(--badge-success-bg) 18%, var(--surface-2))', - '--diffs-bg-addition-emphasis-override': - 'color-mix(in srgb, var(--badge-success-bg) 70%, transparent)', - '--diffs-bg-deletion-override': 'color-mix(in srgb, var(--badge-error-bg) 18%, var(--surface-2))', - '--diffs-bg-deletion-number-override': - 'color-mix(in srgb, var(--badge-error-bg) 18%, var(--surface-2))', - '--diffs-bg-deletion-emphasis-override': - 'color-mix(in srgb, var(--badge-error-bg) 70%, transparent)', - '--diffs-fg-number-override': 'var(--text-muted)', -} as CSSProperties - -function hunkRange(start: number, count: number): string { - return `${count === 0 ? Math.max(start - 1, 0) : start},${count}` -} - -/** - * Pierre takes its base background from the syntax theme and sets it inline, so only an - * `!important` rule inside its shadow root puts the diff on Sim's card surface. - */ -const SIM_SURFACE_CSS = - ':host, pre { --diffs-light-bg: var(--surface-2) !important; --diffs-dark-bg: var(--surface-2) !important; }' - -/** - * Writes one file's hunks back out as a well-formed patch. Agent-written diffs often omit hunk - * line numbers, which Pierre requires, so a hunk without them starts one line past the hunk - * before it: the one-line gap is what makes Pierre draw the separator carrying its heading. The - * caller hides line numbers in that case, so the invented ones never show. - */ -function serializeFile(name: string, hunks: DiffHunk[]): string { - const rows = [`diff --git a/${name} b/${name}`, `--- a/${name}`, `+++ b/${name}`] - let nextOld = 1 - let nextNew = 1 - for (const hunk of hunks) { - const oldStart = hunk.lines.find((line) => line.oldLine !== undefined)?.oldLine ?? nextOld + 1 - const newStart = hunk.lines.find((line) => line.newLine !== undefined)?.newLine ?? nextNew + 1 - const oldCount = hunk.lines.filter((line) => line.type !== 'add').length - const newCount = hunk.lines.filter((line) => line.type !== 'del').length - rows.push( - `@@ -${hunkRange(oldStart, oldCount)} +${hunkRange(newStart, newCount)} @@${hunk.heading ? ` ${hunk.heading}` : ''}` - ) - for (const line of hunk.lines) - rows.push(`${line.type === 'add' ? '+' : line.type === 'del' ? '-' : ' '}${line.text}`) - nextOld = oldStart + oldCount - nextNew = newStart + newCount - } - return rows.join('\n') -} - -function toPatch(diff: UnifiedDiff, fallbackName: string): string { - const files = new Map() - for (const hunk of diff.hunks) { - const name = hunk.file ?? diff.path ?? fallbackName - files.set(name, [...(files.get(name) ?? []), hunk]) - } - return [...files].map(([name, hunks]) => serializeFile(name, hunks)).join('\n') -} - -/** - * A code diff rendered by Pierre: syntax highlighted by file type, with word-level changes and - * collapsible context. A multi-file git diff renders one titled section per file; a single file - * relies on the card header the embed already draws. + * A code diff: syntax highlighted by file type, with word-level changes. A multi-file git diff + * renders one titled section per file; a single file relies on the card header the embed + * already draws. */ export function PatchView({ diff, fallbackName, wrapLines }: PatchViewProps) { - const { forcedTheme, resolvedTheme } = useTheme() const numbered = diff.hunks.some((hunk) => hunk.lines.some((line) => line.oldLine !== undefined || line.newLine !== undefined) ) - const files = useMemo(() => { - const parsed = parsePatchFiles(toPatch(diff, fallbackName)).flatMap((patch) => patch.files) - if (numbered) return parsed - return parsed.map((file) => ({ - ...file, - hunks: file.hunks.map((hunk, index) => ({ - ...hunk, - hunkSpecs: hunk.hunkContext || (index > 0 ? '⋯' : undefined), - })), - })) - }, [diff, fallbackName, numbered]) - const options: FileDiffOptions = { - diffStyle: 'unified', - diffIndicators: 'bars', - lineDiffType: 'word', - overflow: wrapLines ? 'wrap' : 'scroll', - themeType: (forcedTheme ?? resolvedTheme) === 'dark' ? 'dark' : 'light', - disableLineNumbers: !numbered, - disableFileHeader: files.length === 1, - hunkSeparators: 'metadata', - unsafeCSS: SIM_SURFACE_CSS, - } + const files = useMemo(() => toFileDiffs(diff, fallbackName), [diff, fallbackName]) return ( -
+
{files.map((file, index) => ( - + ))}
) diff --git a/apps/sim/components/diff/sim-file-diff.tsx b/apps/sim/components/diff/sim-file-diff.tsx new file mode 100644 index 00000000000..e6365bf9cc5 --- /dev/null +++ b/apps/sim/components/diff/sim-file-diff.tsx @@ -0,0 +1,74 @@ +'use client' + +import '@sim/emcn/components/code/code.css' +import type { CSSProperties } from 'react' +import { type FileDiffMetadata, type FileDiffOptions, registerCustomTheme } from '@pierre/diffs' +import { FileDiff } from '@pierre/diffs/react' +import { buildSyntaxTheme, CODE_TOKEN_COLORS } from '@sim/emcn/syntax-theme' +import { useTheme } from 'next-themes' + +const SYNTAX_THEME = 'sim-code' + +/** + * Pierre highlights with Shiki, so it takes Sim's syntax colors as a TextMate theme built on + * `code.css`'s variables: diffs color code exactly like `Code` does, in both themes. + */ +registerCustomTheme(SYNTAX_THEME, async () => + buildSyntaxTheme(SYNTAX_THEME, 'light', { + ...CODE_TOKEN_COLORS, + background: 'var(--surface-2)', + }) +) + +/** + * Maps Sim's surfaces, change colors and fonts onto Pierre's custom properties, which inherit + * through its shadow root. Row and word tints derive from the two change colors. + */ +const DIFF_TOKENS = { + '--diffs-font-family': 'var(--font-mono)', + '--diffs-header-font-family': 'var(--font-season)', + '--diffs-font-size': '13px', + '--diffs-line-height': '22px', + '--diffs-bg-context-override': 'var(--surface-2)', + '--diffs-bg-context-gutter-override': 'var(--surface-2)', + '--diffs-bg-separator-override': 'var(--surface-3)', + '--diffs-bg-buffer-override': 'var(--surface-3)', + '--diffs-fg-number-override': 'var(--text-muted)', + '--diffs-addition-color-override': 'var(--badge-success-text)', + '--diffs-deletion-color-override': 'var(--badge-error-text)', +} as CSSProperties + +/** What a surface decides; everything else is the same for every diff Sim renders. */ +type SimFileDiffOptions = Pick< + FileDiffOptions, + 'disableFileHeader' | 'disableLineNumbers' | 'expandUnchanged' | 'hunkSeparators' +> + +interface SimFileDiffProps { + fileDiff: FileDiffMetadata + wrapLines?: boolean + options?: SimFileDiffOptions +} + +/** + * One file's changes in Sim's diff style: unified, with `+`/`−` markers, word-level emphasis + * and Sim's syntax colors. Every diff surface renders through this so they stay identical. + */ +export function SimFileDiff({ fileDiff, wrapLines = true, options }: SimFileDiffProps) { + const { forcedTheme, resolvedTheme } = useTheme() + return ( + + ) +} diff --git a/apps/sim/lib/diff/patch.test.ts b/apps/sim/lib/diff/patch.test.ts new file mode 100644 index 00000000000..a4dee8e77bd --- /dev/null +++ b/apps/sim/lib/diff/patch.test.ts @@ -0,0 +1,108 @@ +/** + * @vitest-environment node + */ +import { describe, expect, it } from 'vitest' +import { toFileDiffs } from '@/lib/diff/patch' +import { parseUnifiedDiff } from '@/lib/diff/unified' + +const agentEdit = `--- sim:file/onboarding ++++ sim:file/onboarding +@@ Getting started @@ + ## Getting started +-Invite your team from settings. ++Invite your team from Settings → Members. ++New members get the Editor role. +@@ Billing @@ + Plans are billed monthly. +-Contact sales for annual pricing. ++Annual plans save 20%. +@@ +-Old footer. ++New footer.` + +function render(source: string) { + return toFileDiffs(parseUnifiedDiff(source), 'notes.md') +} + +describe('toFileDiffs', () => { + it('keeps every line of a diff without line numbers, in order and on its side', () => { + const [file] = render(agentEdit) + expect(file.name).toBe('notes.md') + expect(file.deletionLines.map((line) => line.trimEnd())).toEqual([ + '## Getting started', + 'Invite your team from settings.', + 'Plans are billed monthly.', + 'Contact sales for annual pricing.', + 'Old footer.', + ]) + expect(file.additionLines.map((line) => line.trimEnd())).toEqual([ + '## Getting started', + 'Invite your team from Settings → Members.', + 'New members get the Editor role.', + 'Plans are billed monthly.', + 'Annual plans save 20%.', + 'New footer.', + ]) + }) + + it('separates every hunk that has a heading or follows another, showing only the heading', () => { + const [file] = render(agentEdit) + expect(file.hunks.map((hunk) => [hunk.hunkSpecs, hunk.collapsedBefore > 0])).toEqual([ + ['Getting started', true], + ['Billing', true], + ['⋯', true], + ]) + }) + + it('draws no separator above a leading hunk that has no heading', () => { + const [file] = render('-old line\n+new line') + expect(file.hunks).toHaveLength(1) + expect(file.hunks[0].collapsedBefore).toBe(0) + }) + + it('keeps the real line numbers of a git diff', () => { + const [file] = render(`--- a/src/usage.ts ++++ b/src/usage.ts +@@ -12,3 +12,4 @@ export function check() { + const a = 1 +-const b = 2 ++const b = 3 ++const c = 4 + return a`) + expect(file.name).toBe('src/usage.ts') + expect(file.hunks[0]).toMatchObject({ + deletionStart: 12, + deletionCount: 3, + additionStart: 12, + additionCount: 4, + hunkSpecs: 'export function check() {', + }) + }) + + it('accepts a hunk that only adds lines', () => { + const [file] = render(`--- a/new.ts ++++ b/new.ts +@@ -0,0 +1,2 @@ ++export const a = 1 ++export const b = 2`) + expect(file.hunks[0]).toMatchObject({ deletionCount: 0, additionCount: 2, additionStart: 1 }) + expect(file.additionLines).toHaveLength(2) + }) + + it('splits a multi-file git diff into one diff per file', () => { + const files = render(`diff --git a/app/main.py b/app/main.py +--- a/app/main.py ++++ b/app/main.py +@@ -1,2 +1,2 @@ +-import os ++import sys + x = 1 +diff --git a/config.json b/config.json +--- a/config.json ++++ b/config.json +@@ -2,1 +2,1 @@ +- "a": 1 ++ "a": 2`) + expect(files.map((file) => file.name)).toEqual(['app/main.py', 'config.json']) + }) +}) diff --git a/apps/sim/lib/diff/patch.ts b/apps/sim/lib/diff/patch.ts new file mode 100644 index 00000000000..cae737d8f67 --- /dev/null +++ b/apps/sim/lib/diff/patch.ts @@ -0,0 +1,56 @@ +import { type FileDiffMetadata, parsePatchFiles } from '@pierre/diffs' +import type { DiffHunk, UnifiedDiff } from '@/lib/diff/unified' + +/** Marks a break between hunks that has no heading of its own. */ +const UNNAMED_HUNK = '⋯' + +function hunkRange(start: number, count: number): string { + return `${count === 0 ? Math.max(start - 1, 0) : start},${count}` +} + +/** + * Writes one file's hunks back out as a well-formed patch. Agent-written diffs often omit hunk + * line numbers, which Pierre requires, so a hunk without them is numbered after the hunk + * before it. Pierre draws a hunk's heading only above a gap, so a hunk that has a heading, or + * follows another, starts one line later; the caller hides line numbers in that case, so the + * invented ones never show. + */ +function serializeFile(name: string, hunks: DiffHunk[]): string { + const rows = [`diff --git a/${name} b/${name}`, `--- a/${name}`, `+++ b/${name}`] + let nextOld = 1 + let nextNew = 1 + for (const [index, hunk] of hunks.entries()) { + const gap = hunk.heading || index > 0 ? 1 : 0 + const oldStart = hunk.lines.find((line) => line.oldLine !== undefined)?.oldLine ?? nextOld + gap + const newStart = hunk.lines.find((line) => line.newLine !== undefined)?.newLine ?? nextNew + gap + const oldCount = hunk.lines.filter((line) => line.type !== 'add').length + const newCount = hunk.lines.filter((line) => line.type !== 'del').length + rows.push( + `@@ -${hunkRange(oldStart, oldCount)} +${hunkRange(newStart, newCount)} @@${hunk.heading ? ` ${hunk.heading}` : ''}` + ) + for (const line of hunk.lines) + rows.push(`${line.type === 'add' ? '+' : line.type === 'del' ? '-' : ' '}${line.text}`) + nextOld = oldStart + oldCount + nextNew = newStart + newCount + } + return rows.join('\n') +} + +/** + * Pierre's file diffs for a parsed unified diff, one per file. Each hunk separator shows the + * hunk's heading rather than its raw `@@` range. + */ +export function toFileDiffs(diff: UnifiedDiff, fallbackName: string): FileDiffMetadata[] { + const files = new Map() + for (const hunk of diff.hunks) { + const name = hunk.file ?? diff.path ?? fallbackName + files.set(name, [...(files.get(name) ?? []), hunk]) + } + const patch = [...files].map(([name, hunks]) => serializeFile(name, hunks)).join('\n') + return parsePatchFiles(patch) + .flatMap((parsed) => parsed.files) + .map((file) => ({ + ...file, + hunks: file.hunks.map((hunk) => ({ ...hunk, hunkSpecs: hunk.hunkContext || UNNAMED_HUNK })), + })) +} diff --git a/packages/emcn/package.json b/packages/emcn/package.json index 4ef34ae4058..41c361d270e 100644 --- a/packages/emcn/package.json +++ b/packages/emcn/package.json @@ -26,6 +26,10 @@ "types": "./src/icons/index.ts", "default": "./src/icons/index.ts" }, + "./syntax-theme": { + "types": "./src/components/code/syntax-theme.ts", + "default": "./src/components/code/syntax-theme.ts" + }, "./*": { "types": "./src/*", "default": "./src/*" diff --git a/packages/emcn/src/components/code/code.css b/packages/emcn/src/components/code/code.css index 1c49b13eef8..0026dbd758e 100644 --- a/packages/emcn/src/components/code/code.css +++ b/packages/emcn/src/components/code/code.css @@ -2,83 +2,97 @@ * Code editor syntax token theme. * Cursor/VS Code base colors with Sim's vibrant saturation. * Colors aligned to Sim brand where applicable. - * Applied to elements with .code-editor-theme class. * - * Uses double-class specificity (.code-editor-theme.code-editor-theme) + * The palette is a set of `--code-token-*` variables so Shiki surfaces (diffs, via + * `CODE_TOKEN_COLORS` in `syntax-theme.ts`) color code exactly like Prism does here. + * A `.light` island inside `.dark` redeclares the light palette, matching the app's + * nesting-aware dark variant. + * + * Token rules use double-class specificity (.code-editor-theme.code-editor-theme) * to override PrismJS defaults without !important. */ -.code-editor-theme.code-editor-theme { +:root, +.light { + --code-token-comment: #16a34a; + --code-token-punctuation: #383838; + --code-token-variable: #0891b2; + --code-token-constant: #16a34a; + --code-token-string: #b45309; + --code-token-keyword: #2f55ff; + --code-token-function: #ca8a04; + --code-token-regex: #e11d48; + --code-token-deleted: #dc2626; --code-reference: #1d4ed8; } -.dark .code-editor-theme.code-editor-theme { +.dark { + --code-token-comment: #6ec97d; + --code-token-punctuation: #d4d4d4; + --code-token-variable: #4fc3f7; + --code-token-constant: #a5d6a7; + --code-token-string: #f39c6b; + --code-token-keyword: #2fa1ff; + --code-token-function: #fbbf24; + --code-token-regex: #f87171; + --code-token-deleted: #f87171; --code-reference: #35b6ff; } -/** - * Light mode token colors - Cursor style with Sim vibrancy - */ .code-editor-theme.code-editor-theme .token.comment, .code-editor-theme.code-editor-theme .token.block-comment, .code-editor-theme.code-editor-theme .token.prolog, .code-editor-theme.code-editor-theme .token.doctype, .code-editor-theme.code-editor-theme .token.cdata { - color: #16a34a; + color: var(--code-token-comment); } -.code-editor-theme.code-editor-theme .token.punctuation { - color: #383838; +.code-editor-theme.code-editor-theme .token.punctuation, +.code-editor-theme.code-editor-theme .token.operator, +.code-editor-theme.code-editor-theme .token.entity, +.code-editor-theme.code-editor-theme .token.url, +.code-editor-theme.code-editor-theme .token.symbol { + color: var(--code-token-punctuation); } .code-editor-theme.code-editor-theme .token.property, .code-editor-theme.code-editor-theme .token.attr-name, .code-editor-theme.code-editor-theme .token.variable { - color: #0891b2; + color: var(--code-token-variable); } .code-editor-theme.code-editor-theme .token.tag, .code-editor-theme.code-editor-theme .token.boolean, .code-editor-theme.code-editor-theme .token.number, .code-editor-theme.code-editor-theme .token.constant { - color: #16a34a; + color: var(--code-token-constant); } .code-editor-theme.code-editor-theme .token.string, .code-editor-theme.code-editor-theme .token.char, .code-editor-theme.code-editor-theme .token.builtin, .code-editor-theme.code-editor-theme .token.inserted { - color: #b45309; -} - -.code-editor-theme.code-editor-theme .token.operator, -.code-editor-theme.code-editor-theme .token.entity, -.code-editor-theme.code-editor-theme .token.url { - color: #383838; + color: var(--code-token-string); } .code-editor-theme.code-editor-theme .token.atrule, .code-editor-theme.code-editor-theme .token.attr-value, .code-editor-theme.code-editor-theme .token.keyword { - color: #2f55ff; + color: var(--code-token-keyword); } .code-editor-theme.code-editor-theme .token.function, .code-editor-theme.code-editor-theme .token.class-name { - color: #ca8a04; + color: var(--code-token-function); } .code-editor-theme.code-editor-theme .token.regex, .code-editor-theme.code-editor-theme .token.important { - color: #e11d48; -} - -.code-editor-theme.code-editor-theme .token.symbol { - color: #383838; + color: var(--code-token-regex); } .code-editor-theme.code-editor-theme .token.deleted { - color: #dc2626; + color: var(--code-token-deleted); } /* Platform accent for and {{ENV}} placeholders */ @@ -87,71 +101,6 @@ color: var(--code-reference); } -/** - * Dark mode token colors - Cursor style with Sim vibrancy - */ -.dark .code-editor-theme.code-editor-theme .token.comment, -.dark .code-editor-theme.code-editor-theme .token.block-comment, -.dark .code-editor-theme.code-editor-theme .token.prolog, -.dark .code-editor-theme.code-editor-theme .token.doctype, -.dark .code-editor-theme.code-editor-theme .token.cdata { - color: #6ec97d; -} - -.dark .code-editor-theme.code-editor-theme .token.punctuation { - color: #d4d4d4; -} - -.dark .code-editor-theme.code-editor-theme .token.property, -.dark .code-editor-theme.code-editor-theme .token.attr-name, -.dark .code-editor-theme.code-editor-theme .token.variable { - color: #4fc3f7; -} - -.dark .code-editor-theme.code-editor-theme .token.tag, -.dark .code-editor-theme.code-editor-theme .token.boolean, -.dark .code-editor-theme.code-editor-theme .token.number, -.dark .code-editor-theme.code-editor-theme .token.constant { - color: #a5d6a7; -} - -.dark .code-editor-theme.code-editor-theme .token.string, -.dark .code-editor-theme.code-editor-theme .token.char, -.dark .code-editor-theme.code-editor-theme .token.builtin, -.dark .code-editor-theme.code-editor-theme .token.inserted { - color: #f39c6b; -} - -.dark .code-editor-theme.code-editor-theme .token.operator, -.dark .code-editor-theme.code-editor-theme .token.entity, -.dark .code-editor-theme.code-editor-theme .token.url { - color: #d4d4d4; -} - -.dark .code-editor-theme.code-editor-theme .token.atrule, -.dark .code-editor-theme.code-editor-theme .token.attr-value, -.dark .code-editor-theme.code-editor-theme .token.keyword { - color: #2fa1ff; -} - -.dark .code-editor-theme.code-editor-theme .token.function, -.dark .code-editor-theme.code-editor-theme .token.class-name { - color: #fbbf24; -} - -.dark .code-editor-theme.code-editor-theme .token.regex, -.dark .code-editor-theme.code-editor-theme .token.important { - color: #f87171; -} - -.dark .code-editor-theme.code-editor-theme .token.symbol { - color: #d4d4d4; -} - -.dark .code-editor-theme.code-editor-theme .token.deleted { - color: #f87171; -} - /* Ensure transparent backgrounds */ .code-editor-theme.code-editor-theme .token { background: transparent; diff --git a/packages/emcn/src/components/code/syntax-theme.ts b/packages/emcn/src/components/code/syntax-theme.ts new file mode 100644 index 00000000000..a5fad1cafda --- /dev/null +++ b/packages/emcn/src/components/code/syntax-theme.ts @@ -0,0 +1,198 @@ +/** + * Sim's syntax colors as a TextMate (Shiki) theme, for surfaces that highlight with Shiki + * rather than with Prism through `Code`: docs code blocks and the app's diffs. + * + * Prism tokenizes into its own classes, colored by `code.css`. Shiki assigns TextMate + * scopes, so parity is a scope-to-color mapping, derived by highlighting the same snippets + * through both engines and comparing output. Two places where the grammars genuinely + * disagree, and the choice made: + * + * - Prism only colors a shell command when it recognises the binary, so `npm` highlights + * but `sim` does not. TextMate scopes the first word of a statement as the command + * regardless, so `sim` highlights too. Shiki's reading is the correct one and is kept. + * - TextMate scopes every bare shell argument as `string.unquoted.argument`, which would + * paint most of a command line in the string color. Prism leaves those plain, so the + * scope is pinned back to the base foreground and only genuinely quoted strings carry + * the string color. + * + * Bare `variable` is deliberately unmapped. TextMate scopes declared JavaScript + * identifiers as `variable.other.*`, and coloring those would make JS and TS far busier + * than the platform editor, which leaves them at the base foreground. Only the things + * Prism actually calls out — object and config keys, shell flags and shell expansions — + * are mapped to the variable color. + */ + +/** One color per Prism token role, plus the surface the code sits on. */ +export interface SyntaxColors { + foreground: string + background: string + comment: string + punctuation: string + variable: string + constant: string + string: string + keyword: string + function: string + regex: string + deleted: string +} + +interface SyntaxThemeRule { + scope?: string[] + settings: { foreground?: string; background?: string } +} + +/** A raw TextMate theme; structurally what Shiki's `ThemeRegistrationRaw` accepts. */ +interface SyntaxTheme { + name: string + type: 'light' | 'dark' + settings: SyntaxThemeRule[] +} + +/** + * The token palette `code.css` defines for Prism, as CSS variables. A theme built from it + * colors Shiki output exactly like `Code` and follows light and dark through the same + * variables; the caller supplies the background of the surface it renders on. + */ +export const CODE_TOKEN_COLORS: Omit = { + foreground: 'var(--code-foreground)', + comment: 'var(--code-token-comment)', + punctuation: 'var(--code-token-punctuation)', + variable: 'var(--code-token-variable)', + constant: 'var(--code-token-constant)', + string: 'var(--code-token-string)', + keyword: 'var(--code-token-keyword)', + function: 'var(--code-token-function)', + regex: 'var(--code-token-regex)', + deleted: 'var(--code-token-deleted)', +} + +/** + * Builds a Shiki theme from one palette. Scope order matters: TextMate resolves a token by + * longest matching scope, so the narrower entries below (`string.unquoted.argument`, + * `keyword.operator`) intentionally follow and override the broader ones. + */ +export function buildSyntaxTheme( + name: string, + type: 'light' | 'dark', + c: SyntaxColors +): SyntaxTheme { + return { + name, + type, + settings: [ + { settings: { background: c.background, foreground: c.foreground } }, + + /** Prism `comment` / `block-comment` / `prolog` / `doctype` / `cdata`. */ + { + scope: ['comment', 'punctuation.definition.comment', 'string.comment'], + settings: { foreground: c.comment }, + }, + + /** Prism `keyword` / `atrule` — plus TextMate's storage scopes, which Prism folds into keywords. */ + { + scope: [ + 'keyword', + 'keyword.control', + 'keyword.other', + 'storage', + 'storage.type', + 'storage.modifier', + 'meta.import keyword', + 'meta.export keyword', + ], + settings: { foreground: c.keyword }, + }, + + /** Prism `function` and `class-name`. */ + { + scope: [ + 'entity.name.function', + 'entity.name.command', + 'support.function', + 'meta.function-call.generic', + 'entity.name.class', + 'entity.name.type', + 'support.class', + 'support.type', + ], + settings: { foreground: c.function }, + }, + + /** + * Prism `property` / `attr-name` / `variable` — object and config keys, markup + * attributes, shell flags and shell expansions. Bare `variable` is excluded on + * purpose; see the module docblock. + */ + { + scope: [ + 'support.type.property-name', + 'meta.object-literal.key', + 'entity.name.tag.yaml', + 'entity.other.attribute-name', + 'constant.other.option', + 'variable.other.normal', + 'variable.other.special', + 'punctuation.definition.variable', + ], + settings: { foreground: c.variable }, + }, + + /** Prism `number` / `boolean` / `constant` / `tag`. */ + { + scope: [ + 'constant.numeric', + 'constant.language', + 'constant.character', + 'support.constant', + 'entity.name.tag', + 'variable.language', + ], + settings: { foreground: c.constant }, + }, + + /** Prism `string` / `char` / `inserted`. */ + { + scope: [ + 'string', + 'string.quoted', + 'string.template', + 'punctuation.definition.string', + 'markup.inserted', + ], + settings: { foreground: c.string }, + }, + + /** + * Bare shell arguments. TextMate calls these strings; Prism leaves them plain, and + * plain is what keeps a command line readable. Declared after `string` so it wins. + */ + { scope: ['string.unquoted.argument'], settings: { foreground: c.foreground } }, + + /** Prism `punctuation` / `operator` / `entity` / `symbol`. */ + { + scope: [ + 'punctuation', + 'meta.brace', + 'keyword.operator', + 'punctuation.separator', + 'punctuation.terminator', + 'punctuation.accessor', + ], + settings: { foreground: c.punctuation }, + }, + + /** Prism `regex` / `important`. */ + { + scope: ['string.regexp', 'constant.regexp', 'keyword.other.important'], + settings: { foreground: c.regex }, + }, + + /** Prism `deleted`, plus TextMate's invalid scopes. */ + { + scope: ['markup.deleted', 'invalid', 'invalid.illegal'], + settings: { foreground: c.deleted }, + }, + ], + } +} From 2d64700578f54b33a85a588755c6bf45cc51b8fe Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Thu, 19 Nov 2026 22:41:09 -0800 Subject: [PATCH 3/6] fix(diff): keep a hunk header's own line positions A numbered hunk that only inserts or only deletes has no line carrying the empty side's position, so it was re-derived from the previous hunk and the header pointed at the wrong lines. The parser now keeps the header's starts and the patch conversion emits them verbatim, inventing positions only for diffs written without line numbers. --- apps/sim/lib/diff/patch.test.ts | 24 +++++++++++++++++++ apps/sim/lib/diff/patch.ts | 42 +++++++++++++++++++++++---------- apps/sim/lib/diff/unified.ts | 16 +++++++++++-- 3 files changed, 68 insertions(+), 14 deletions(-) diff --git a/apps/sim/lib/diff/patch.test.ts b/apps/sim/lib/diff/patch.test.ts index a4dee8e77bd..9db6c2fb668 100644 --- a/apps/sim/lib/diff/patch.test.ts +++ b/apps/sim/lib/diff/patch.test.ts @@ -1,6 +1,7 @@ /** * @vitest-environment node */ +import { parsePatchFiles } from '@pierre/diffs' import { describe, expect, it } from 'vitest' import { toFileDiffs } from '@/lib/diff/patch' import { parseUnifiedDiff } from '@/lib/diff/unified' @@ -20,6 +21,8 @@ const agentEdit = `--- sim:file/onboarding -Old footer. +New footer.` +type Hunk = ReturnType[number]['hunks'][number] + function render(source: string) { return toFileDiffs(parseUnifiedDiff(source), 'notes.md') } @@ -89,6 +92,27 @@ describe('toFileDiffs', () => { expect(file.additionLines).toHaveLength(2) }) + it.each([ + [ + 'a pure insertion', + '--- a/big.ts\n+++ b/big.ts\n@@ -100,0 +101,2 @@ function tail() {\n+const a = 1\n+const b = 2', + ], + [ + 'a pure deletion', + '--- a/big.ts\n+++ b/big.ts\n@@ -40,2 +39,0 @@\n-const a = 1\n-const b = 2', + ], + ])('places %s where its header says, as Pierre reads the original patch', (_, source) => { + const [original] = parsePatchFiles(source)[0].files + const [file] = render(source) + const position = ({ deletionStart, deletionCount, additionStart, additionCount }: Hunk) => ({ + deletionStart, + deletionCount, + additionStart, + additionCount, + }) + expect(position(file.hunks[0])).toEqual(position(original.hunks[0])) + }) + it('splits a multi-file git diff into one diff per file', () => { const files = render(`diff --git a/app/main.py b/app/main.py --- a/app/main.py diff --git a/apps/sim/lib/diff/patch.ts b/apps/sim/lib/diff/patch.ts index cae737d8f67..e7fcbb0c645 100644 --- a/apps/sim/lib/diff/patch.ts +++ b/apps/sim/lib/diff/patch.ts @@ -4,14 +4,28 @@ import type { DiffHunk, UnifiedDiff } from '@/lib/diff/unified' /** Marks a break between hunks that has no heading of its own. */ const UNNAMED_HUNK = '⋯' -function hunkRange(start: number, count: number): string { - return `${count === 0 ? Math.max(start - 1, 0) : start},${count}` +/** + * One side of a hunk header and the line after it. A header's own start is kept verbatim + * (for an empty side it names the line before the change, as unified diffs do); a hunk + * without one is placed at `next`. + */ +function side( + headerStart: number | undefined, + next: number, + count: number +): { range: string; next: number } { + if (headerStart !== undefined) + return { + range: `${headerStart},${count}`, + next: (count === 0 ? headerStart + 1 : headerStart) + count, + } + return { range: `${count === 0 ? next - 1 : next},${count}`, next: next + count } } /** * Writes one file's hunks back out as a well-formed patch. Agent-written diffs often omit hunk - * line numbers, which Pierre requires, so a hunk without them is numbered after the hunk - * before it. Pierre draws a hunk's heading only above a gap, so a hunk that has a heading, or + * line numbers, which Pierre requires, so a hunk without them is placed after the hunk before + * it. Pierre draws a hunk's heading only above a gap, so such a hunk that has a heading, or * follows another, starts one line later; the caller hides line numbers in that case, so the * invented ones never show. */ @@ -21,17 +35,21 @@ function serializeFile(name: string, hunks: DiffHunk[]): string { let nextNew = 1 for (const [index, hunk] of hunks.entries()) { const gap = hunk.heading || index > 0 ? 1 : 0 - const oldStart = hunk.lines.find((line) => line.oldLine !== undefined)?.oldLine ?? nextOld + gap - const newStart = hunk.lines.find((line) => line.newLine !== undefined)?.newLine ?? nextNew + gap - const oldCount = hunk.lines.filter((line) => line.type !== 'add').length - const newCount = hunk.lines.filter((line) => line.type !== 'del').length - rows.push( - `@@ -${hunkRange(oldStart, oldCount)} +${hunkRange(newStart, newCount)} @@${hunk.heading ? ` ${hunk.heading}` : ''}` + const oldSide = side( + hunk.range?.oldStart, + nextOld + gap, + hunk.lines.filter((line) => line.type !== 'add').length + ) + const newSide = side( + hunk.range?.newStart, + nextNew + gap, + hunk.lines.filter((line) => line.type !== 'del').length ) + rows.push(`@@ -${oldSide.range} +${newSide.range} @@${hunk.heading ? ` ${hunk.heading}` : ''}`) for (const line of hunk.lines) rows.push(`${line.type === 'add' ? '+' : line.type === 'del' ? '-' : ' '}${line.text}`) - nextOld = oldStart + oldCount - nextNew = newStart + newCount + nextOld = oldSide.next + nextNew = newSide.next } return rows.join('\n') } diff --git a/apps/sim/lib/diff/unified.ts b/apps/sim/lib/diff/unified.ts index 7f43af6f983..e28b19b3175 100644 --- a/apps/sim/lib/diff/unified.ts +++ b/apps/sim/lib/diff/unified.ts @@ -16,6 +16,12 @@ export interface DiffHunk { heading: string /** The file a hunk belongs to, set only when a git diff spans several files. */ file?: string + /** + * The `-start` and `+start` of the `@@` header, verbatim, when it carries line numbers. A + * side with no lines (a pure insertion or deletion) has no line to carry its position, so + * only the header knows it. + */ + range?: { oldStart: number; newStart: number } lines: DiffLine[] } @@ -105,10 +111,16 @@ export function parseUnifiedDiff(text: string): UnifiedDiff { if (!hunk && GIT_HEADER.test(row)) continue const header = HUNK_HEADER.exec(row) if (row.startsWith('@@') && header) { - hunk = { heading: header[3] ?? '', file: file ?? undefined, lines: [] } - hunks.push(hunk) oldLine = header[1] ? Number(header[1]) : undefined newLine = header[2] ? Number(header[2]) : undefined + hunk = { + heading: header[3] ?? '', + file: file ?? undefined, + ...(oldLine !== undefined && + newLine !== undefined && { range: { oldStart: oldLine, newStart: newLine } }), + lines: [], + } + hunks.push(hunk) continue } if (row.startsWith('\\')) continue From cbe3895714f09008c36558642c00d6bac485c64f Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Thu, 19 Nov 2026 22:53:44 -0800 Subject: [PATCH 4/6] chore(audits): drop baseline entries for the removed text-diff-lines --- scripts/check-unused-exports.baseline.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/scripts/check-unused-exports.baseline.json b/scripts/check-unused-exports.baseline.json index 867f1073c1b..bf0f811b55e 100644 --- a/scripts/check-unused-exports.baseline.json +++ b/scripts/check-unused-exports.baseline.json @@ -709,8 +709,6 @@ "apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/search-modal/utils.ts#ActionContext", "apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/search-modal/utils.ts#FolderedItem", "apps/sim/app/workspace/[workspaceId]/w/components/sidebar/hooks/index.ts#DropIndicator", - "apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff-lines.ts#CONTEXT_LINES", - "apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff-lines.ts#MIN_FOLD_LINES", "apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/value-presentation.ts#StructuredValueRow", "apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/utils.ts#BlockMove", "apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/utils.ts#MembershipRow", From 5f9453a0800e5062cc3256caa5f5f4e5d56be973 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Thu, 19 Nov 2026 23:06:52 -0800 Subject: [PATCH 5/6] fix(diff): address review findings on the diff renderers - Render a diff's lines in the order they were written: Pierre's similarity realignment could split one written change into several blocks. - Show line numbers only when every hunk has them, so positions invented for an unnumbered hunk never appear in a mixed diff. - Highlight code fields through an explicit map of the languages blocks offer, adding TypeScript (Daytona) and keeping shell as bash. - Say "Only line endings changed" when normalizing line endings leaves a changed value with no visible difference, instead of an empty diff. - Watch the syntax theme's folder in docs watch mode, since an editor's atomic save ends a watch on the file itself. --- apps/docs/scripts/compile-content.ts | 5 ++++- .../change-list/field-change-row.tsx | 22 +++++++++++++------ .../components/change-list/text-diff.tsx | 4 ++++ apps/sim/components/diff/patch-view.tsx | 5 ++--- apps/sim/lib/diff/patch.test.ts | 16 ++++++++++++++ apps/sim/lib/diff/patch.ts | 3 ++- 6 files changed, 43 insertions(+), 12 deletions(-) diff --git a/apps/docs/scripts/compile-content.ts b/apps/docs/scripts/compile-content.ts index 502111deab7..704cd01b75d 100644 --- a/apps/docs/scripts/compile-content.ts +++ b/apps/docs/scripts/compile-content.ts @@ -473,7 +473,10 @@ if (process.argv.includes('--watch')) { return scheduleRefresh() }) - watch(SYNTAX_THEME_FILE, scheduleRefresh) + /* Editors save by replacing the file, which ends a watch on the file itself; watch its folder. */ + watch(path.dirname(SYNTAX_THEME_FILE), (_, filename) => { + if (filename?.toString() === path.basename(SYNTAX_THEME_FILE)) scheduleRefresh() + }) logger.info('Watching documentation source') await refresh() } else await generate() diff --git a/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/field-change-row.tsx b/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/field-change-row.tsx index 024d748ba12..064c8ee8bac 100644 --- a/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/field-change-row.tsx +++ b/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/field-change-row.tsx @@ -2,7 +2,6 @@ import { ChipModalField, ChipTag, cn } from '@sim/emcn' import { ArrowRight } from '@sim/emcn/icons' -import { isValidCodeLanguage } from '@/lib/execution/languages' import { formatValueForDisplay, resolveFieldLabel } from '@/lib/workflows/comparison/resolve-values' import { StructuredValueDiff } from '@/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/structured-value-diff' import { @@ -26,6 +25,15 @@ import { import type { SubBlockConfig } from '@/blocks/types' import { formatParameterLabel } from '@/tools/params' +/** Code languages blocks offer, as the highlighter names them. */ +const CODE_DIFF_LANGUAGES: Record = { + javascript: 'javascript', + typescript: 'typescript', + python: 'python', + shell: 'bash', + json: 'json', +} + /** * The syntax a text diff highlights as: JSON for structured values, a code field's language * (the block's own `language` setting first, as the editor does), and plain text for prose. @@ -37,12 +45,12 @@ function diffLanguage( ): string { if (kind === 'json') return 'json' if (config?.type !== 'code') return 'text' - const selected = values.language - const language = - typeof selected === 'string' && isValidCodeLanguage(selected) - ? selected - : (config.language ?? 'javascript') - return language === 'shell' ? 'bash' : language + const selected = typeof values.language === 'string' ? values.language : undefined + return ( + (selected && CODE_DIFF_LANGUAGES[selected]) ?? + CODE_DIFF_LANGUAGES[config.language ?? 'javascript'] ?? + 'javascript' + ) } interface FieldChangeRowProps { diff --git a/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff.tsx b/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff.tsx index dcdba5dab5e..b93bda748cb 100644 --- a/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff.tsx +++ b/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/text-diff.tsx @@ -90,6 +90,10 @@ export function TextDiff({ oldText, newText, language }: TextDiffProps) { ) const regions = fileDiff.hunks.length + /* Normalizing endings can leave nothing to show for a value that did change. */ + if (regions === 0) + return

Only line endings changed

+ return (
{regions > MANY_REGIONS && ( diff --git a/apps/sim/components/diff/patch-view.tsx b/apps/sim/components/diff/patch-view.tsx index 8db561ef7a0..430b854fee4 100644 --- a/apps/sim/components/diff/patch-view.tsx +++ b/apps/sim/components/diff/patch-view.tsx @@ -18,9 +18,8 @@ interface PatchViewProps { * already draws. */ export function PatchView({ diff, fallbackName, wrapLines }: PatchViewProps) { - const numbered = diff.hunks.some((hunk) => - hunk.lines.some((line) => line.oldLine !== undefined || line.newLine !== undefined) - ) + /* Positions are invented for hunks without numbers, so numbers show only when every hunk has them. */ + const numbered = diff.hunks.every((hunk) => hunk.range !== undefined) const files = useMemo(() => toFileDiffs(diff, fallbackName), [diff, fallbackName]) return (
diff --git a/apps/sim/lib/diff/patch.test.ts b/apps/sim/lib/diff/patch.test.ts index 9db6c2fb668..c3fe634a20a 100644 --- a/apps/sim/lib/diff/patch.test.ts +++ b/apps/sim/lib/diff/patch.test.ts @@ -113,6 +113,22 @@ describe('toFileDiffs', () => { expect(position(file.hunks[0])).toEqual(position(original.hunks[0])) }) + it('keeps a change block as written instead of realigning its lines by similarity', () => { + const [file] = render(`--- a/total.ts ++++ b/total.ts +@@ -1,2 +1,4 @@ +-const total = sum(items) ++// recompute ++const total = sum(items) + tax ++return total + x`) + expect( + file.hunks[0].hunkContent.map((block) => + block.type === 'change' ? [block.deletions, block.additions] : block.type + ) + ).toEqual([[1, 3], 'context']) + }) + it('splits a multi-file git diff into one diff per file', () => { const files = render(`diff --git a/app/main.py b/app/main.py --- a/app/main.py diff --git a/apps/sim/lib/diff/patch.ts b/apps/sim/lib/diff/patch.ts index e7fcbb0c645..37b52796a14 100644 --- a/apps/sim/lib/diff/patch.ts +++ b/apps/sim/lib/diff/patch.ts @@ -65,7 +65,8 @@ export function toFileDiffs(diff: UnifiedDiff, fallbackName: string): FileDiffMe files.set(name, [...(files.get(name) ?? []), hunk]) } const patch = [...files].map(([name, hunks]) => serializeFile(name, hunks)).join('\n') - return parsePatchFiles(patch) + /* Render the lines in the order they were written, not realigned by similarity. */ + return parsePatchFiles(patch, { disableLineMatching: true }) .flatMap((parsed) => parsed.files) .map((file) => ({ ...file, From d39e9f22e392b60b7f99eb11663d320f530175ca Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Thu, 19 Nov 2026 23:20:13 -0800 Subject: [PATCH 6/6] fix(diff): resolve a code field's language from either version A removed block's language exists only in the base version, and an empty saved language short-circuited the lookup, so both fell back to JavaScript or to no highlighting. The language now comes from the target values, then the base values, then the field default, through a Map so only the listed languages can match. --- .../change-list/field-change-row.tsx | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/field-change-row.tsx b/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/field-change-row.tsx index 064c8ee8bac..e9880fbb621 100644 --- a/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/field-change-row.tsx +++ b/apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/field-change-row.tsx @@ -26,31 +26,33 @@ import type { SubBlockConfig } from '@/blocks/types' import { formatParameterLabel } from '@/tools/params' /** Code languages blocks offer, as the highlighter names them. */ -const CODE_DIFF_LANGUAGES: Record = { - javascript: 'javascript', - typescript: 'typescript', - python: 'python', - shell: 'bash', - json: 'json', -} +const CODE_DIFF_LANGUAGES = new Map([ + ['javascript', 'javascript'], + ['typescript', 'typescript'], + ['python', 'python'], + ['shell', 'bash'], + ['json', 'json'], +]) /** - * The syntax a text diff highlights as: JSON for structured values, a code field's language - * (the block's own `language` setting first, as the editor does), and plain text for prose. + * The syntax a text diff highlights as: JSON for structured values, plain text for prose, and + * for code the first language that names one the highlighter knows: the block's `language` + * setting after the change, then before it (a removed block has only that), then the field's + * own default. */ function diffLanguage( config: SubBlockConfig | undefined, kind: ValueKind, - values: Record + targetValues: Record, + baseValues: Record ): string { if (kind === 'json') return 'json' if (config?.type !== 'code') return 'text' - const selected = typeof values.language === 'string' ? values.language : undefined - return ( - (selected && CODE_DIFF_LANGUAGES[selected]) ?? - CODE_DIFF_LANGUAGES[config.language ?? 'javascript'] ?? - 'javascript' - ) + for (const value of [targetValues.language, baseValues.language, config.language]) { + const language = typeof value === 'string' ? CODE_DIFF_LANGUAGES.get(value) : undefined + if (language) return language + } + return 'javascript' } interface FieldChangeRowProps { @@ -150,7 +152,7 @@ export function FieldChangeRow({ )} {scalar &&