|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import * as path from 'path' |
| 3 | +import { SavedContext } from '@/types/context' |
| 4 | +import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider' |
| 5 | +import { resolve_context_paths } from './resolve-context-paths' |
| 6 | +import { display_token_count } from '../../../../utils/display-token-count' |
| 7 | +import { t } from '@/i18n' |
| 8 | + |
| 9 | +let active_deletion_timestamp: number | undefined |
| 10 | + |
| 11 | +export const select_context_paths = async ( |
| 12 | + context: SavedContext, |
| 13 | + workspace_provider: WorkspaceProvider, |
| 14 | + update_context_paths?: (remaining_files: string[]) => Promise<void> |
| 15 | +): Promise<'back' | void> => { |
| 16 | + const primary_workspace_root = workspace_provider.get_workspace_roots()[0]! |
| 17 | + let resolved_paths = await resolve_context_paths( |
| 18 | + context, |
| 19 | + primary_workspace_root, |
| 20 | + workspace_provider |
| 21 | + ) |
| 22 | + |
| 23 | + const delete_button = { |
| 24 | + iconPath: new vscode.ThemeIcon('trash'), |
| 25 | + tooltip: t('command.apply-context.action.delete') |
| 26 | + } |
| 27 | + |
| 28 | + while (true) { |
| 29 | + const create_quick_pick_items = async ( |
| 30 | + paths: string[], |
| 31 | + selected_paths_override?: Set<string> |
| 32 | + ) => { |
| 33 | + const currently_checked = workspace_provider.get_checked_files() |
| 34 | + const currently_checked_set = |
| 35 | + selected_paths_override || new Set(currently_checked) |
| 36 | + |
| 37 | + return Promise.all( |
| 38 | + paths.map(async (file_path) => { |
| 39 | + const token_count = |
| 40 | + await workspace_provider.calculate_file_tokens(file_path) |
| 41 | + const formatted_token_count = display_token_count(token_count.total) |
| 42 | + const workspace_root = |
| 43 | + workspace_provider.get_workspace_root_for_file(file_path) |
| 44 | + const relative_path = workspace_root |
| 45 | + ? path.relative(workspace_root, file_path) |
| 46 | + : file_path |
| 47 | + const dir_name = path.dirname(relative_path) |
| 48 | + const display_dir = dir_name == '.' ? '' : dir_name |
| 49 | + |
| 50 | + const buttons: vscode.QuickInputButton[] = [ |
| 51 | + { |
| 52 | + iconPath: new vscode.ThemeIcon('go-to-file'), |
| 53 | + tooltip: 'Go to file' |
| 54 | + } |
| 55 | + ] |
| 56 | + |
| 57 | + if (update_context_paths) { |
| 58 | + buttons.push(delete_button) |
| 59 | + } |
| 60 | + |
| 61 | + return { |
| 62 | + label: path.basename(file_path), |
| 63 | + description: display_dir |
| 64 | + ? `${formatted_token_count} · ${display_dir}` |
| 65 | + : formatted_token_count, |
| 66 | + picked: currently_checked_set.has(file_path), |
| 67 | + file_path, |
| 68 | + buttons |
| 69 | + } |
| 70 | + }) |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + const quick_pick_items = await create_quick_pick_items(resolved_paths) |
| 75 | + |
| 76 | + const list_quick_pick = vscode.window.createQuickPick< |
| 77 | + vscode.QuickPickItem & { file_path: string } |
| 78 | + >() |
| 79 | + list_quick_pick.title = context.name |
| 80 | + list_quick_pick.placeholder = 'Select files to apply to context' |
| 81 | + list_quick_pick.canSelectMany = true |
| 82 | + list_quick_pick.ignoreFocusOut = true |
| 83 | + list_quick_pick.items = quick_pick_items |
| 84 | + list_quick_pick.selectedItems = quick_pick_items.filter((i) => i.picked) |
| 85 | + list_quick_pick.buttons = [vscode.QuickInputButtons.Back] |
| 86 | + |
| 87 | + const list_selection = await new Promise< |
| 88 | + | readonly (vscode.QuickPickItem & { file_path: string })[] |
| 89 | + | 'back' |
| 90 | + | undefined |
| 91 | + >((resolve) => { |
| 92 | + let is_resolved = false |
| 93 | + const resolve_once = (value: any) => { |
| 94 | + if (!is_resolved) { |
| 95 | + is_resolved = true |
| 96 | + resolve(value) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + list_quick_pick.onDidTriggerButton((button) => { |
| 101 | + if (button === vscode.QuickInputButtons.Back) { |
| 102 | + list_quick_pick.hide() |
| 103 | + resolve_once('back') |
| 104 | + } |
| 105 | + }) |
| 106 | + |
| 107 | + list_quick_pick.onDidTriggerItemButton(async (e) => { |
| 108 | + if (e.button.tooltip === 'Go to file') { |
| 109 | + const uri = vscode.Uri.file(e.item.file_path) |
| 110 | + vscode.window.showTextDocument(uri, { |
| 111 | + preview: true, |
| 112 | + preserveFocus: true |
| 113 | + }) |
| 114 | + } else if (e.button === delete_button) { |
| 115 | + const current_timestamp = Date.now() |
| 116 | + active_deletion_timestamp = current_timestamp |
| 117 | + const deleted_path = e.item.file_path |
| 118 | + |
| 119 | + const original_paths = [...resolved_paths] |
| 120 | + resolved_paths = resolved_paths.filter((p) => p !== deleted_path) |
| 121 | + |
| 122 | + if (update_context_paths) { |
| 123 | + await update_context_paths(resolved_paths) |
| 124 | + } |
| 125 | + |
| 126 | + const current_selected = new Set( |
| 127 | + list_quick_pick.selectedItems.map((i) => i.file_path) |
| 128 | + ) |
| 129 | + list_quick_pick.items = await create_quick_pick_items( |
| 130 | + resolved_paths, |
| 131 | + current_selected |
| 132 | + ) |
| 133 | + list_quick_pick.selectedItems = list_quick_pick.items.filter( |
| 134 | + (i) => i.picked |
| 135 | + ) |
| 136 | + |
| 137 | + const choice = await vscode.window.showInformationMessage( |
| 138 | + 'Removed from context.', |
| 139 | + 'Undo' |
| 140 | + ) |
| 141 | + |
| 142 | + if (active_deletion_timestamp !== current_timestamp) { |
| 143 | + if (choice === 'Undo') { |
| 144 | + vscode.window.showInformationMessage( |
| 145 | + t('command.apply-context.undo.failed') |
| 146 | + ) |
| 147 | + } |
| 148 | + return |
| 149 | + } |
| 150 | + |
| 151 | + if (choice === 'Undo') { |
| 152 | + resolved_paths = original_paths |
| 153 | + if (update_context_paths) { |
| 154 | + await update_context_paths(resolved_paths) |
| 155 | + } |
| 156 | + const current_selected_undo = new Set( |
| 157 | + list_quick_pick.selectedItems.map((i) => i.file_path) |
| 158 | + ) |
| 159 | + list_quick_pick.items = await create_quick_pick_items( |
| 160 | + resolved_paths, |
| 161 | + current_selected_undo |
| 162 | + ) |
| 163 | + list_quick_pick.selectedItems = list_quick_pick.items.filter( |
| 164 | + (i) => i.picked |
| 165 | + ) |
| 166 | + } |
| 167 | + } |
| 168 | + }) |
| 169 | + |
| 170 | + list_quick_pick.onDidAccept(() => { |
| 171 | + list_quick_pick.hide() |
| 172 | + resolve_once(list_quick_pick.selectedItems) |
| 173 | + }) |
| 174 | + |
| 175 | + list_quick_pick.onDidHide(() => { |
| 176 | + resolve_once(undefined) |
| 177 | + list_quick_pick.dispose() |
| 178 | + }) |
| 179 | + |
| 180 | + list_quick_pick.show() |
| 181 | + }) |
| 182 | + |
| 183 | + if (!list_selection) return undefined |
| 184 | + if (list_selection === 'back') return 'back' |
| 185 | + |
| 186 | + if (Array.isArray(list_selection)) { |
| 187 | + const new_checked = list_selection.map((i) => i.file_path) |
| 188 | + const select_paths_set = new Set(resolved_paths) |
| 189 | + const current_checked_files = workspace_provider.get_checked_files() |
| 190 | + const final_checked = current_checked_files |
| 191 | + .filter((p) => !select_paths_set.has(p)) |
| 192 | + .concat(new_checked) |
| 193 | + await workspace_provider.set_checked_files([...new Set(final_checked)]) |
| 194 | + return undefined |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments