88 map_display_pos_to_raw_pos ,
99 map_raw_pos_to_display_pos
1010} from '../utils/position-mapping'
11- import { search_paths } from '@shared /utils/search-paths '
11+ import { get_file_match_hint_data } from '.. /utils/get-file-match-hint-data '
1212
1313type HistoryEntry = {
1414 value : string
@@ -40,50 +40,6 @@ const getKeywordRanges = (
4040 return ranges
4141}
4242
43- const should_show_file_match_hint = (
44- value : string ,
45- caret_position : number ,
46- context_file_paths : string [ ] | undefined
47- ) : boolean => {
48- if (
49- ! value ||
50- caret_position !== value . length ||
51- value . endsWith ( ' ' ) ||
52- value . endsWith ( '\n' ) ||
53- ! context_file_paths
54- ) {
55- return false
56- }
57-
58- // Check if cursor is at the end of a shortened filename
59- const text_before_cursor = value . substring ( 0 , caret_position )
60- const filename_match = text_before_cursor . match (
61- / ( [ ^ \s , ; : ! ? ` ] * \. [ ^ \s , ; : ! ? ` ] + ) $ /
62- )
63-
64- if ( filename_match ) {
65- const filename = filename_match [ 1 ]
66- const is_shortened_filename = context_file_paths . some (
67- ( path ) => path . endsWith ( '/' + filename ) || path === filename
68- )
69- if ( is_shortened_filename ) {
70- return false
71- }
72- }
73-
74- const last_word = value . trim ( ) . split ( / \s + / ) . pop ( )
75-
76- if ( last_word && last_word . length >= 3 ) {
77- const matching_paths = search_paths ( {
78- paths : context_file_paths ,
79- search_value : last_word
80- } )
81- return matching_paths . length === 1
82- }
83-
84- return false
85- }
86-
8743const reconstruct_raw_value_from_node = ( node : Node ) : string => {
8844 if ( node . nodeType == Node . TEXT_NODE ) {
8945 return node . textContent || ''
@@ -239,7 +195,6 @@ export const use_handlers = (
239195 let leading_part = props . value . substring ( 0 , start_pos )
240196 let trailing_part = props . value . substring ( end_pos )
241197
242- // Handle spacing
243198 if ( leading_part . endsWith ( ' ' ) ) {
244199 leading_part = leading_part . slice ( 0 , - 1 )
245200 } else if ( trailing_part . startsWith ( ' ' ) ) {
@@ -424,38 +379,29 @@ export const use_handlers = (
424379 0 ,
425380 caret_position - 1
426381 )
427- const show_hint = should_show_file_match_hint (
382+ const hint_data = get_file_match_hint_data (
428383 display_value_before_backslash ,
429384 caret_position - 1 ,
430385 props . context_file_paths
431386 )
432387
433- if ( show_hint ) {
434- const last_word = display_value_before_backslash . trim ( ) . split ( / \s + / ) . pop ( )
435- if ( last_word ) {
436- const matching_paths = search_paths ( {
437- paths : props . context_file_paths ?? [ ] ,
438- search_value : last_word
439- } )
440- if ( matching_paths . length === 1 ) {
441- const matched_path = matching_paths [ 0 ]
442- const old_raw_value = props . value
443-
444- const last_word_in_raw_index = old_raw_value . lastIndexOf ( last_word )
445- if (
446- last_word_in_raw_index !== - 1 &&
447- old_raw_value . endsWith ( last_word )
448- ) {
449- const value_before = old_raw_value . substring (
450- 0 ,
451- last_word_in_raw_index
452- )
453- const final_new_value = `${ value_before } \`${ matched_path } \` `
454- update_value ( final_new_value , final_new_value . length )
455- set_history_index ( - 1 )
456- return
457- }
458- }
388+ if ( hint_data ) {
389+ const { word : last_word , path : matched_path } = hint_data
390+ const old_raw_value = props . value
391+
392+ const last_word_in_raw_index = old_raw_value . lastIndexOf ( last_word )
393+ if (
394+ last_word_in_raw_index !== - 1 &&
395+ old_raw_value . endsWith ( last_word )
396+ ) {
397+ const value_before = old_raw_value . substring (
398+ 0 ,
399+ last_word_in_raw_index
400+ )
401+ const final_new_value = `${ value_before } \`${ matched_path } \` `
402+ update_value ( final_new_value , final_new_value . length )
403+ set_history_index ( - 1 )
404+ return
459405 }
460406 }
461407 }
@@ -468,15 +414,14 @@ export const use_handlers = (
468414 let is_after_non_word_char = false
469415 if ( caret_position > 1 ) {
470416 const char_before_at = new_display_value . charAt ( caret_position - 2 )
471- // Check if the character before '@' is not a word character (a-z, A-Z, 0-9, _)
472417 is_after_non_word_char = ! / \w / . test ( char_before_at )
473418 }
474419
475420 if ( is_at_start || is_after_non_word_char ) {
476- props . on_at_sign_click ( )
421+ props . on_at_sign_click ( )
477422 }
478423 } else if ( char_before_caret == '#' ) {
479- props . on_hash_sign_click ( )
424+ props . on_hash_sign_click ( )
480425 }
481426 }
482427
@@ -641,12 +586,6 @@ export const use_handlers = (
641586 return false
642587 }
643588
644- const handle_tab_key = ( e : React . KeyboardEvent < HTMLDivElement > ) => {
645- if ( ! input_ref . current ) return
646- e . preventDefault ( )
647- props . on_at_sign_click ( )
648- }
649-
650589 const handle_backspace_key = ( e : React . KeyboardEvent < HTMLDivElement > ) => {
651590 const selection = window . getSelection ( )
652591 if (
@@ -662,10 +601,7 @@ export const use_handlers = (
662601 const { startContainer, startOffset } = range
663602 let node_before_cursor : Node | null = null
664603
665- if (
666- startContainer . nodeType == Node . TEXT_NODE &&
667- startOffset == 0
668- ) {
604+ if ( startContainer . nodeType == Node . TEXT_NODE && startOffset == 0 ) {
669605 node_before_cursor = startContainer . previousSibling
670606 } else if (
671607 startContainer . nodeType == Node . ELEMENT_NODE &&
@@ -769,15 +705,17 @@ export const use_handlers = (
769705 if ( ghost_node ) {
770706 const { startContainer, startOffset } = range
771707
772- // Check if we're at the position just before ghost text
773708 let is_before_ghost = false
774709
775710 if ( startContainer === ghost_node . previousSibling ) {
776711 if ( startContainer . nodeType === Node . TEXT_NODE ) {
777- is_before_ghost = startOffset === startContainer . textContent ?. length
712+ is_before_ghost =
713+ startOffset === startContainer . textContent ?. length
778714 }
779715 } else if ( startContainer === ghost_node . parentNode ) {
780- const ghost_index = Array . from ( startContainer . childNodes ) . indexOf ( ghost_node as ChildNode )
716+ const ghost_index = Array . from ( startContainer . childNodes ) . indexOf (
717+ ghost_node as ChildNode
718+ )
781719 is_before_ghost = startOffset === ghost_index
782720 }
783721
@@ -799,12 +737,16 @@ export const use_handlers = (
799737 const { value, context_file_paths = [ ] } = props
800738 let i = raw_pos - 1
801739
802- // First, skip any whitespace characters before the cursor.
803740 while ( i >= 0 && / \s / . test ( value [ i ] ) ) {
804741 i --
805742 }
806743 if ( i < 0 ) {
807- set_caret_position_after_change ( input_ref , 0 , value , context_file_paths )
744+ set_caret_position_after_change (
745+ input_ref ,
746+ 0 ,
747+ value ,
748+ context_file_paths
749+ )
808750 return
809751 }
810752
@@ -849,7 +791,6 @@ export const use_handlers = (
849791
850792 let i = raw_pos
851793
852- // Skip any whitespace characters after the cursor.
853794 while ( i < value . length && / \s / . test ( value [ i ] ) ) {
854795 i ++
855796 }
@@ -935,9 +876,7 @@ export const use_handlers = (
935876 if ( ghost_text ) {
936877 e . preventDefault ( )
937878 on_accept_ghost_text ( )
938- return
939879 }
940- handle_tab_key ( e )
941880 return
942881 }
943882
@@ -950,7 +889,6 @@ export const use_handlers = (
950889 const value = props . value
951890 let i = raw_pos - 1
952891
953- // First, skip any whitespace characters before the cursor.
954892 while ( i >= 0 && / \s / . test ( value [ i ] ) ) {
955893 i --
956894 }
0 commit comments