|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import { WorkspaceProvider } from '../../context/providers/workspace/workspace-provider' |
| 3 | +import { prompt_for_imported_files } from './utils/prompt-for-imported-files' |
| 4 | +import { t } from '@/i18n' |
| 5 | +import { WebSocketManager } from '@/services/websocket-manager' |
| 6 | + |
| 7 | +export const select_imported_files_commands = ( |
| 8 | + workspace_provider: WorkspaceProvider, |
| 9 | + extension_context: vscode.ExtensionContext, |
| 10 | + websocket_manager: WebSocketManager |
| 11 | +) => { |
| 12 | + return [ |
| 13 | + vscode.commands.registerCommand( |
| 14 | + 'codeWebChat.selectImportedFiles', |
| 15 | + async (item?: any) => { |
| 16 | + let target_uri: vscode.Uri | undefined |
| 17 | + |
| 18 | + if (item?.resourceUri) { |
| 19 | + target_uri = item.resourceUri |
| 20 | + } else if ( |
| 21 | + item instanceof vscode.Uri || |
| 22 | + (item && item.fsPath && item.scheme) |
| 23 | + ) { |
| 24 | + target_uri = item as vscode.Uri |
| 25 | + } else { |
| 26 | + const editor = vscode.window.activeTextEditor |
| 27 | + if (editor) { |
| 28 | + target_uri = editor.document.uri |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + if (!target_uri) return |
| 33 | + |
| 34 | + const starting_uris = |
| 35 | + await workspace_provider.get_all_files_for_uri(target_uri) |
| 36 | + |
| 37 | + if (starting_uris.length == 0) { |
| 38 | + vscode.window.showInformationMessage( |
| 39 | + t('command.select-imported-files.no-valid-files') |
| 40 | + ) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + const result = await prompt_for_imported_files({ |
| 45 | + starting_uris, |
| 46 | + workspace_provider, |
| 47 | + extension_context, |
| 48 | + websocket_manager |
| 49 | + }) |
| 50 | + |
| 51 | + if (result) { |
| 52 | + const final_checked = workspace_provider.get_checked_files() |
| 53 | + const paths_to_apply = [ |
| 54 | + ...new Set([ |
| 55 | + ...final_checked.filter((p) => !result.shown_paths.includes(p)), |
| 56 | + ...result.selected_paths |
| 57 | + ]) |
| 58 | + ] |
| 59 | + await workspace_provider.set_checked_files(paths_to_apply) |
| 60 | + } |
| 61 | + } |
| 62 | + ), |
| 63 | + vscode.commands.registerCommand( |
| 64 | + 'codeWebChat.selectImportedFilesForSelected', |
| 65 | + async () => { |
| 66 | + const checked_files = workspace_provider.get_checked_files() |
| 67 | + |
| 68 | + if (checked_files.length === 0) { |
| 69 | + vscode.window.showInformationMessage( |
| 70 | + t('command.select-imported-files.no-valid-files') |
| 71 | + ) |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + const starting_uris = checked_files.map((file_path) => |
| 76 | + vscode.Uri.file(file_path) |
| 77 | + ) |
| 78 | + |
| 79 | + const result = await prompt_for_imported_files({ |
| 80 | + starting_uris, |
| 81 | + workspace_provider, |
| 82 | + extension_context, |
| 83 | + websocket_manager |
| 84 | + }) |
| 85 | + |
| 86 | + if (result) { |
| 87 | + const final_checked = workspace_provider.get_checked_files() |
| 88 | + const paths_to_apply = [ |
| 89 | + ...new Set([ |
| 90 | + ...final_checked.filter((p) => !result.shown_paths.includes(p)), |
| 91 | + ...result.selected_paths |
| 92 | + ]) |
| 93 | + ] |
| 94 | + await workspace_provider.set_checked_files(paths_to_apply) |
| 95 | + } |
| 96 | + } |
| 97 | + ) |
| 98 | + ] |
| 99 | +} |
0 commit comments