@@ -4092,6 +4092,79 @@ fn inline_diff_header(
40924092 }
40934093 let mut header = Line :: from ( header) ;
40944094 if let Some ( path) = file_path {
4095+ /// Keep expanded source lines inside their frame. Wrap the styled source, not
4096+ /// the gutter, so continuation rows cannot masquerade as another changed line.
4097+ fn expanded_diff_row (
4098+ line : & ParsedDiffLine ,
4099+ file_ext : Option < & str > ,
4100+ number_width : usize ,
4101+ width : usize ,
4102+ ) -> Vec < Line < ' static > > {
4103+ let dim = Style :: default ( ) . fg ( dim_color ( ) ) ;
4104+ let color = if line. kind == DiffLineKind :: Add {
4105+ diff_add_color ( )
4106+ } else {
4107+ diff_del_color ( )
4108+ } ;
4109+ let sign = if line. kind == DiffLineKind :: Add {
4110+ "+"
4111+ } else {
4112+ "-"
4113+ } ;
4114+ let number = line. prefix . trim ( ) . trim_end_matches ( [ '+' , '-' ] ) ;
4115+ let gutter = vec ! [
4116+ Span :: styled( format!( "│ {number:>number_width$} " ) , dim) ,
4117+ Span :: styled( format!( "{sign} " ) , Style :: default ( ) . fg( color) ) ,
4118+ ] ;
4119+ let gutter_width = number_width + 5 ;
4120+ // Leave room for a wide source character even on very narrow terminals.
4121+ if width < gutter_width + 2 {
4122+ return vec ! [ super :: truncate_line_with_ellipsis_to_width(
4123+ & Line :: from( gutter) ,
4124+ width,
4125+ ) ] ;
4126+ }
4127+ let content_width = width - gutter_width;
4128+ let mut rows = Vec :: new ( ) ;
4129+ let mut spans = gutter;
4130+ let mut used = 0 ;
4131+ // Expand tabs in source-column coordinates, independent of the gutter.
4132+ let mut column = 0usize ;
4133+ let mut source = String :: new ( ) ;
4134+ for ch in line. content . chars ( ) {
4135+ if ch == '\t' {
4136+ let spaces = 4 - column % 4 ;
4137+ source. push_str ( & " " . repeat ( spaces) ) ;
4138+ column += spaces;
4139+ } else {
4140+ source. push ( ch) ;
4141+ column += unicode_width:: UnicodeWidthChar :: width ( ch) . unwrap_or ( 0 ) ;
4142+ }
4143+ }
4144+ for span in markdown:: highlight_line ( & source, file_ext) {
4145+ let span = tint_span_with_diff_color ( span, color) ;
4146+ let mut text = String :: new ( ) ;
4147+ for ch in span. content . chars ( ) {
4148+ let cells = unicode_width:: UnicodeWidthChar :: width ( ch) . unwrap_or ( 0 ) ;
4149+ if used + cells > content_width {
4150+ if !text. is_empty ( ) {
4151+ spans. push ( Span :: styled ( std:: mem:: take ( & mut text) , span. style ) ) ;
4152+ }
4153+ rows. push ( Line :: from ( std:: mem:: take ( & mut spans) ) . alignment ( Alignment :: Left ) ) ;
4154+ spans. push ( Span :: styled ( format ! ( "│ {:>number_width$} ↪ " , "" ) , dim) ) ;
4155+ used = 0 ;
4156+ }
4157+ text. push ( ch) ;
4158+ used += cells;
4159+ }
4160+ if !text. is_empty ( ) {
4161+ spans. push ( Span :: styled ( text, span. style ) ) ;
4162+ }
4163+ }
4164+ rows. push ( Line :: from ( spans) . alignment ( Alignment :: Left ) ) ;
4165+ rows
4166+ }
4167+
40954168 let path_width = width. saturating_sub ( header. width ( ) . saturating_add ( 3 ) ) ;
40964169 if path_width > 0 {
40974170 header. spans . push ( Span :: styled (
@@ -4653,6 +4726,11 @@ pub(crate) fn render_tool_message(
46534726 } ) ;
46544727 const MAX_DIFF_LINES : usize = MAX_INLINE_DIFF_LINES ;
46554728 let total_changes = change_lines. len ( ) ;
4729+ let number_width = change_lines
4730+ . iter ( )
4731+ . map ( |line| line. prefix . trim ( ) . trim_end_matches ( [ '+' , '-' ] ) . len ( ) )
4732+ . max ( )
4733+ . unwrap_or ( 0 ) ;
46564734 let additions = change_lines
46574735 . iter ( )
46584736 . filter ( |line| line. kind == DiffLineKind :: Add )
@@ -4678,6 +4756,15 @@ pub(crate) fn render_tool_message(
46784756 |line : & ParsedDiffLine | line. file_path . clone ( ) . or_else ( || file_path_for_ext. clone ( ) ) ;
46794757 let first_file_path = display_lines. first ( ) . and_then ( |line| diff_file_path ( line) ) ;
46804758 lines. push ( inline_diff_header (
4759+ if full_inline {
4760+ let file_ext = current_file_path
4761+ . as_deref ( )
4762+ . and_then ( |p| std:: path:: Path :: new ( p) . extension ( ) )
4763+ . and_then ( |e| e. to_str ( ) ) ;
4764+ lines. extend ( expanded_diff_row ( line, file_ext, number_width, row_width) ) ;
4765+ continue ;
4766+ }
4767+
46814768 "╭─" ,
46824769 Some ( ( additions, deletions) ) ,
46834770 first_file_path. as_deref ( ) ,
@@ -4720,7 +4807,7 @@ pub(crate) fn render_tool_message(
47204807 let border_prefix = format ! ( "{}│ " , pad_str) ;
47214808 let prefix_visual_width = unicode_width:: UnicodeWidthStr :: width ( border_prefix. as_str ( ) )
47224809 + unicode_width:: UnicodeWidthStr :: width ( line. prefix . as_str ( ) ) ;
4723- let max_content_width = ( width as usize ) . saturating_sub ( prefix_visual_width + 1 ) ;
4810+ let max_content_width = row_width . saturating_sub ( prefix_visual_width) ;
47244811
47254812 let mut spans: Vec < Span < ' static > > = vec ! [
47264813 Span :: styled( border_prefix, Style :: default ( ) . fg( dim_color( ) ) ) ,
0 commit comments