Skip to content

Commit 0ac570a

Browse files
committed
Improve diff processor to support CRLF endings and refine patch normalization and block parsing
- assess correctness of the processor └── apps └── editor └── src └── utils └── changes-integration └── diff-processor ├── diff-processor.spec.ts ├── diff-processor.ts ├── index.ts ├── test-cases │ ├── deletion-only │ │ ├── diff.txt │ │ ├── expected.txt │ │ └── original.txt │ ├── empty-file │ │ ├── diff.txt │ │ ├── expected.txt │ │ └── original.txt │ ├── empty-file-2 │ │ ├── diff.txt │ │ ├── expected.txt │ │ └── original.txt │ ├── failing │ │ ├── diff.txt │ │ └── original.txt │ ├── failing-2 │ │ ├── diff.txt │ │ └── original.txt │ ├── generic-1 │ │ ├── diff.txt │ │ ├── expected.txt │ │ └── original.txt │ ├── generic-2 │ │ ├── diff.txt │ │ ├── expected.txt │ │ └── original.txt │ ├── generic-3 │ │ ├── diff.txt │ │ ├── expected.txt │ │ └── original.txt │ ├── generic-4 │ │ ├── diff.txt │ │ ├── expected.txt │ │ └── original.txt │ ├── generic-5 │ │ ├── diff.txt │ │ ├── expected.txt │ │ └── original.txt │ ├── generic-6 │ │ ├── diff.txt │ │ ├── expected.txt │ │ └── original.txt │ ├── generic-7 │ │ ├── diff.txt │ │ ├── expected.txt │ │ └── original.txt │ ├── generic-8 │ │ ├── diff.txt │ │ ├── expected.txt │ │ └── original.txt │ ├── generic-9 │ │ ├── diff.txt │ │ ├── expected.txt │ │ └── original.txt │ ├── insert-start-end │ │ ├── diff.txt │ │ ├── expected.txt │ │ └── original.txt │ ├── loose-gap-match │ │ ├── diff.txt │ │ ├── expected.txt │ │ └── original.txt │ ├── multiple-hunks │ │ ├── diff.txt │ │ ├── expected.txt │ │ └── original.txt │ ├── redundant-new-empty-line │ │ ├── diff.txt │ │ ├── expected.txt │ │ └── original.txt │ └── same-line-replace │ ├── diff.txt │ ├── expected.txt │ └── original.txt ├── types.ts └── utils ├── apply-blocks.ts ├── empty-file-handler.ts ├── match-blocks.ts ├── normalize.ts └── parse-blocks.ts
1 parent d864d00 commit 0ac570a

3 files changed

Lines changed: 32 additions & 17 deletions

File tree

‎apps/editor/src/utils/changes-integration/diff-processor/diff-processor.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ export const apply_diff = (params: {
1313
use_strict_whitespace?: boolean
1414
}): string => {
1515
const original_code_normalized = params.original_code.replace(/\r\n/g, '\n')
16+
const uses_crlf = params.original_code.includes('\r\n')
17+
const restore_eol = (text: string): string =>
18+
uses_crlf ? text.replace(/\n/g, '\r\n') : text
1619

1720
if (original_code_normalized.trim() === '') {
1821
const empty_file_result = handle_empty_file(params.diff_patch)
1922
if (empty_file_result !== null) {
20-
return empty_file_result
23+
return restore_eol(empty_file_result)
2124
}
2225
}
2326

@@ -60,8 +63,10 @@ export const apply_diff = (params: {
6063
(a, b) => b.search_block_start_index - a.search_block_start_index
6164
)
6265

63-
return apply_blocks({
64-
valid_blocks,
65-
original_code_lines
66-
})
66+
return restore_eol(
67+
apply_blocks({
68+
valid_blocks,
69+
original_code_lines
70+
})
71+
)
6772
}

‎apps/editor/src/utils/changes-integration/diff-processor/utils/normalize.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ export const normalize_original_code = (params: {
1919
.replace(/\r/g, '')
2020
.replace(/\n/g, '')
2121

22-
const clean_invisibles = line_normalized.replace(/[\u200B-\u200F\uFEFF]/g, '')
22+
const clean_invisibles = line_normalized.replace(
23+
/[\u200B-\u200F\uFEFF]/g,
24+
''
25+
)
2326
let line_normalized_processed = clean_invisibles.replace(/\s+/g, '')
2427

2528
if (params.use_strict_whitespace) {
@@ -43,15 +46,21 @@ export const normalize_patch_lines = (params: {
4346
}): { patch_lines_original: string[]; patch_lines_normalized: string[] } => {
4447
const patch_lines_original: string[] = []
4548
const patch_lines_normalized: string[] = []
49+
let in_hunk = false
4650

4751
for (let i = 0; i < params.patch_lines.length; i++) {
4852
let line = params.patch_lines[i]
4953

54+
if (line.startsWith('@@')) {
55+
in_hunk = true
56+
}
57+
5058
if (
51-
line.startsWith('diff --git') ||
52-
line.startsWith('index') ||
53-
line.startsWith('---') ||
54-
line.startsWith('+++')
59+
!in_hunk &&
60+
(line.startsWith('diff --git') ||
61+
line.startsWith('index') ||
62+
line.startsWith('---') ||
63+
line.startsWith('+++'))
5564
) {
5665
continue
5766
}
@@ -65,9 +74,10 @@ export const normalize_patch_lines = (params: {
6574
}
6675

6776
const cleaned_line = line.replace(/[\u200B-\u200F\uFEFF]/g, '')
68-
77+
78+
const content_line = line.replace(/[\u200B\uFEFF]/g, '')
6979
patch_lines_original.push(
70-
cleaned_line.startsWith(' ') ? cleaned_line.substring(1, cleaned_line.length) : cleaned_line
80+
content_line.startsWith(' ') ? content_line.substring(1) : content_line
7181
)
7282

7383
let line_normalized = cleaned_line

‎apps/editor/src/utils/changes-integration/diff-processor/utils/parse-blocks.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,23 @@ export const parse_patch_blocks = (params: {
7171
current_block_has_changes = true
7272
}
7373

74-
if (line.startsWith('~nnn') || line.startsWith('-~nnn')) {
74+
if (line === '~nnn' || line === '-~nnn') {
7575
search_chunks.push('~nnn')
7676
} else {
77-
search_chunks.push(line.replace(/^-/, '').replace(/^~/, ''))
77+
search_chunks.push(line.substring(1))
7878
}
7979

8080
current_search_index_tracker = search_chunks.length - 1
8181

8282
if (!line.startsWith('-')) {
83-
if (line.startsWith('~nnn')) {
83+
if (line === '~nnn') {
8484
after_search_chunks.push('~nnn')
8585
} else {
86-
after_search_chunks.push(line.replace(/^~/, ''))
86+
after_search_chunks.push(line.substring(1))
8787
}
8888
}
8989

90-
if (line.startsWith('~nnn')) {
90+
if (line === '~nnn') {
9191
replace_chunks.push({
9292
content: line_original.replace(/^~nnn/, '') + '\n',
9393
search_index: search_chunks.length - 1,

0 commit comments

Comments
 (0)