MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / hunks_from_changed_lines

Method hunks_from_changed_lines

atomic-cli/src/commands/diff/helpers.rs:240–393  ·  view source on GitHub ↗

Group numbered changed lines into diff hunks with true file offsets. `changed` must be in file order. Changes separated by more than `2 * context` unchanged lines become separate hunks; closer changes merge with the gap emitted as context. Context line content is taken from `before_lines` (the file's recorded before-content); when it is `None`, hunks are emitted with zero context but still carry

(
        changed: &[NumberedLine],
        before_lines: Option<&[Vec<u8>]>,
        context: usize,
    )

Source from the content-addressed store, hash-verified

238 /// at boundary `p` is `@@ -p,0 +n,k @@`, a pure deletion is
239 /// `@@ -l,k +p,0 @@`, and context lines count toward both sides.
240 pub(super) fn hunks_from_changed_lines(
241 changed: &[NumberedLine],
242 before_lines: Option<&[Vec<u8>]>,
243 context: usize,
244 ) -> Vec<DiffHunk> {
245 let mut hunks = Vec::new();
246 if changed.is_empty() {
247 return hunks;
248 }
249
250 let old_len = before_lines.map(|l| l.len()).unwrap_or(0);
251 let ctx = if before_lines.is_some() { context } else { 0 };
252 let merge_gap = 2 * ctx as isize;
253
254 // 1. Group into runs of nearby changes, measured in old-file
255 // coordinates.
256 let mut runs: Vec<(usize, usize)> = Vec::new();
257 let mut run_start = 0usize;
258 let mut run_hi = changed[0].old_pos();
259 for (i, nl) in changed.iter().enumerate().skip(1) {
260 let lo = nl.old_pos();
261 // Unchanged lines between the run and this change. An addition
262 // sits at a boundary, so the gap is measured from the boundary
263 // itself; a removal occupies a line, hence the extra -1.
264 let gap = match nl.line.status {
265 LineStatus::Removed => lo as isize - run_hi as isize - 1,
266 _ => lo as isize - run_hi as isize,
267 };
268 if gap > merge_gap {
269 runs.push((run_start, i));
270 run_start = i;
271 }
272 run_hi = run_hi.max(nl.old_pos());
273 }
274 runs.push((run_start, changed.len()));
275
276 // 2. Emit one hunk per run.
277 for (start, end) in runs {
278 let lines = &changed[start..end];
279
280 let removed_count = lines.iter().filter(|l| l.line.is_removed()).count();
281 let added_count = lines.len() - removed_count;
282
283 let old_lo = lines.iter().map(|l| l.old_pos()).min().unwrap_or(0);
284 let old_hi = lines.iter().map(|l| l.old_pos()).max().unwrap_or(0);
285 let new_lo = lines.iter().map(|l| l.new_pos()).min().unwrap_or(0);
286
287 // Old-file range covered by the hunk, including context.
288 let (old_start, old_end, old_count) = if removed_count > 0 {
289 let s = old_lo.saturating_sub(ctx).max(1);
290 let e = if ctx > 0 {
291 old_hi.saturating_add(ctx).min(old_len)
292 } else {
293 old_hi
294 };
295 let e = e.max(s);
296 (s, e, e - s + 1)
297 } else {

Callers

nothing calls this directly

Calls 13

contextFunction · 0.85
skipMethod · 0.80
getMethod · 0.65
is_emptyMethod · 0.45
lenMethod · 0.45
old_posMethod · 0.45
iterMethod · 0.45
pushMethod · 0.45
countMethod · 0.45
is_removedMethod · 0.45
new_posMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected