Skip to content

Commit cc18540

Browse files
committed
Enhance parse_multiple_files to correctly identify and extract file paths enclosed within a code block that immediately precedes the content code block
1 parent 5312405 commit cc18540

5 files changed

Lines changed: 54 additions & 7 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,26 @@ describe('clipboard-parser', () => {
598598
})
599599
})
600600

601+
it('parses file path from a code block preceding a content code block', () => {
602+
const test_case = 'path-above-code-block-enclosed-in-code-block'
603+
const text = load_test_case_file(test_case, `${test_case}.txt`)
604+
const result = parse_multiple_files({
605+
response: text,
606+
is_single_root_folder_workspace: true
607+
})
608+
609+
expect(result).toHaveLength(2)
610+
expect(result[0]).toMatchObject({
611+
type: 'text',
612+
content: load_test_case_file(test_case, '1-text.txt')
613+
})
614+
expect(result[1]).toMatchObject({
615+
type: 'file',
616+
file_path: 'src/hello-world.ts',
617+
content: load_test_case_file(test_case, '2-file.txt')
618+
})
619+
})
620+
601621
it('extracts file path from backticks when there are no intermediate lines before code block', () => {
602622
const test_case = 'path-in-backticks-no-intermediate-empty-lines'
603623
const text = load_test_case_file(test_case, `${test_case}.txt`)

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,7 @@ export const parse_multiple_files = (params: {
436436
current_content.trim() === '' &&
437437
i > 0 &&
438438
lines[i - 1].trim() !== '' &&
439-
(extract_path_from_line_of_code(lines[i - 1]) === null ||
440-
(i + 1 < lines.length && lines[i + 1].trim() !== '')) &&
439+
lines[i - 1].trim().startsWith('```') &&
441440
(() => {
442441
for (let j = i + 1; j < lines.length; j++) {
443442
const next_line = lines[j].trim()
@@ -469,12 +468,25 @@ export const parse_multiple_files = (params: {
469468
is_markdown_container_block = true
470469
backtick_nesting_level++
471470
} else if (trimmed_line.endsWith('```')) {
472-
backtick_nesting_level--
471+
let should_close = true
473472
if (
474-
backtick_nesting_level == 1 &&
475-
(current_language == 'markdown' || current_language == 'md')
473+
trimmed_line === '```' &&
474+
backtick_nesting_level === 1 &&
475+
current_file_name &&
476+
i + 1 < lines.length &&
477+
lines[i + 1].trim() !== ''
476478
) {
477-
is_first_content_line = true
479+
should_close = false
480+
}
481+
482+
if (should_close) {
483+
backtick_nesting_level--
484+
if (
485+
backtick_nesting_level == 1 &&
486+
(current_language == 'markdown' || current_language == 'md')
487+
) {
488+
is_first_content_line = true
489+
}
478490
}
479491
}
480492

@@ -515,6 +527,10 @@ export const parse_multiple_files = (params: {
515527
results.push({ type: 'text', content: raw_code_block })
516528
}
517529

530+
if (current_file_name && !current_content.trim()) {
531+
last_seen_file_path_comment = current_file_name
532+
}
533+
518534
current_file_name = ''
519535
current_content = ''
520536
is_first_content_line = false
@@ -747,4 +763,4 @@ export const parse_multiple_files = (params: {
747763
}
748764

749765
return merged_results
750-
}
766+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lorem ipsum.
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,9 @@
1+
Lorem ipsum.
2+
3+
```typescript
4+
src/hello-world.ts
5+
```
6+
7+
```typescript
8+
console.log('Hello world!')
9+
```

0 commit comments

Comments
 (0)