use std::collections::BTreeSet;
use std::path::Path;
use vvv_core::RelPath;
use vvv_core::{Address, LanguageId, Position, Query};
use super::{Candidate, Graph, Namespace};
use crate::{EngineError, Match, Occurrence, ReferencesQuery};
#[derive(Debug, Clone, Copy)]
pub struct Declaration<'a> {
m: &'a Match,
}
impl<'a> Declaration<'a> {
pub fn of(m: &'a Match) -> Option<Self> {
m.symbol.as_ref().map(|_| Self { m })
}
pub fn addressable_in(
matches: &'a [Match],
language: &LanguageId,
semantics: &vvv_core::Semantics,
) -> Vec<Self> {
matches
.iter()
.filter(|m| &m.language == language)
.filter_map(Self::of)
.filter(|d| semantics.is_addressable(d.symbol().kind))
.collect()
}
pub fn site(&self) -> DeclarationSite {
DeclarationSite {
path: self.m.path.clone(),
start: self.m.start,
}
}
pub fn path(&self) -> &'a Path {
&self.m.path
}
pub fn language(&self) -> &'a LanguageId {
&self.m.language
}
fn symbol(&self) -> &'a vvv_core::Symbol {
self.m.symbol.as_ref().expect("a declaration has a symbol")
}
}
pub struct Target {
pub address: Address,
pub ns: Namespace,
pub others: Vec<Address>,
pub aliases: Vec<Address>,
}
impl Target {
pub fn judge(&self, candidate: &Candidate, name: &str) -> Result<Vec<Occurrence>, EngineError> {
let tokens = candidate.references(name)?;
if tokens.is_empty() {
return Ok(Vec::new());
}
let scope = candidate.scope(&self.ns)?;
Ok(tokens
.into_iter()
.map(|m| {
let reason = scope.classify(&m, name, &self.aliases, &self.others);
Occurrence::judged(m, reason)
})
.collect())
}
pub fn of(
graph: &mut Graph,
query: &ReferencesQuery,
addressable: &[Declaration<'_>],
) -> Result<Option<Target>, EngineError> {
let chosen = match addressable {
[] => return Ok(None),
[only] => only,
[first, ..] if query.declared_in.is_some() => first,
many => {
return Err(EngineError::AmbiguousSymbol {
name: query.name.clone(),
declarations: many.iter().map(Declaration::site).collect(),
});
}
};
let Some(ns) = graph.namespace(chosen.language()) else {
return Ok(None);
};
let Ok(module) = ns.address(chosen.path()) else {
return Ok(None);
};
let all = graph
.search(&Query::named(query.name.as_str()).in_language(chosen.language().clone()))?
.matches;
let others = Declaration::addressable_in(&all, chosen.language(), ns.semantics())
.into_iter()
.filter(|d| d.path() != chosen.path())
.filter_map(|d| ns.address(d.path()).ok())
.collect();
let address = module.join(query.name.as_str());
Ok(Some(Target {
aliases: vec![address.clone()],
address,
ns,
others,
}))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeclarationSite {
pub path: RelPath,
pub start: Position,
}
impl std::fmt::Display for DeclarationSite {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.path.display(), self.start.display())
}
}
pub struct Evidence {
pub declarations: Vec<Match>,
pub occurrences: Vec<Occurrence>,
pub ambiguous: BTreeSet<LanguageId>,
}