@@ -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+ }
0 commit comments