use std::fs;
use anyhow::Result;
use prqlc::Error;
use crate::ProjectCompiled;
pub fn edit_source_file(
project: &ProjectCompiled,
decl_id: usize,
new_source: String,
) -> Result<()> {
let span = project.root_module.span_map.get(&decl_id);
let Some(span) = span else {
return Err(Error::new_simple(format!(
"cannot find where declaration {decl_id} came from"
))
.into());
};
let file_path = project.sources.get_path(span.source_id).unwrap().clone();
let mut source = project.sources.sources.get(&file_path).unwrap().clone();
source.replace_range(span.start..span.end, &new_source);
let mut file_path_full = project.sources.root.clone().unwrap();
file_path_full.extend(&file_path);
fs::write(file_path_full, &source)?;
Ok(())
}