@@ -58,7 +58,7 @@ export type PromptFieldProps = {
5858 on_caret_position_change : ( caret_position : number ) => void
5959 is_web_mode : boolean
6060 on_search_click : ( ) => void
61- on_at_sign_click : ( search_value ?: string ) => void
61+ on_at_sign_click : ( ) => void
6262 on_hash_sign_click : ( ) => void
6363 on_curly_braces_click : ( ) => void
6464 caret_position_to_set ?: number
@@ -71,13 +71,15 @@ export type PromptFieldProps = {
7171 on_edit_format_change ?: ( format : EditFormat ) => void
7272 edit_format_instructions ?: Record < EditFormat , string >
7373 context_file_paths ?: string [ ]
74+ currently_open_file_text ?: string
7475 on_go_to_file ?: ( file_path : string ) => void
7576}
7677
7778export const PromptField : React . FC < PromptFieldProps > = ( props ) => {
7879 const input_ref = useRef < HTMLDivElement > ( null )
7980 const container_ref = useRef < HTMLDivElement > ( null )
8081 const [ caret_position , set_caret_position ] = useState ( 0 )
82+ const [ ghost_text , set_ghost_text ] = useState ( '' )
8183 const [ show_at_sign_tooltip , set_show_at_sign_tooltip ] = useState ( false )
8284 const [ show_submit_tooltip , set_show_submit_tooltip ] = useState ( false )
8385 const [ is_text_selecting , set_is_text_selecting ] = useState ( false )
@@ -97,7 +99,7 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
9799 is_history_enabled,
98100 handle_copy,
99101 handle_input_click
100- } = use_handlers ( props , input_ref )
102+ } = use_handlers ( props , input_ref , ghost_text , handle_accept_ghost_text )
101103 const { handle_drag_start, handle_drag_over, handle_drop, handle_drag_end } =
102104 use_drag_drop ( {
103105 input_ref,
@@ -108,6 +110,31 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
108110
109111 const mouse_down_pos_ref = useRef < { x : number ; y : number } | null > ( null )
110112
113+ function handle_accept_ghost_text ( ) {
114+ if ( ! ghost_text || ! input_ref . current ) return
115+
116+ const ghost_node = input_ref . current . querySelector (
117+ 'span[data-type="ghost-text"]'
118+ )
119+ if ( ghost_node ) {
120+ const text_node = document . createTextNode ( ghost_node . textContent || '' )
121+ ghost_node . parentNode ?. replaceChild ( text_node , ghost_node )
122+
123+ const selection = window . getSelection ( )
124+ if ( selection ) {
125+ const range = document . createRange ( )
126+ range . setStartAfter ( text_node )
127+ range . collapse ( true )
128+ selection . removeAllRanges ( )
129+ selection . addRange ( range )
130+ }
131+
132+ input_ref . current . dispatchEvent (
133+ new Event ( 'input' , { bubbles : true , cancelable : true } )
134+ )
135+ }
136+ }
137+
111138 const handle_mouse_down = useCallback (
112139 ( e : React . MouseEvent < HTMLDivElement > ) => {
113140 mouse_down_pos_ref . current = { x : e . clientX , y : e . clientY }
@@ -142,7 +169,7 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
142169 return get_display_text ( props . value , props . context_file_paths ?? [ ] )
143170 } , [ props . value , props . context_file_paths ] )
144171
145- const show_tab_hint = useMemo ( ( ) => {
172+ const show_file_match_hint = useMemo ( ( ) => {
146173 const value = display_text
147174 if (
148175 ! value ||
@@ -197,6 +224,66 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
197224 props . context_file_paths
198225 ] )
199226
227+ const identifiers = useMemo ( ( ) => {
228+ if ( ! props . currently_open_file_text ) return new Set < string > ( )
229+ const matches =
230+ props . currently_open_file_text . match ( / [ a - z A - Z _ ] [ a - z A - Z 0 - 9 _ ] * / g)
231+ if ( ! matches ) return new Set < string > ( )
232+ return new Set ( matches . filter ( ( id ) => id . length >= 3 ) )
233+ } , [ props . currently_open_file_text ] )
234+
235+ useEffect ( ( ) => {
236+ if ( ! input_ref . current ) {
237+ set_ghost_text ( '' )
238+ return
239+ }
240+
241+ const selection = window . getSelection ( )
242+ if ( ! selection || selection . rangeCount === 0 ) {
243+ set_ghost_text ( '' )
244+ return
245+ }
246+
247+ const range = selection . getRangeAt ( 0 )
248+ if ( ! range . collapsed ) {
249+ set_ghost_text ( '' )
250+ return
251+ }
252+
253+ const pre_caret_range = range . cloneRange ( )
254+ pre_caret_range . selectNodeContents ( input_ref . current )
255+ pre_caret_range . setEnd ( range . startContainer , range . startOffset )
256+ const text_before_cursor = pre_caret_range . toString ( )
257+
258+ const last_word_match = text_before_cursor . match ( / [ \S ] + $ / )
259+ if ( ! last_word_match ) {
260+ set_ghost_text ( '' )
261+ return
262+ }
263+
264+ const last_word_with_prefix = last_word_match [ 0 ]
265+ const word_start_match = last_word_with_prefix . match ( / [ a - z A - Z _ ] / )
266+ if ( ! word_start_match ) {
267+ set_ghost_text ( '' )
268+ return
269+ }
270+ const last_word = last_word_with_prefix . substring ( word_start_match . index ?? 0 )
271+
272+ if ( last_word . length < 2 ) {
273+ set_ghost_text ( '' )
274+ return
275+ }
276+
277+ for ( const id of identifiers ) {
278+ if ( id . startsWith ( last_word ) && id !== last_word ) {
279+ set_ghost_text ( id . substring ( last_word . length ) )
280+ return
281+ }
282+ }
283+
284+ set_ghost_text ( '' )
285+ } , [ props . value , caret_position , identifiers ] )
286+
200287 useEffect ( ( ) => {
201288 const input_element = input_ref . current
202289 if ( input_element && input_element . innerHTML !== highlighted_html ) {
@@ -273,6 +360,37 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
273360 document . removeEventListener ( 'selectionchange' , on_selection_change )
274361 } , [ props . on_caret_position_change , props . value , props . context_file_paths ] )
275362
363+ useEffect ( ( ) => {
364+ const input = input_ref . current
365+ if ( ! input ) return
366+
367+ const existing_ghost = input . querySelector ( 'span[data-type="ghost-text"]' )
368+ if ( existing_ghost ) {
369+ const parent = existing_ghost . parentNode
370+ if ( parent ) {
371+ parent . removeChild ( existing_ghost )
372+ parent . normalize ( )
373+ }
374+ }
375+
376+ if ( ghost_text ) {
377+ const selection = window . getSelection ( )
378+ if ( selection && selection . rangeCount > 0 ) {
379+ const range = selection . getRangeAt ( 0 )
380+ if ( range . collapsed ) {
381+ const span = document . createElement ( 'span' )
382+ span . dataset . type = 'ghost-text'
383+ span . textContent = ghost_text
384+ span . style . color = 'var(--vscode-editorGhostText-foreground, #888)'
385+ span . style . pointerEvents = 'none'
386+
387+ range . insertNode ( span )
388+ selection . collapse ( range . startContainer , range . startOffset )
389+ }
390+ }
391+ }
392+ } , [ ghost_text ] )
393+
276394 const placeholder = useMemo ( ( ) => {
277395 const active_history = props . chat_history
278396
@@ -361,7 +479,7 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
361479 onDragOver = { handle_drag_over }
362480 onDragEnd = { handle_drag_end }
363481 className = { cn ( styles . input , {
364- [ styles [ 'input-with-tab- hint' ] ] : show_tab_hint ,
482+ [ styles [ 'input-with-file-match- hint' ] ] : show_file_match_hint ,
365483 [ styles [ 'input--empty' ] ] : ! props . value
366484 } ) }
367485 data-placeholder = { placeholder }
0 commit comments