@@ -38,6 +38,7 @@ export type ChatInputProps = {
3838 on_edit_format_change ?: ( format : EditFormat ) => void
3939 edit_format_instructions ?: Record < EditFormat , string >
4040 context_file_paths ?: string [ ]
41+ on_go_to_file ?: ( file_path : string ) => void
4142}
4243
4344const get_caret_position_from_div = ( element : HTMLElement ) : number => {
@@ -317,6 +318,14 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
317318 }
318319 } , [ props . focus_and_select_key ] )
319320
321+ useEffect ( ( ) => {
322+ const input_element = input_ref . current
323+ if ( input_element ) {
324+ const is_focused = document . activeElement === input_element
325+ if ( ! is_focused ) input_element . focus ( )
326+ }
327+ } , [ props . value ] )
328+
320329 useEffect ( ( ) => {
321330 const on_selection_change = ( ) => {
322331 if ( document . activeElement === input_ref . current && input_ref . current ) {
@@ -357,28 +366,27 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
357366
358367 const custom_handle_key_down = ( e : React . KeyboardEvent < HTMLDivElement > ) => {
359368 if ( e . key == 'Tab' && ! e . shiftKey ) {
360- const value = e . currentTarget . innerText
369+ const value = e . currentTarget . textContent ?? ''
361370 const selection_start = caret_position
371+ const text_before_cursor = value . substring ( 0 , selection_start )
362372
363- if ( selection_start > 0 ) {
364- const text_before_cursor = value . substring ( 0 , selection_start )
365- if (
366- text_before_cursor . trim ( ) != '' &&
367- ! / \s $ / . test ( text_before_cursor )
368- ) {
369- e . preventDefault ( )
370- const match = text_before_cursor . match ( / ( \S + ) $ / )
371- if ( match ) {
372- props . on_at_sign_click ( match [ 1 ] )
373- return
374- }
375- }
373+ if ( selection_start == 0 || / \s $ / . test ( text_before_cursor ) ) {
374+ e . preventDefault ( )
375+ props . on_at_sign_click ( )
376+ return
377+ }
378+
379+ e . preventDefault ( )
380+ const match = text_before_cursor . match ( / ( \S + ) $ / )
381+ if ( match ) {
382+ props . on_at_sign_click ( match [ 1 ] )
383+ return
376384 }
377385 }
378386
379387 // Handle backspace on shortened filenames
380388 if ( e . key == 'Backspace' && ! e . shiftKey && ! e . ctrlKey && ! e . metaKey ) {
381- const display_value = e . currentTarget . innerText
389+ const display_value = e . currentTarget . textContent ?? ''
382390 const cursor_pos = caret_position
383391
384392 // Check if we're at the end of a word that looks like a filename
@@ -409,13 +417,17 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
409417
410418 const handle_input_click = ( e : React . MouseEvent < HTMLDivElement > ) => {
411419 const target = e . target as HTMLElement
412- if ( target . classList . contains ( styles [ 'file-keyword' ] ) ) {
420+ const icon_element = target . closest ( `.${ styles [ 'file-keyword__icon' ] } ` )
421+ const text_element = target . closest ( `.${ styles [ 'file-keyword__text' ] } ` )
422+
423+ if ( icon_element ) {
413424 e . preventDefault ( )
414425 e . stopPropagation ( )
415426
416427 const input_element = input_ref . current
417428 if ( ! input_element ) return
418429
430+ // Logic for deletion based on caret position
419431 const display_pos = get_caret_position_from_div ( input_element )
420432 const raw_pos = map_display_pos_to_raw_pos (
421433 display_pos ,
@@ -426,20 +438,20 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
426438 let start_of_path = - 1
427439 let end_of_path = - 1
428440
429- if ( props . value [ raw_pos ] === '`' ) {
441+ if ( props . value [ raw_pos ] == '`' ) {
430442 // case: raw_pos at start
431443 start_of_path = raw_pos
432444 end_of_path = props . value . indexOf ( '`' , start_of_path + 1 )
433- } else if ( raw_pos > 0 && props . value [ raw_pos - 1 ] === '`' ) {
445+ } else if ( raw_pos > 0 && props . value [ raw_pos - 1 ] == '`' ) {
434446 // case: raw_pos at end
435447 end_of_path = raw_pos - 1
436448 const text_before = props . value . substring ( 0 , end_of_path )
437449 start_of_path = text_before . lastIndexOf ( '`' )
438450 }
439451
440452 if (
441- start_of_path !== - 1 &&
442- end_of_path !== - 1 &&
453+ start_of_path != - 1 &&
454+ end_of_path != - 1 &&
443455 start_of_path < end_of_path
444456 ) {
445457 const path_in_backticks = props . value . substring (
@@ -473,6 +485,25 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
473485 } , 0 )
474486 }
475487 }
488+ } else if ( text_element ) {
489+ // Clicking on file name text should open the file
490+ e . preventDefault ( )
491+ e . stopPropagation ( )
492+
493+ const filename = text_element . textContent
494+ if ( filename && props . on_go_to_file && props . context_file_paths ) {
495+ const file_path = props . context_file_paths . find (
496+ ( p ) => p . endsWith ( '/' + filename ) || p == filename
497+ )
498+ if ( file_path ) {
499+ props . on_go_to_file ( file_path )
500+ }
501+ }
502+
503+ // Keep focus on input
504+ if ( input_ref . current ) {
505+ input_ref . current . focus ( )
506+ }
476507 }
477508 }
478509
@@ -580,6 +611,7 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
580611 >
581612 < Icon variant = "CURLY_BRACES" />
582613 </ button >
614+ < span className = { styles . icon } > </ span >
583615 </ div >
584616 < div
585617 className = { styles . footer__right }
0 commit comments