Skip to content

Commit c41f72f

Browse files
committed
Refine file parsing logic to correctly handle empty code blocks and ensure that empty file content blocks are ignored
1 parent c952915 commit c41f72f

9 files changed

Lines changed: 60 additions & 17 deletions

File tree

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,10 @@ describe('clipboard-parser', () => {
3939
is_single_root_folder_workspace: true
4040
})
4141

42-
expect(result).toHaveLength(2)
42+
expect(result).toHaveLength(1)
4343
expect(result[0]).toMatchObject({
44-
file_path: 'src/index.ts',
45-
content: load_test_case_file(test_case, '1-file.txt')
46-
})
47-
expect(result[1]).toMatchObject({
4844
file_path: 'src/hello.ts',
49-
content: load_test_case_file(test_case, '2-file.txt')
45+
content: load_test_case_file(test_case, '1-file.txt')
5046
})
5147
})
5248

@@ -502,6 +498,25 @@ describe('clipboard-parser', () => {
502498
content: load_test_case_file(test_case, '1-file.txt')
503499
})
504500
})
501+
502+
it('handles path duplicated in empty code block', () => {
503+
const test_case = 'path-duplicated-in-empty-code-block'
504+
const text = load_test_case_file(test_case, `${test_case}.txt`)
505+
const result = parse_multiple_files({
506+
response: text,
507+
is_single_root_folder_workspace: true
508+
})
509+
510+
expect(result).toHaveLength(2)
511+
expect(result[0]).toMatchObject({
512+
type: 'text',
513+
content: load_test_case_file(test_case, '1-text.txt')
514+
})
515+
expect(result[1]).toMatchObject({
516+
file_path: 'src/hello-world.ts',
517+
content: load_test_case_file(test_case, '2-file.txt')
518+
})
519+
})
505520
})
506521

507522
describe('parse_file_content_only', () => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const extract_workspace_and_path = (params: {
5454

5555
export const has_real_code = (content: string): boolean => {
5656
if (content.trim() == '') {
57-
return true
57+
return false
5858
}
5959
const lines = content.split('\n')
6060
const non_comment_lines = lines.filter((line) => {

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/parsers/multiple-files-parser.ts
12
import { extract_path_from_line_of_code } from '@shared/utils/extract-path-from-line-of-code'
23
import {
34
FileItem,
@@ -354,14 +355,31 @@ export const parse_multiple_files = (params: {
354355
is_markdown_container_block = true
355356
}
356357
} else if (
357-
trimmed_line == '```' &&
358-
backtick_nesting_level == 1 &&
359-
((current_content.trim() == '' &&
358+
trimmed_line === '```' &&
359+
backtick_nesting_level === 1 &&
360+
current_content.trim() === '' &&
361+
i > 0 &&
362+
lines[i - 1].trim() !== '' &&
363+
(extract_path_from_line_of_code(lines[i - 1]) === null ||
364+
(i + 1 < lines.length && lines[i + 1].trim() !== '')) &&
365+
(() => {
366+
for (let j = i + 1; j < lines.length; j++) {
367+
const next_line = lines[j].trim()
368+
if (next_line) return true
369+
}
370+
return false
371+
})()
372+
) {
373+
backtick_nesting_level++
374+
} else if (
375+
trimmed_line === '```' &&
376+
backtick_nesting_level === 1 &&
377+
(current_language === 'markdown' || current_language === 'md') &&
378+
((current_content.trim() === '' &&
360379
i > 0 &&
361380
lines[i - 1].trim() !== '') ||
362-
((current_language == 'markdown' || current_language == 'md') &&
363-
i > 0 &&
364-
(lines[i - 1].trim() == '' ||
381+
(i > 0 &&
382+
(lines[i - 1].trim() === '' ||
365383
(current_file_name.endsWith('.md') &&
366384
lines[i - 1].trim().endsWith('```'))))) &&
367385
(() => {
@@ -372,9 +390,7 @@ export const parse_multiple_files = (params: {
372390
return false
373391
})()
374392
) {
375-
if (current_language == 'markdown' || current_language === 'md') {
376-
is_markdown_container_block = true
377-
}
393+
is_markdown_container_block = true
378394
backtick_nesting_level++
379395
} else if (trimmed_line.endsWith('```')) {
380396
backtick_nesting_level--
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("hello")

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/empty-file/2-file.txt‎

Lines changed: 0 additions & 1 deletion
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('Hello world!')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
```typescript
2+
// src/hello-world.ts
3+
```
4+
5+
Test
6+
7+
```typescript
8+
// src/hello-world.ts
9+
console.log('Hello world!')
10+
```

‎src/index.js‎

Whitespace-only changes.

0 commit comments

Comments
 (0)