Skip to content

Commit 9ca9ace

Browse files
authored
feat(diff): render diffs with Pierre diffs and share one syntax palette (#10234)
* 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. * 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. * 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. * chore(audits): drop baseline entries for the removed text-diff-lines * 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. * 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.
1 parent 02fb744 commit 9ca9ace

20 files changed

Lines changed: 784 additions & 859 deletions

File tree

‎apps/docs/lib/shiki-theme.ts‎

Lines changed: 9 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
1-
import type { ThemeRegistrationRaw } from 'shiki'
2-
31
/**
4-
* Sim's syntax theme for docs code blocks.
5-
*
6-
* The platform highlights code with PrismJS through `@sim/emcn`'s `Code` component, whose
7-
* token colors live in `packages/emcn/src/components/code/code.css`. Docs highlight with
8-
* Shiki, which is TextMate-scoped rather than Prism-tokenized, so parity has to be
9-
* expressed as a scope-to-color mapping rather than a shared stylesheet. The palette follows
10-
* `code.css` in light mode, with darker hues for readable contrast. Dark mode consumes the
2+
* Sim's syntax theme for docs code blocks. The scope mapping is shared with the app in
3+
* `@sim/emcn/syntax-theme`; docs supply their own palettes. Light mode
4+
* follows `code.css` with darker hues for readable contrast, and dark mode consumes the
115
* docs semantic tokens so authored MDX and generated examples share the reading palette.
12-
*
13-
* The mapping was derived by highlighting the same snippets through both engines and
14-
* comparing output. Two places where the grammars genuinely disagree, and the choice made:
15-
*
16-
* - Prism only colors a shell command when it recognises the binary, so `npm` highlights
17-
* but `sim` does not. TextMate scopes the first word of a statement as the command
18-
* regardless, so `sim` highlights too. Shiki's reading is the correct one and is kept.
19-
* - TextMate scopes every bare shell argument as `string.unquoted.argument`, which would
20-
* paint most of a command line in the string color. Prism leaves those plain, so the
21-
* scope is pinned back to the base foreground and only genuinely quoted strings carry
22-
* the string color.
23-
*
24-
* Bare `variable` is deliberately unmapped. TextMate scopes declared JavaScript
25-
* identifiers as `variable.other.*`, and coloring those would make JS and TS far busier
26-
* than the platform editor, which leaves them at the base foreground. Only the things
27-
* Prism actually calls out — object and config keys, shell flags and shell expansions —
28-
* are mapped to the variable color.
296
*/
7+
import { buildSyntaxTheme, type SyntaxColors } from '@sim/emcn/syntax-theme'
308

319
/** Light-mode platform hues adjusted to meet WCAG AA text contrast on the code surface. */
32-
const LIGHT = {
10+
const LIGHT: SyntaxColors = {
3311
/** `--text-primary` — the platform's base code foreground. */
3412
foreground: '#1a1a1a',
3513
/** `--surface-5` — the fill `chipFieldSurfaceClass` puts under a code surface. */
@@ -43,10 +21,10 @@ const LIGHT = {
4321
function: '#854d0e',
4422
regex: '#be123c',
4523
deleted: '#b91c1c',
46-
} as const
24+
}
4725

4826
/** Dark syntax roles resolve through the shared docs theme, including portal surfaces. */
49-
const DARK = {
27+
const DARK: SyntaxColors = {
5028
foreground: 'var(--docs-text)',
5129
background: 'var(--docs-surface)',
5230
comment: 'var(--docs-green)',
@@ -58,138 +36,10 @@ const DARK = {
5836
function: 'var(--docs-orange)',
5937
regex: 'var(--docs-red)',
6038
deleted: 'var(--docs-red)',
61-
} as const
62-
63-
type TokenColors = typeof LIGHT | typeof DARK
64-
65-
/**
66-
* Builds a Shiki theme from one palette. Scope order matters: TextMate resolves a token by
67-
* longest matching scope, so the narrower entries below (`string.unquoted.argument`,
68-
* `keyword.operator`) intentionally follow and override the broader ones.
69-
*/
70-
function buildTheme(name: string, type: 'light' | 'dark', c: TokenColors): ThemeRegistrationRaw {
71-
return {
72-
name,
73-
type,
74-
settings: [
75-
{ settings: { background: c.background, foreground: c.foreground } },
76-
77-
/** Prism `comment` / `block-comment` / `prolog` / `doctype` / `cdata`. */
78-
{
79-
scope: ['comment', 'punctuation.definition.comment', 'string.comment'],
80-
settings: { foreground: c.comment },
81-
},
82-
83-
/** Prism `keyword` / `atrule` — plus TextMate's storage scopes, which Prism folds into keywords. */
84-
{
85-
scope: [
86-
'keyword',
87-
'keyword.control',
88-
'keyword.other',
89-
'storage',
90-
'storage.type',
91-
'storage.modifier',
92-
'meta.import keyword',
93-
'meta.export keyword',
94-
],
95-
settings: { foreground: c.keyword },
96-
},
97-
98-
/** Prism `function` and `class-name`. */
99-
{
100-
scope: [
101-
'entity.name.function',
102-
'entity.name.command',
103-
'support.function',
104-
'meta.function-call.generic',
105-
'entity.name.class',
106-
'entity.name.type',
107-
'support.class',
108-
'support.type',
109-
],
110-
settings: { foreground: c.function },
111-
},
112-
113-
/**
114-
* Prism `property` / `attr-name` / `variable` — object and config keys, markup
115-
* attributes, shell flags and shell expansions. Bare `variable` is excluded on
116-
* purpose; see the module docblock.
117-
*/
118-
{
119-
scope: [
120-
'support.type.property-name',
121-
'meta.object-literal.key',
122-
'entity.name.tag.yaml',
123-
'entity.other.attribute-name',
124-
'constant.other.option',
125-
'variable.other.normal',
126-
'variable.other.special',
127-
'punctuation.definition.variable',
128-
],
129-
settings: { foreground: c.variable },
130-
},
131-
132-
/** Prism `number` / `boolean` / `constant` / `tag`. */
133-
{
134-
scope: [
135-
'constant.numeric',
136-
'constant.language',
137-
'constant.character',
138-
'support.constant',
139-
'entity.name.tag',
140-
'variable.language',
141-
],
142-
settings: { foreground: c.constant },
143-
},
144-
145-
/** Prism `string` / `char` / `inserted`. */
146-
{
147-
scope: [
148-
'string',
149-
'string.quoted',
150-
'string.template',
151-
'punctuation.definition.string',
152-
'markup.inserted',
153-
],
154-
settings: { foreground: c.string },
155-
},
156-
157-
/**
158-
* Bare shell arguments. TextMate calls these strings; Prism leaves them plain, and
159-
* plain is what keeps a command line readable. Declared after `string` so it wins.
160-
*/
161-
{ scope: ['string.unquoted.argument'], settings: { foreground: c.foreground } },
162-
163-
/** Prism `punctuation` / `operator` / `entity` / `symbol`. */
164-
{
165-
scope: [
166-
'punctuation',
167-
'meta.brace',
168-
'keyword.operator',
169-
'punctuation.separator',
170-
'punctuation.terminator',
171-
'punctuation.accessor',
172-
],
173-
settings: { foreground: c.punctuation },
174-
},
175-
176-
/** Prism `regex` / `important`. */
177-
{
178-
scope: ['string.regexp', 'constant.regexp', 'keyword.other.important'],
179-
settings: { foreground: c.regex },
180-
},
181-
182-
/** Prism `deleted`, plus TextMate's invalid scopes. */
183-
{
184-
scope: ['markup.deleted', 'invalid', 'invalid.illegal'],
185-
settings: { foreground: c.deleted },
186-
},
187-
],
188-
}
18939
}
19040

191-
const simShikiLight = buildTheme('sim-light', 'light', LIGHT)
192-
const simShikiDark = buildTheme('sim-dark', 'dark', DARK)
41+
const simShikiLight = buildSyntaxTheme('sim-light', 'light', LIGHT)
42+
const simShikiDark = buildSyntaxTheme('sim-dark', 'dark', DARK)
19343

19444
/** Shared syntax palette for build-time MDX and generated API examples. */
19545
export const simShikiOptions = {

‎apps/docs/scripts/compile-content.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ import { buildSearchCatalog, buildSearchIndex } from '@/lib/search/build'
1818

1919
const logger = createLogger('DocsContent')
2020
const DOCS_ROOT = path.resolve(import.meta.dirname, '..')
21+
/** The Shiki scope mapping docs highlight with; it lives in emcn, outside the docs tree. */
22+
const SYNTAX_THEME_FILE = path.resolve(
23+
DOCS_ROOT,
24+
'../../packages/emcn/src/components/code/syntax-theme.ts'
25+
)
2126
const CONTENT_ROOT = path.join(DOCS_ROOT, 'content/docs')
2227
const OUTPUT_ROOT = path.join(DOCS_ROOT, '.content')
2328
const MAX_CONTENT_BYTES = 64 * 1024 * 1024
@@ -197,6 +202,7 @@ async function generate() {
197202
path.join(DOCS_ROOT, 'lib/content/markdown.ts'),
198203
path.join(DOCS_ROOT, 'lib/content/output.ts'),
199204
path.join(DOCS_ROOT, 'lib/shiki-theme.ts'),
205+
SYNTAX_THEME_FILE,
200206
path.join(DOCS_ROOT, 'components/workflow-preview/examples.ts'),
201207
path.join(DOCS_ROOT, 'components/workflow-preview/block-display-workflows.ts'),
202208
path.join(DOCS_ROOT, 'package.json'),
@@ -449,6 +455,12 @@ if (process.argv.includes('--watch')) {
449455
}
450456
}
451457
}
458+
const scheduleRefresh = () => {
459+
clearTimeout(timer)
460+
timer = setTimeout(() => {
461+
void refresh()
462+
}, 150)
463+
}
452464
watch(DOCS_ROOT, { recursive: true }, (_, filename) => {
453465
const changed = filename?.toString().replaceAll('\\', '/') ?? ''
454466
if (
@@ -459,10 +471,11 @@ if (process.argv.includes('--watch')) {
459471
!['scripts/compile-content.ts', 'lib/shiki-theme.ts', ...OPENAPI_SPEC_FILES].includes(changed)
460472
)
461473
return
462-
clearTimeout(timer)
463-
timer = setTimeout(() => {
464-
void refresh()
465-
}, 150)
474+
scheduleRefresh()
475+
})
476+
/* Editors save by replacing the file, which ends a watch on the file itself; watch its folder. */
477+
watch(path.dirname(SYNTAX_THEME_FILE), (_, filename) => {
478+
if (filename?.toString() === path.basename(SYNTAX_THEME_FILE)) scheduleRefresh()
466479
})
467480
logger.info('Watching documentation source')
468481
await refresh()

‎apps/sim/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/field-change-row.tsx‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,41 @@ import {
2020
isBlankValue,
2121
isSentenceLike,
2222
toDiffText,
23+
type ValueKind,
2324
} from '@/app/workspace/[workspaceId]/w/components/workflow-diff/utils'
25+
import type { SubBlockConfig } from '@/blocks/types'
2426
import { formatParameterLabel } from '@/tools/params'
2527

28+
/** Code languages blocks offer, as the highlighter names them. */
29+
const CODE_DIFF_LANGUAGES = new Map([
30+
['javascript', 'javascript'],
31+
['typescript', 'typescript'],
32+
['python', 'python'],
33+
['shell', 'bash'],
34+
['json', 'json'],
35+
])
36+
37+
/**
38+
* The syntax a text diff highlights as: JSON for structured values, plain text for prose, and
39+
* for code the first language that names one the highlighter knows: the block's `language`
40+
* setting after the change, then before it (a removed block has only that), then the field's
41+
* own default.
42+
*/
43+
function diffLanguage(
44+
config: SubBlockConfig | undefined,
45+
kind: ValueKind,
46+
targetValues: Record<string, unknown>,
47+
baseValues: Record<string, unknown>
48+
): string {
49+
if (kind === 'json') return 'json'
50+
if (config?.type !== 'code') return 'text'
51+
for (const value of [targetValues.language, baseValues.language, config.language]) {
52+
const language = typeof value === 'string' ? CODE_DIFF_LANGUAGES.get(value) : undefined
53+
if (language) return language
54+
}
55+
return 'javascript'
56+
}
57+
2658
interface FieldChangeRowProps {
2759
blockType: string
2860
scope: 'block' | 'subblock' | 'container'
@@ -117,7 +149,11 @@ export function FieldChangeRow({
117149
/>
118150
)}
119151
{textual && !sameDisplayValue && !emptyTextChange && (
120-
<TextDiff oldText={oldText} newText={newText} />
152+
<TextDiff
153+
oldText={oldText}
154+
newText={newText}
155+
language={diffLanguage(config, kind, targetValues, baseValues)}
156+
/>
121157
)}
122158
{scalar &&
123159
!sameDisplayValue &&

0 commit comments

Comments
 (0)