ratatui_toolkit/widgets/code_diff/foundation/diff_hunk/mod.rs
1//! Diff hunk representation.
2//!
3//! A hunk is a contiguous section of a diff, representing a group of related changes.
4//! Each hunk starts with a header line like `@@ -1,4 +1,5 @@ context` that indicates
5//! the line ranges in both the old and new versions.
6//!
7//! # Structure
8//!
9//! - [`DiffHunk`] - The diff hunk struct
10//! - [`constructors`] - Constructor functions (`new`, `from_header`)
11//! - [`methods`] - Instance methods for working with hunks
12//!
13//! # Example
14//!
15//! ```rust
16//! use ratatui_toolkit::code_diff::{DiffHunk, DiffLine};
17//!
18//! let mut hunk = DiffHunk::new(1, 4, 1, 5);
19//! hunk.add_line(DiffLine::context("unchanged", 1, 1));
20//! hunk.add_line(DiffLine::removed("old line", 2));
21//! hunk.add_line(DiffLine::added("new line", 2));
22//! ```
23
24pub mod constructors;
25pub mod methods;
26
27use crate::widgets::code_diff::diff_line::DiffLine;
28
29/// Represents a single hunk (section) in a diff.
30///
31/// A hunk contains the line range information and a collection of lines that
32/// make up this section of the diff. The header information (old_start, old_count,
33/// new_start, new_count) corresponds to the unified diff format:
34/// `@@ -old_start,old_count +new_start,new_count @@ context`
35///
36/// # Fields
37///
38/// * `old_start` - Starting line number in the old file
39/// * `old_count` - Number of lines from the old file in this hunk
40/// * `new_start` - Starting line number in the new file
41/// * `new_count` - Number of lines from the new file in this hunk
42/// * `context` - Optional context text (e.g., function name) shown in the header
43/// * `lines` - The actual diff lines in this hunk
44#[derive(Debug, Clone, PartialEq, Eq)]
45pub struct DiffHunk {
46 /// Starting line number in the old/original file.
47 pub old_start: usize,
48
49 /// Number of lines from the old file included in this hunk.
50 pub old_count: usize,
51
52 /// Starting line number in the new/modified file.
53 pub new_start: usize,
54
55 /// Number of lines from the new file included in this hunk.
56 pub new_count: usize,
57
58 /// Optional context text from the hunk header (e.g., function name).
59 pub context: Option<String>,
60
61 /// The lines in this hunk, including context, added, and removed lines.
62 pub lines: Vec<DiffLine>,
63}