use smol_str::SmolStr;
use super::*;
#[derive(Debug, Clone, Default)]
pub struct MacroEnvironment {
pub(crate) name: String,
pub(crate) candidates: Vec<MacroCandidate>,
}
impl MacroEnvironment {
pub fn at(&self, offset: usize) -> Known<&MacroCandidate> {
let in_force: Vec<&MacroCandidate> = self.in_force(offset).collect();
let best = last(
in_force
.iter()
.copied()
.filter(|candidate| !candidate.is_conditional()),
)
.or_else(|| last(in_force.iter().copied()));
let Some(best) = best else {
return Known::Unknown(UnknownReason::NotDeclaredHere(Box::from(
self.name.as_str(),
)));
};
if best.is_conditional() {
return Known::Unknown(UnknownReason::ConditionalCompilation);
}
self.as_answer(best)
}
pub fn is_a_macro_at(&self, offset: usize) -> Known<&MacroCandidate> {
let in_force: Vec<&MacroCandidate> = self.in_force(offset).collect();
let Some(best) = last(
in_force
.iter()
.copied()
.filter(|candidate| candidate.is_certain_about_the_name()),
) else {
return if in_force.is_empty() {
Known::Unknown(UnknownReason::NotDeclaredHere(Box::from(
self.name.as_str(),
)))
} else {
Known::Unknown(UnknownReason::ConditionalCompilation)
};
};
if best.is_definition() {
return Known::Yes(best);
}
let overturned = in_force
.iter()
.any(|candidate| candidate.position > best.position && candidate.is_definition());
if overturned {
return Known::Unknown(UnknownReason::ConditionalCompilation);
}
Known::Unknown(UnknownReason::UndefinedHere(Box::from(self.name.as_str())))
}
pub fn definitions_in_force(&self, offset: usize) -> Vec<&MacroCandidate> {
self.in_force(offset)
.filter(|candidate| candidate.is_definition())
.collect()
}
pub(crate) fn in_force(&self, offset: usize) -> impl Iterator<Item = &MacroCandidate> {
self.candidates
.iter()
.filter(move |candidate| candidate.position[0] <= offset)
}
pub(crate) fn as_answer<'a>(&self, best: &'a MacroCandidate) -> Known<&'a MacroCandidate> {
if best.is_definition() {
return Known::Yes(best);
}
Known::Unknown(UnknownReason::UndefinedHere(Box::from(self.name.as_str())))
}
pub fn is_empty(&self) -> bool {
self.candidates.is_empty()
}
}
pub(crate) fn last<'a>(
candidates: impl Iterator<Item = &'a MacroCandidate>,
) -> Option<&'a MacroCandidate> {
candidates.max_by(|one, other| one.position.cmp(&other.position))
}
#[derive(Debug, Clone)]
pub struct MacroCandidate {
pub(crate) position: Vec<usize>,
pub(crate) file: PathBuf,
pub(crate) fact: MacroFact,
pub(crate) path_conditional: bool,
pub(crate) region: FactReach,
}
impl MacroCandidate {
pub fn file(&self) -> &Path {
&self.file
}
pub fn fact(&self) -> &MacroFact {
&self.fact
}
pub fn is_definition(&self) -> bool {
self.fact.kind.is_definition()
}
pub fn is_conditional(&self) -> bool {
self.path_conditional || self.region == FactReach::Unknown
}
pub fn is_certain_about_the_name(&self) -> bool {
!self.path_conditional && (self.fact.settles_the_name || self.region == FactReach::Active)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VisibleDeclaration<'a> {
pub file: PathBuf,
pub fact: &'a DeclFact,
pub visibility: IncludeVisibility,
}
pub(crate) struct ProjectDeclaration {
pub(crate) file: PathBuf,
pub(crate) fact: DeclFact,
pub(crate) start_offset: usize,
}
impl ProjectDeclaration {
pub(crate) fn of(found: &VisibleDeclaration<'_>) -> ProjectDeclaration {
ProjectDeclaration {
file: found.file.clone(),
fact: found.fact.clone(),
start_offset: found.fact.name_range.start_offset,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct IndexedKind {
pub kind: DeclKind,
pub scope: Option<Box<str>>,
pub enumerator: bool,
pub template: bool,
pub constructor: bool,
}
impl IndexedKind {
pub(crate) fn of(found: &DeclFact) -> IndexedKind {
IndexedKind {
kind: found.kind,
scope: found.scope.clone().map(|scope| Box::from(scope.as_str())),
enumerator: found.enumerator,
template: found.kind == DeclKind::Variable && !found.parameters().is_empty(),
constructor: found.kind == DeclKind::Function
&& found.returns.is_none()
&& found
.scope
.as_deref()
.is_some_and(|scope| scope.rsplit("::").next() == Some(found.name.as_str())),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectDefinition {
pub file: PathBuf,
pub fact: DeclFact,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectDefinitions {
pub found: Vec<ProjectDefinition>,
pub conditional: usize,
}
impl ProjectDefinitions {
pub fn one(found: ProjectDefinition) -> Self {
ProjectDefinitions {
found: vec![found],
conditional: 0,
}
}
pub fn is_one(&self) -> bool {
self.found.len() == 1
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct MemberList {
pub members: Vec<ProjectMember>,
pub unlisted: Vec<UnlistedBase>,
}
impl MemberList {
pub fn own(&self) -> impl Iterator<Item = &ProjectMember> {
self.members.iter().filter(|member| member.depth == 0)
}
pub fn at_depth(&self, depth: usize) -> impl Iterator<Item = &ProjectMember> {
self.members
.iter()
.filter(move |member| member.depth == depth)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectMember {
pub file: PathBuf,
pub fact: DeclFact,
pub declared_in: String,
pub depth: usize,
pub ambiguous: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnlistedBase {
pub spelling: String,
pub reason: UnknownReason,
}
impl ProjectDefinition {
pub fn from_binding(
path: &Path,
binding: crate::Binding,
scope: Option<String>,
local: bool,
) -> Self {
ProjectDefinition {
file: path.to_path_buf(),
fact: DeclFact {
enumerator: binding.kind == crate::BindingKind::Enumerator,
modifiers: crate::DeclModifiers::default(),
frees: 0,
detail: None,
name: binding.name.identifier_text().unwrap_or_default().into(),
scope: scope.map(SmolStr::from),
local,
kind: crate::DeclKind::from_binding_kind(binding.kind),
definition: false,
type_of: None,
returns: None,
split_by_a_directive: false,
access: None,
exported: false,
range: binding.range,
name_range: binding.name_range,
clean: true,
guard: FactGuard::Unconditional,
},
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum IncludeVisibility {
Unconditional,
Conditional,
}
#[derive(Clone, Copy)]
pub(crate) enum Narrow<'q> {
Nothing,
Named(&'q [&'q str]),
Scoped(&'q str),
}
pub(crate) struct Hit<'a> {
pub(crate) rank: usize,
pub(crate) name: String,
pub(crate) qualified: String,
pub(crate) file: &'a Path,
pub(crate) posting: Posting,
pub(crate) fact: &'a DeclFact,
}
pub(crate) fn hit_order(one: &Hit<'_>, two: &Hit<'_>) -> std::cmp::Ordering {
one.rank
.cmp(&two.rank)
.then_with(|| one.name.cmp(&two.name))
.then_with(|| one.qualified.cmp(&two.qualified))
.then_with(|| one.file.cmp(two.file))
.then_with(|| one.posting.cmp(&two.posting))
}
pub(crate) fn sort_hits(hits: &mut [Hit<'_>]) {
hits.sort_by(hit_order);
}
pub(crate) fn matches(fact: &DeclFact, name: &str) -> bool {
if fact.local {
return false;
}
if fact.name.is_empty() {
return false;
}
if let Some(global) = name.strip_prefix("::") {
return fact.scope.is_none() && fact.name == global;
}
fact.qualified_name() == name || fact.name == name
}
pub(crate) fn include_targets(summary: &FileSummary) -> Vec<String> {
summary
.includes
.iter()
.filter_map(|include| include.resolved.as_ref())
.map(|path| normalize(path))
.collect()
}
pub(crate) fn normalize(path: &Path) -> String {
normalize_path(path, cfg!(windows))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectSymbol {
pub file: PathBuf,
pub fact: DeclFact,
}
pub(crate) fn rank_of(qualified: &str, name: &str, wanted: &str) -> Option<usize> {
let wanted = wanted.trim_start_matches("::").to_lowercase();
if wanted.is_empty() {
return None;
}
let query: Vec<&str> = wanted.split("::").collect();
if query.len() == 1 {
let name = name.to_lowercase();
if name == wanted {
return Some(0);
}
if name.starts_with(&wanted) {
return Some(1);
}
if name.contains(&wanted) {
return Some(2);
}
return None;
}
let segments: Vec<String> = qualified
.to_lowercase()
.split("::")
.map(str::to_string)
.collect();
if segments.len() < query.len() {
return None;
}
let tail = &segments[segments.len() - query.len()..];
let (last_asked, asked) = query.split_last()?;
let (last, rest) = tail.split_last()?;
if rest.iter().map(String::as_str).ne(asked.iter().copied()) {
return None;
}
if last == last_asked {
return Some(0);
}
if last.starts_with(last_asked) {
return Some(1);
}
None
}
pub(crate) fn the_same_declaration(one: &DeclFact, other: &DeclFact) -> bool {
one.name == other.name
&& one.scope == other.scope
&& one.local == other.local
&& one.kind == other.kind
&& one.type_of == other.type_of
&& one.returns == other.returns
&& one.bases() == other.bases()
&& one.parameters() == other.parameters()
&& one.parameter_list() == other.parameter_list()
}
pub(crate) fn the_same_declaration_in_one_file(
one: &ProjectDeclaration,
other: &ProjectDeclaration,
) -> bool {
if one.file != other.file || !the_same_declaration(&one.fact, &other.fact) {
return false;
}
match one.fact.kind {
DeclKind::Variable | DeclKind::Type => one.fact.type_of.is_some(),
DeclKind::Function => one.fact.parameter_list().is_some(),
_ => false,
}
}