Skip to content

Commit 376b0ee

Browse files
committed
Prevent code blocks containing truncation comments from being incorrectly converted into new file patches
1 parent e1882b4 commit 376b0ee

4 files changed

Lines changed: 43 additions & 10 deletions

File tree

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/clipboard-parser.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,7 @@ export const parse_response = (params: {
101101
is_single_root: is_single_root_folder_workspace
102102
})
103103
if (patches_or_text.length) {
104-
return patches_or_text.map((item) => {
105-
if ('type' in item && item.type == 'text') {
106-
return item
107-
}
108-
return { type: 'diff', ...item }
109-
})
104+
return patches_or_text
110105
}
111106
}
112107

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/extract-diff-patches/extract-diffs.ts‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { extract_path_from_line_of_code } from '@shared/utils/extract-path-from-
33
import { extract_workspace_and_path } from '../clipboard-parser'
44

55
export type Diff = {
6+
type: 'diff'
67
file_path: string
78
content: string
89
workspace_name?: string
@@ -133,6 +134,7 @@ const process_collected_patch_lines = (params: {
133134
})
134135

135136
const patch: Diff = {
137+
type: 'diff',
136138
file_path: relative_path,
137139
workspace_name,
138140
content: content_str.endsWith('\n') ? content_str : content_str + '\n'
@@ -227,7 +229,8 @@ const convert_code_block_to_new_file_diff = (params: {
227229

228230
if (
229231
(potential_path.includes('.') || potential_path.includes('/')) &&
230-
!potential_path.includes(' ')
232+
!potential_path.includes(' ') &&
233+
/[a-zA-Z0-9]/.test(potential_path)
231234
) {
232235
const rest_of_line = line
233236
.substring(line.indexOf(potential_path) + potential_path.length)
@@ -279,6 +282,7 @@ const convert_code_block_to_new_file_diff = (params: {
279282
].join('\n')
280283

281284
return {
285+
type: 'diff',
282286
file_path: relative_path,
283287
workspace_name,
284288
content: patch_content + '\n'
@@ -299,6 +303,7 @@ const convert_code_block_to_new_file_diff = (params: {
299303
].join('\n')
300304

301305
return {
306+
type: 'diff',
302307
file_path: relative_path,
303308
workspace_name,
304309
content: patch_content + '\n'
@@ -417,13 +422,30 @@ const extract_all_code_block_patches = (params: {
417422
// Process each found code block in order of appearance.
418423
for (let i = 0; i < code_blocks.length; i++) {
419424
const block = code_blocks[i]
425+
const block_lines = lines.slice(block.start + 1, block.end)
426+
427+
if (block.type != 'diff' && block.type != 'patch') {
428+
const has_truncation_comment = block_lines.some((line) => {
429+
const trimmed = line.trim()
430+
return (
431+
trimmed.startsWith('// ...') ||
432+
trimmed.startsWith('# ...') ||
433+
trimmed.startsWith('/* ...') ||
434+
trimmed.startsWith('* ...') ||
435+
trimmed.startsWith('-- ...') ||
436+
trimmed.startsWith('<!-- ...')
437+
)
438+
})
439+
if (has_truncation_comment) {
440+
continue
441+
}
442+
}
420443

421444
const text_before = lines.slice(last_block_end + 1, block.start).join('\n')
422445
if (text_before.trim()) {
423446
items.push({ type: 'text', content: text_before.trim() })
424447
}
425448

426-
const block_lines = lines.slice(block.start + 1, block.end) // Exclude the ``` lines
427449
if (block.type == 'diff' || block.type == 'patch') {
428450
items.push(...(diff_block_patches.get(block.start) || []))
429451
} else {
@@ -493,6 +515,11 @@ const extract_all_code_block_patches = (params: {
493515
}
494516
items.push(patch)
495517
}
518+
} else {
519+
const block_content = lines.slice(block.start, block.end + 1).join('\n')
520+
if (block_lines.join('').trim()) {
521+
items.push({ type: 'text', content: block_content })
522+
}
496523
}
497524
}
498525
last_block_end = block.end
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
Top.
1+
Lorem ipsum `src/index.ts`
2+
3+
```ts
4+
console.log();
5+
// ...
6+
```

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/diff-multiple-files-text-between/diff-multiple-files-text-between.txt‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
Top.
1+
Lorem ipsum `src/index.ts`
2+
3+
```ts
4+
console.log();
5+
// ...
6+
```
7+
28

39
```diff
410
--- a/src/lorem.ts

0 commit comments

Comments
 (0)