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

Function coalesce_replace

atomic-core/src/merge/three_way.rs:238–265  ·  view source on GitHub ↗

Turn adjacent Delete / Insert pairs at the same position into Replace.

(ops: &mut Vec<EditOp>)

Source from the content-addressed store, hash-verified

236
237/// Turn adjacent Delete / Insert pairs at the same position into Replace.
238fn coalesce_replace(ops: &mut Vec<EditOp>) {
239 let mut i = 0;
240 while i + 1 < ops.len() {
241 let is_pair = {
242 if let (EditOp::Delete(del_idx), EditOp::Insert(ins_after, _)) = (&ops[i], &ops[i + 1])
243 {
244 // The insert sits right after the deleted index.
245 *ins_after == *del_idx || (*del_idx == 0 && *ins_after == usize::MAX)
246 } else {
247 false
248 }
249 };
250
251 if is_pair {
252 let content = match &ops[i + 1] {
253 EditOp::Insert(_, c) => c.clone(),
254 _ => unreachable!(),
255 };
256 let idx = match &ops[i] {
257 EditOp::Delete(idx) => *idx,
258 _ => unreachable!(),
259 };
260 ops[i] = EditOp::Replace(idx, content);
261 ops.remove(i + 1);
262 }
263 i += 1;
264 }
265}
266
267// ---------------------------------------------------------------------------
268// Helpers: analyse edit scripts

Callers 1

diff_tokensFunction · 0.85

Calls 3

removeMethod · 0.65
lenMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected