Skip to content

Commit 59b6d29

Browse files
committed
Add searchFilesForContextFromFile command to enable auto-submitting context searches based on selected text
- when text selection is active, should not automatically search, just fill the input field - 'keywords' wording doesn't make much sense - we allow running the command from the active editor, from a right click context menu. When invoked from there, the search should automatically proceed. In other cases (directory or action bar) only place the search value in the field. - rename searchFilesForContextFromEditor to searchFilesForContextFromFile
1 parent c10d5b7 commit 59b6d29

2 files changed

Lines changed: 47 additions & 37 deletions

File tree

‎apps/editor/package.json‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,11 @@
330330
"title": "%codeWebChat.searchFilesForContextFromDirectory.title%",
331331
"category": "Code Web Chat"
332332
},
333+
{
334+
"command": "codeWebChat.searchFilesForContextFromFile",
335+
"title": "%codeWebChat.searchFilesForContext.title%",
336+
"category": "Code Web Chat"
337+
},
333338
{
334339
"command": "codeWebChat.checkReferencingFilesForContext",
335340
"title": "%codeWebChat.checkReferencingFilesForContext.title%",
@@ -397,7 +402,7 @@
397402
"group": "1_action@5"
398403
},
399404
{
400-
"command": "codeWebChat.searchFilesForContext",
405+
"command": "codeWebChat.searchFilesForContextFromFile",
401406
"group": "1_action@7"
402407
}
403408
],
@@ -522,6 +527,10 @@
522527
"command": "codeWebChat.searchFilesForContextFromDirectory",
523528
"when": "false"
524529
},
530+
{
531+
"command": "codeWebChat.searchFilesForContextFromFile",
532+
"when": "false"
533+
},
525534
{
526535
"command": "codeWebChat.checkDefinitionFileForContext",
527536
"when": "false"

‎apps/editor/src/commands/search-files-for-context-command.ts‎

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const search_files_for_context_commands = (
1515
workspace_provider: WorkspaceProvider,
1616
extension_context: vscode.ExtensionContext
1717
) => {
18-
const search_handler = async (item?: any) => {
18+
const search_handler = async (item?: any, auto_submit: boolean = false) => {
1919
let folder_path = item?.resourceUri?.fsPath
2020

2121
if (folder_path) {
@@ -29,46 +29,45 @@ export const search_files_for_context_commands = (
2929
folder_path = undefined
3030
}
3131
}
32-
let initial_keywords =
32+
let initial_search_term =
3333
extension_context.workspaceState.get<string>(
3434
LAST_SEARCH_FILES_FOR_CONTEXT_QUERY_STATE_KEY
3535
) || ''
3636

37-
let skip_input = false
38-
const is_context_menu = item !== undefined
39-
40-
if (is_context_menu) {
41-
const active_editor = vscode.window.activeTextEditor
42-
if (active_editor) {
43-
const selection = active_editor.selection
44-
if (!selection.isEmpty) {
45-
const selected_text = active_editor.document.getText(selection).trim()
46-
if (selected_text.length > 0) {
47-
initial_keywords = selected_text
48-
skip_input = true
37+
let should_auto_submit = false
38+
39+
const active_editor = vscode.window.activeTextEditor
40+
if (active_editor) {
41+
const selection = active_editor.selection
42+
if (!selection.isEmpty) {
43+
const selected_text = active_editor.document.getText(selection).trim()
44+
if (selected_text.length > 0) {
45+
initial_search_term = selected_text
46+
if (auto_submit) {
47+
should_auto_submit = true
4948
}
5049
}
5150
}
5251
}
5352

53+
const close_button = {
54+
iconPath: new vscode.ThemeIcon('close'),
55+
tooltip: t('common.close')
56+
}
57+
5458
while (true) {
5559
try {
56-
const close_button = {
57-
iconPath: new vscode.ThemeIcon('close'),
58-
tooltip: t('common.close')
59-
}
60-
61-
let keywords_input: string | undefined
60+
let search_term_input: string | undefined
6261

63-
if (skip_input) {
64-
keywords_input = initial_keywords
65-
skip_input = false
62+
if (should_auto_submit && initial_search_term.length > 0) {
63+
search_term_input = initial_search_term
64+
should_auto_submit = false // Only auto-submit on the first iteration
6665
} else {
6766
const input_box = vscode.window.createInputBox()
6867
input_box.title = t('command.search.title')
6968
input_box.prompt = t('command.search.prompt')
7069
input_box.placeholder = t('command.search.placeholder')
71-
input_box.value = initial_keywords
70+
input_box.value = initial_search_term
7271

7372
input_box.buttons = [close_button]
7473

@@ -113,19 +112,18 @@ export const search_files_for_context_commands = (
113112
}
114113
)
115114

116-
keywords_input = result.value
115+
if (!result.value) return
116+
search_term_input = result.value
117117
}
118118

119-
if (!keywords_input) return
120-
121119
await extension_context.workspaceState.update(
122120
LAST_SEARCH_FILES_FOR_CONTEXT_QUERY_STATE_KEY,
123-
keywords_input
121+
search_term_input
124122
)
125123

126-
initial_keywords = keywords_input
124+
initial_search_term = search_term_input
127125

128-
const search_term = keywords_input.trim()
126+
const search_term = search_term_input.trim()
129127
if (search_term.length == 0) return
130128

131129
let all_files: string[] = []
@@ -140,7 +138,7 @@ export const search_files_for_context_commands = (
140138
}
141139
}
142140

143-
const matched_files = await search_files_by_keywords({
141+
const matched_files = await search_files_by_term({
144142
files: all_files,
145143
search_term
146144
})
@@ -398,18 +396,21 @@ export const search_files_for_context_commands = (
398396
}
399397

400398
return [
401-
vscode.commands.registerCommand(
402-
'codeWebChat.searchFilesForContext',
403-
(item?: any) => search_handler(item)
399+
vscode.commands.registerCommand('codeWebChat.searchFilesForContext', () =>
400+
search_handler()
404401
),
405402
vscode.commands.registerCommand(
406403
'codeWebChat.searchFilesForContextFromDirectory',
407404
(item: any) => search_handler(item)
405+
),
406+
vscode.commands.registerCommand(
407+
'codeWebChat.searchFilesForContextFromFile',
408+
(item: any) => search_handler(item, true)
408409
)
409410
]
410411
}
411412

412-
const search_files_by_keywords = async (params: {
413+
const search_files_by_term = async (params: {
413414
files: string[]
414415
search_term: string
415416
}): Promise<string[]> => {
@@ -438,7 +439,7 @@ const search_files_by_keywords = async (params: {
438439
}
439440
} catch (error) {
440441
Logger.error({
441-
function_name: 'search_files_by_keywords',
442+
function_name: 'search_files_by_term',
442443
message: `Error reading file during search: ${file_path}`,
443444
data: error
444445
})

0 commit comments

Comments
 (0)