|
| 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/workspace-provider' |
| 5 | +import { Logger } from '@shared/utils/logger' |
| 6 | +import { LAST_CONTEXT_MERGE_REPLACE_OPTION_STATE_KEY } from '../constants/state-keys' |
| 7 | +import { dictionary } from '@shared/constants/dictionary' |
| 8 | + |
| 9 | +export const keyword_search_in_folder_command = ( |
| 10 | + workspace_provider: WorkspaceProvider, |
| 11 | + extension_context: vscode.ExtensionContext |
| 12 | +) => { |
| 13 | + return vscode.commands.registerCommand( |
| 14 | + 'codeWebChat.keywordSearchInFolder', |
| 15 | + async (item: any) => { |
| 16 | + if (!item || !item.resourceUri) { |
| 17 | + return |
| 18 | + } |
| 19 | + |
| 20 | + const folder_path = item.resourceUri.fsPath |
| 21 | + |
| 22 | + try { |
| 23 | + const input_box = vscode.window.createInputBox() |
| 24 | + input_box.title = `Keyword Search in ${path.basename(folder_path)}` |
| 25 | + input_box.prompt = 'Enter keywords separated by comma' |
| 26 | + input_box.placeholder = 'e.g. user, login, auth' |
| 27 | + input_box.ignoreFocusOut = true |
| 28 | + |
| 29 | + const keywords_input = await new Promise<string | undefined>( |
| 30 | + (resolve) => { |
| 31 | + let is_resolved = false |
| 32 | + const disposables: vscode.Disposable[] = [] |
| 33 | + |
| 34 | + disposables.push( |
| 35 | + input_box.onDidAccept(() => { |
| 36 | + const value = input_box.value.trim() |
| 37 | + if (value.length === 0) { |
| 38 | + input_box.validationMessage = |
| 39 | + 'Please enter at least one keyword' |
| 40 | + return |
| 41 | + } |
| 42 | + is_resolved = true |
| 43 | + resolve(value) |
| 44 | + input_box.hide() |
| 45 | + }), |
| 46 | + input_box.onDidChangeValue(() => { |
| 47 | + if (input_box.value.trim().length > 0) { |
| 48 | + input_box.validationMessage = undefined |
| 49 | + } |
| 50 | + }), |
| 51 | + input_box.onDidHide(() => { |
| 52 | + if (!is_resolved) { |
| 53 | + resolve(undefined) |
| 54 | + } |
| 55 | + disposables.forEach((d) => d.dispose()) |
| 56 | + input_box.dispose() |
| 57 | + }) |
| 58 | + ) |
| 59 | + input_box.show() |
| 60 | + } |
| 61 | + ) |
| 62 | + |
| 63 | + if (!keywords_input) return |
| 64 | + |
| 65 | + const keywords = keywords_input |
| 66 | + .split(',') |
| 67 | + .map((k) => k.trim().toLowerCase()) |
| 68 | + .filter((k) => k.length > 0) |
| 69 | + |
| 70 | + if (keywords.length == 0) return |
| 71 | + |
| 72 | + const all_files = await workspace_provider.find_all_files(folder_path) |
| 73 | + const matched_files: string[] = [] |
| 74 | + |
| 75 | + await vscode.window.withProgress( |
| 76 | + { |
| 77 | + location: vscode.ProgressLocation.Notification, |
| 78 | + title: `Searching for keywords: ${keywords.join(', ')}...`, |
| 79 | + cancellable: true |
| 80 | + }, |
| 81 | + async (progress, token) => { |
| 82 | + const total = all_files.length |
| 83 | + let processed = 0 |
| 84 | + const increment = (1 / total) * 100 |
| 85 | + |
| 86 | + for (const file_path of all_files) { |
| 87 | + if (token.isCancellationRequested) break |
| 88 | + |
| 89 | + try { |
| 90 | + // Skip large files (> 1MB) |
| 91 | + const stats = await fs.promises.stat(file_path) |
| 92 | + if (stats.size > 1024 * 1024) { |
| 93 | + processed++ |
| 94 | + progress.report({ |
| 95 | + increment, |
| 96 | + message: `${processed}/${total}` |
| 97 | + }) |
| 98 | + continue |
| 99 | + } |
| 100 | + |
| 101 | + const content = await fs.promises.readFile(file_path, 'utf-8') |
| 102 | + const content_lower = content.toLowerCase() |
| 103 | + |
| 104 | + if (keywords.some((k) => content_lower.includes(k))) { |
| 105 | + matched_files.push(file_path) |
| 106 | + } |
| 107 | + } catch (error) { |
| 108 | + // Ignore read errors |
| 109 | + } |
| 110 | + |
| 111 | + processed++ |
| 112 | + progress.report({ |
| 113 | + increment, |
| 114 | + message: `${processed}/${total}` |
| 115 | + }) |
| 116 | + } |
| 117 | + } |
| 118 | + ) |
| 119 | + |
| 120 | + if (matched_files.length == 0) { |
| 121 | + vscode.window.showInformationMessage( |
| 122 | + 'No files found containing these keywords.' |
| 123 | + ) |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + const open_file_button = { |
| 128 | + iconPath: new vscode.ThemeIcon('go-to-file'), |
| 129 | + tooltip: 'Open file' |
| 130 | + } |
| 131 | + |
| 132 | + const quick_pick_items = matched_files.map((file_path) => { |
| 133 | + const workspace_root = |
| 134 | + workspace_provider.get_workspace_root_for_file(file_path) |
| 135 | + const relative_path = workspace_root |
| 136 | + ? path.relative(workspace_root, file_path) |
| 137 | + : file_path |
| 138 | + |
| 139 | + const dir_name = path.dirname(relative_path) |
| 140 | + return { |
| 141 | + label: path.basename(file_path), |
| 142 | + description: dir_name == '.' ? '' : dir_name, |
| 143 | + file_path: file_path, |
| 144 | + buttons: [open_file_button] |
| 145 | + } |
| 146 | + }) |
| 147 | + |
| 148 | + const quick_pick = vscode.window.createQuickPick< |
| 149 | + vscode.QuickPickItem & { file_path: string } |
| 150 | + >() |
| 151 | + quick_pick.items = quick_pick_items |
| 152 | + quick_pick.selectedItems = quick_pick_items |
| 153 | + quick_pick.canSelectMany = true |
| 154 | + quick_pick.placeholder = 'Select files to include' |
| 155 | + quick_pick.title = `Found ${matched_files.length} file${ |
| 156 | + matched_files.length == 1 ? '' : 's' |
| 157 | + }` |
| 158 | + quick_pick.ignoreFocusOut = true |
| 159 | + |
| 160 | + const selected_items = await new Promise< |
| 161 | + readonly (vscode.QuickPickItem & { file_path: string })[] | undefined |
| 162 | + >((resolve) => { |
| 163 | + let is_accepted = false |
| 164 | + |
| 165 | + quick_pick.onDidAccept(() => { |
| 166 | + is_accepted = true |
| 167 | + resolve(quick_pick.selectedItems) |
| 168 | + quick_pick.hide() |
| 169 | + }) |
| 170 | + |
| 171 | + quick_pick.onDidHide(() => { |
| 172 | + if (!is_accepted) { |
| 173 | + resolve(undefined) |
| 174 | + } |
| 175 | + quick_pick.dispose() |
| 176 | + }) |
| 177 | + |
| 178 | + quick_pick.onDidTriggerItemButton(async (e) => { |
| 179 | + if (e.button === open_file_button) { |
| 180 | + try { |
| 181 | + const doc = await vscode.workspace.openTextDocument( |
| 182 | + e.item.file_path |
| 183 | + ) |
| 184 | + await vscode.window.showTextDocument(doc, { |
| 185 | + preview: true |
| 186 | + }) |
| 187 | + } catch (error) { |
| 188 | + vscode.window.showErrorMessage(`Error opening file: ${error}`) |
| 189 | + } |
| 190 | + } |
| 191 | + }) |
| 192 | + |
| 193 | + quick_pick.show() |
| 194 | + }) |
| 195 | + |
| 196 | + if (!selected_items || selected_items.length == 0) { |
| 197 | + return |
| 198 | + } |
| 199 | + |
| 200 | + const selected_paths = selected_items.map((item) => item.file_path) |
| 201 | + const currently_checked = workspace_provider.get_checked_files() |
| 202 | + let paths_to_apply = selected_paths |
| 203 | + |
| 204 | + if (currently_checked.length > 0) { |
| 205 | + const selected_paths_set = new Set(selected_paths) |
| 206 | + const all_current_files_in_new_context = currently_checked.every( |
| 207 | + (file) => selected_paths_set.has(file) |
| 208 | + ) |
| 209 | + |
| 210 | + if (!all_current_files_in_new_context) { |
| 211 | + const quick_pick_options = [ |
| 212 | + { |
| 213 | + label: 'Replace', |
| 214 | + description: 'Replace the current context with selected files' |
| 215 | + }, |
| 216 | + { |
| 217 | + label: 'Merge', |
| 218 | + description: 'Merge selected files with the current context' |
| 219 | + } |
| 220 | + ] |
| 221 | + |
| 222 | + const last_choice_label = |
| 223 | + extension_context.workspaceState.get<string>( |
| 224 | + LAST_CONTEXT_MERGE_REPLACE_OPTION_STATE_KEY |
| 225 | + ) |
| 226 | + |
| 227 | + const quick_pick_apply = vscode.window.createQuickPick() |
| 228 | + quick_pick_apply.items = quick_pick_options |
| 229 | + quick_pick_apply.placeholder = `How would you like to apply the ${selected_paths.length} selected files?` |
| 230 | + |
| 231 | + if (last_choice_label) { |
| 232 | + const active_item = quick_pick_options.find( |
| 233 | + (opt) => opt.label === last_choice_label |
| 234 | + ) |
| 235 | + if (active_item) { |
| 236 | + quick_pick_apply.activeItems = [active_item] |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + const choice = await new Promise<vscode.QuickPickItem | undefined>( |
| 241 | + (resolve) => { |
| 242 | + let is_accepted = false |
| 243 | + quick_pick_apply.onDidAccept(() => { |
| 244 | + is_accepted = true |
| 245 | + resolve(quick_pick_apply.selectedItems[0]) |
| 246 | + quick_pick_apply.hide() |
| 247 | + }) |
| 248 | + quick_pick_apply.onDidHide(() => { |
| 249 | + if (!is_accepted) resolve(undefined) |
| 250 | + quick_pick_apply.dispose() |
| 251 | + }) |
| 252 | + quick_pick_apply.show() |
| 253 | + } |
| 254 | + ) |
| 255 | + |
| 256 | + if (!choice) return |
| 257 | + |
| 258 | + await extension_context.workspaceState.update( |
| 259 | + LAST_CONTEXT_MERGE_REPLACE_OPTION_STATE_KEY, |
| 260 | + choice.label |
| 261 | + ) |
| 262 | + |
| 263 | + if (choice.label == 'Merge') { |
| 264 | + paths_to_apply = [ |
| 265 | + ...new Set([...currently_checked, ...selected_paths]) |
| 266 | + ] |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + Logger.info({ |
| 272 | + message: `Selected ${selected_paths.length} files from keyword search in folder.`, |
| 273 | + data: { paths: selected_paths, folder: folder_path } |
| 274 | + }) |
| 275 | + |
| 276 | + await workspace_provider.set_checked_files(paths_to_apply) |
| 277 | + vscode.window.showInformationMessage( |
| 278 | + dictionary.information_message.SELECTED_FILES(paths_to_apply.length) |
| 279 | + ) |
| 280 | + } catch (error) { |
| 281 | + vscode.window.showErrorMessage( |
| 282 | + `Search failed: ${ |
| 283 | + error instanceof Error ? error.message : String(error) |
| 284 | + }` |
| 285 | + ) |
| 286 | + Logger.error({ |
| 287 | + function_name: 'keyword_search_in_folder_command', |
| 288 | + message: 'Error searching keywords in folder', |
| 289 | + data: error |
| 290 | + }) |
| 291 | + } |
| 292 | + } |
| 293 | + ) |
| 294 | +} |
0 commit comments