|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import * as path from 'path' |
| 3 | +import * as fs from 'fs' |
| 4 | +import { WorkspaceProvider } from '../../../context/providers/workspace-provider' |
| 5 | +import { Logger } from '@shared/utils/logger' |
| 6 | +import { dictionary } from '@shared/constants/dictionary' |
| 7 | +import { extract_paths_from_text } from '../../../utils/path-parser' |
| 8 | +import { display_token_count } from '../../../utils/display-token-count' |
| 9 | + |
| 10 | +export async function handle_clipboard_source( |
| 11 | + workspace_provider: WorkspaceProvider |
| 12 | +): Promise<void> { |
| 13 | + try { |
| 14 | + const clipboard_text = await vscode.env.clipboard.readText() |
| 15 | + if (!clipboard_text) { |
| 16 | + vscode.window.showInformationMessage( |
| 17 | + dictionary.information_message.NO_FILE_PATHS_FOUND_IN_CLIPBOARD |
| 18 | + ) |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + const paths = extract_paths_from_text(clipboard_text) |
| 23 | + if (paths.length == 0) { |
| 24 | + vscode.window.showInformationMessage( |
| 25 | + dictionary.information_message.NO_FILE_PATHS_FOUND_IN_CLIPBOARD |
| 26 | + ) |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + const workspace_roots = workspace_provider.getWorkspaceRoots() |
| 31 | + const absolute_paths: string[] = [] |
| 32 | + |
| 33 | + const workspace_map = new Map<string, string>() |
| 34 | + if (vscode.workspace.workspaceFolders) { |
| 35 | + vscode.workspace.workspaceFolders.forEach((folder) => { |
| 36 | + workspace_map.set(folder.name, folder.uri.fsPath) |
| 37 | + }) |
| 38 | + } |
| 39 | + |
| 40 | + for (const raw_path of paths) { |
| 41 | + if (path.isAbsolute(raw_path)) { |
| 42 | + absolute_paths.push(raw_path) |
| 43 | + continue |
| 44 | + } |
| 45 | + |
| 46 | + let resolved_path: string | null = null |
| 47 | + |
| 48 | + if (workspace_map.size > 1) { |
| 49 | + for (const [workspace_name, workspace_root] of workspace_map) { |
| 50 | + if ( |
| 51 | + raw_path.startsWith(workspace_name + '/') || |
| 52 | + raw_path.startsWith(workspace_name + '\\') |
| 53 | + ) { |
| 54 | + const relative_path = raw_path.substring(workspace_name.length + 1) |
| 55 | + resolved_path = path.join(workspace_root, relative_path) |
| 56 | + break |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (resolved_path) { |
| 62 | + absolute_paths.push(resolved_path) |
| 63 | + } else { |
| 64 | + for (const root of workspace_roots) { |
| 65 | + const potential_path = path.join(root, raw_path) |
| 66 | + absolute_paths.push(potential_path) |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + const existing_paths = absolute_paths.filter((p) => { |
| 72 | + try { |
| 73 | + return fs.existsSync(p) && fs.statSync(p).isFile() |
| 74 | + } catch { |
| 75 | + return false |
| 76 | + } |
| 77 | + }) |
| 78 | + |
| 79 | + if (existing_paths.length == 0) { |
| 80 | + vscode.window.showInformationMessage( |
| 81 | + dictionary.information_message |
| 82 | + .NO_MATCHING_FILES_FOUND_FOR_CLIPBOARD_PATHS |
| 83 | + ) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + const quick_pick_items = await Promise.all( |
| 88 | + existing_paths.map(async (file_path) => { |
| 89 | + const token_count = await workspace_provider.calculate_file_tokens( |
| 90 | + file_path |
| 91 | + ) |
| 92 | + |
| 93 | + const formatted_token_count = display_token_count(token_count) |
| 94 | + |
| 95 | + return { |
| 96 | + label: path.basename(file_path), |
| 97 | + description: `${formatted_token_count} ${path.dirname( |
| 98 | + path.relative(workspace_roots[0] || '', file_path) |
| 99 | + )}`, |
| 100 | + picked: true, |
| 101 | + file_path: file_path |
| 102 | + } |
| 103 | + }) |
| 104 | + ) |
| 105 | + |
| 106 | + const selected_items = await vscode.window.showQuickPick(quick_pick_items, { |
| 107 | + canPickMany: true, |
| 108 | + placeHolder: 'Select files to include', |
| 109 | + title: `Found ${existing_paths.length} file path${ |
| 110 | + existing_paths.length == 1 ? '' : 's' |
| 111 | + }` |
| 112 | + }) |
| 113 | + |
| 114 | + if (!selected_items || selected_items.length === 0) { |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + const selected_paths = selected_items.map((item) => item.file_path) |
| 119 | + |
| 120 | + Logger.info({ |
| 121 | + message: `Selected ${selected_paths.length} file${ |
| 122 | + selected_paths.length == 1 ? '' : 's' |
| 123 | + }.`, |
| 124 | + data: { paths: selected_paths } |
| 125 | + }) |
| 126 | + |
| 127 | + await workspace_provider.set_checked_files(selected_paths) |
| 128 | + vscode.window.showInformationMessage( |
| 129 | + `Selected ${selected_paths.length} file${ |
| 130 | + selected_paths.length == 1 ? '' : 's' |
| 131 | + }.` |
| 132 | + ) |
| 133 | + } catch (error) { |
| 134 | + vscode.window.showErrorMessage( |
| 135 | + dictionary.error_message.FAILED_TO_SELECT_FILES_FROM_CLIPBOARD( |
| 136 | + error instanceof Error ? error.message : String(error) |
| 137 | + ) |
| 138 | + ) |
| 139 | + } |
| 140 | +} |
0 commit comments