Skip to content

Commit f04fbd4

Browse files
committed
Ensure file content is correctly parsed when the file path is identified inside a markdown-labeled code block
1 parent 9013adb commit f04fbd4

4 files changed

Lines changed: 33 additions & 2 deletions

File tree

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

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

303+
it('parses file when file path is in a comment inside a code block of markdown type', () => {
304+
const test_case = 'markdown-code-block-with-code'
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(1)
312+
expect(result[0].file_path).toBe('src/main.js')
313+
expect(result[0].content).toBe(
314+
load_test_case_file(test_case, 'file-1.txt')
315+
)
316+
})
317+
303318
it('parses multiple files when each is wrapped in its own outer markdown code block', () => {
304319
const test_case = 'markdown-wrappers'
305320
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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/clipboard-parser.ts
21
import { cleanup_api_response } from '@/utils/cleanup-api-response'
32
import { extract_path_from_line_of_code } from '@shared/utils/extract-path-from-line-of-code'
43
import { Diff, extract_diffs } from './extract-diff-patches'
@@ -175,6 +174,7 @@ export const parse_multiple_files = (params: {
175174
let in_cdata = false
176175
let backtick_nesting_level = 0
177176
let last_seen_file_path_comment: string | null = null
177+
let is_markdown_container_block = false
178178
let current_language = ''
179179

180180
const lines = params.response.split('\n')
@@ -240,6 +240,7 @@ export const parse_multiple_files = (params: {
240240
xml_file_mode = false
241241
top_level_xml_file_mode = false
242242
in_cdata = false
243+
is_markdown_container_block = false
243244
continue
244245
} else if (line.trim().startsWith('<file')) {
245246
const extracted_filename = extract_file_path_from_xml(line)
@@ -347,6 +348,12 @@ export const parse_multiple_files = (params: {
347348
is_first_content_line = true
348349
}
349350
backtick_nesting_level++
351+
if (
352+
(current_language === 'markdown' || current_language === 'md') &&
353+
backtick_nesting_level > 1
354+
) {
355+
is_markdown_container_block = true
356+
}
350357
} else if (
351358
trimmed_line == '```' &&
352359
backtick_nesting_level == 1 &&
@@ -364,6 +371,9 @@ export const parse_multiple_files = (params: {
364371
return false
365372
})()
366373
) {
374+
if (current_language === 'markdown' || current_language === 'md') {
375+
is_markdown_container_block = true
376+
}
367377
// Heuristic: A raw ``` at the start of a block (after the filename comment)
368378
// or after an empty line is likely opening a nested block.
369379
backtick_nesting_level++
@@ -588,7 +598,8 @@ export const parse_multiple_files = (params: {
588598
if (
589599
!lang_is_markdown ||
590600
is_markdown_file ||
591-
backtick_nesting_level > 1
601+
backtick_nesting_level > 1 ||
602+
(current_file_name && !is_markdown_container_block)
592603
) {
593604
if (current_content) {
594605
current_content += '\n' + line
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("hello")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
```markdown
2+
// src/main.js
3+
console.log("hello")
4+
```

0 commit comments

Comments
 (0)