MCPcopy Create free account
hub / github.com/ChrisFeldmeier/OpenCodeRust / create_diff

Function create_diff

crates/opencode-tool/src/edit/tool.rs:400–428  ·  view source on GitHub ↗
(filepath: &str, old_content: &str, new_content: &str)

Source from the content-addressed store, hash-verified

398}
399
400fn create_diff(filepath: &str, old_content: &str, new_content: &str) -> String {
401 let old_lines: Vec<&str> = old_content.lines().collect();
402 let new_lines: Vec<&str> = new_content.lines().collect();
403
404 let mut diff = format!("--- {}\n+++ {}\n", filepath, filepath);
405
406 let mut old_idx = 0;
407 let mut new_idx = 0;
408
409 while old_idx < old_lines.len() || new_idx < new_lines.len() {
410 if old_idx >= old_lines.len() {
411 diff.push_str(&format!("+{}\n", new_lines[new_idx]));
412 new_idx += 1;
413 } else if new_idx >= new_lines.len() {
414 diff.push_str(&format!("-{}\n", old_lines[old_idx]));
415 old_idx += 1;
416 } else if old_lines[old_idx] == new_lines[new_idx] {
417 old_idx += 1;
418 new_idx += 1;
419 } else {
420 diff.push_str(&format!("-{}\n", old_lines[old_idx]));
421 diff.push_str(&format!("+{}\n", new_lines[new_idx]));
422 old_idx += 1;
423 new_idx += 1;
424 }
425 }
426
427 diff
428}
429
430fn normalize_line_endings(text: &str) -> String {
431 text.replace("\r\n", "\n")

Callers 1

executeMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected