Skip to content

Commit db21380

Browse files
committed
Enhance the search files for context command with match highlighting and improved tracking of newly added files
1 parent ce86141 commit db21380

2 files changed

Lines changed: 52 additions & 27 deletions

File tree

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

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ export const search_files_for_context_commands = (
129129
tooltip: t('common.go-to-file')
130130
}
131131

132+
const currently_checked = workspace_provider.get_checked_files()
133+
132134
const quick_pick_items = matched_files.map((file_path) => {
133135
const workspace_root =
134136
workspace_provider.get_workspace_root_for_file(file_path)
@@ -149,7 +151,9 @@ export const search_files_for_context_commands = (
149151
vscode.QuickPickItem & { file_path: string }
150152
>()
151153
quick_pick.items = quick_pick_items
152-
quick_pick.selectedItems = quick_pick_items
154+
quick_pick.selectedItems = quick_pick_items.filter((item) =>
155+
currently_checked.includes(item.file_path)
156+
)
153157
quick_pick.canSelectMany = true
154158
quick_pick.matchOnDescription = true
155159
quick_pick.placeholder = t('command.search.select-files')
@@ -193,8 +197,21 @@ export const search_files_for_context_commands = (
193197
const doc = await vscode.workspace.openTextDocument(
194198
e.item.file_path
195199
)
200+
201+
const text = doc.getText()
202+
const regex = create_search_regex(search_term)
203+
const match = regex.exec(text)
204+
205+
let selection: vscode.Range | undefined
206+
if (match) {
207+
const start_pos = doc.positionAt(match.index)
208+
const end_pos = doc.positionAt(match.index + match[0].length)
209+
selection = new vscode.Range(start_pos, end_pos)
210+
}
211+
196212
await vscode.window.showTextDocument(doc, {
197-
preview: true
213+
preview: true,
214+
selection
198215
})
199216
} catch (error) {
200217
vscode.window.showErrorMessage(
@@ -220,7 +237,6 @@ export const search_files_for_context_commands = (
220237
const selected_paths = (
221238
selected_items as (vscode.QuickPickItem & { file_path: string })[]
222239
).map((item) => item.file_path)
223-
const currently_checked = workspace_provider.get_checked_files()
224240

225241
let files_outside_search_folder: string[] = []
226242
let files_inside_search_folder: string[] = []
@@ -318,9 +334,14 @@ export const search_files_for_context_commands = (
318334
})
319335

320336
await workspace_provider.set_checked_files(paths_to_apply)
337+
338+
const newly_selected_count = selected_paths.filter(
339+
(p) => !currently_checked.includes(p)
340+
).length
341+
321342
vscode.window.showInformationMessage(
322-
dictionary.information_message.SELECTED_FILES(
323-
paths_to_apply.length - files_outside_search_folder.length
343+
dictionary.information_message.ADDED_FILES_TO_CONTEXT(
344+
newly_selected_count
324345
)
325346
)
326347
break
@@ -357,26 +378,7 @@ const search_files_by_keywords = async (params: {
357378
}): Promise<string[]> => {
358379
const matched_files: string[] = []
359380

360-
let actual_term = params.search_term
361-
let match_whole_word = false
362-
363-
if (
364-
actual_term.length >= 2 &&
365-
actual_term.startsWith('"') &&
366-
actual_term.endsWith('"')
367-
) {
368-
match_whole_word = true
369-
actual_term = actual_term.slice(1, -1)
370-
}
371-
372-
const escaped_term = actual_term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
373-
let pattern = escaped_term.replace(/\s+/g, '\\s+')
374-
375-
if (match_whole_word) {
376-
pattern = `\\b${pattern}\\b`
377-
}
378-
379-
const regex = new RegExp(pattern, 'mi')
381+
const regex = create_search_regex(params.search_term)
380382

381383
for (const file_path of params.files) {
382384
try {
@@ -408,3 +410,26 @@ const search_files_by_keywords = async (params: {
408410

409411
return matched_files
410412
}
413+
414+
const create_search_regex = (search_term: string): RegExp => {
415+
let actual_term = search_term
416+
let match_whole_word = false
417+
418+
if (
419+
actual_term.length >= 2 &&
420+
actual_term.startsWith('"') &&
421+
actual_term.endsWith('"')
422+
) {
423+
match_whole_word = true
424+
actual_term = actual_term.slice(1, -1)
425+
}
426+
427+
const escaped_term = actual_term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
428+
let pattern = escaped_term.replace(/\s+/g, '\\s+')
429+
430+
if (match_whole_word) {
431+
pattern = `\\b${pattern}\\b`
432+
}
433+
434+
return new RegExp(pattern, 'mi')
435+
}

‎packages/shared/src/constants/dictionary.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ export const dictionary = {
8989
NO_UNSTAGED_FILES_FOUND: 'No unstaged files found.',
9090
NO_ACTIONABLE_UNSTAGED_FILES_FOUND:
9191
'No actionable unstaged files found (e.g. only deletions).',
92-
SELECTED_FILES: (count: number) =>
93-
`Selected ${count} file${count == 1 ? '' : 's'}.`,
92+
ADDED_FILES_TO_CONTEXT: (count: number) =>
93+
`Added ${count} file${count == 1 ? '' : 's'} to context.`,
9494
DELETED_CONTEXT_FROM_WORKSPACE_STATE: 'Deleted context.',
9595
CONTEXT_SAVED_SUCCESSFULLY: 'Saved successfully.',
9696
CONTEXT_APPLIED_SUCCESSFULLY: 'Context applied successfully.',

0 commit comments

Comments
 (0)