pub struct DiffHunk {
pub old_start: usize,
pub old_count: usize,
pub new_start: usize,
pub new_count: usize,
pub context: Option<String>,
pub lines: Vec<DiffLine>,
}Expand description
Represents a single hunk (section) in a diff.
A hunk contains the line range information and a collection of lines that
make up this section of the diff. The header information (old_start, old_count,
new_start, new_count) corresponds to the unified diff format:
@@ -old_start,old_count +new_start,new_count @@ context
§Fields
old_start- Starting line number in the old fileold_count- Number of lines from the old file in this hunknew_start- Starting line number in the new filenew_count- Number of lines from the new file in this hunkcontext- Optional context text (e.g., function name) shown in the headerlines- The actual diff lines in this hunk
Fields§
§old_start: usizeStarting line number in the old/original file.
old_count: usizeNumber of lines from the old file included in this hunk.
new_start: usizeStarting line number in the new/modified file.
new_count: usizeNumber of lines from the new file included in this hunk.
context: Option<String>Optional context text from the hunk header (e.g., function name).
lines: Vec<DiffLine>The lines in this hunk, including context, added, and removed lines.
Implementations§
Source§impl DiffHunk
impl DiffHunk
Sourcepub fn from_header(header: &str) -> Option<Self>
pub fn from_header(header: &str) -> Option<Self>
Creates a new diff hunk by parsing a unified diff header line.
Parses headers in the format: @@ -old_start,old_count +new_start,new_count @@ context
§Arguments
header- The hunk header line to parse
§Returns
Some(DiffHunk) if parsing succeeds, None otherwise
§Example
use ratatui_toolkit::code_diff::DiffHunk;
let hunk = DiffHunk::from_header("@@ -1,4 +1,5 @@ fn main()").unwrap();
assert_eq!(hunk.old_start, 1);
assert_eq!(hunk.old_count, 4);
assert_eq!(hunk.new_start, 1);
assert_eq!(hunk.new_count, 5);
assert_eq!(hunk.context.as_deref(), Some("fn main()"));Source§impl DiffHunk
impl DiffHunk
Sourcepub fn new(
old_start: usize,
old_count: usize,
new_start: usize,
new_count: usize,
) -> Self
pub fn new( old_start: usize, old_count: usize, new_start: usize, new_count: usize, ) -> Self
Creates a new diff hunk with the specified line ranges.
§Arguments
old_start- Starting line number in the old fileold_count- Number of lines from the old filenew_start- Starting line number in the new filenew_count- Number of lines from the new file
§Returns
A new DiffHunk with empty lines and no context
§Example
use ratatui_toolkit::code_diff::DiffHunk;
let hunk = DiffHunk::new(1, 4, 1, 5);
assert_eq!(hunk.old_start, 1);
assert_eq!(hunk.old_count, 4);Source§impl DiffHunk
impl DiffHunk
Sourcepub fn added_count(&self) -> usize
pub fn added_count(&self) -> usize
Returns the number of added lines in this hunk.
§Returns
The count of lines with DiffLineKind::Added
§Example
use ratatui_toolkit::code_diff::{DiffHunk, DiffLine};
let mut hunk = DiffHunk::new(1, 2, 1, 3);
hunk.add_line(DiffLine::added("new line 1", 1));
hunk.add_line(DiffLine::added("new line 2", 2));
assert_eq!(hunk.added_count(), 2);Source§impl DiffHunk
impl DiffHunk
Sourcepub fn removed_count(&self) -> usize
pub fn removed_count(&self) -> usize
Returns the number of removed lines in this hunk.
§Returns
The count of lines with DiffLineKind::Removed
§Example
use ratatui_toolkit::code_diff::{DiffHunk, DiffLine};
let mut hunk = DiffHunk::new(1, 3, 1, 2);
hunk.add_line(DiffLine::removed("old line 1", 1));
hunk.add_line(DiffLine::removed("old line 2", 2));
assert_eq!(hunk.removed_count(), 2);Trait Implementations§
impl Eq for DiffHunk
impl StructuralPartialEq for DiffHunk
Auto Trait Implementations§
impl Freeze for DiffHunk
impl RefUnwindSafe for DiffHunk
impl Send for DiffHunk
impl Sync for DiffHunk
impl Unpin for DiffHunk
impl UnwindSafe for DiffHunk
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more