Refactor change iteration
This commit is contained in:
parent
94890c52a5
commit
503d912262
1 changed files with 24 additions and 15 deletions
17
src/udiff.rs
17
src/udiff.rs
|
|
@ -19,7 +19,7 @@ use std::fmt;
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
|
|
||||||
use crate::algorithms::{Algorithm, DiffOp};
|
use crate::algorithms::{Algorithm, DiffOp};
|
||||||
use crate::text::{ChangeTag, TextDiff};
|
use crate::text::{Change, ChangeTag, TextDiff};
|
||||||
|
|
||||||
/// Represents a range of a unified diff hunk.
|
/// Represents a range of a unified diff hunk.
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
|
@ -153,6 +153,17 @@ impl<'diff, 'old, 'new, 'bufs> Hunk<'diff, 'old, 'new, 'bufs> {
|
||||||
pub fn ops(&self) -> &[DiffOp] {
|
pub fn ops(&self) -> &[DiffOp] {
|
||||||
&self.ops
|
&self.ops
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Iterates over all changes in a hunk.
|
||||||
|
pub fn iter_changes(&self) -> impl Iterator<Item = Change<'_>> {
|
||||||
|
// unclear why this needs Box::new here. It seems to infer some really
|
||||||
|
// odd lifetimes I can't figure out how to work with.
|
||||||
|
(Box::new(
|
||||||
|
self.ops()
|
||||||
|
.iter()
|
||||||
|
.flat_map(move |op| self.diff.iter_changes(op)),
|
||||||
|
)) as Box<dyn Iterator<Item = _>>
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'diff, 'old, 'new, 'bufs> fmt::Display for Hunk<'diff, 'old, 'new, 'bufs> {
|
impl<'diff, 'old, 'new, 'bufs> fmt::Display for Hunk<'diff, 'old, 'new, 'bufs> {
|
||||||
|
|
@ -163,8 +174,7 @@ impl<'diff, 'old, 'new, 'bufs> fmt::Display for Hunk<'diff, 'old, 'new, 'bufs> {
|
||||||
"\n"
|
"\n"
|
||||||
};
|
};
|
||||||
writeln!(f, "{}", self.header())?;
|
writeln!(f, "{}", self.header())?;
|
||||||
for op in self.ops() {
|
for change in self.iter_changes() {
|
||||||
for change in self.diff.iter_changes(&op) {
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"{}{}{}",
|
"{}{}{}",
|
||||||
|
|
@ -177,7 +187,6 @@ impl<'diff, 'old, 'new, 'bufs> fmt::Display for Hunk<'diff, 'old, 'new, 'bufs> {
|
||||||
nl
|
nl
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue