Skip to content

Commit 76d59fe

Browse files
committed
Refactor clipboard parsing logic
1 parent 99694a6 commit 76d59fe

85 files changed

Lines changed: 360 additions & 375 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎file-X.txt‎

Whitespace-only changes.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export const apply_chat_response_command = (
2424
return vscode.commands.registerCommand(
2525
'codeWebChat.applyChatResponse',
2626
async (args?: CommandArgs) => {
27-
console.log('xxx', args)
2827
if (!vscode.workspace.workspaceFolders?.length) {
2928
vscode.window.showErrorMessage(
3029
dictionary.error_message.NO_WORKSPACE_FOLDER_OPEN

‎packages/vscode/src/commands/apply-chat-response-command/response-processor.ts‎

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as vscode from 'vscode'
22
import * as fs from 'fs'
33
import { OriginalFileState } from './types/original-file-state'
44
import { handle_restore_review } from './handlers/restore-review-handler'
5-
import { parse_response } from './utils/clipboard-parser'
5+
import { parse_response, FileItem, DiffItem } from './utils/clipboard-parser'
66
import { create_safe_path } from '@/utils/path-sanitizer'
77
import { dictionary } from '@shared/constants/dictionary'
88
import { Logger } from '@shared/utils/logger'
@@ -72,16 +72,13 @@ export const process_chat_response = async (
7272
const is_single_root_folder_workspace =
7373
vscode.workspace.workspaceFolders?.length == 1
7474

75-
let clipboard_content = parse_response({
75+
let clipboard_items = parse_response({
7676
response: chat_response,
7777
is_single_root_folder_workspace
7878
})
7979

80-
if (
81-
clipboard_content.type == 'code-completion' &&
82-
clipboard_content.code_completion
83-
) {
84-
const completion = clipboard_content.code_completion
80+
if (clipboard_items.length == 1 && clipboard_items[0].type == 'completion') {
81+
const completion = clipboard_items[0]
8582
const workspace_map = new Map<string, string>()
8683
vscode.workspace.workspaceFolders!.forEach((folder) => {
8784
workspace_map.set(folder.name, folder.uri.fsPath)
@@ -146,15 +143,22 @@ export const process_chat_response = async (
146143
}
147144
args.suppress_fast_replace_inaccuracies_dialog = true
148145

149-
clipboard_content = {
150-
type: 'files',
151-
files: [{ ...completion, content: new_content }]
152-
} as any
146+
clipboard_items = [
147+
{
148+
type: 'file',
149+
file_path: completion.file_path,
150+
content: new_content,
151+
workspace_name: completion.workspace_name
152+
}
153+
]
153154
}
154155

155-
if (clipboard_content.type == 'patches' && clipboard_content.patches) {
156+
const item_type = clipboard_items.length > 0 ? clipboard_items[0].type : null
157+
158+
if (item_type == 'diff') {
159+
const patches = clipboard_items as DiffItem[]
156160
const rename_map = new Map<string, string>()
157-
clipboard_content.patches.forEach((patch) => {
161+
patches.forEach((patch) => {
158162
if (patch.new_file_path && patch.file_path) {
159163
rename_map.set(patch.file_path, patch.new_file_path)
160164
}
@@ -187,10 +191,10 @@ export const process_chat_response = async (
187191
}[] = []
188192
let any_patch_used_fallback = false
189193

190-
const total_patches = clipboard_content.patches.length
194+
const total_patches = patches.length
191195

192196
for (let i = 0; i < total_patches; i++) {
193-
const patch = clipboard_content.patches[i]
197+
const patch = patches[i]
194198
let workspace_path = default_workspace
195199

196200
if (patch.workspace_name && workspace_map.has(patch.workspace_name)) {
@@ -354,7 +358,10 @@ export const process_chat_response = async (
354358

355359
return null
356360
} else {
357-
if (!clipboard_content.files || clipboard_content.files.length == 0) {
361+
const files = clipboard_items.filter(
362+
(item): item is FileItem => item.type == 'file'
363+
)
364+
if (files.length == 0) {
358365
const editor = vscode.window.activeTextEditor
359366
if (editor) {
360367
const choice = await vscode.window.showWarningMessage(
@@ -427,7 +434,7 @@ export const process_chat_response = async (
427434
let selected_mode_label: 'Fast replace' | 'Intelligent update' | undefined =
428435
undefined
429436

430-
const all_files_new = await check_if_all_files_new(clipboard_content.files)
437+
const all_files_new = await check_if_all_files_new(files)
431438

432439
if (all_files_new) {
433440
selected_mode_label = 'Fast replace'
@@ -441,9 +448,7 @@ export const process_chat_response = async (
441448
args?.edit_format === undefined || // Is undefined when invoked manually
442449
args?.edit_format == 'truncated'
443450
) {
444-
has_truncated_fragments = check_for_truncated_fragments(
445-
clipboard_content.files
446-
)
451+
has_truncated_fragments = check_for_truncated_fragments(files)
447452
}
448453

449454
if (has_truncated_fragments) {
@@ -466,7 +471,7 @@ export const process_chat_response = async (
466471
let operation_success = false
467472

468473
if (selected_mode_label == 'Fast replace') {
469-
const result = await handle_fast_replace(clipboard_content.files)
474+
const result = await handle_fast_replace(files)
470475
if (result.success && result.original_states) {
471476
if (!args?.suppress_fast_replace_inaccuracies_dialog) {
472477
result.original_states.forEach((s) => (s.is_replaced = true))

0 commit comments

Comments
 (0)