Skip to content

Commit 83200e3

Browse files
committed
Refactor clipboard parsing types by unifying ClipboardFile and ClipboardCodeCompletion into the new FileItem and CompletionItem interfaces, ensuring all parsed items explicitly include a type field
1 parent 4c32a99 commit 83200e3

12 files changed

Lines changed: 73 additions & 59 deletions

File tree

‎packages/vscode/src/commands/apply-chat-response-command/handlers/fast-replace-handler.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import * as fs from 'fs'
44
import { Logger } from '@shared/utils/logger'
55
import { dictionary } from '@shared/constants/dictionary'
66
import { create_safe_path, sanitize_file_name } from '@/utils/path-sanitizer'
7-
import { ClipboardFile } from '../utils/clipboard-parser'
7+
import { FileItem } from '../utils/clipboard-parser'
88
import { OriginalFileState } from '@/commands/apply-chat-response-command/types/original-file-state'
99

1010
export const handle_fast_replace = async (
11-
files: ClipboardFile[]
11+
files: FileItem[]
1212
): Promise<{ success: boolean; original_states?: OriginalFileState[] }> => {
1313
Logger.info({
1414
function_name: 'handle_fast_replace',
1515
message: 'start',
1616
data: { file_count: files.length }
1717
})
1818
try {
19-
const safe_files: ClipboardFile[] = []
19+
const safe_files: FileItem[] = []
2020
const unsafe_files: string[] = []
2121

2222
if (

‎packages/vscode/src/commands/apply-chat-response-command/handlers/intelligent-update-handler.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as vscode from 'vscode'
22
import axios from 'axios'
33
import * as path from 'path'
44
import * as fs from 'fs'
5-
import { ClipboardFile } from '../utils/clipboard-parser'
5+
import { FileItem } from '../utils/clipboard-parser'
66
import { sanitize_file_name, create_safe_path } from '@/utils/path-sanitizer'
77
import { Logger } from '@shared/utils/logger'
88
import { OriginalFileState } from '@/commands/apply-chat-response-command/types/original-file-state'
@@ -53,10 +53,10 @@ export const handle_intelligent_update = async (params: {
5353
})
5454

5555
const raw_files = raw_items.filter(
56-
(item): item is ClipboardFile => 'file_path' in item
56+
(item): item is FileItem => item.type == 'file'
5757
)
5858

59-
const files: ClipboardFile[] = []
59+
const files: FileItem[] = []
6060
const skipped_files: string[] = []
6161

6262
for (const file of raw_files) {
@@ -120,8 +120,8 @@ export const handle_intelligent_update = async (params: {
120120
return null
121121
}
122122

123-
const new_files: ClipboardFile[] = []
124-
const existing_files: ClipboardFile[] = []
123+
const new_files: FileItem[] = []
124+
const existing_files: FileItem[] = []
125125

126126
for (const file of files) {
127127
let file_exists = false

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
11
import { extract_diffs } from './extract-diff-patches'
22
import { parse_code_completion, parse_multiple_files } from './parsers'
33

4-
export interface ClipboardFile {
5-
file_path: string
6-
content: string
7-
workspace_name?: string
8-
}
9-
10-
export interface ClipboardCodeCompletion {
11-
file_path: string
12-
content: string
13-
line: number
14-
character: number
15-
workspace_name?: string
16-
}
17-
184
export interface FileItem {
195
type: 'file'
206
file_path: string
@@ -98,7 +84,7 @@ export const parse_response = (params: {
9884
is_single_root_folder_workspace
9985
})
10086
if (code_completion) {
101-
return [{ type: 'completion', ...code_completion }]
87+
return [code_completion]
10288
}
10389

10490
const processed_response = params.response.replace(/``````/g, '```\n```')
@@ -129,10 +115,5 @@ export const parse_response = (params: {
129115
is_single_root_folder_workspace
130116
})
131117

132-
return items.map((item) => {
133-
if ('file_path' in item) {
134-
return { type: 'file', ...item }
135-
}
136-
return item
137-
})
118+
return items
138119
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { cleanup_api_response } from '@/utils/cleanup-api-response'
2-
import {
3-
ClipboardCodeCompletion,
4-
extract_workspace_and_path
5-
} from '../clipboard-parser'
2+
import { CompletionItem, extract_workspace_and_path } from '../clipboard-parser'
63

74
const extract_path_and_position = (
85
line: string
@@ -31,7 +28,7 @@ const extract_path_and_position = (
3128
export const parse_code_completion = (params: {
3229
response: string
3330
is_single_root_folder_workspace: boolean
34-
}): ClipboardCodeCompletion | null => {
31+
}): CompletionItem | null => {
3532
const lines = params.response.split('\n')
3633
let in_code_block = false
3734
let first_line_of_block: string | null = null
@@ -50,6 +47,7 @@ export const parse_code_completion = (params: {
5047
params.is_single_root_folder_workspace
5148
})
5249
return {
50+
type: 'completion',
5351
file_path: relative_path,
5452
content: cleanup_api_response({
5553
content: content_lines.join('\n')

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { extract_path_from_line_of_code } from '@shared/utils/extract-path-from-line-of-code'
22
import {
3-
ClipboardFile,
3+
FileItem,
44
extract_workspace_and_path,
55
has_real_code
66
} from '../clipboard-parser'
77

88
export const parse_file_content_only = (params: {
99
response: string
1010
is_single_root_folder_workspace: boolean
11-
}): ClipboardFile | null => {
11+
}): FileItem | null => {
1212
if (params.response.includes('```')) {
1313
return null
1414
}
@@ -44,6 +44,7 @@ export const parse_file_content_only = (params: {
4444

4545
if (has_real_code(cleaned_content)) {
4646
return {
47+
type: 'file',
4748
file_path: relative_path,
4849
content: cleaned_content,
4950
workspace_name: workspace_name

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { extract_path_from_line_of_code } from '@shared/utils/extract-path-from-line-of-code'
22
import {
3-
ClipboardFile,
3+
FileItem,
44
TextItem,
55
extract_workspace_and_path,
66
has_real_code
@@ -15,14 +15,14 @@ const extract_file_path_from_xml = (line: string): string | null => {
1515
export const parse_multiple_files = (params: {
1616
response: string
1717
is_single_root_folder_workspace: boolean
18-
}): (ClipboardFile | TextItem)[] => {
18+
}): (FileItem | TextItem)[] => {
1919
const file_content_result = parse_file_content_only(params)
2020
if (file_content_result) {
2121
return [file_content_result]
2222
}
2323

24-
const results: (ClipboardFile | TextItem)[] = []
25-
const file_ref_map = new Map<string, ClipboardFile>()
24+
const results: (FileItem | TextItem)[] = []
25+
const file_ref_map = new Map<string, FileItem>()
2626
let current_text_block = ''
2727

2828
let state = 'TEXT'
@@ -222,6 +222,7 @@ export const parse_multiple_files = (params: {
222222
existing_file.content += '\n\n' + final_content
223223
} else {
224224
const new_file = {
225+
type: 'file' as const,
225226
file_path: current_file_name,
226227
content: final_content,
227228
workspace_name: current_workspace_name
@@ -323,13 +324,28 @@ export const parse_multiple_files = (params: {
323324
existing_file.content += '\n\n' + cleaned_content
324325
} else {
325326
const new_file = {
327+
type: 'file' as const,
326328
file_path: current_file_name,
327329
content: cleaned_content,
328330
workspace_name: current_workspace_name
329331
}
330332
file_ref_map.set(file_key, new_file)
331333
results.push(new_file)
332334
}
335+
} else if (!current_file_name) {
336+
const raw_code_block = [
337+
`\`\`\`${current_language}`,
338+
current_content,
339+
'```'
340+
].join('\n')
341+
342+
const last_result =
343+
results.length > 0 ? results[results.length - 1] : null
344+
if (last_result && last_result.type === 'text') {
345+
last_result.content += '\n' + raw_code_block
346+
} else {
347+
results.push({ type: 'text', content: raw_code_block })
348+
}
333349
}
334350

335351
current_file_name = ''
@@ -441,6 +457,7 @@ export const parse_multiple_files = (params: {
441457
existing_file.content += '\n\n' + cleaned_content
442458
} else {
443459
const new_file = {
460+
type: 'file' as const,
444461
file_path: current_file_name,
445462
content: cleaned_content,
446463
workspace_name: current_workspace_name
@@ -552,6 +569,7 @@ export const parse_multiple_files = (params: {
552569
existing_file.content += '\n\n' + cleaned_content
553570
} else {
554571
const new_file = {
572+
type: 'file' as const,
555573
file_path: current_file_name,
556574
content: cleaned_content,
557575
workspace_name: current_workspace_name
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Top.
1+
Top. `abc.ts`
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
Middle.
1+
Middle.
2+
```ts
3+
console.log('some inline code');
4+
```

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
Top.
1+
Top. `abc.ts`
22

33
```ts
44
// src/lorem.ts
55
console.log('lorem');
66
```
77

88
Middle.
9+
```ts
10+
console.log('some inline code');
11+
```
912

1013
```ts
1114
// src/ipsum.ts

‎packages/vscode/src/commands/apply-chat-response-command/utils/file-checks.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as vscode from 'vscode'
22
import * as fs from 'fs'
3-
import { ClipboardFile } from './clipboard-parser'
3+
import { FileItem } from './clipboard-parser'
44
import { create_safe_path } from '@/utils/path-sanitizer'
55

66
export const check_if_all_files_new = async (
7-
files: ClipboardFile[]
7+
files: FileItem[]
88
): Promise<boolean> => {
99
const workspace_map = new Map<string, string>()
1010
vscode.workspace.workspaceFolders!.forEach((folder) => {

0 commit comments

Comments
 (0)