Skip to content

Commit 7a498ca

Browse files
committed
Replace external diff dependency with a custom line diff utility
- in `CodeWebChat/apps/editor/src/utils` create 'diff-lines.ts' util, so that we can drop 'diff' package dependency. include tests and use snake case. └── apps └── editor └── src ├── features │ └── apply-response │ └── utils │ └── code-review │ └── diff-utils.ts └── utils ├── cleanup-api-response │ ├── cleanup-api-response.spec.ts │ ├── cleanup-api-response.ts │ ├── index.ts │ └── test-cases │ ├── extract-code-block-surrounded-by-text │ │ ├── expected.txt │ │ └── input.txt │ ├── extract-markdown-with-surrounding-text │ │ ├── expected.txt │ │ └── input.txt │ ├── keep-think-block-not-at-start │ │ ├── expected.txt │ │ └── input.txt │ ├── markdown-block │ │ ├── expected.txt │ │ └── input.txt │ ├── plain-text │ │ ├── expected.txt │ │ └── input.txt │ ├── remove-doctype │ │ ├── expected.txt │ │ └── input.txt │ ├── remove-file-tags │ │ ├── expected.txt │ │ └── input.txt │ ├── remove-files-tags │ │ ├── expected.txt │ │ └── input.txt │ ├── remove-markdown-wrapper │ │ ├── expected.txt │ │ └── input.txt │ ├── remove-markdown-wrapper-with-lang │ │ ├── expected.txt │ │ └── input.txt │ ├── remove-think-block-at-start │ │ ├── expected.txt │ │ └── input.txt │ ├── remove-think-block-with-newlines │ │ ├── expected.txt │ │ └── input.txt │ ├── remove-thought-block-at-start │ │ ├── expected.txt │ │ └── input.txt │ └── trim-whitespace │ ├── expected.txt │ └── input.txt ├── diff-lines │ ├── diff-lines.spec.ts │ ├── diff-lines.ts │ └── index.ts └── is-newer ├── index.ts ├── is-newer.spec.ts └── is-newer.ts
1 parent 88bbd6e commit 7a498ca

6 files changed

Lines changed: 407 additions & 13 deletions

File tree

‎apps/editor/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,6 @@
13941394
"axios": "1.21.0",
13951395
"classnames": "2.5.1",
13961396
"dayjs": "1.11.10",
1397-
"diff": "8.0.4",
13981397
"dotenv": "16.4.5",
13991398
"glob": "14.0.4",
14001399
"ignore": "6.0.2",

‎apps/editor/src/features/apply-response/utils/code-review/diff-utils.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { diffLines } from 'diff'
1+
import { diff_lines } from '@/utils/diff-lines'
22

33
export const get_diff_stats = (params: {
44
original_content: string
@@ -14,8 +14,10 @@ export const get_diff_stats = (params: {
1414
return { lines_added: 0, lines_removed: 0 }
1515
}
1616

17-
const changes = diffLines(original_content_processed, new_content_processed, {
18-
ignoreWhitespace: true
17+
const changes = diff_lines({
18+
old_str: original_content_processed,
19+
new_str: new_content_processed,
20+
ignore_whitespace: true
1921
})
2022

2123
let lines_added = 0
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
import { diff_lines, Diff_Change } from './diff-lines'
2+
3+
const to_xml = (changes: Diff_Change[]): string =>
4+
changes
5+
.map((change) => {
6+
if (change.added) return `<ins>${change.value}</ins>`
7+
if (change.removed) return `<del>${change.value}</del>`
8+
return change.value
9+
})
10+
.join('')
11+
12+
describe('diff_lines', () => {
13+
it('should return an empty array for two empty strings', () => {
14+
expect(diff_lines({ old_str: '', new_str: '' })).toEqual([])
15+
})
16+
17+
it('should treat identical texts as a single unchanged change', () => {
18+
expect(diff_lines({ old_str: 'a\nb\nc\n', new_str: 'a\nb\nc\n' })).toEqual([
19+
{ value: 'a\nb\nc\n', added: false, removed: false, count: 3 }
20+
])
21+
})
22+
23+
it('should diff a replaced line', () => {
24+
const changes = diff_lines({
25+
old_str: 'line\nold value\nline',
26+
new_str: 'line\nnew value\nline'
27+
})
28+
expect(changes).toEqual([
29+
{ value: 'line\n', added: false, removed: false, count: 1 },
30+
{ value: 'old value\n', added: false, removed: true, count: 1 },
31+
{ value: 'new value\n', added: true, removed: false, count: 1 },
32+
{ value: 'line', added: false, removed: false, count: 1 }
33+
])
34+
})
35+
36+
it('should detect added lines', () => {
37+
const changes = diff_lines({ old_str: 'a\nd\n', new_str: 'a\nb\nc\nd\n' })
38+
expect(to_xml(changes)).toBe('a\n<ins>b\nc\n</ins>d\n')
39+
expect(changes[1].count).toBe(2)
40+
})
41+
42+
it('should detect removed lines', () => {
43+
const changes = diff_lines({ old_str: 'a\nb\nc\nd\n', new_str: 'a\nd\n' })
44+
expect(to_xml(changes)).toBe('a\n<del>b\nc\n</del>d\n')
45+
expect(changes[1].count).toBe(2)
46+
})
47+
48+
it('should handle an empty old string', () => {
49+
expect(diff_lines({ old_str: '', new_str: 'a\nb' })).toEqual([
50+
{ value: 'a\nb', added: true, removed: false, count: 2 }
51+
])
52+
})
53+
54+
it('should handle an empty new string', () => {
55+
expect(diff_lines({ old_str: 'a\nb\n', new_str: '' })).toEqual([
56+
{ value: 'a\nb\n', added: false, removed: true, count: 2 }
57+
])
58+
})
59+
60+
it('should treat a missing trailing newline as a change by default', () => {
61+
expect(to_xml(diff_lines({ old_str: 'a\nb', new_str: 'a\nb\n' }))).toBe(
62+
'a\n<del>b</del><ins>b\n</ins>'
63+
)
64+
})
65+
66+
it('should handle windows line endings', () => {
67+
const changes = diff_lines({
68+
old_str: 'line\r\nold value\r\nline',
69+
new_str: 'line\r\nnew value\r\nline'
70+
})
71+
expect(to_xml(changes)).toBe(
72+
'line\r\n<del>old value\r\n</del><ins>new value\r\n</ins>line'
73+
)
74+
})
75+
76+
it('should handle empty lines', () => {
77+
const changes = diff_lines({
78+
old_str: 'line\n\nold value\n\nline',
79+
new_str: 'line\n\nnew value\n\nline'
80+
})
81+
expect(to_xml(changes)).toBe(
82+
'line\n\n<del>old value\n</del><ins>new value\n</ins>\nline'
83+
)
84+
})
85+
86+
it('should prefer deletions before insertions', () => {
87+
const changes = diff_lines({
88+
old_str: 'a\nb\nc\nd\n',
89+
new_str: 'a\nc\nb\nd\n'
90+
})
91+
expect(to_xml(changes)).toBe('a\n<del>b\n</del>c\n<ins>b\n</ins>d\n')
92+
93+
const changes_reversed = diff_lines({
94+
old_str: 'a\nc\nb\nd\n',
95+
new_str: 'a\nb\nc\nd\n'
96+
})
97+
expect(to_xml(changes_reversed)).toBe(
98+
'a\n<del>c\n</del>b\n<ins>c\n</ins>d\n'
99+
)
100+
})
101+
102+
it('should find a minimal diff for interleaved changes', () => {
103+
const changes = diff_lines({
104+
old_str: 'a\nb\nc\nd\ne\nf\n',
105+
new_str: 'a\nx\nc\ny\ne\nf\n'
106+
})
107+
expect(to_xml(changes)).toBe(
108+
'a\n<del>b\n</del><ins>x\n</ins>c\n<del>d\n</del><ins>y\n</ins>e\nf\n'
109+
)
110+
})
111+
112+
it('should reconstruct both texts from the changes', () => {
113+
const old_str = 'one\ntwo\nthree\nfour\nfive\nsix\nseven\n'
114+
const new_str = 'zero\none\nthree\nfour\nFIVE\nsix\neight\nnine\n'
115+
const changes = diff_lines({ old_str, new_str })
116+
117+
const rebuilt_old = changes
118+
.filter((change) => !change.added)
119+
.map((change) => change.value)
120+
.join('')
121+
const rebuilt_new = changes
122+
.filter((change) => !change.removed)
123+
.map((change) => change.value)
124+
.join('')
125+
126+
expect(rebuilt_old).toBe(old_str)
127+
expect(rebuilt_new).toBe(new_str)
128+
})
129+
130+
describe('ignore_whitespace', () => {
131+
it('should detect whitespace differences when disabled', () => {
132+
const changes = diff_lines({
133+
old_str: 'line\nvalue \nline',
134+
new_str: 'line\nvalue\nline'
135+
})
136+
expect(to_xml(changes)).toBe(
137+
'line\n<del>value \n</del><ins>value\n</ins>line'
138+
)
139+
})
140+
141+
it('should ignore leading and trailing whitespace when enabled', () => {
142+
const changes = diff_lines({
143+
old_str: 'line\n value \nline',
144+
new_str: 'line\nvalue\nline',
145+
ignore_whitespace: true
146+
})
147+
expect(changes).toEqual([
148+
{ value: 'line\nvalue\nline', added: false, removed: false, count: 3 }
149+
])
150+
})
151+
152+
it('should ignore line ending differences when enabled', () => {
153+
const changes = diff_lines({
154+
old_str: 'a\r\nb\r\n',
155+
new_str: 'a\nb\n',
156+
ignore_whitespace: true
157+
})
158+
expect(changes.every((change) => !change.added && !change.removed)).toBe(
159+
true
160+
)
161+
})
162+
163+
it('should not treat adding whitespace to an empty line as an insertion', () => {
164+
const changes = diff_lines({
165+
old_str: 'foo\n\nbar',
166+
new_str: 'foo\n \nbar',
167+
ignore_whitespace: true
168+
})
169+
expect(to_xml(changes)).toBe('foo\n \nbar')
170+
})
171+
172+
it('should still report added whitespace-only lines at the end', () => {
173+
const changes = diff_lines({
174+
old_str: 'foo\nbar\n',
175+
new_str: 'foo\nbar\n \n \n \n',
176+
ignore_whitespace: true
177+
})
178+
const last = changes[changes.length - 1]
179+
expect(last.added).toBe(true)
180+
expect(last.count).toBe(3)
181+
})
182+
183+
it('should still report removed whitespace-only lines at the end', () => {
184+
const changes = diff_lines({
185+
old_str: 'foo\nbar\n\n',
186+
new_str: 'foo\nbar\n',
187+
ignore_whitespace: true
188+
})
189+
expect(changes[changes.length - 1].removed).toBe(true)
190+
})
191+
})
192+
})

0 commit comments

Comments
 (0)