@@ -81,34 +81,19 @@ fn trauncate_line_for_ai(
8181 match_ranges : Option < & [ ( u32 , u32 ) ] > ,
8282 max_len : usize ,
8383) -> String {
84- // Strip leading/trailing whitespace to save tokens — the LLM has file:line for location.
85- let trimmed = line. trim ( ) ;
84+ // Leading whitespace is already stripped by core (trim_whitespace option).
85+ // Only strip trailing whitespace here.
86+ let trimmed = line. trim_end ( ) ;
8687 if trimmed. is_empty ( ) {
8788 return String :: new ( ) ;
8889 }
8990
90- let strip_offset = line. len ( ) - line. trim_start ( ) . len ( ) ;
91-
9291 if trimmed. len ( ) <= max_len {
9392 return trimmed. to_string ( ) ;
9493 }
9594
96- // Adjust match ranges for the stripped leading whitespace
97- let adjusted: Vec < ( u32 , u32 ) > ;
98- let ranges = match match_ranges {
99- Some ( r) if strip_offset > 0 => {
100- let off = strip_offset as u32 ;
101- adjusted = r
102- . iter ( )
103- . map ( |& ( s, e) | ( s. saturating_sub ( off) , e. saturating_sub ( off) ) )
104- . collect ( ) ;
105- Some ( adjusted. as_slice ( ) )
106- }
107- other => other,
108- } ;
109-
11095 // Use first match range to center the window
111- if let Some ( ranges) = ranges
96+ if let Some ( ranges) = match_ranges
11297 && let Some ( & ( match_start, match_end) ) = ranges. first ( )
11398 {
11499 let match_start = match_start as usize ;
@@ -569,26 +554,29 @@ mod tests {
569554 use super :: * ;
570555
571556 #[ test]
572- fn trunc_strips_whitespace ( ) {
573- assert_eq ! ( trauncate_line_for_ai( " foo()" , None , 180 ) , "foo()" ) ;
574- assert_eq ! ( trauncate_line_for_ai( " bar " , None , 180 ) , "bar" ) ;
557+ fn trunc_strips_trailing_whitespace ( ) {
558+ // Leading whitespace is now stripped by core's trim_whitespace option.
559+ // This function only strips trailing whitespace.
560+ assert_eq ! ( trauncate_line_for_ai( "foo()" , None , 180 ) , "foo()" ) ;
561+ assert_eq ! ( trauncate_line_for_ai( "bar " , None , 180 ) , "bar" ) ;
575562 assert_eq ! ( trauncate_line_for_ai( " " , None , 180 ) , "" ) ;
576563 }
577564
578565 #[ test]
579- fn trunc_adjusts_match_ranges_after_strip ( ) {
580- // " hello" — match on "hello" at bytes 4..9
581- let line = " hello" ;
582- let ranges = [ ( 4 , 9 ) ] ;
566+ fn trunc_preserves_pre_trimmed_match_ranges ( ) {
567+ // Core already stripped leading whitespace and adjusted offsets,
568+ // so "hello" arrives with match at bytes 0..5.
569+ let line = "hello" ;
570+ let ranges = [ ( 0 , 5 ) ] ;
583571 let result = trauncate_line_for_ai ( line, Some ( & ranges) , 180 ) ;
584- // After stripping 4 leading spaces, the trimmed line is "hello"
585572 assert_eq ! ( result, "hello" ) ;
586573 }
587574
588575 #[ test]
589576 fn trunc_long_line_centered ( ) {
590- let line = format ! ( "{}match_here{}" , " " . repeat( 8 ) , "x" . repeat( 200 ) ) ;
591- let ranges = [ ( 8u32 , 18u32 ) ] ;
577+ // Core already stripped leading whitespace; offsets are pre-adjusted.
578+ let line = format ! ( "match_here{}" , "x" . repeat( 200 ) ) ;
579+ let ranges = [ ( 0u32 , 10u32 ) ] ;
592580 let result = trauncate_line_for_ai ( & line, Some ( & ranges) , 50 ) ;
593581 assert ! ( result. contains( "match_here" ) ) ;
594582 assert ! ( result. len( ) <= 55 ) ; // budget + ellipsis chars
0 commit comments