use super::*;
use crate::diff::{format_colored_diff, format_unified_diff, should_use_color};
pub fn generate_preview_diff(
file_path: &Path,
original_content: &str,
modified_content: &str,
) -> String {
format_unified_diff(
original_content,
modified_content,
&file_path.display().to_string(),
3, )
}
pub fn generate_colored_preview(
file_path: &Path,
original_content: &str,
modified_content: &str,
) -> String {
let use_color = should_use_color();
if use_color {
format_colored_diff(original_content, modified_content, use_color)
} else {
format_unified_diff(
original_content,
modified_content,
&file_path.display().to_string(),
3,
)
}
}
pub fn simulate_replacements_content(
content: &str,
references: &[ReferenceFact],
_old_name: &str,
new_name: &str,
) -> Result<String> {
let content_bytes = content.as_bytes();
let new_name_bytes = new_name.as_bytes();
let mut current_content = content_bytes.to_vec();
for reference in references {
match replace_at_span(¤t_content, reference, new_name_bytes) {
Ok(new_content) => {
current_content = new_content;
}
Err(e) => {
return Err(SpliceError::Other(format!(
"Failed to simulate replacement at {}..{}: {}",
reference.byte_start, reference.byte_end, e
)));
}
}
}
String::from_utf8(current_content).map_err(|e| SpliceError::InvalidUtf8 {
file: PathBuf::from("<preview>"),
source: e.utf8_error(),
})
}