use std::sync::LazyLock;
use sem_core::model::entity::SemanticEntity;
use sem_core::parser::plugins::create_default_registry;
use sem_core::parser::registry::ParserRegistry;
pub(crate) static REGISTRY: LazyLock<ParserRegistry> = LazyLock::new(create_default_registry);
pub(crate) fn is_supported(path: &str) -> bool {
REGISTRY.get_explicit_plugin(path).is_some()
}
pub(crate) fn entities_of(path: &str, content: &str) -> Vec<SemanticEntity> {
let Some(plugin) = REGISTRY.get_explicit_plugin(path) else {
return Vec::new();
};
top_level(plugin.extract_entities(content, path))
}
fn top_level(entities: Vec<SemanticEntity>) -> Vec<SemanticEntity> {
let spans: Vec<(usize, usize)> = entities
.iter()
.filter(|e| e.parent_id.is_none())
.map(|e| (e.start_line, e.end_line))
.collect();
let mut out: Vec<SemanticEntity> = entities
.into_iter()
.filter(|e| {
if e.parent_id.is_some() || e.name.is_empty() {
return false;
}
!spans
.iter()
.any(|&(s, t)| s < e.start_line && e.end_line <= t)
})
.collect();
out.sort_by_key(|e| e.start_line);
out
}