Skip to content

Commit 033c8be

Browse files
committed
Add keyword search feature to context application
1 parent e1e0d8c commit 033c8be

4 files changed

Lines changed: 276 additions & 5 deletions

File tree

‎packages/vscode/src/commands/apply-context-command/apply-context-command.ts‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
handle_unstaged_files_source,
1313
handle_json_file_source,
1414
handle_workspace_state_source,
15-
handle_commit_files_source
15+
handle_commit_files_source,
16+
handle_keyword_search_source
1617
} from './sources'
1718
import {
1819
load_all_contexts,
@@ -257,7 +258,11 @@ export const apply_context_command = (
257258
show_other_menu = false
258259

259260
const other_quick_pick_options: (vscode.QuickPickItem & {
260-
value?: 'clipboard' | 'unstaged' | 'commit_files'
261+
value?:
262+
| 'clipboard'
263+
| 'unstaged'
264+
| 'commit_files'
265+
| 'keyword_search'
261266
})[] = [
262267
{
263268
label: 'Scan the clipboard for valid paths',
@@ -270,12 +275,20 @@ export const apply_context_command = (
270275
{
271276
label: 'Files of a commit...',
272277
value: 'commit_files'
278+
},
279+
{
280+
label: 'Keyword search...',
281+
value: 'keyword_search'
273282
}
274283
]
275284

276285
const other_quick_pick = vscode.window.createQuickPick<
277286
vscode.QuickPickItem & {
278-
value?: 'clipboard' | 'unstaged' | 'commit_files'
287+
value?:
288+
| 'clipboard'
289+
| 'unstaged'
290+
| 'commit_files'
291+
| 'keyword_search'
279292
}
280293
>()
281294
other_quick_pick.title = 'Context Sources'
@@ -295,7 +308,11 @@ export const apply_context_command = (
295308
const other_selection = await new Promise<
296309
| 'back'
297310
| (vscode.QuickPickItem & {
298-
value?: 'clipboard' | 'unstaged' | 'commit_files'
311+
value?:
312+
| 'clipboard'
313+
| 'unstaged'
314+
| 'commit_files'
315+
| 'keyword_search'
299316
})
300317
| undefined
301318
>((resolve) => {
@@ -355,6 +372,15 @@ export const apply_context_command = (
355372
}
356373
return
357374
}
375+
if (other_selection.value == 'keyword_search') {
376+
const result =
377+
await handle_keyword_search_source(workspace_provider)
378+
if (result === 'back') {
379+
show_other_menu = true
380+
continue
381+
}
382+
return
383+
}
358384
}
359385
} else if (main_selection.value.startsWith('quick_save_')) {
360386
const slot = parseInt(main_selection.value.split('_')[2])

‎packages/vscode/src/commands/apply-context-command/sources/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './json-file-source'
22
export * from './unstaged-files-source'
33
export * from './workspace-state-source'
44
export * from './commit-files-source'
5+
export * from './keyword-search-source'
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
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 { dictionary } from '@shared/constants/dictionary'
6+
import { Logger } from '@shared/utils/logger'
7+
8+
export const handle_keyword_search_source = async (
9+
workspace_provider: WorkspaceProvider
10+
): Promise<'back' | void> => {
11+
try {
12+
const input_box = vscode.window.createInputBox()
13+
input_box.title = 'Keyword Search'
14+
input_box.prompt = 'Enter keywords separated by comma'
15+
input_box.placeholder = 'e.g. user, login, auth'
16+
input_box.ignoreFocusOut = true
17+
input_box.buttons = [vscode.QuickInputButtons.Back]
18+
19+
const keywords_input = await new Promise<string | 'back' | undefined>(
20+
(resolve) => {
21+
let is_resolved = false
22+
const disposables: vscode.Disposable[] = []
23+
24+
disposables.push(
25+
input_box.onDidTriggerButton((button) => {
26+
if (button === vscode.QuickInputButtons.Back) {
27+
is_resolved = true
28+
resolve('back')
29+
input_box.hide()
30+
}
31+
}),
32+
input_box.onDidAccept(() => {
33+
const value = input_box.value.trim()
34+
if (value.length === 0) {
35+
input_box.validationMessage = 'Please enter at least one keyword'
36+
return
37+
}
38+
is_resolved = true
39+
resolve(value)
40+
input_box.hide()
41+
}),
42+
input_box.onDidChangeValue(() => {
43+
if (input_box.value.trim().length > 0) {
44+
input_box.validationMessage = undefined
45+
}
46+
}),
47+
input_box.onDidHide(() => {
48+
if (!is_resolved) {
49+
resolve(undefined)
50+
}
51+
disposables.forEach((d) => d.dispose())
52+
input_box.dispose()
53+
})
54+
)
55+
input_box.show()
56+
}
57+
)
58+
59+
if (keywords_input === 'back') {
60+
return 'back'
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 roots = workspace_provider.get_workspace_roots()
73+
const all_files: string[] = []
74+
75+
for (const root of roots) {
76+
const files = await workspace_provider.find_all_files(root)
77+
all_files.push(...files)
78+
}
79+
80+
const matched_files: string[] = []
81+
82+
await vscode.window.withProgress(
83+
{
84+
location: vscode.ProgressLocation.Notification,
85+
title: `Searching for keywords: ${keywords.join(', ')}...`,
86+
cancellable: true
87+
},
88+
async (progress, token) => {
89+
const total = all_files.length
90+
let processed = 0
91+
const increment = (1 / total) * 100
92+
93+
for (const file_path of all_files) {
94+
if (token.isCancellationRequested) break
95+
96+
try {
97+
// Skip large files (> 1MB) to avoid performance issues
98+
const stats = await fs.promises.stat(file_path)
99+
if (stats.size > 1024 * 1024) {
100+
processed++
101+
progress.report({
102+
increment,
103+
message: `${processed}/${total}`
104+
})
105+
continue
106+
}
107+
108+
const content = await fs.promises.readFile(file_path, 'utf-8')
109+
const content_lower = content.toLowerCase()
110+
111+
if (keywords.some((k) => content_lower.includes(k))) {
112+
matched_files.push(file_path)
113+
}
114+
} catch (error) {
115+
// Ignore read errors (binary files, permissions, etc.)
116+
}
117+
118+
processed++
119+
progress.report({
120+
increment,
121+
message: `${processed}/${total}`
122+
})
123+
}
124+
}
125+
)
126+
127+
if (matched_files.length == 0) {
128+
vscode.window.showInformationMessage(
129+
'No files found containing these keywords.'
130+
)
131+
return
132+
}
133+
134+
const open_file_button = {
135+
iconPath: new vscode.ThemeIcon('go-to-file'),
136+
tooltip: 'Open file'
137+
}
138+
139+
const quick_pick_items = matched_files.map((file_path) => {
140+
const workspace_root =
141+
workspace_provider.get_workspace_root_for_file(file_path)
142+
const relative_path = workspace_root
143+
? path.relative(workspace_root, file_path)
144+
: file_path
145+
146+
const dir_name = path.dirname(relative_path)
147+
return {
148+
label: path.basename(file_path),
149+
description: dir_name == '.' ? '' : dir_name,
150+
file_path: file_path,
151+
buttons: [open_file_button]
152+
}
153+
})
154+
155+
const quick_pick = vscode.window.createQuickPick<
156+
vscode.QuickPickItem & { file_path: string }
157+
>()
158+
quick_pick.items = quick_pick_items
159+
quick_pick.selectedItems = quick_pick_items
160+
quick_pick.canSelectMany = true
161+
quick_pick.placeholder = 'Select files to include'
162+
quick_pick.title = `Found ${matched_files.length} file${
163+
matched_files.length == 1 ? '' : 's'
164+
}`
165+
quick_pick.ignoreFocusOut = true
166+
quick_pick.buttons = [vscode.QuickInputButtons.Back]
167+
168+
const selected_items = await new Promise<
169+
| readonly (vscode.QuickPickItem & { file_path: string })[]
170+
| 'back'
171+
| undefined
172+
>((resolve) => {
173+
let is_accepted = false
174+
let did_trigger_back = false
175+
176+
quick_pick.onDidAccept(() => {
177+
is_accepted = true
178+
resolve(quick_pick.selectedItems)
179+
quick_pick.hide()
180+
})
181+
182+
quick_pick.onDidTriggerButton((button) => {
183+
if (button === vscode.QuickInputButtons.Back) {
184+
did_trigger_back = true
185+
resolve('back')
186+
quick_pick.hide()
187+
}
188+
})
189+
190+
quick_pick.onDidHide(() => {
191+
if (!is_accepted && !did_trigger_back) {
192+
resolve(undefined)
193+
}
194+
quick_pick.dispose()
195+
})
196+
197+
quick_pick.onDidTriggerItemButton(async (e) => {
198+
if (e.button === open_file_button) {
199+
try {
200+
const doc = await vscode.workspace.openTextDocument(
201+
e.item.file_path
202+
)
203+
await vscode.window.showTextDocument(doc, {
204+
preview: true
205+
})
206+
} catch (error) {
207+
vscode.window.showErrorMessage(`Error opening file: ${error}`)
208+
}
209+
}
210+
})
211+
212+
quick_pick.show()
213+
})
214+
215+
if (selected_items === 'back') {
216+
return 'back'
217+
}
218+
219+
if (!selected_items || selected_items.length == 0) {
220+
return
221+
}
222+
223+
const selected_paths = selected_items.map((item) => item.file_path)
224+
225+
Logger.info({
226+
message: `Selected ${selected_paths.length} files from keyword search.`,
227+
data: { paths: selected_paths }
228+
})
229+
230+
await workspace_provider.set_checked_files(selected_paths)
231+
vscode.window.showInformationMessage(
232+
dictionary.information_message.SELECTED_FILES(selected_paths.length)
233+
)
234+
} catch (error) {
235+
vscode.window.showErrorMessage(
236+
`Search failed: ${error instanceof Error ? error.message : String(error)}`
237+
)
238+
Logger.error({
239+
function_name: 'handle_keyword_search_source',
240+
message: 'Error searching keywords',
241+
data: error
242+
})
243+
}
244+
}

‎packages/vscode/src/commands/apply-context-command/sources/unstaged-files-source.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const handle_unstaged_files_source = async (
6767
const token_count =
6868
await workspace_provider.calculate_file_tokens(file_path)
6969

70-
const formatted_token_count = display_token_count(token_count)
70+
const formatted_token_count = display_token_count(token_count.total)
7171

7272
return {
7373
label: path.basename(file_path),

0 commit comments

Comments
 (0)