use std::io::Write;
use crate::error::Result;
use crate::file_refs;
use crate::store::{g_str, IntoParam};
use crate::Ctx;
pub fn run(ctx: &Ctx, id: &str, refs: bool) -> Result<()> {
let prj = ctx.open()?;
let (mut store, _) = ctx.load(&prj)?;
let dkey = store.dkey_of(id)?;
let relpath = store
.scalar(
"SELECT path FROM docs WHERE dkey = $1",
vec![dkey.into_param()],
)?
.as_ref()
.and_then(g_str)
.unwrap_or_default();
let path = prj.base.join(relpath);
let text = std::fs::read_to_string(&path)?;
print!("{text}");
std::io::stdout().flush()?;
if refs {
if !text.ends_with('\n') {
println!();
}
let hits = file_refs::scan(&prj, &[id]);
println!("\n--- file references ---");
if hits.is_empty() {
println!("(none)");
} else {
for h in &hits {
println!("{}:{}: {}", h.path.display(), h.line, h.text);
}
}
}
Ok(())
}