Skip to content

Commit ee1c2b9

Browse files
committed
diff: Remove changes
1 parent 2086db4 commit ee1c2b9

File tree

2 files changed

+66
-16
lines changed

2 files changed

+66
-16
lines changed

text/diff_util/file_diff.rs

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,32 @@ pub struct FileDiff<'a> {
2424
file2: &'a mut FileData<'a>,
2525
hunks: Hunks,
2626
format_options: &'a FormatOptions,
27-
pub are_different: bool,
27+
are_different: bool,
2828
}
2929

3030
impl<'a> FileDiff<'a> {
31+
pub fn are_different(&self) -> bool {
32+
self.are_different
33+
}
34+
35+
pub fn set_different(&mut self, are_different: bool) {
36+
self.are_different = are_different;
37+
}
38+
39+
fn new(
40+
file1: &'a mut FileData<'a>,
41+
file2: &'a mut FileData<'a>,
42+
format_options: &'a FormatOptions,
43+
) -> Self {
44+
Self {
45+
file1,
46+
file2,
47+
hunks: Hunks::new(),
48+
format_options,
49+
are_different: false,
50+
}
51+
}
52+
3153
pub fn file_diff(
3254
path1: PathBuf,
3355
path2: PathBuf,
@@ -63,13 +85,7 @@ impl<'a> FileDiff<'a> {
6385
let mut file1 = FileData::get_file(path1, lines1, ends_with_newline1)?;
6486
let mut file2 = FileData::get_file(path2, lines2, ends_with_newline2)?;
6587

66-
let mut diff = FileDiff {
67-
file1: &mut file1,
68-
file2: &mut file2,
69-
hunks: Default::default(),
70-
format_options,
71-
are_different: Default::default(),
72-
};
88+
let mut diff = FileDiff::new(&mut file1, &mut file2, format_options);
7389

7490
// histogram diff
7591
let mut lcs_indices: Vec<i32> = vec![-1; diff.file1.lines().len()];
@@ -89,10 +105,10 @@ impl<'a> FileDiff<'a> {
89105
.create_hunks_from_lcs(&lcs_indices, num_lines1, num_lines2);
90106

91107
if diff.hunks.hunk_count() > 0 {
92-
diff.are_different = true;
108+
diff.set_different(true);
93109
}
94110

95-
if diff.are_different {
111+
if diff.are_different() {
96112
if let Some(show_if_different) = show_if_different {
97113
println!("{}", show_if_different);
98114
}
@@ -200,7 +216,7 @@ impl<'a> FileDiff<'a> {
200216
}
201217
}
202218

203-
if self.are_different {
219+
if self.are_different() {
204220
Ok(DiffExitStatus::Different)
205221
} else {
206222
Ok(DiffExitStatus::NotDifferent)
@@ -315,7 +331,7 @@ impl<'a> FileDiff<'a> {
315331
Self::get_header(self.file2, self.format_options.label2())
316332
);
317333

318-
let mut diff_disp = ContextDiffDisplay::default();
334+
let mut diff_disp = ContextDiffDisplay::new();
319335

320336
for hunk in self.hunks.hunks() {
321337
// move cursor to the start of context for first hunk
@@ -442,7 +458,7 @@ impl<'a> FileDiff<'a> {
442458
Self::get_header(self.file2, self.format_options.label2())
443459
);
444460

445-
let mut diff_disp = UnifiedDiffDisplay::default();
461+
let mut diff_disp = UnifiedDiffDisplay::new();
446462

447463
for hunk in self.hunks.hunks() {
448464
// move cursor to the start of context for first hunk
@@ -510,7 +526,6 @@ impl<'a> FileDiff<'a> {
510526
}
511527
}
512528

513-
#[derive(Default)]
514529
pub struct UnifiedDiffDisplay {
515530
curr_pos1: usize,
516531
curr_pos2: usize,
@@ -525,6 +540,18 @@ pub struct UnifiedDiffDisplay {
525540
}
526541

527542
impl UnifiedDiffDisplay {
543+
pub fn new() -> Self {
544+
Self {
545+
curr_pos1: 0,
546+
curr_pos2: 0,
547+
context_start1: 0,
548+
context_start2: 0,
549+
hunk1_len: 0,
550+
hunk2_len: 0,
551+
hunk_lines: String::new(),
552+
}
553+
}
554+
528555
pub fn write_line(
529556
&mut self,
530557
file: &FileData,
@@ -580,7 +607,6 @@ impl UnifiedDiffDisplay {
580607
}
581608
}
582609

583-
#[derive(Default)]
584610
pub struct ContextDiffDisplay {
585611
curr_pos1: usize,
586612
curr_pos2: usize,
@@ -595,6 +621,18 @@ pub struct ContextDiffDisplay {
595621
}
596622

597623
impl ContextDiffDisplay {
624+
pub fn new() -> Self {
625+
Self {
626+
curr_pos1: 0,
627+
curr_pos2: 0,
628+
context_start1: 0,
629+
context_start2: 0,
630+
hunk1_len: 0,
631+
hunk2_len: 0,
632+
hunk_lines: [String::new(), String::new()],
633+
}
634+
}
635+
598636
pub fn set_context_start(&mut self) {
599637
self.context_start1 = self.curr_pos1 + 1;
600638
self.context_start2 = self.curr_pos2 + 1;

text/diff_util/hunks.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ impl Default for Hunk {
3333
}
3434

3535
impl Hunk {
36+
pub fn new() -> Self {
37+
Self::default()
38+
}
39+
3640
pub fn f1_range(&self, is_ed: bool) -> String {
3741
if self.ln1_start == self.ln1_end {
3842
format!("{}", self.ln1_start)
@@ -189,12 +193,20 @@ impl Hunk {
189193
}
190194
}
191195

192-
#[derive(Default)]
193196
pub struct Hunks {
194197
hunks: Vec<Hunk>,
195198
}
196199

197200
impl Hunks {
201+
pub fn new() -> Self {
202+
Self {
203+
hunks: {
204+
Hunk::new();
205+
vec![] as Vec<Hunk>
206+
},
207+
}
208+
}
209+
198210
pub fn hunks(&self) -> &Vec<Hunk> {
199211
&self.hunks
200212
}

0 commit comments

Comments
 (0)