@@ -10,6 +10,7 @@ use std::time::{
1010} ;
1111
1212use fig_api_client:: Client ;
13+ use fig_api_client:: clients:: FILE_CONTEXT_LEFT_FILE_CONTENT_MAX_LEN ;
1314use fig_api_client:: model:: {
1415 FileContext ,
1516 LanguageName ,
@@ -232,7 +233,7 @@ pub async fn handle_request(
232233 } ,
233234 } ;
234235
235- let prompt = prompt ( & history, buffer) ;
236+ let prompt = prompt ( & history, buffer) . unwrap ( ) ;
236237
237238 let input = RecommendationsInput {
238239 file_context : FileContext {
@@ -362,20 +363,28 @@ pub async fn handle_set_enabled(figterm_request: InlineShellCompletionSetEnabled
362363 * INLINE_ENABLED . lock ( ) . await = figterm_request. enabled ;
363364}
364365
365- fn prompt ( history : & [ CommandInfo ] , buffer : & str ) -> String {
366- history
367- . iter ( )
368- . rev ( )
369- . filter_map ( |c| c. command . clone ( ) )
370- . chain ( [ buffer. into ( ) ] )
371- . enumerate ( )
372- . fold ( String :: new ( ) , |mut acc, ( i, c) | {
373- if i > 0 {
374- acc. push ( '\n' ) ;
375- }
376- let _ = write ! ( acc, "{:>5} {c}" , i + 1 ) ;
377- acc
378- } )
366+ fn prompt ( history : & [ CommandInfo ] , buffer : & str ) -> Option < String > {
367+ for i in ( 0 ..history. len ( ) ) . rev ( ) {
368+ let formatted_prompt = history
369+ . iter ( )
370+ . rev ( )
371+ . take ( i + 1 )
372+ . filter_map ( |c| c. command . clone ( ) )
373+ . chain ( [ buffer. into ( ) ] )
374+ . enumerate ( )
375+ . fold ( String :: new ( ) , |mut acc, ( i, c) | {
376+ if i > 0 {
377+ acc. push ( '\n' ) ;
378+ }
379+ let _ = write ! ( acc, "{:>5} {c}" , i + 1 ) ;
380+ acc
381+ } ) ;
382+
383+ if formatted_prompt. len ( ) < FILE_CONTEXT_LEFT_FILE_CONTENT_MAX_LEN {
384+ return Some ( formatted_prompt) ;
385+ }
386+ }
387+ None
379388}
380389
381390static RE_1 : LazyLock < Regex > = LazyLock :: new ( || Regex :: new ( & format ! ( "{}\\ s+.*" , * HISTORY_COUNT + 1 ) ) . unwrap ( ) ) ;
@@ -420,7 +429,7 @@ mod tests {
420429 } ,
421430 ] ;
422431
423- let prompt = prompt ( & history, "echo " ) ;
432+ let prompt = prompt ( & history, "echo " ) . unwrap ( ) ;
424433 println ! ( "{prompt}" ) ;
425434
426435 assert_eq ! ( prompt, " 1 echo hello\n 2 echo world\n 3 echo " ) ;
@@ -446,6 +455,16 @@ mod tests {
446455 ) ;
447456 }
448457
458+ #[ test]
459+ fn too_long_prompt ( ) {
460+ let history = vec ! [ CommandInfo {
461+ command: Some ( "a" . repeat( FILE_CONTEXT_LEFT_FILE_CONTENT_MAX_LEN + 1 ) ) ,
462+ ..Default :: default ( )
463+ } ] ;
464+
465+ assert ! ( prompt( & history, "echo " ) . is_none( ) ) ;
466+ }
467+
449468 #[ ignore = "not in CI" ]
450469 #[ tokio:: test]
451470 async fn test_inline_suggestion_prompt ( ) {
@@ -458,7 +477,7 @@ mod tests {
458477 0 ,
459478 )
460479 . unwrap ( ) ;
461- let prompt = prompt ( & commands, "cd " ) ;
480+ let prompt = prompt ( & commands, "cd " ) . unwrap ( ) ;
462481
463482 let client = fig_api_client:: Client :: new ( ) . await . unwrap ( ) ;
464483 let out = client
0 commit comments