@@ -7,10 +7,7 @@ use super::{
77 hunks:: Hunks ,
88} ;
99
10- use crate :: diff_util:: {
11- change:: { Change , ChangeContext } ,
12- constants:: NO_NEW_LINE_AT_END_OF_FILE ,
13- } ;
10+ use crate :: diff_util:: constants:: NO_NEW_LINE_AT_END_OF_FILE ;
1411
1512use std:: {
1613 collections:: HashMap ,
@@ -291,75 +288,6 @@ impl<'a> FileDiff<'a> {
291288 }
292289 }
293290
294- fn get_context_ranges ( & self , context : usize ) -> Vec < ChangeContext > {
295- let f1_lines = self . file1 . lines ( ) . len ( ) ;
296- let f2_lines = self . file2 . lines ( ) . len ( ) ;
297-
298- let mut change_ranges = self
299- . hunks
300- . hunks ( )
301- . iter ( )
302- . filter ( |hunk| !Change :: is_none ( & hunk. kind ( ) ) && !Change :: is_unchanged ( & hunk. kind ( ) ) )
303- . map ( |hunk| {
304- (
305- hunk. kind ( ) . clone ( ) ,
306- hunk. ln1_start ( ) as i64 ,
307- hunk. ln1_end ( ) as i64 ,
308- hunk. ln2_start ( ) as i64 ,
309- hunk. ln2_end ( ) as i64 ,
310- )
311- } )
312- . collect :: < Vec < ( Change , i64 , i64 , i64 , i64 ) > > ( ) ;
313-
314- change_ranges. sort_by_key ( |change| ( change. 1 , change. 3 ) ) ;
315-
316- let mut context_ranges: Vec < ChangeContext > = Vec :: new ( ) ;
317-
318- let f1_max = if self . file1 . ends_with_newline ( ) && f1_lines - 1 >= 1 {
319- f1_lines - 1
320- } else {
321- f1_lines
322- } ;
323-
324- let f2_max = if self . file2 . ends_with_newline ( ) && f2_lines - 1 >= 1 {
325- f2_lines - 1
326- } else {
327- f2_lines
328- } ;
329-
330- for cr in change_ranges {
331- let ln1s = i64:: clamp ( cr. 1 - context as i64 , 1 , f1_max as i64 ) ;
332- let ln1e = i64:: clamp ( cr. 2 + context as i64 , 1 , f1_max as i64 ) ;
333- let ln2s = i64:: clamp ( cr. 3 - context as i64 , 1 , f2_max as i64 ) ;
334- let ln2e = i64:: clamp ( cr. 4 + context as i64 , 1 , f2_max as i64 ) ;
335-
336- if context_ranges. len ( ) > 0 {
337- // Overlap check
338- if let Some ( change_ctx) = context_ranges. last_mut ( ) {
339- if change_ctx. ln1_end >= ln1s as usize || change_ctx. ln2_end >= ln2s as usize {
340- change_ctx. ln1_end = ln1e as usize ;
341- change_ctx. ln2_end = ln2e as usize ;
342- continue ;
343- }
344- }
345- }
346-
347- context_ranges. push ( ChangeContext {
348- change : cr. 0 ,
349- ln1_start : ln1s as usize ,
350- ln1_end : ln1e as usize ,
351- hk1_start : cr. 1 as usize ,
352- hk1_end : cr. 2 as usize ,
353- ln2_start : ln2s as usize ,
354- ln2_end : ln2e as usize ,
355- hk2_start : cr. 3 as usize ,
356- hk2_end : cr. 4 as usize ,
357- } ) ;
358- }
359-
360- context_ranges
361- }
362-
363291 fn order_hunks_by_output_format ( & mut self ) {
364292 match self . format_options . output_format {
365293 OutputFormat :: Debug => self . order_hunks_ascending ( ) ,
@@ -392,55 +320,61 @@ impl<'a> FileDiff<'a> {
392320 Self :: get_header( self . file2, & self . format_options. label2)
393321 ) ;
394322
395- let change_ranges = self . get_context_ranges ( context) ;
323+ for hunk in self . hunks . hunks ( ) {
324+ println ! ( "***************" ) ;
325+ let mut hunk_len = ( hunk. ln1_end ( ) - hunk. ln1_start ( ) + 2 * context) . min ( self . file1 . lines ( ) . len ( ) ) ;
326+ let hunk_start = hunk. ln1_start ( ) . saturating_sub ( context) ;
327+
328+ // make sure we don't go out of bounds
329+ if hunk_start + hunk_len > self . file1 . lines ( ) . len ( ) - 1 {
330+ hunk_len = self . file1 . lines ( ) . len ( ) - hunk_start;
331+ }
396332
397- for cr_index in 0 ..change_ranges . len ( ) {
398- let cr = & change_ranges [ cr_index ] ;
333+ println ! ( "*** {},{} ***" , hunk_start + 1 , hunk_start + hunk_len ) ;
334+ let h1_prefix = if hunk . ln2_start ( ) == hunk . ln2_end ( ) { "- " } else { "! " } ;
399335
400- println ! ( "***************" ) ;
401- println ! ( "*** {} ***" , format!( "{},{}" , cr. ln1_start, cr. ln1_end) ) ;
402- // if self.file1.expected_changed_in_range(
403- // cr.0 - 1,
404- // cr.1 - 1,
405- // &vec![Change::is_delete, Change::is_substitute],
406- // ) {
407- // for i in cr.0..=cr.1 {
408- // println!(
409- // "{}",
410- // // self.file1.get_context_identifier(i - 1),
411- // self.file1.line(i - 1)
412- // );
413- // }
414- // }
415-
416- if cr_index == change_ranges. len ( ) - 1 {
417- if self . file1 . ends_with_newline ( ) == false {
418- println ! ( "{}" , NO_NEW_LINE_AT_END_OF_FILE ) ;
336+ // dont print context for empty hunk
337+ if hunk. ln1_start ( ) == hunk. ln1_end ( ) {
338+ continue ;
339+ }
340+
341+ for i in hunk_start..hunk_start + hunk_len {
342+ if i < hunk. ln1_start ( ) {
343+ println ! ( " {}" , self . file1. line( i) ) ;
344+ } else if i < hunk. ln1_end ( ) {
345+ println ! ( "{h1_prefix}{}" , self . file1. line( i) ) ;
346+ } else {
347+ println ! ( " {}" , self . file1. line( i) ) ;
419348 }
420349 }
421350
422- println ! ( "--- {} ---" , format!( "{},{}" , cr. ln2_start, cr. ln2_end) ) ;
423-
424- // if self.file2.expected_changed_in_range(
425- // cr.2 - 1,
426- // cr.3 - 1,
427- // &vec![Change::is_insert, Change::is_substitute],
428- // ) {
429- // for i in cr.2..=cr.3 {
430- // println!(
431- // "{}",
432- // // self.file2.get_context_identifier(i - 1),
433- // self.file2.line(i - 1)
434- // );
435- // }
436- // }
437-
438- if cr_index == change_ranges. len ( ) - 1 {
439- if self . file2 . ends_with_newline ( ) == false {
440- println ! ( "{}" , NO_NEW_LINE_AT_END_OF_FILE ) ;
351+ let mut hunk_len = ( hunk. ln2_end ( ) - hunk. ln2_start ( ) + 2 * context) . min ( self . file2 . lines ( ) . len ( ) ) ;
352+ let hunk_start = hunk. ln2_start ( ) . saturating_sub ( context) ;
353+
354+ // make sure we don't go out of bounds
355+ if hunk_start + hunk_len > self . file2 . lines ( ) . len ( ) - 1 {
356+ hunk_len = self . file2 . lines ( ) . len ( ) - hunk_start;
357+ }
358+
359+ println ! ( "--- {},{} ---" , hunk_start + 1 , hunk_start + hunk_len) ;
360+ let h2_prefix = if hunk. ln1_start ( ) == hunk. ln1_end ( ) { "+ " } else { "! " } ;
361+
362+ // dont print context for empty hunk
363+ if hunk. ln2_start ( ) == hunk. ln2_end ( ) {
364+ continue ;
365+ }
366+
367+ for i in hunk_start..hunk_start + hunk_len {
368+ if i < hunk. ln2_start ( ) {
369+ println ! ( " {}" , self . file2. line( i) ) ;
370+ } else if i < hunk. ln2_end ( ) {
371+ println ! ( "{h2_prefix}{}" , self . file2. line( i) ) ;
372+ } else {
373+ println ! ( " {}" , self . file2. line( i) ) ;
441374 }
442375 }
443376 }
377+
444378 }
445379
446380 fn print_line ( & self , lines : & mut String , start : usize , end : usize , prefix : & str ) -> Result < usize , std:: fmt:: Error > {
0 commit comments