| 1 |
Description: use older minor version of crate imara-diff |
| 2 |
This essentially reverts upstream git commits 37fe42d and 205e7ec. |
| 3 |
Author: Jonas Smedegaard <[email protected]> |
| 4 |
Bug-Debian: https://bugs.debian.org/1116060 |
| 5 |
Forwarded: not-needed |
| 6 |
Last-Update: 2025-12-08 |
| 7 |
--- |
| 8 |
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/ |
| 9 |
--- a/helix-core/Cargo.toml |
| 10 |
+++ b/helix-core/Cargo.toml |
| 11 |
@@ -45,7 +45,7 @@ |
| 12 |
serde_json = "1.0" |
| 13 |
toml.workspace = true |
| 14 |
|
| 15 |
|
| 16 |
+imara-diff = "0.1.8" |
| 17 |
encoding_rs = "0.8" |
| 18 |
|
| 19 |
chrono = { version = "0.4", default-features = false, features = ["alloc", "std"] } |
| 20 |
--- a/helix-vcs/Cargo.toml |
| 21 |
+++ b/helix-vcs/Cargo.toml |
| 22 |
@@ -18,7 +18,7 @@ |
| 23 |
arc-swap.workspace = true |
| 24 |
|
| 25 |
gix = { version = ">= 0.83.0, <= 0.84", features = ["attributes", "status", "max-performance", "sha1"], default-features = false, optional = true } |
| 26 |
|
| 27 |
+imara-diff = "0.1.8" |
| 28 |
anyhow = "1" |
| 29 |
|
| 30 |
log = "0.4" |
| 31 |
--- a/helix-core/src/diff.rs |
| 32 |
+++ b/helix-core/src/diff.rs |
| 33 |
@@ -1,22 +1,51 @@ |
| 34 |
use std::ops::Range; |
| 35 |
use std::time::Instant; |
| 36 |
|
| 37 |
|
| 38 |
+use imara_diff::intern::InternedInput; |
| 39 |
+use imara_diff::Algorithm; |
| 40 |
use ropey::RopeSlice; |
| 41 |
|
| 42 |
use crate::{ChangeSet, Rope, Tendril, Transaction}; |
| 43 |
|
| 44 |
|
| 45 |
+/// A `imara_diff::Sink` that builds a `ChangeSet` for a character diff of a hunk |
| 46 |
+struct CharChangeSetBuilder<'a> { |
| 47 |
+ res: &'a mut ChangeSet, |
| 48 |
+ hunk: &'a InternedInput<char>, |
| 49 |
+ pos: u32, |
| 50 |
+} |
| 51 |
+ |
| 52 |
+impl imara_diff::Sink for CharChangeSetBuilder<'_> { |
| 53 |
+ type Out = (); |
| 54 |
+ fn process_change(&mut self, before: Range<u32>, after: Range<u32>) { |
| 55 |
+ self.res.retain((before.start - self.pos) as usize); |
| 56 |
+ self.res.delete(before.len()); |
| 57 |
+ self.pos = before.end; |
| 58 |
+ |
| 59 |
+ let res = self.hunk.after[after.start as usize..after.end as usize] |
| 60 |
+ .iter() |
| 61 |
+ .map(|&token| self.hunk.interner[token]) |
| 62 |
+ .collect(); |
| 63 |
+ |
| 64 |
+ self.res.insert(res); |
| 65 |
+ } |
| 66 |
+ |
| 67 |
+ fn finish(self) -> Self::Out { |
| 68 |
+ self.res.retain(self.hunk.before.len() - self.pos as usize); |
| 69 |
+ } |
| 70 |
+} |
| 71 |
+ |
| 72 |
+struct LineChangeSetBuilder<'a> { |
| 73 |
res: ChangeSet, |
| 74 |
after: RopeSlice<'a>, |
| 75 |
file: &'a InternedInput<RopeSlice<'a>>, |
| 76 |
current_hunk: InternedInput<char>, |
| 77 |
|
| 78 |
pos: u32, |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
+impl imara_diff::Sink for LineChangeSetBuilder<'_> { |
| 84 |
+ type Out = ChangeSet; |
| 85 |
+ |
| 86 |
+ fn process_change(&mut self, before: Range<u32>, after: Range<u32>) { |
| 87 |
let len = self.file.before[self.pos as usize..before.start as usize] |
| 88 |
.iter() |
| 89 |
.map(|&it| self.file.interner[it].len_chars()) |
| 90 |
@@ -80,36 +109,25 @@ |
| 91 |
.flat_map(|&it| self.file.interner[it].chars()); |
| 92 |
self.current_hunk.update_before(hunk_before); |
| 93 |
self.current_hunk.update_after(hunk_after); |
| 94 |
+ |
| 95 |
// the histogram heuristic does not work as well |
| 96 |
// for characters because the same characters often reoccur |
| 97 |
// use myer diff instead |
| 98 |
|
| 99 |
+ imara_diff::diff( |
| 100 |
Algorithm::Myers, |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
+ &self.current_hunk, |
| 105 |
+ CharChangeSetBuilder { |
| 106 |
+ res: &mut self.res, |
| 107 |
+ hunk: &self.current_hunk, |
| 108 |
+ pos: 0, |
| 109 |
+ }, |
| 110 |
); |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
+ |
| 128 |
self.current_hunk.clear(); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
+ fn finish(mut self) -> Self::Out { |
| 134 |
let len = self.file.before[self.pos as usize..] |
| 135 |
.iter() |
| 136 |
.map(|&it| self.file.interner[it].len_chars()) |
| 137 |
@@ -122,7 +140,7 @@ |
| 138 |
|
| 139 |
struct RopeLines<'a>(RopeSlice<'a>); |
| 140 |
|
| 141 |
|
| 142 |
+impl<'a> imara_diff::intern::TokenSource for RopeLines<'a> { |
| 143 |
type Token = RopeSlice<'a>; |
| 144 |
type Tokenizer = ropey::iter::Lines<'a>; |
| 145 |
|
| 146 |
@@ -143,23 +161,15 @@ |
| 147 |
let res = ChangeSet::with_capacity(32); |
| 148 |
let after = after.slice(..); |
| 149 |
let file = InternedInput::new(RopeLines(before.slice(..)), RopeLines(after)); |
| 150 |
|
| 151 |
+ let builder = LineChangeSetBuilder { |
| 152 |
res, |
| 153 |
file: &file, |
| 154 |
after, |
| 155 |
pos: 0, |
| 156 |
current_hunk: InternedInput::default(), |
| 157 |
|
| 158 |
}; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
+ |
| 169 |
+ let res = imara_diff::diff(Algorithm::Histogram, &file, builder).into(); |
| 170 |
|
| 171 |
log::debug!( |
| 172 |
"rope diff took {}s", |
| 173 |
--- a/helix-vcs/src/diff.rs |
| 174 |
+++ b/helix-vcs/src/diff.rs |
| 175 |
@@ -1,4 +1,5 @@ |
| 176 |
use std::iter::Peekable; |
| 177 |
+use std::ops::Range; |
| 178 |
use std::sync::Arc; |
| 179 |
|
| 180 |
use helix_core::Rope; |
| 181 |
@@ -11,8 +12,6 @@ |
| 182 |
|
| 183 |
use crate::diff::worker::DiffWorker; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
mod line_cache; |
| 188 |
mod worker; |
| 189 |
|
| 190 |
@@ -54,8 +53,8 @@ |
| 191 |
let worker = DiffWorker { |
| 192 |
channel: receiver, |
| 193 |
diff: diff.clone(), |
| 194 |
+ new_hunks: Vec::default(), |
| 195 |
diff_finished_notify: Arc::default(), |
| 196 |
|
| 197 |
}; |
| 198 |
let handle = tokio::spawn(worker.run(diff_base, doc)); |
| 199 |
let differ = DiffHandle { |
| 200 |
@@ -123,6 +122,48 @@ |
| 201 |
// cap average line length to 128 for files with MAX_DIFF_LINES |
| 202 |
const MAX_DIFF_BYTES: usize = MAX_DIFF_LINES * 128; |
| 203 |
|
| 204 |
+/// A single change in a file potentially spanning multiple lines |
| 205 |
+/// Hunks produced by the differs are always ordered by their position |
| 206 |
+/// in the file and non-overlapping. |
| 207 |
+/// Specifically for any two hunks `x` and `y` the following properties hold: |
| 208 |
+/// |
| 209 |
+/// ``` no_compile |
| 210 |
+/// assert!(x.before.end <= y.before.start); |
| 211 |
+/// assert!(x.after.end <= y.after.start); |
| 212 |
+/// ``` |
| 213 |
+#[derive(PartialEq, Eq, Clone, Debug)] |
| 214 |
+pub struct Hunk { |
| 215 |
+ pub before: Range<u32>, |
| 216 |
+ pub after: Range<u32>, |
| 217 |
+} |
| 218 |
+ |
| 219 |
+impl Hunk { |
| 220 |
+ /// Can be used instead of `Option::None` for better performance |
| 221 |
+ /// because lines larger then `i32::MAX` are not supported by `imara-diff` anyways. |
| 222 |
+ /// Has some nice properties where it usually is not necessary to check for `None` separately: |
| 223 |
+ /// Empty ranges fail contains checks and also fails smaller then checks. |
| 224 |
+ pub const NONE: Hunk = Hunk { |
| 225 |
+ before: u32::MAX..u32::MAX, |
| 226 |
+ after: u32::MAX..u32::MAX, |
| 227 |
+ }; |
| 228 |
+ |
| 229 |
+ /// Inverts a change so that `before` |
| 230 |
+ pub fn invert(&self) -> Hunk { |
| 231 |
+ Hunk { |
| 232 |
+ before: self.after.clone(), |
| 233 |
+ after: self.before.clone(), |
| 234 |
+ } |
| 235 |
+ } |
| 236 |
+ |
| 237 |
+ pub fn is_pure_insertion(&self) -> bool { |
| 238 |
+ self.before.is_empty() |
| 239 |
+ } |
| 240 |
+ |
| 241 |
+ pub fn is_pure_removal(&self) -> bool { |
| 242 |
+ self.after.is_empty() |
| 243 |
+ } |
| 244 |
+} |
| 245 |
+ |
| 246 |
/// A list of changes in a file sorted in ascending |
| 247 |
/// non-overlapping order |
| 248 |
#[derive(Debug)] |
| 249 |
--- a/helix-vcs/src/diff/line_cache.rs |
| 250 |
+++ b/helix-vcs/src/diff/line_cache.rs |
| 251 |
@@ -13,7 +13,7 @@ |
| 252 |
use std::mem::transmute; |
| 253 |
|
| 254 |
use helix_core::{Rope, RopeSlice}; |
| 255 |
|
| 256 |
+use imara_diff::intern::{InternedInput, Interner}; |
| 257 |
|
| 258 |
use super::{MAX_DIFF_BYTES, MAX_DIFF_LINES}; |
| 259 |
|
| 260 |
--- a/helix-vcs/src/diff/worker.rs |
| 261 |
+++ b/helix-vcs/src/diff/worker.rs |
| 262 |
@@ -1,7 +1,9 @@ |
| 263 |
+use std::mem::swap; |
| 264 |
+use std::ops::Range; |
| 265 |
use std::sync::Arc; |
| 266 |
|
| 267 |
use helix_core::{Rope, RopeSlice}; |
| 268 |
|
| 269 |
+use imara_diff::intern::InternedInput; |
| 270 |
use parking_lot::RwLock; |
| 271 |
use tokio::sync::mpsc::UnboundedReceiver; |
| 272 |
use tokio::sync::Notify; |
| 273 |
@@ -12,6 +14,7 @@ |
| 274 |
}; |
| 275 |
|
| 276 |
use super::line_cache::InternedRopeLines; |
| 277 |
+use super::Hunk; |
| 278 |
|
| 279 |
#[cfg(test)] |
| 280 |
mod test; |
| 281 |
@@ -19,8 +22,8 @@ |
| 282 |
pub(super) struct DiffWorker { |
| 283 |
pub channel: UnboundedReceiver<Event>, |
| 284 |
pub diff: Arc<RwLock<DiffInner>>, |
| 285 |
+ pub new_hunks: Vec<Hunk>, |
| 286 |
pub diff_finished_notify: Arc<Notify>, |
| 287 |
|
| 288 |
} |
| 289 |
|
| 290 |
impl DiffWorker { |
| 291 |
@@ -73,26 +76,15 @@ |
| 292 |
let mut diff = self.diff.write(); |
| 293 |
diff.diff_base = diff_base; |
| 294 |
diff.doc = doc; |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
+ swap(&mut diff.hunks, &mut self.new_hunks); |
| 299 |
self.diff_finished_notify.notify_waiters(); |
| 300 |
+ self.new_hunks.clear(); |
| 301 |
} |
| 302 |
|
| 303 |
fn perform_diff(&mut self, input: &InternedInput<RopeSlice>) { |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
+ imara_diff::diff(ALGORITHM, input, |before: Range<u32>, after: Range<u32>| { |
| 318 |
+ self.new_hunks.push(Hunk { before, after }) |
| 319 |
+ }) |
| 320 |
} |
| 321 |
} |
| 322 |
|