use super::refs::{FileRefs, Place};
use super::{lines_of, position_at, read_lossy};
use htl::typeindex::{Decl, DeclKind};
use lsp_types::{Range, SymbolKind};
use std::collections::{BTreeMap, HashSet};
use std::hash::{DefaultHasher, Hash, Hasher};
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime};
pub const MAX_RESULTS: usize = 1000;
#[derive(Debug, Clone)]
pub struct Sym {
pub name: String,
pub lower: String,
pub full_lower: String,
pub container: Option<String>,
pub kind: SymbolKind,
pub range: Range,
pub same: Option<[(usize, usize); 2]>,
pub full: Range,
pub body: Option<Range>,
pub detail: Option<String>,
pub at: [(usize, usize); 2],
}
impl Sym {
pub fn is_function(&self) -> bool {
matches!(self.kind, SymbolKind::FUNCTION | SymbolKind::METHOD)
}
}
pub fn find(syms: &[Sym], place: (usize, usize)) -> Option<usize> {
let by = |f: &dyn Fn(&Sym) -> bool| syms.iter().position(f);
by(&|s| s.at[1] == place)
.or_else(|| by(&|s| s.at[0] == place))
.or_else(|| by(&|s| s.same.is_some_and(|pair| pair.contains(&place))))
}
pub fn kind_of(kind: DeclKind) -> SymbolKind {
match kind {
DeclKind::Local => SymbolKind::VARIABLE,
DeclKind::Function => SymbolKind::FUNCTION,
DeclKind::Method => SymbolKind::METHOD,
DeclKind::Field => SymbolKind::FIELD,
DeclKind::Record => SymbolKind::STRUCT,
DeclKind::Interface => SymbolKind::INTERFACE,
DeclKind::Enum => SymbolKind::ENUM,
DeclKind::Alias => SymbolKind::TYPE_PARAMETER,
}
}
pub fn flatten(text: &str, decls: &[Decl]) -> Vec<Sym> {
fn walk(lines: &[&str], decls: &[Decl], container: Option<&str>, out: &mut Vec<Sym>) {
for d in decls {
let start = position_at(lines, d.name_line, d.name_col);
let end = position_at(
lines,
d.name_line,
d.name_col.saturating_add(d.short_name().len()),
);
let start_decl = position_at(lines, d.line, d.col);
let end_decl = position_at(lines, d.end_line, d.end_col.unwrap_or(usize::MAX));
let first = (d.line, d.col);
let first = if matches!(d.kind, DeclKind::Function | DeclKind::Method)
&& container.is_some()
&& d.body.is_none()
&& first == (d.name_line, d.name_col)
{
type_after_name(lines, d).unwrap_or(first)
} else {
first
};
out.push(Sym {
name: d.name.clone(),
lower: d.short_name().to_lowercase(),
full_lower: d.name.to_lowercase(),
container: container.map(str::to_string),
kind: kind_of(d.kind),
range: Range::new(start, end),
same: d.body.map(|b| [(d.line, d.col), b]),
full: Range::new(start_decl.min(start), end_decl.max(end)),
body: d.fn_body.map(|b| {
Range::new(
position_at(lines, b.line, b.col),
position_at(lines, b.end_line, b.end_col.unwrap_or(usize::MAX)),
)
}),
detail: d.detail.clone(),
at: [first, (d.name_line, d.name_col)],
});
walk(lines, &d.children, Some(&d.name), out);
}
}
fn type_after_name(lines: &[&str], d: &Decl) -> Option<(usize, usize)> {
let row = lines.get(d.name_line.checked_sub(1)?)?;
let rest = row.get(d.name_col.checked_sub(1)? + d.name.len()..)?;
let rest = rest.trim_start().strip_prefix(':')?.trim_start();
(!rest.is_empty()).then(|| (d.name_line, row.len() - rest.len() + 1))
}
let lines = lines_of(text);
let mut out = Vec::new();
walk(&lines, decls, None, &mut out);
out
}
pub fn rank(query: &str, sym: &Sym) -> Option<u8> {
if query.is_empty() {
return Some(2);
}
[sym.lower.as_str(), sym.full_lower.as_str()]
.into_iter()
.filter_map(|n| {
if n == query {
Some(0)
} else if n.starts_with(query) {
Some(1)
} else if n.contains(query) {
Some(2)
} else {
None
}
})
.min()
}
pub struct Hit<'a> {
pub rank: u8,
pub path: &'a Path,
pub sym: &'a Sym,
}
pub fn search<'a>(
files: impl IntoIterator<Item = &'a Entry>,
query: &str,
) -> (Vec<Hit<'a>>, usize) {
let query = query.to_lowercase();
let mut hits: Vec<Hit<'a>> = Vec::new();
for e in files {
for sym in &e.syms {
if let Some(rank) = rank(&query, sym) {
hits.push(Hit {
rank,
path: &e.path,
sym,
});
}
}
}
let order = |h: &Hit<'a>| {
(
h.rank,
h.path,
h.sym.range.start.line,
h.sym.range.start.character,
)
};
let total = hits.len();
if total > MAX_RESULTS {
hits.select_nth_unstable_by(MAX_RESULTS, |a, b| order(a).cmp(&order(b)));
hits.truncate(MAX_RESULTS);
}
hits.sort_by(|a, b| order(a).cmp(&order(b)));
(hits, total)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Stamp {
mtime: Option<SystemTime>,
len: u64,
}
impl Stamp {
pub fn of(path: &Path) -> Option<Self> {
let m = std::fs::metadata(path).ok()?;
Some(Self {
mtime: m.modified().ok(),
len: m.len(),
})
}
fn settled(&self) -> bool {
self.mtime
.is_some_and(|t| t + Duration::from_secs(2) <= SystemTime::now())
}
}
pub fn hash_of(text: &str) -> u64 {
let mut h = DefaultHasher::new();
text.hash(&mut h);
h.finish()
}
#[derive(Debug, Clone)]
pub struct Entry {
pub path: PathBuf,
stamp: Stamp,
hash: u64,
settled: bool,
pub syms: Vec<Sym>,
pub refs: Option<FileRefs>,
distrust: bool,
deps: Option<HashSet<PathBuf>>,
}
impl Entry {
pub fn new(path: PathBuf, stamp: Stamp, text: &str, syms: Vec<Sym>) -> Self {
Self {
path,
stamp,
hash: hash_of(text),
settled: stamp.settled(),
syms,
refs: None,
distrust: false,
deps: None,
}
}
}
#[derive(Debug)]
pub struct SymbolIndex {
files: BTreeMap<PathBuf, Entry>,
dirty: HashSet<PathBuf>,
relist: bool,
refs_built: bool,
inputs: Option<u64>,
}
impl Default for SymbolIndex {
fn default() -> Self {
Self {
files: BTreeMap::new(),
dirty: HashSet::new(),
relist: true,
refs_built: false,
inputs: None,
}
}
}
impl SymbolIndex {
pub fn entries(&self) -> impl Iterator<Item = &Entry> {
self.files.values()
}
pub fn relist(&mut self) {
self.relist = true;
}
pub fn event(&mut self, canonical: &Path) {
if self.files.contains_key(canonical) {
self.dirty.insert(canonical.to_path_buf());
} else {
self.relist = true;
}
}
pub fn put(&mut self, canonical: &Path, mut entry: Entry, deps: HashSet<PathBuf>) {
let Some(slot) = self.files.get_mut(canonical) else {
return;
};
entry.path = slot.path.clone();
let changed = slot.hash != entry.hash;
if !changed {
entry.refs = slot.refs.take();
}
entry.deps = Some(deps);
*slot = entry;
self.dirty.remove(canonical);
if changed {
self.moved(&[canonical.to_path_buf()], false);
}
}
fn moved(&mut self, changed: &[PathBuf], structural: bool) {
if changed.is_empty() && !structural {
return;
}
for (path, e) in &mut self.files {
let hit = structural
|| (!changed.contains(path)
&& e.deps
.as_ref()
.is_none_or(|d| changed.iter().any(|c| d.contains(c))));
if hit {
e.refs = None;
e.distrust = true;
}
}
}
pub fn first_refs(&mut self) -> bool {
if self.refs_built {
return false;
}
self.refs_built = true;
self.moved(&[], true);
true
}
pub fn holds(&self, canonical: &Path) -> bool {
self.files.contains_key(canonical)
}
pub fn inputs_changed(&mut self, fingerprint: u64) -> bool {
self.inputs
.replace(fingerprint)
.is_some_and(|was| was != fingerprint)
}
pub fn forget_refs(&mut self) {
self.moved(&[], true);
}
pub fn distrusts(&self, canonical: &Path) -> bool {
self.files.get(canonical).is_some_and(|e| e.distrust)
}
pub fn pending(&self) -> Vec<Pending> {
self.files
.iter()
.filter(|(_, e)| e.refs.is_none())
.map(|(canonical, e)| Pending {
canonical: canonical.clone(),
path: e.path.clone(),
distrust: e.distrust,
})
.collect()
}
pub fn fill(&mut self, canonical: &Path, hash: u64, refs: FileRefs) {
if let Some(e) = self.files.get_mut(canonical)
&& e.hash == hash
{
e.deps = refs.deps.clone();
e.refs = Some(refs);
e.distrust = false;
}
}
pub fn entry(&self, canonical: &Path) -> Option<&Entry> {
self.files.get(canonical)
}
pub fn iter(&self) -> impl Iterator<Item = (&PathBuf, &Entry)> {
self.files.iter()
}
pub fn same_as(&self, target: &Place) -> Vec<Place> {
let mut places = vec![target.clone()];
let Some(e) = self.files.get(&*target.file) else {
return places;
};
for pair in e.syms.iter().filter_map(|s| s.same.as_ref()) {
if pair.contains(&(target.line, target.col)) {
for &(line, col) in pair {
let p = Place {
file: target.file.clone(),
line,
col,
};
if !places.contains(&p) {
places.push(p);
}
}
}
}
places
}
pub fn refresh(
&mut self,
sweep: bool,
list: impl FnOnce() -> anyhow::Result<Vec<PathBuf>>,
build: &mut dyn FnMut(&Path, &Path, &str) -> Vec<Sym>,
) -> anyhow::Result<()> {
let mut changed: Vec<PathBuf> = Vec::new();
let mut structural = false;
if sweep || self.relist {
let listed = list()?;
let first = self.files.is_empty();
let mut kept = BTreeMap::new();
for path in listed {
let canonical = super::canonical(&path);
let old = self.files.remove(&canonical);
let had = old.as_ref().map(|e| e.hash);
let force = self.dirty.contains(&canonical);
match revalidate(old, &canonical, path, force, build) {
Some(e) => {
match had {
Some(h) if h != e.hash => changed.push(canonical.clone()),
None if !first => structural = true,
_ => {}
}
kept.insert(canonical, e);
}
None => structural = true,
}
}
structural |= !self.files.is_empty();
self.files = kept;
} else {
for canonical in std::mem::take(&mut self.dirty) {
if let Some(old) = self.files.remove(&canonical) {
let (path, had) = (old.path.clone(), old.hash);
match revalidate(Some(old), &canonical, path, true, build) {
Some(e) => {
if e.hash != had {
changed.push(canonical.clone());
}
self.files.insert(canonical, e);
}
None => structural = true,
}
}
}
}
self.moved(&changed, structural);
self.dirty.clear();
self.relist = false;
Ok(())
}
}
pub struct Pending {
pub canonical: PathBuf,
pub path: PathBuf,
pub distrust: bool,
}
fn revalidate(
old: Option<Entry>,
canonical: &Path,
path: PathBuf,
force: bool,
build: &mut dyn FnMut(&Path, &Path, &str) -> Vec<Sym>,
) -> Option<Entry> {
let stamp = Stamp::of(&path)?;
if let Some(e) = &old
&& !force
&& e.stamp == stamp
&& e.settled
{
return old;
}
let text = read_lossy(&path);
if let Some(mut e) = old
&& e.hash == hash_of(&text)
{
e.stamp = stamp;
e.settled = stamp.settled();
return Some(e);
}
let syms = build(canonical, &path, &text);
Some(Entry::new(path, stamp, &text, syms))
}