Skip to content

Commit 5f9453a

Browse files
committed
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.
1 parent cbe3895 commit 5f9453a

6 files changed

Lines changed: 43 additions & 12 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,10 @@ if (process.argv.includes('--watch')) {
473473
return
474474
scheduleRefresh()
475475
})
476-
watch(SYNTAX_THEME_FILE, scheduleRefresh)
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()
479+
})
477480
logger.info('Watching documentation source')
478481
await refresh()
479482
} else await generate()

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import { ChipModalField, ChipTag, cn } from '@sim/emcn'
44
import { ArrowRight } from '@sim/emcn/icons'
5-
import { isValidCodeLanguage } from '@/lib/execution/languages'
65
import { formatValueForDisplay, resolveFieldLabel } from '@/lib/workflows/comparison/resolve-values'
76
import { StructuredValueDiff } from '@/app/workspace/[workspaceId]/w/components/workflow-diff/components/change-list/structured-value-diff'
87
import {
@@ -26,6 +25,15 @@ import {
2625
import type { SubBlockConfig } from '@/blocks/types'
2726
import { formatParameterLabel } from '@/tools/params'
2827

28+
/** Code languages blocks offer, as the highlighter names them. */
29+
const CODE_DIFF_LANGUAGES: Record<string, string> = {
30+
javascript: 'javascript',
31+
typescript: 'typescript',
32+
python: 'python',
33+
shell: 'bash',
34+
json: 'json',
35+
}
36+
2937
/**
3038
* The syntax a text diff highlights as: JSON for structured values, a code field's language
3139
* (the block's own `language` setting first, as the editor does), and plain text for prose.
@@ -37,12 +45,12 @@ function diffLanguage(
3745
): string {
3846
if (kind === 'json') return 'json'
3947
if (config?.type !== 'code') return 'text'
40-
const selected = values.language
41-
const language =
42-
typeof selected === 'string' && isValidCodeLanguage(selected)
43-
? selected
44-
: (config.language ?? 'javascript')
45-
return language === 'shell' ? 'bash' : language
48+
const selected = typeof values.language === 'string' ? values.language : undefined
49+
return (
50+
(selected && CODE_DIFF_LANGUAGES[selected]) ??
51+
CODE_DIFF_LANGUAGES[config.language ?? 'javascript'] ??
52+
'javascript'
53+
)
4654
}
4755

4856
interface FieldChangeRowProps {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ export function TextDiff({ oldText, newText, language }: TextDiffProps) {
9090
)
9191

9292
const regions = fileDiff.hunks.length
93+
/* Normalizing endings can leave nothing to show for a value that did change. */
94+
if (regions === 0)
95+
return <p className='text-[var(--text-secondary)] text-small'>Only line endings changed</p>
96+
9397
return (
9498
<div className='overflow-hidden rounded-sm border border-[var(--border-1)]'>
9599
{regions > MANY_REGIONS && (

‎apps/sim/components/diff/patch-view.tsx‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ interface PatchViewProps {
1818
* already draws.
1919
*/
2020
export function PatchView({ diff, fallbackName, wrapLines }: PatchViewProps) {
21-
const numbered = diff.hunks.some((hunk) =>
22-
hunk.lines.some((line) => line.oldLine !== undefined || line.newLine !== undefined)
23-
)
21+
/* Positions are invented for hunks without numbers, so numbers show only when every hunk has them. */
22+
const numbered = diff.hunks.every((hunk) => hunk.range !== undefined)
2423
const files = useMemo(() => toFileDiffs(diff, fallbackName), [diff, fallbackName])
2524
return (
2625
<div>

‎apps/sim/lib/diff/patch.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ describe('toFileDiffs', () => {
113113
expect(position(file.hunks[0])).toEqual(position(original.hunks[0]))
114114
})
115115

116+
it('keeps a change block as written instead of realigning its lines by similarity', () => {
117+
const [file] = render(`--- a/total.ts
118+
+++ b/total.ts
119+
@@ -1,2 +1,4 @@
120+
-const total = sum(items)
121+
+// recompute
122+
+const total = sum(items) + tax
123+
+return total
124+
x`)
125+
expect(
126+
file.hunks[0].hunkContent.map((block) =>
127+
block.type === 'change' ? [block.deletions, block.additions] : block.type
128+
)
129+
).toEqual([[1, 3], 'context'])
130+
})
131+
116132
it('splits a multi-file git diff into one diff per file', () => {
117133
const files = render(`diff --git a/app/main.py b/app/main.py
118134
--- a/app/main.py

‎apps/sim/lib/diff/patch.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ export function toFileDiffs(diff: UnifiedDiff, fallbackName: string): FileDiffMe
6565
files.set(name, [...(files.get(name) ?? []), hunk])
6666
}
6767
const patch = [...files].map(([name, hunks]) => serializeFile(name, hunks)).join('\n')
68-
return parsePatchFiles(patch)
68+
/* Render the lines in the order they were written, not realigned by similarity. */
69+
return parsePatchFiles(patch, { disableLineMatching: true })
6970
.flatMap((parsed) => parsed.files)
7071
.map((file) => ({
7172
...file,

0 commit comments

Comments
 (0)