Skip to content

Commit 711ddca

Browse files
committed
Use first character to determine line_type
1 parent 3e7a8ee commit 711ddca

File tree

1 file changed

+67
-14
lines changed

1 file changed

+67
-14
lines changed

asyncgit/src/sync/diff.rs

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -227,21 +227,74 @@ impl ConsumeHunk for FileDiff {
227227
) -> std::io::Result<()> {
228228
let lines = hunk
229229
.lines()
230-
.zip(0..)
231-
.map(|(line, index)| DiffLine {
232-
position: DiffLinePosition {
233-
old_lineno: Some(before_hunk_start + index),
234-
new_lineno: Some(after_hunk_start + index),
230+
.scan(
231+
(before_hunk_start, after_hunk_start),
232+
|(old_lineno, new_lineno), line| {
233+
let (line_type, content, old_lineno, new_lineno) =
234+
match line {
235+
[b'+', rest @ ..] => {
236+
let result = (
237+
DiffLineType::Add,
238+
rest,
239+
None,
240+
Some(*new_lineno),
241+
);
242+
*new_lineno += 1;
243+
result
244+
}
245+
[b'-', rest @ ..] => {
246+
let result = (
247+
DiffLineType::Delete,
248+
rest,
249+
Some(*old_lineno),
250+
None,
251+
);
252+
*old_lineno += 1;
253+
result
254+
}
255+
[b' ', rest @ ..] => {
256+
let result = (
257+
DiffLineType::None,
258+
rest,
259+
Some(*old_lineno),
260+
Some(*new_lineno),
261+
);
262+
*old_lineno += 1;
263+
*new_lineno += 1;
264+
result
265+
}
266+
[b'@', ..] => (
267+
DiffLineType::Header,
268+
line,
269+
None,
270+
None,
271+
),
272+
_ => {
273+
// Empty lines or unknown prefixes are treated as context.
274+
let result = (
275+
DiffLineType::None,
276+
line,
277+
Some(*old_lineno),
278+
Some(*new_lineno),
279+
);
280+
*old_lineno += 1;
281+
*new_lineno += 1;
282+
result
283+
}
284+
};
285+
286+
Some(DiffLine {
287+
position: DiffLinePosition {
288+
old_lineno,
289+
new_lineno,
290+
},
291+
content: String::from_utf8_lossy(content)
292+
.trim_matches(is_newline)
293+
.into(),
294+
line_type,
295+
})
235296
},
236-
content: String::from_utf8_lossy(line)
237-
.trim_matches(is_newline)
238-
.into(),
239-
// TODO:
240-
// Get correct `line_type`. We could potentially do this by looking at the line's first
241-
// character. We probably want to split it anyway as `gitui` takes care of that character
242-
// itself later in the UI.
243-
line_type: DiffLineType::default(),
244-
})
297+
)
245298
.collect();
246299

247300
let hunk_header = HunkHeader {

0 commit comments

Comments
 (0)