1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use crate::diff_algorithm::common::{Change, ChangeTag, DiffAlgorithm, DiffOp};
/// Implementation of the diff algorithm using the similar crate
#[derive(Debug, Default)]
pub struct SimilarDiff;
impl DiffAlgorithm for SimilarDiff {
fn ops<'a>(&self, old: &'a str, new: &'a str) -> Vec<DiffOp> {
// Use the similar crate to compute the diff
let diff = similar::TextDiff::from_lines(old, new);
// Convert the similar::DiffOp to our DiffOp
let mut result = Vec::new();
for op in diff.ops() {
match op {
similar::DiffOp::Equal {
old_index,
new_index,
len,
} => {
result.push(DiffOp::new(
ChangeTag::Equal,
*old_index,
*len,
*new_index,
*len,
));
}
similar::DiffOp::Delete {
old_index,
old_len,
new_index,
} => {
result.push(DiffOp::new(
ChangeTag::Delete,
*old_index,
*old_len,
*new_index,
0,
));
}
similar::DiffOp::Insert {
old_index,
new_index,
new_len,
} => {
result.push(DiffOp::new(
ChangeTag::Insert,
*old_index,
0,
*new_index,
*new_len,
));
}
similar::DiffOp::Replace {
old_index,
old_len,
new_index,
new_len,
} => {
// A replace is a delete followed by an insert
result.push(DiffOp::new(
ChangeTag::Delete,
*old_index,
*old_len,
*new_index,
0,
));
result.push(DiffOp::new(
ChangeTag::Insert,
*old_index,
0,
*new_index,
*new_len,
));
}
}
}
result
}
fn iter_inline_changes<'a>(&self, old: &'a str, new: &'a str, op: &DiffOp) -> Vec<Change<'a>> {
let mut changes = Vec::new();
// Create a similar::TextDiff to use its inline_changes method
let diff = similar::TextDiff::from_lines(old, new);
// Convert our DiffOp to a similar::DiffOp
let similar_op = match op.tag() {
ChangeTag::Equal => similar::DiffOp::Equal {
old_index: op.old_start(),
new_index: op.new_start(),
len: op.old_len(),
},
ChangeTag::Delete => similar::DiffOp::Delete {
old_index: op.old_start(),
old_len: op.old_len(),
new_index: op.new_start(),
},
ChangeTag::Insert => similar::DiffOp::Insert {
old_index: op.old_start(),
new_index: op.new_start(),
new_len: op.new_len(),
},
};
// Use similar's inline_changes method to get the inline changes
for group in diff.iter_inline_changes(&similar_op) {
let tag = match group.tag() {
similar::ChangeTag::Equal => ChangeTag::Equal,
similar::ChangeTag::Delete => ChangeTag::Delete,
similar::ChangeTag::Insert => ChangeTag::Insert,
};
let mut change = Change::new(tag);
// Add each value with its highlighting information
for (highlighted, value) in group.iter_strings_lossy() {
// Clone the value to avoid borrowing issues
change.add_value(highlighted, value.to_string().into());
}
// Set missing_newline flag
change.set_missing_newline(group.missing_newline());
changes.push(change);
}
changes
}
}