Skip to content

Commit 35a1a79

Browse files
committed
Refactor parse_multiple_files to correctly merge adjacent text blocks and ensure text content is properly trimmed when parsing chat responses
1 parent 9e4be30 commit 35a1a79

3 files changed

Lines changed: 50 additions & 14 deletions

File tree

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

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ export const parse_multiple_files = (params: {
7474
}
7575

7676
if (current_text_block.trim()) {
77-
results.push({ type: 'text', content: current_text_block })
77+
const content =
78+
results.length == 0
79+
? current_text_block.trim()
80+
: current_text_block.trimEnd()
81+
results.push({ type: 'text', content })
7882
}
7983
current_text_block = ''
8084

@@ -232,7 +236,11 @@ export const parse_multiple_files = (params: {
232236

233237
if (extracted_filename) {
234238
if (current_text_block.trim()) {
235-
results.push({ type: 'text', content: current_text_block.trim() })
239+
const content =
240+
results.length == 0
241+
? current_text_block.trim()
242+
: current_text_block.trimEnd()
243+
results.push({ type: 'text', content })
236244
}
237245
current_text_block = ''
238246
last_seen_file_path_comment = extracted_filename
@@ -421,13 +429,7 @@ export const parse_multiple_files = (params: {
421429
'```'
422430
].join('\n')
423431

424-
const last_result =
425-
results.length > 0 ? results[results.length - 1] : null
426-
if (last_result && last_result.type == 'text') {
427-
last_result.content += raw_code_block
428-
} else {
429-
results.push({ type: 'text', content: raw_code_block })
430-
}
432+
results.push({ type: 'text', content: raw_code_block })
431433
}
432434

433435
current_file_name = ''
@@ -667,15 +669,31 @@ export const parse_multiple_files = (params: {
667669
}
668670
}
669671
} else if (state == 'TEXT' && current_text_block.trim()) {
670-
results.push({ type: 'text', content: current_text_block.trim() })
672+
const content =
673+
results.length == 0
674+
? current_text_block.trim()
675+
: current_text_block.trimEnd()
676+
results.push({ type: 'text', content })
671677
}
672678

673-
// Trim text items at the end to ensure clean output
679+
const merged_results: (FileItem | TextItem)[] = []
674680
for (const result of results) {
681+
if (merged_results.length > 0) {
682+
const last = merged_results[merged_results.length - 1]
683+
if (last.type == 'text' && result.type == 'text') {
684+
last.content += '\n' + result.content
685+
continue
686+
}
687+
}
688+
merged_results.push(result)
689+
}
690+
691+
// Trim text items at the end to ensure clean output
692+
for (const result of merged_results) {
675693
if (result.type == 'text') {
676694
result.content = result.content.trim()
677695
}
678696
}
679697

680-
return results
698+
return merged_results
681699
}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
Bottom `test test`.
1+
The change is on line 415 where I modified:
2+
```typescript
3+
message={dictionary.warning_message.NOTHING_SELECTED_IN_CONTEXT}
4+
```
5+
to:
6+
```typescript
7+
message={dictionary.warning_message.NOTHING_SELECTED_IN_CONTEXT.replace('.', '')}
8+
```
9+
10+
This removes the trailing period from the message when it's displayed in the tooltip, while keeping the original string unchanged in the dictionary.

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,13 @@ console.log('some inline code');
2424
console.log('ipsum');
2525
```
2626

27-
Bottom `test test`.
27+
The change is on line 415 where I modified:
28+
```typescript
29+
message={dictionary.warning_message.NOTHING_SELECTED_IN_CONTEXT}
30+
```
31+
to:
32+
```typescript
33+
message={dictionary.warning_message.NOTHING_SELECTED_IN_CONTEXT.replace('.', '')}
34+
```
35+
36+
This removes the trailing period from the message when it's displayed in the tooltip, while keeping the original string unchanged in the dictionary.

0 commit comments

Comments
 (0)