Skip to content

Commit c1db780

Browse files
committed
Refactor to consolidate and simplify the handling of applying raw clipboard content to the active editor via Intelligent Update
1 parent bc11062 commit c1db780

2 files changed

Lines changed: 93 additions & 15 deletions

File tree

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from 'vscode'
22
import * as fs from 'fs'
3+
import * as path from 'path'
34
import { parse_response, ClipboardFile } from './utils/clipboard-parser'
45
import {
56
LAST_APPLIED_CHANGES_EDITOR_STATE_STATE_KEY,
@@ -728,6 +729,87 @@ export const apply_chat_response_command = (
728729
return null
729730
} else {
730731
if (!clipboard_content.files || clipboard_content.files.length == 0) {
732+
const editor = vscode.window.activeTextEditor
733+
if (editor) {
734+
const choice = await vscode.window.showWarningMessage(
735+
'No valid code blocks found in the clipboard text. Apply it to the active editor with the Intelligent Update API tool?',
736+
{ modal: true },
737+
'Apply'
738+
)
739+
740+
if (choice == 'Apply') {
741+
const document = editor.document
742+
const workspaceFolder = vscode.workspace.getWorkspaceFolder(
743+
document.uri
744+
)
745+
const relativePath = (
746+
workspaceFolder
747+
? path.relative(
748+
workspaceFolder.uri.fsPath,
749+
document.uri.fsPath
750+
)
751+
: path.basename(document.uri.fsPath)
752+
).replace(/\\/g, '/')
753+
754+
const workspace_name = workspaceFolder?.name
755+
756+
const file_path_for_block =
757+
workspace_name && !is_single_root_folder_workspace
758+
? `${workspace_name}/${relativePath}`
759+
: relativePath
760+
761+
const fake_chat_response = `\`\`\`\n// ${file_path_for_block}\n${chat_response}\n\`\`\``
762+
763+
const api_providers_manager = new ModelProvidersManager(context)
764+
const config_result = await get_intelligent_update_config(
765+
api_providers_manager,
766+
false,
767+
context
768+
)
769+
770+
if (!config_result) {
771+
return null
772+
}
773+
774+
const { provider, config: intelligent_update_config } =
775+
config_result
776+
777+
let endpoint_url = ''
778+
if (provider.type == 'built-in') {
779+
const provider_info =
780+
PROVIDERS[provider.name as keyof typeof PROVIDERS]
781+
endpoint_url = provider_info.base_url
782+
} else {
783+
endpoint_url = provider.base_url
784+
}
785+
786+
const intelligent_update_states =
787+
await handle_intelligent_update({
788+
endpoint_url,
789+
api_key: provider.api_key,
790+
config: intelligent_update_config,
791+
chat_response: fake_chat_response,
792+
context: context,
793+
is_single_root_folder_workspace,
794+
view_provider
795+
})
796+
797+
if (intelligent_update_states) {
798+
update_undo_button_state(
799+
intelligent_update_states,
800+
chat_response,
801+
args?.original_editor_state
802+
)
803+
804+
return {
805+
original_states: intelligent_update_states,
806+
chat_response
807+
}
808+
}
809+
return null
810+
}
811+
}
812+
731813
vscode.window.showWarningMessage(
732814
dictionary.warning_message.NO_VALID_CODE_BLOCKS_IN_CLIPBOARD
733815
)

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export const handle_intelligent_update = async (params: {
2121
context: vscode.ExtensionContext
2222
is_single_root_folder_workspace: boolean
2323
view_provider?: ViewProvider
24-
progress_title_override?: string
2524
}): Promise<OriginalFileState[] | null> => {
2625
const workspace_map = new Map<string, string>()
2726
if (vscode.workspace.workspaceFolders) {
@@ -137,20 +136,17 @@ export const handle_intelligent_update = async (params: {
137136
}
138137

139138
let progress_title = ''
140-
if (params.progress_title_override) {
141-
progress_title = params.progress_title_override
142-
} else {
143-
if (existing_files.length > 0 && new_files.length > 0) {
144-
progress_title = `Called Intelligent Update API tool for ${
145-
existing_files.length
146-
} file${existing_files.length > 1 ? 's' : ''} and creating ${
147-
new_files.length
148-
} new file${new_files.length > 1 ? 's' : ''}...`
149-
} else if (existing_files.length > 0) {
150-
progress_title = `Called Intelligent Update API tool for ${
151-
existing_files.length
152-
} file${existing_files.length > 1 ? 's' : ''}...`
153-
}
139+
140+
if (existing_files.length > 0 && new_files.length > 0) {
141+
progress_title = `Called Intelligent Update API tool for ${
142+
existing_files.length
143+
} file${existing_files.length > 1 ? 's' : ''} and creating ${
144+
new_files.length
145+
} new file${new_files.length > 1 ? 's' : ''}...`
146+
} else if (existing_files.length > 0) {
147+
progress_title = `Called Intelligent Update API tool for ${
148+
existing_files.length
149+
} file${existing_files.length > 1 ? 's' : ''}...`
154150
}
155151

156152
const file_progress_list: FileProgress[] = files.map((f) => ({

0 commit comments

Comments
 (0)