ratatui_toolkit/widgets/code_diff/
mod.rs

1//! Code diff widget for displaying side-by-side diffs.
2//!
3//! This module provides a VS Code-style diff viewer widget for ratatui,
4//! supporting side-by-side display of code changes with syntax highlighting.
5//!
6//! # Features
7//!
8//! - **Side-by-side layout**: Old version on left, new version on right
9//! - **Line number display**: Independent line numbers per side
10//! - **Visual markers**: Green for additions, red for deletions
11//! - **Hunk headers**: Gray bars showing `@@ -X,Y +A,B @@ context`
12//! - **Alignment**: Matching lines align horizontally
13//! - **Scrolling**: Support for vertical scrolling through large diffs
14//!
15//! # Structure
16//!
17//! - [`widget`] - The primary widget implementation (`CodeDiff`)
18//! - [`foundation`] - Core diff types (config, hunks, lines, enums)
19//! - [`extensions`] - Optional extensions like the file tree sidebar
20//! - [`CodeDiff`] - The main diff widget
21//! - [`DiffConfig`] - Display configuration (colors, styles)
22//! - [`DiffHunk`] - A section of the diff with related changes
23//! - [`DiffLine`] - A single line in a diff
24//! - [`DiffLineKind`] - Type of diff line (context, added, removed)
25//! - [`DiffStyle`] - Display style (side-by-side, unified, inline)
26//!
27//! # Example
28//!
29//! ```rust
30//! use ratatui_toolkit::code_diff::{CodeDiff, DiffConfig};
31//!
32//! // Parse a unified diff
33//! let diff_text = r#"
34//! --- a/example.rs
35//! +++ b/example.rs
36//! @@ -1,4 +1,5 @@
37//!  fn main() {
38//! -    println!("Hello");
39//! +    println!("Hello, World!");
40//! +    println!("Goodbye!");
41//!  }
42//! "#;
43//!
44//! let widget = CodeDiff::from_unified_diff(diff_text)
45//!     .with_config(DiffConfig::new().show_line_numbers(true));
46//!
47//! // Render with ratatui...
48//! // widget.render(area, &mut frame.buffer_mut());
49//! ```
50//!
51//! # Building Diffs Programmatically
52//!
53//! ```rust
54//! use ratatui_toolkit::code_diff::{CodeDiff, DiffHunk, DiffLine, DiffConfig};
55//!
56//! let mut diff = CodeDiff::new()
57//!     .with_file_path("src/lib.rs")
58//!     .with_config(DiffConfig::new());
59//!
60//! let mut hunk = DiffHunk::new(1, 2, 1, 3);
61//! hunk.add_line(DiffLine::context("fn main() {", 1, 1));
62//! hunk.add_line(DiffLine::removed("    old_code();", 2));
63//! hunk.add_line(DiffLine::added("    new_code();", 2));
64//! hunk.add_line(DiffLine::added("    extra_code();", 3));
65//! hunk.add_line(DiffLine::context("}", 3, 4));
66//!
67//! diff.add_hunk(hunk);
68//! ```
69
70// Core modules
71pub mod extensions;
72pub mod foundation;
73pub mod widget;
74
75// Backwards-compatible module re-exports
76pub use extensions::file_tree as diff_file_tree;
77pub use foundation::diff_config;
78pub use foundation::diff_hunk;
79pub use foundation::diff_line;
80pub use foundation::enums;
81pub use foundation::helpers;
82pub use widget as code_diff;
83
84// Re-export main types at the module level
85pub use foundation::diff_config::DiffConfig;
86pub use foundation::diff_hunk::DiffHunk;
87pub use foundation::diff_line::DiffLine;
88pub use foundation::enums::{DiffLineKind, DiffStyle};
89pub use widget::CodeDiff;