use crate::graph::extractor::ScanCtx;
use brokk_bifrost_core::analyzer::Range;
use brokk_bifrost_core::analyzer::usages::common::{
SNIPPET_CONTEXT_LINES, reclassify_self_receiver_hit_at, usage_hit,
};
use brokk_bifrost_core::text_utils::{find_line_index_for_offset, trimmed_snippet_around_line};
use tree_sitter::Node;
pub fn record_hit(node: Node<'_>, ctx: &mut ScanCtx<'_>) {
if let Some(hit) = build_hit(node, ctx) {
ctx.hits.insert(hit);
}
}
pub fn record_unproven_hit(node: Node<'_>, ctx: &mut ScanCtx<'_>) {
if let Some(hit) = build_hit(node, ctx) {
ctx.unproven_hits.insert(hit.into_unproven());
}
}
pub fn record_self_receiver_hit(node: Node<'_>, ctx: &mut ScanCtx<'_>) {
record_hit(node, ctx);
reclassify_self_receiver_hit_at(ctx.hits, ctx.file, node.start_byte(), node.end_byte());
}
pub fn record_import_hit(node: Node<'_>, ctx: &mut ScanCtx<'_>) {
if let Some(hit) = build_hit(node, ctx) {
ctx.hits.insert(hit.into_import());
}
}
fn build_hit(
node: Node<'_>,
ctx: &mut ScanCtx<'_>,
) -> Option<brokk_bifrost_core::analyzer::usages::model::UsageHit> {
let start_byte = node.start_byte();
let end_byte = node.end_byte();
if start_byte >= end_byte {
return None;
}
let line_idx = find_line_index_for_offset(ctx.line_starts, start_byte);
let snippet =
trimmed_snippet_around_line(ctx.source, ctx.line_starts, line_idx, SNIPPET_CONTEXT_LINES);
let range = Range {
start_byte,
end_byte,
start_line: line_idx,
end_line: line_idx,
};
let enclosing = ctx.graph.index.enclosing_code_unit(ctx.file, &range)?;
Some(usage_hit(
ctx.file, line_idx, start_byte, end_byte, enclosing, snippet,
))
}