Skip to content

Commit 7a5981c

Browse files
committed
Improve file content parsing to correctly extract multiple files when they are wrapped within outer markdown code blocks
1 parent 113e4eb commit 7a5981c

8 files changed

Lines changed: 100 additions & 4 deletions

File tree

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,44 @@ describe('clipboard-parser', () => {
300300
)
301301
})
302302

303+
it('parses multiple files when each is wrapped in its own outer markdown code block', () => {
304+
const test_case = 'markdown-wrappers'
305+
const text = load_test_case_file(test_case, `${test_case}.txt`)
306+
const result = parse_multiple_files({
307+
response: text,
308+
is_single_root_folder_workspace: true
309+
})
310+
311+
expect(result).toHaveLength(2)
312+
expect(result[0].file_path).toBe('src/lorem.ts')
313+
expect(result[0].content).toBe(
314+
load_test_case_file(test_case, 'file-1.txt')
315+
)
316+
expect(result[1].file_path).toBe('src/ipsum.ts')
317+
expect(result[1].content).toBe(
318+
load_test_case_file(test_case, 'file-2.txt')
319+
)
320+
})
321+
322+
it('parses multiple files when wrapped in a single outer markdown code block', () => {
323+
const test_case = 'unclosed-markdown-wrapper'
324+
const text = load_test_case_file(test_case, `${test_case}.txt`)
325+
const result = parse_multiple_files({
326+
response: text,
327+
is_single_root_folder_workspace: true
328+
})
329+
330+
expect(result).toHaveLength(2)
331+
expect(result[0].file_path).toBe('src/lorem.ts')
332+
expect(result[0].content).toBe(
333+
load_test_case_file(test_case, 'file-1.txt')
334+
)
335+
expect(result[1].file_path).toBe('src/ipsum.ts')
336+
expect(result[1].content).toBe(
337+
load_test_case_file(test_case, 'file-2.txt')
338+
)
339+
})
340+
303341
it('parses PHP files when the opening tag appears before the file path comment', () => {
304342
const test_case = 'php-opening-tag'
305343
const text = load_test_case_file(test_case, `${test_case}.txt`)

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

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,12 @@ export const parse_multiple_files = (params: {
347347
((current_language == 'markdown' || current_language == 'md') &&
348348
i > 0 &&
349349
(lines[i - 1].trim() == '' ||
350-
lines[i - 1].trim().endsWith('```')))) &&
350+
(current_file_name.endsWith('.md') &&
351+
lines[i - 1].trim().endsWith('```'))))) &&
351352
(() => {
352353
for (let j = i + 1; j < lines.length; j++) {
353354
const next_line = lines[j].trim()
354-
if (next_line !== '') {
355-
return !next_line.startsWith('```')
356-
}
355+
if (next_line) return true
357356
}
358357
return false
359358
})()
@@ -363,6 +362,12 @@ export const parse_multiple_files = (params: {
363362
backtick_nesting_level++
364363
} else if (trimmed_line.endsWith('```')) {
365364
backtick_nesting_level--
365+
if (
366+
backtick_nesting_level === 1 &&
367+
(current_language === 'markdown' || current_language === 'md')
368+
) {
369+
is_first_content_line = true
370+
}
366371
}
367372

368373
if (backtick_nesting_level <= 0) {
@@ -500,6 +505,30 @@ export const parse_multiple_files = (params: {
500505
}
501506

502507
if (extracted_filename) {
508+
if (
509+
(current_language === 'markdown' || current_language === 'md') &&
510+
current_file_name
511+
) {
512+
// This is a new file inside a markdown block. Save the previous one.
513+
const cleaned_content = current_content.trim()
514+
if (has_real_code(cleaned_content)) {
515+
const file_key = `${
516+
current_workspace_name || ''
517+
}:${current_file_name}`
518+
if (files_map.has(file_key)) {
519+
const existing_file = files_map.get(file_key)!
520+
existing_file.content += '\n\n' + cleaned_content
521+
} else {
522+
files_map.set(file_key, {
523+
file_path: current_file_name,
524+
content: cleaned_content,
525+
workspace_name: current_workspace_name
526+
})
527+
}
528+
}
529+
current_content = ''
530+
}
531+
503532
const { workspace_name, relative_path } =
504533
extract_workspace_and_path(
505534
extracted_filename,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('lorem');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('ipsum');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
```markdown
2+
```ts
3+
// src/lorem.ts
4+
console.log('lorem');
5+
```
6+
```
7+
8+
Lorem ipsum.
9+
10+
```markdown
11+
```ts
12+
// src/ipsum.ts
13+
console.log('ipsum');
14+
```
15+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('lorem');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('ipsum');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
```markdown
2+
```ts
3+
// src/lorem.ts
4+
console.log('lorem');
5+
```
6+
7+
```ts
8+
// src/ipsum.ts
9+
console.log('ipsum');
10+
```

0 commit comments

Comments
 (0)