Description: use older minor version of crate imara-diff This essentially reverts upstream git commits 37fe42d and 205e7ec. Author: Jonas Smedegaard Bug-Debian: https://bugs.debian.org/1116060 Forwarded: not-needed Last-Update: 2025-12-08 --- This patch header follows DEP-3: http://dep.debian.net/deps/dep3/ --- a/helix-core/Cargo.toml +++ b/helix-core/Cargo.toml @@ -45,7 +45,7 @@ serde_json = "1.0" toml.workspace = true -imara-diff = "0.2.0" +imara-diff = "0.1.8" encoding_rs = "0.8" chrono = { version = "0.4", default-features = false, features = ["alloc", "std"] } --- a/helix-vcs/Cargo.toml +++ b/helix-vcs/Cargo.toml @@ -18,7 +18,7 @@ arc-swap.workspace = true gix = { version = "0.86.0", features = ["attributes", "status", "max-performance", "sha1"], default-features = false, optional = true } -imara-diff = "0.2.0" +imara-diff = "0.1.8" anyhow = "1" log = "0.4" --- a/helix-core/src/diff.rs +++ b/helix-core/src/diff.rs @@ -1,22 +1,51 @@ use std::ops::Range; use std::time::Instant; -use imara_diff::{Algorithm, Diff, Hunk, IndentHeuristic, IndentLevel, InternedInput}; +use imara_diff::intern::InternedInput; +use imara_diff::Algorithm; use ropey::RopeSlice; use crate::{ChangeSet, Rope, Tendril, Transaction}; -struct ChangeSetBuilder<'a> { +/// A `imara_diff::Sink` that builds a `ChangeSet` for a character diff of a hunk +struct CharChangeSetBuilder<'a> { + res: &'a mut ChangeSet, + hunk: &'a InternedInput, + pos: u32, +} + +impl imara_diff::Sink for CharChangeSetBuilder<'_> { + type Out = (); + fn process_change(&mut self, before: Range, after: Range) { + self.res.retain((before.start - self.pos) as usize); + self.res.delete(before.len()); + self.pos = before.end; + + let res = self.hunk.after[after.start as usize..after.end as usize] + .iter() + .map(|&token| self.hunk.interner[token]) + .collect(); + + self.res.insert(res); + } + + fn finish(self) -> Self::Out { + self.res.retain(self.hunk.before.len() - self.pos as usize); + } +} + +struct LineChangeSetBuilder<'a> { res: ChangeSet, after: RopeSlice<'a>, file: &'a InternedInput>, current_hunk: InternedInput, - char_diff: Diff, pos: u32, } -impl ChangeSetBuilder<'_> { - fn process_hunk(&mut self, before: Range, after: Range) { +impl imara_diff::Sink for LineChangeSetBuilder<'_> { + type Out = ChangeSet; + + fn process_change(&mut self, before: Range, after: Range) { let len = self.file.before[self.pos as usize..before.start as usize] .iter() .map(|&it| self.file.interner[it].len_chars()) @@ -80,36 +109,25 @@ .flat_map(|&it| self.file.interner[it].chars()); self.current_hunk.update_before(hunk_before); self.current_hunk.update_after(hunk_after); + // the histogram heuristic does not work as well // for characters because the same characters often reoccur // use myer diff instead - self.char_diff.compute_with( + imara_diff::diff( Algorithm::Myers, - &self.current_hunk.before, - &self.current_hunk.after, - self.current_hunk.interner.num_tokens(), + &self.current_hunk, + CharChangeSetBuilder { + res: &mut self.res, + hunk: &self.current_hunk, + pos: 0, + }, ); - let mut pos = 0; - for Hunk { before, after } in self.char_diff.hunks() { - self.res.retain((before.start - pos) as usize); - self.res.delete(before.len()); - pos = before.end; - - let res = self.current_hunk.after[after.start as usize..after.end as usize] - .iter() - .map(|&token| self.current_hunk.interner[token]) - .collect(); - - self.res.insert(res); - } - self.res - .retain(self.current_hunk.before.len() - pos as usize); - // reuse allocations + self.current_hunk.clear(); } } - fn finish(mut self) -> ChangeSet { + fn finish(mut self) -> Self::Out { let len = self.file.before[self.pos as usize..] .iter() .map(|&it| self.file.interner[it].len_chars()) @@ -122,7 +140,7 @@ struct RopeLines<'a>(RopeSlice<'a>); -impl<'a> imara_diff::TokenSource for RopeLines<'a> { +impl<'a> imara_diff::intern::TokenSource for RopeLines<'a> { type Token = RopeSlice<'a>; type Tokenizer = ropey::iter::Lines<'a>; @@ -143,23 +161,15 @@ let res = ChangeSet::with_capacity(32); let after = after.slice(..); let file = InternedInput::new(RopeLines(before.slice(..)), RopeLines(after)); - let mut builder = ChangeSetBuilder { + let builder = LineChangeSetBuilder { res, file: &file, after, pos: 0, current_hunk: InternedInput::default(), - char_diff: Diff::default(), }; - let mut diff = Diff::compute(Algorithm::Histogram, &file); - diff.postprocess_with_heuristic( - &file, - IndentHeuristic::new(|token| IndentLevel::for_ascii_line(file.interner[token].bytes(), 4)), - ); - for hunk in diff.hunks() { - builder.process_hunk(hunk.before, hunk.after) - } - let res = builder.finish().into(); + + let res = imara_diff::diff(Algorithm::Histogram, &file, builder).into(); log::debug!( "rope diff took {}s", --- a/helix-vcs/src/diff.rs +++ b/helix-vcs/src/diff.rs @@ -1,4 +1,5 @@ use std::iter::Peekable; +use std::ops::Range; use std::sync::Arc; use helix_core::Rope; @@ -11,8 +12,6 @@ use crate::diff::worker::DiffWorker; -pub use imara_diff::Hunk; - mod line_cache; mod worker; @@ -54,8 +53,8 @@ let worker = DiffWorker { channel: receiver, diff: diff.clone(), + new_hunks: Vec::default(), diff_finished_notify: Arc::default(), - diff_alloc: imara_diff::Diff::default(), }; let handle = tokio::spawn(worker.run(diff_base, doc)); let differ = DiffHandle { @@ -123,6 +122,48 @@ // cap average line length to 128 for files with MAX_DIFF_LINES const MAX_DIFF_BYTES: usize = MAX_DIFF_LINES * 128; +/// A single change in a file potentially spanning multiple lines +/// Hunks produced by the differs are always ordered by their position +/// in the file and non-overlapping. +/// Specifically for any two hunks `x` and `y` the following properties hold: +/// +/// ``` no_compile +/// assert!(x.before.end <= y.before.start); +/// assert!(x.after.end <= y.after.start); +/// ``` +#[derive(PartialEq, Eq, Clone, Debug)] +pub struct Hunk { + pub before: Range, + pub after: Range, +} + +impl Hunk { + /// Can be used instead of `Option::None` for better performance + /// because lines larger then `i32::MAX` are not supported by `imara-diff` anyways. + /// Has some nice properties where it usually is not necessary to check for `None` separately: + /// Empty ranges fail contains checks and also fails smaller then checks. + pub const NONE: Hunk = Hunk { + before: u32::MAX..u32::MAX, + after: u32::MAX..u32::MAX, + }; + + /// Inverts a change so that `before` + pub fn invert(&self) -> Hunk { + Hunk { + before: self.after.clone(), + after: self.before.clone(), + } + } + + pub fn is_pure_insertion(&self) -> bool { + self.before.is_empty() + } + + pub fn is_pure_removal(&self) -> bool { + self.after.is_empty() + } +} + /// A list of changes in a file sorted in ascending /// non-overlapping order #[derive(Debug)] --- a/helix-vcs/src/diff/line_cache.rs +++ b/helix-vcs/src/diff/line_cache.rs @@ -13,7 +13,7 @@ use std::mem::transmute; use helix_core::{Rope, RopeSlice}; -use imara_diff::{InternedInput, Interner}; +use imara_diff::intern::{InternedInput, Interner}; use super::{MAX_DIFF_BYTES, MAX_DIFF_LINES}; --- a/helix-vcs/src/diff/worker.rs +++ b/helix-vcs/src/diff/worker.rs @@ -1,7 +1,9 @@ +use std::mem::swap; +use std::ops::Range; use std::sync::Arc; use helix_core::{Rope, RopeSlice}; -use imara_diff::{IndentHeuristic, IndentLevel, InternedInput}; +use imara_diff::intern::InternedInput; use parking_lot::RwLock; use tokio::sync::mpsc::UnboundedReceiver; use tokio::sync::Notify; @@ -12,6 +14,7 @@ }; use super::line_cache::InternedRopeLines; +use super::Hunk; #[cfg(test)] mod test; @@ -19,8 +22,8 @@ pub(super) struct DiffWorker { pub channel: UnboundedReceiver, pub diff: Arc>, + pub new_hunks: Vec, pub diff_finished_notify: Arc, - pub diff_alloc: imara_diff::Diff, } impl DiffWorker { @@ -73,26 +76,15 @@ let mut diff = self.diff.write(); diff.diff_base = diff_base; diff.doc = doc; - diff.hunks.clear(); - diff.hunks.extend(self.diff_alloc.hunks()); - drop(diff); + swap(&mut diff.hunks, &mut self.new_hunks); self.diff_finished_notify.notify_waiters(); + self.new_hunks.clear(); } fn perform_diff(&mut self, input: &InternedInput) { - self.diff_alloc.compute_with( - ALGORITHM, - &input.before, - &input.after, - input.interner.num_tokens(), - ); - self.diff_alloc.postprocess_with( - &input.before, - &input.after, - IndentHeuristic::new(|token| { - IndentLevel::for_ascii_line(input.interner[token].bytes(), 4) - }), - ); + imara_diff::diff(ALGORITHM, input, |before: Range, after: Range| { + self.new_hunks.push(Hunk { before, after }) + }) } }