use std::ops::Range;
use std::path::Path;
use std::sync::OnceLock;
use ratatui::style::Color;
use ratatui::style::Modifier;
use ratatui::style::Style;
use syntect::easy::HighlightLines;
use syntect::highlighting::FontStyle;
use syntect::parsing::SyntaxReference;
use syntect::parsing::SyntaxSet;
use crate::git::DiffSection;
use crate::git::sanitize;
use crate::theme::Theme;
const MAX_ROWS: usize = 20_000;
const MAX_HIGHLIGHT_ROWS: usize = 8_000;
const MAX_HIGHLIGHT_LINE: usize = 1_000;
const MIN_SIMILARITY: f32 = 0.4;
const MAX_WORD_DIFF_TOKENS: usize = 400;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RowKind {
Hunk,
Context,
Added,
Removed,
Info,
Error,
Section,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Seg {
pub text: String,
pub style: Style,
pub emph: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Row {
pub kind: RowKind,
pub old: Option<u32>,
pub new: Option<u32>,
pub segs: Vec<Seg>,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct DiffView {
pub rows: Vec<Row>,
pub added: usize,
pub removed: usize,
pub max_line_no: u32,
pub max_width: usize,
}
impl DiffView {
pub fn number_width(&self) -> usize {
self.max_line_no.max(1).to_string().len()
}
pub fn gutter_width(&self) -> usize {
2 * self.number_width() + 4
}
pub fn error(message: &str) -> Self {
Self {
rows: vec![text_row(RowKind::Error, &format!("error: {message}"))],
..Self::default()
}
}
}
struct RawRow {
kind: RowKind,
old: Option<u32>,
new: Option<u32>,
text: String,
emph: Vec<Range<usize>>,
hunk_start: bool,
}
fn syntax_set() -> &'static SyntaxSet {
static SET: OnceLock<SyntaxSet> = OnceLock::new();
SET.get_or_init(two_face::syntax::extra_no_newlines)
}
fn find_syntax<'a>(set: &'a SyntaxSet, path: &str) -> Option<&'a SyntaxReference> {
let path = Path::new(path);
let name = path.file_name()?.to_str()?;
set
.find_syntax_by_extension(name)
.or_else(|| set.find_syntax_by_extension(path.extension()?.to_str()?))
.filter(|s| s.name != "Plain Text")
}
pub fn build(sections: &[DiffSection], path: &str, theme: &Theme) -> DiffView {
let mut raw = vec![];
let titled = sections.len() > 1 || sections.iter().any(|s| s.title.is_some());
for (i, section) in sections.iter().enumerate() {
if titled {
if i > 0 {
raw.push(raw_row(RowKind::Info, ""));
}
if let Some(title) = §ion.title {
raw.push(raw_row(RowKind::Section, title));
}
}
let before = raw.len();
parse(§ion.text, &mut raw);
if raw.len() == before {
raw.push(raw_row(RowKind::Info, "no textual changes"));
}
}
let truncated = raw.len().saturating_sub(MAX_ROWS);
raw.truncate(MAX_ROWS);
mark_word_changes(&mut raw);
let mut view = DiffView::default();
let set = syntax_set();
let syntax = (raw.len() <= MAX_HIGHLIGHT_ROWS)
.then(|| find_syntax(set, path))
.flatten();
let mut old_hl = syntax.map(|s| HighlightLines::new(s, &theme.syntax));
let mut new_hl = syntax.map(|s| HighlightLines::new(s, &theme.syntax));
for row in raw {
if row.hunk_start
&& let Some(s) = syntax
{
old_hl = Some(HighlightLines::new(s, &theme.syntax));
new_hl = Some(HighlightLines::new(s, &theme.syntax));
}
match row.kind {
RowKind::Added => view.added += 1,
RowKind::Removed => view.removed += 1,
_ => {}
}
view.max_line_no = view
.max_line_no
.max(row.old.unwrap_or(0))
.max(row.new.unwrap_or(0));
let segs = match row.kind {
RowKind::Context | RowKind::Added | RowKind::Removed => {
let highlighted = if row.text.len() > MAX_HIGHLIGHT_LINE {
None
} else {
let hl = match row.kind {
RowKind::Removed => old_hl.as_mut(),
RowKind::Added => new_hl.as_mut(),
_ => {
if let Some(old) = old_hl.as_mut() {
let _ = old.highlight_line(&row.text, set);
}
new_hl.as_mut()
}
};
hl.and_then(|h| h.highlight_line(&row.text, set).ok())
.map(|ranges| {
ranges
.into_iter()
.map(|(style, text)| (convert_style(style, theme), text))
.collect::<Vec<_>>()
})
};
let pieces = highlighted.unwrap_or_else(|| vec![(Style::default(), row.text.as_str())]);
split_emphasis(&pieces, &row.emph)
}
RowKind::Hunk => hunk_segs(&row.text),
_ => vec![Seg {
text: sanitize(&row.text),
style: Style::default(),
emph: false,
}],
};
let width: usize = segs
.iter()
.map(|s| unicode_width::UnicodeWidthStr::width(s.text.as_str()))
.sum();
view.max_width = view.max_width.max(width);
view.rows.push(Row {
kind: row.kind,
old: row.old,
new: row.new,
segs,
});
}
if truncated > 0 {
view.rows.push(text_row(
RowKind::Info,
&format!("… {truncated} more lines not shown"),
));
}
view
}
fn raw_row(kind: RowKind, text: &str) -> RawRow {
RawRow {
kind,
old: None,
new: None,
text: text.to_string(),
emph: vec![],
hunk_start: false,
}
}
fn text_row(kind: RowKind, text: &str) -> Row {
Row {
kind,
old: None,
new: None,
segs: vec![Seg {
text: sanitize(text),
style: Style::default(),
emph: false,
}],
}
}
fn hunk_segs(text: &str) -> Vec<Seg> {
let (range, context) = text.split_once('\0').unwrap_or((text, ""));
vec![
Seg {
text: sanitize(range),
style: Style::default(),
emph: false,
},
Seg {
text: sanitize(context.trim()),
style: Style::default(),
emph: false,
},
]
}
fn parse(text: &str, rows: &mut Vec<RawRow>) {
let mut in_hunk = false;
let mut prefix = 1;
let mut old = 0u32;
let mut new = 0u32;
for line in text.lines() {
if line.starts_with("diff ") {
in_hunk = false;
continue;
}
if line.starts_with("@@") {
let ats = line.chars().take_while(|&c| c == '@').count();
prefix = ats - 1;
let marker = "@".repeat(ats);
let body = &line[ats..];
let (ranges, context) = body.split_once(&*marker).unwrap_or((body, ""));
for token in ranges.split_whitespace() {
let start = token[1..]
.split(',')
.next()
.and_then(|n| n.parse::<u32>().ok())
.unwrap_or(1);
match token.as_bytes().first() {
Some(b'-') => old = start,
Some(b'+') => new = start,
_ => {}
}
}
let range = hunk_range(ranges.trim());
rows.push(RawRow {
hunk_start: true,
..raw_row(RowKind::Hunk, &format!("{range}\0{context}"))
});
in_hunk = true;
continue;
}
if !in_hunk {
if let Some(note) = header_note(line) {
rows.push(raw_row(RowKind::Info, ¬e));
}
continue;
}
if line.starts_with('\\') {
rows.push(raw_row(RowKind::Info, "no newline at end of file"));
continue;
}
let split = line
.char_indices()
.nth(prefix)
.map_or(line.len(), |(i, _)| i);
let (marks, content) = line.split_at(split);
let combined = prefix > 1;
let kind = if marks.contains('+') {
RowKind::Added
} else if marks.contains('-') {
RowKind::Removed
} else {
RowKind::Context
};
let (o, n) = match kind {
RowKind::Added => (None, Some(new)),
RowKind::Removed => (Some(old), None),
_ => (Some(old), Some(new)),
};
if kind != RowKind::Added {
old += 1;
}
if kind != RowKind::Removed {
new += 1;
}
rows.push(RawRow {
old: if combined { None } else { o },
new: n,
..raw_row(kind, content)
});
}
}
fn hunk_range(ranges: &str) -> String {
let span = |token: &str| -> Option<String> {
let mut parts = token[1..].split(',');
let start: u32 = parts.next()?.parse().ok()?;
let len: u32 = parts.next().map_or(Some(1), |n| n.parse().ok())?;
Some(match len {
0 => "∅".to_string(),
1 => start.to_string(),
_ => format!("{start}–{}", start + len - 1),
})
};
let tokens: Vec<&str> = ranges.split_whitespace().collect();
let old = tokens
.iter()
.find(|t| t.starts_with('-'))
.and_then(|t| span(t));
let new = tokens
.iter()
.find(|t| t.starts_with('+'))
.and_then(|t| span(t));
match (old, new) {
(Some(o), Some(n)) if o == n => format!("lines {n}"),
(Some(o), Some(n)) => format!("lines {o} → {n}"),
_ => ranges.to_string(),
}
}
fn header_note(line: &str) -> Option<String> {
let note = if line.starts_with("new file mode") {
"new file".to_string()
} else if line.starts_with("deleted file mode") {
"file deleted".to_string()
} else if let Some(mode) = line.strip_prefix("new mode ") {
format!("file mode changed to {mode}")
} else if let Some(from) = line.strip_prefix("rename from ") {
format!("renamed from {from}")
} else if let Some(from) = line.strip_prefix("copy from ") {
format!("copied from {from}")
} else if line.starts_with("Binary files") {
"binary file changed".to_string()
} else if line.starts_with("Submodule") {
line.to_string()
} else {
return None;
};
Some(note)
}
fn mark_word_changes(rows: &mut [RawRow]) {
let mut i = 0;
while i < rows.len() {
if rows[i].kind != RowKind::Removed {
i += 1;
continue;
}
let removed_start = i;
while i < rows.len() && rows[i].kind == RowKind::Removed {
i += 1;
}
let added_start = i;
while i < rows.len() && rows[i].kind == RowKind::Added {
i += 1;
}
let pairs = (added_start - removed_start).min(i - added_start);
for k in 0..pairs {
let (a, b) = (removed_start + k, added_start + k);
if let Some((old, new)) = changed_ranges(&rows[a].text, &rows[b].text) {
rows[a].emph = old;
rows[b].emph = new;
}
}
}
}
fn tokens(text: &str) -> Vec<Range<usize>> {
let mut out = vec![];
let mut start = None;
for (i, c) in text.char_indices() {
let word = c.is_alphanumeric() || c == '_';
match (word, start) {
(true, None) => start = Some(i),
(true, Some(_)) => {}
(false, s) => {
if let Some(s) = s {
out.push(s..i);
start = None;
}
out.push(i..i + c.len_utf8());
}
}
}
if let Some(s) = start {
out.push(s..text.len());
}
out
}
pub type Ranges = Vec<Range<usize>>;
pub fn changed_ranges(old: &str, new: &str) -> Option<(Ranges, Ranges)> {
let ot = tokens(old);
let nt = tokens(new);
if ot.len() > MAX_WORD_DIFF_TOKENS || nt.len() > MAX_WORD_DIFF_TOKENS {
return None;
}
let same = |i: usize, j: usize| old[ot[i].clone()] == new[nt[j].clone()];
let (n, m) = (ot.len(), nt.len());
let mut lcs = vec![vec![0u16; m + 1]; n + 1];
for i in (0..n).rev() {
for j in (0..m).rev() {
lcs[i][j] = if same(i, j) {
lcs[i + 1][j + 1] + 1
} else {
lcs[i + 1][j].max(lcs[i][j + 1])
};
}
}
let mut old_changed = vec![];
let mut new_changed = vec![];
let mut common = 0;
let (mut i, mut j) = (0, 0);
while i < n || j < m {
if i < n && j < m && same(i, j) {
common += ot[i].len() * 2;
i += 1;
j += 1;
} else if j == m || (i < n && lcs[i + 1][j] >= lcs[i][j + 1]) {
old_changed.push(i);
i += 1;
} else {
new_changed.push(j);
j += 1;
}
}
let total = old.len() + new.len();
if total == 0 || (old_changed.is_empty() && new_changed.is_empty()) {
return None;
}
if (common as f32 / total as f32) < MIN_SIMILARITY {
return None;
}
Some((
merge_tokens(old, &ot, &old_changed),
merge_tokens(new, &nt, &new_changed),
))
}
fn merge_tokens(text: &str, tokens: &[Range<usize>], changed: &[usize]) -> Vec<Range<usize>> {
let mut ranges: Vec<Range<usize>> = vec![];
let mut last_index: Option<usize> = None;
for &index in changed {
let token = tokens[index].clone();
let bridge = match (last_index, ranges.last()) {
(Some(last), Some(_)) if index == last + 1 => true,
(Some(last), Some(_)) if index == last + 2 => text[tokens[last + 1].clone()]
.chars()
.all(char::is_whitespace),
_ => false,
};
match ranges.last_mut() {
Some(range) if bridge => range.end = token.end,
_ => ranges.push(token),
}
last_index = Some(index);
}
ranges
}
fn convert_style(style: syntect::highlighting::Style, theme: &Theme) -> Style {
let mut out = Style::default();
if theme.syntax.settings.foreground != Some(style.foreground) {
out = out.fg(theme.color(style.foreground));
} else {
out = out.fg(Color::Reset);
}
if style.font_style.contains(FontStyle::BOLD) {
out = out.add_modifier(Modifier::BOLD);
}
if style.font_style.contains(FontStyle::ITALIC) {
out = out.add_modifier(Modifier::ITALIC);
}
out
}
fn split_emphasis(pieces: &[(Style, &str)], emph: &[Range<usize>]) -> Vec<Seg> {
let mut segs: Vec<Seg> = vec![];
let mut push = |text: &str, style: Style, emph: bool| {
if text.is_empty() {
return;
}
let text = sanitize(text);
match segs.last_mut() {
Some(last) if last.style == style && last.emph == emph => last.text.push_str(&text),
_ => segs.push(Seg { text, style, emph }),
}
};
let mut offset = 0;
for (style, text) in pieces {
let (start, end) = (offset, offset + text.len());
offset = end;
let mut cuts = vec![start];
for r in emph {
for b in [r.start, r.end] {
if b > start && b < end {
cuts.push(b);
}
}
}
cuts.push(end);
cuts.sort_unstable();
for w in cuts.windows(2) {
let (a, b) = (w[0], w[1]);
let emphasized = emph.iter().any(|r| r.start <= a && b <= r.end && a < b);
push(&text[a - start..b - start], *style, emphasized);
}
}
segs
}