ridx-format 0.3.19

Fast, compact, zero-copy binary index format for Rust Intelligence
Documentation
/*
 * rust-intelligence — Ultra-fast, zero-memory code intelligence for Rust
 * Copyright (c) 2026 Alexander Panasenko
 *
 * Contact: alex@prod.codes
 * Author: https://prod.codes/about/
 * Project: https://github.com/alex09x/rust-intelligence
 * SPDX-License-Identifier: MIT OR Apache-2.0
 */

use super::*;

impl WorkspaceIndex {
    /// Substring symbol search across all definitions in the workspace.
    pub fn search_symbols<'a>(&'a self, query: &str) -> Vec<WorkspaceSymbol<'a>> {
        self.search_symbols_filtered(query, |_, _| true)
    }

    /// Substring symbol search across non-dependency definitions in the workspace.
    pub fn search_local_symbols<'a>(&'a self, query: &str) -> Vec<WorkspaceSymbol<'a>> {
        self.search_symbols_filtered(query, |i, _| !self.reader_is_dependency.get(i).copied().unwrap_or(false))
    }

    /// Substring symbol search with custom reader predicate.
    pub fn search_symbols_filtered<'a, F>(&'a self, query: &str, mut filter: F) -> Vec<WorkspaceSymbol<'a>>
    where
        F: FnMut(usize, &IndexReader<memmap2::Mmap>) -> bool,
    {
        let mut matches = Vec::new();
        let mut seen = std::collections::HashSet::new();

        let queries = super::std_map::std_path_candidates(query);
        for q in &queries {
            let q_ascii_lower = if q.is_ascii() { Some(q.to_ascii_lowercase()) } else { None };
            for (i, reader) in self.readers.iter().enumerate() {
                if !filter(i, reader) {
                    continue;
                }
                let crate_name = reader.crate_name();
                let defs = match &q_ascii_lower {
                    Some(lower) => reader.search_symbols_ascii(lower.as_bytes()),
                    None => reader.search_symbols(q),
                };
                let source_root = self.source_roots.get(i).and_then(|r| r.as_deref()).or(self.workspace_root.as_deref());
                let unit_key = self.unit_keys.get(i).and_then(|u| u.as_deref());

                for def in defs {
                    let name = reader.get_str(def.name_offset());
                    let path = reader.get_str(def.path_offset());
                    let span = def.span();
                    let file = reader.get_file(span.file_id());
                    let sym_key = (path, file, span.start_line(), span.start_col());
                    if !seen.insert(sym_key) {
                        continue;
                    }
                    let signature = if def.sig_offset() != 0 { Some(reader.get_str(def.sig_offset())) } else { None };

                    matches.push(WorkspaceSymbol {
                        crate_name,
                        name,
                        path,
                        kind: DefKind::from(def.kind),
                        file,
                        span,
                        signature,
                        is_macro_expansion: def.is_macro_expansion(),
                        source_root,
                        unit_key,
                    });
                }
            }
        }

        matches
    }

    /// Finds the first symbol matching `query` and `predicate` without eagerly collecting all matches.
    pub fn find_first_symbol<'a, F>(&'a self, query: &str, mut predicate: F) -> Option<WorkspaceSymbol<'a>>
    where
        F: FnMut(&WorkspaceSymbol<'a>) -> bool,
    {
        let queries = super::std_map::std_path_candidates(query);
        for q in &queries {
            let q_ascii_lower = if q.is_ascii() { Some(q.to_ascii_lowercase()) } else { None };
            for (i, reader) in self.readers.iter().enumerate() {
                let crate_name = reader.crate_name();
                let defs = match &q_ascii_lower {
                    Some(lower) => reader.search_symbols_ascii(lower.as_bytes()),
                    None => reader.search_symbols(q),
                };
                let source_root = self.source_roots.get(i).and_then(|r| r.as_deref()).or(self.workspace_root.as_deref());
                let unit_key = self.unit_keys.get(i).and_then(|u| u.as_deref());

                for def in defs {
                    let name = reader.get_str(def.name_offset());
                    let path = reader.get_str(def.path_offset());
                    let span = def.span();
                    let file = reader.get_file(span.file_id());
                    let signature = if def.sig_offset() != 0 { Some(reader.get_str(def.sig_offset())) } else { None };

                    let sym = WorkspaceSymbol {
                        crate_name,
                        name,
                        path,
                        kind: DefKind::from(def.kind),
                        file,
                        span,
                        signature,
                        is_macro_expansion: def.is_macro_expansion(),
                        source_root,
                        unit_key,
                    };
                    if predicate(&sym) {
                        return Some(sym);
                    }
                }
            }
        }
        None
    }

    /// Iterates over matching definitions across workspace readers without eager allocation,
    /// searching preferred crate first and stopping when `cb` returns false or scan budget is exhausted.
    pub fn for_each_symbol_ascii<F>(&self, needle_lower: &[u8], preferred_crate: Option<&str>, mut cb: F) -> bool
    where
        F: FnMut(&IndexReader<memmap2::Mmap>, &DefEntry) -> bool,
    {
        let mut budget: usize = 2_000;
        let mut record_budget: usize = 30_000;
        if let Some(pref) = preferred_crate {
            for &i in self.readers_for_crate_fast(pref).iter() {
                let reader = &self.readers[i];
                if !reader.for_each_symbol_ascii_budgeted(needle_lower, &mut budget, &mut record_budget, |def| cb(reader, def)) {
                    return false;
                }
            }
        }
        for reader in &self.readers {
            if preferred_crate.is_some_and(|pref| reader.crate_name() == pref) {
                continue;
            }
            if !reader.for_each_symbol_ascii_budgeted(needle_lower, &mut budget, &mut record_budget, |def| cb(reader, def)) {
                return false;
            }
        }
        true
    }

    /// Iterates over matching definitions specifically for a single target crate without eager allocation.
    pub fn for_each_symbol_in_crate<F>(&self, target_crate: &str, needle_lower: &[u8], mut cb: F) -> bool
    where
        F: FnMut(&IndexReader<memmap2::Mmap>, &DefEntry) -> bool,
    {
        let mut budget: usize = 2_000;
        let mut record_budget: usize = 30_000;
        for &i in self.readers_for_crate_fast(target_crate).iter() {
            let reader = &self.readers[i];
            if !reader.for_each_symbol_ascii_budgeted(needle_lower, &mut budget, &mut record_budget, |def| cb(reader, def)) {
                return false;
            }
        }
        true
    }

    /// Returns true if any index reader in the workspace has the given crate name.
    pub fn has_crate(&self, target_crate: &str) -> bool {
        !self.readers_for_crate_fast(target_crate).is_empty()
    }

    /// Finds all direct references to the symbol at cursor location without expanding across
    /// trait/impl containers.
    pub fn find_direct_references_at<'a>(&'a self, file: &str, line: u32, col: u32) -> Option<LocationReferences<'a>> {
        let (def, refs) = self.resolve_direct_references_at(file, line, col)?;
        Some(LocationReferences {
            target_symbol: def.name,
            target_kind: def.kind,
            target_path: def.path,
            target_file: def.file,
            target_line: def.start_line,
            target_col: def.start_col,
            target_end_line: def.end_line,
            target_end_col: def.end_col,
            target_source_root: def.source_root,
            target_unit_key: def.unit_key,
            target_reader_idx: def.reader_idx,
            references: refs,
        })
    }

    /// The definitions the symbols at `positions` in `file` refer to, as [`Self::find_direct_references_at`] resolves
    /// them, without collecting their references. The file is located among the index's units once.
    pub fn resolve_definitions_at<'a>(&'a self, file: &str, positions: &[(u32, u32)]) -> Vec<Option<ResolvedDefinition<'a>>> {
        self.resolve_definitions_in(&self.resolved_reader_file_ids(file), file, positions)
    }

    /// [`Self::resolve_definitions_at`] with the file's `(reader, file id)` pairs from
    /// [`Self::resolved_reader_file_ids`], for callers that resolve in one file many times.
    pub fn resolve_definitions_in<'a>(&'a self, pairs: &[(usize, u32)], file: &str, positions: &[(u32, u32)]) -> Vec<Option<ResolvedDefinition<'a>>> {
        positions
            .iter()
            .map(|&(line, col)| {
                let (exact, fallback_wide_ref, fallback_mod) = self.find_indexed_definition_candidates_in_pairs(pairs, file, line, col);
                exact.or_else(|| self.resolve_cursor_path_at(file, line, col)).or(fallback_wide_ref).or(fallback_mod)
            })
            .collect()
    }

    /// Finds all references to the symbol at cursor location across the workspace, including
    /// container-scoped trait/impl links for associated items.
    pub fn find_references_at<'a>(&'a self, file: &str, line: u32, col: u32) -> Option<LocationReferences<'a>> {
        let (def, mut refs) = self.resolve_direct_references_at(file, line, col)?;
        self.expand_trait_impl_references(&def, &mut refs);
        self.expand_trait_item_impl_uses(&def, &mut refs);
        if def.kind == DefKind::Mod {
            // A module path a macro wrote (a derive's `bevy_ecs::resource::Resource`) names no module in the source.
            refs.retain(|r| !r.is_macro_expansion);
        } else {
            // A macro's reference is a use only where the source spells the name: a derive's generated `impl Debug
            // for Thing` points at `#[derive(Debug)]`, which names no `Thing`.
            let name = def.name.strip_prefix("r#").unwrap_or(def.name);
            let mut lines: std::collections::HashMap<std::path::PathBuf, Option<Vec<String>>> = std::collections::HashMap::new();
            refs.retain(|r| {
                if !r.is_macro_expansion {
                    return true;
                }
                let path = r.resolved_path();
                let file = lines.entry(path.clone()).or_insert_with(|| std::fs::read_to_string(&path).ok().map(|s| s.lines().map(str::to_string).collect()));
                let Some(text) = file.as_ref().and_then(|f| f.get((r.span.start_line() as usize).wrapping_sub(1))) else { return true };
                let at: String = text.chars().skip(r.span.start_col().saturating_sub(1) as usize).collect();
                let at = at.strip_prefix("r#").unwrap_or(&at);
                let spelled: String = at.chars().take_while(|c| c.is_alphanumeric() || *c == '_').collect();
                spelled == name
            });
        }
        Some(LocationReferences {
            target_symbol: def.name,
            target_kind: def.kind,
            target_path: def.path,
            target_file: def.file,
            target_line: def.start_line,
            target_col: def.start_col,
            target_end_line: def.end_line,
            target_end_col: def.end_col,
            target_source_root: def.source_root,
            target_unit_key: def.unit_key,
            target_reader_idx: def.reader_idx,
            references: refs,
        })
    }

    /// Finds all references and sibling trait/impl declarations required to rename the symbol at cursor.
    pub fn find_rename_references_at<'a>(&'a self, file: &str, line: u32, col: u32) -> Option<LocationReferences<'a>> {
        let (def, mut refs) = self.resolve_direct_references_at(file, line, col)?;
        self.expand_trait_impl_for_rename(&def, &mut refs);
        self.expand_rename_type_and_sibling_refs(&def, &mut refs);
        Some(LocationReferences {
            target_symbol: def.name,
            target_kind: def.kind,
            target_path: def.path,
            target_file: def.file,
            target_line: def.start_line,
            target_col: def.start_col,
            target_end_line: def.end_line,
            target_end_col: def.end_col,
            target_source_root: def.source_root,
            target_unit_key: def.unit_key,
            target_reader_idx: def.reader_idx,
            references: refs,
        })
    }

    pub(super) fn resolve_direct_references_at<'a>(&'a self, file: &str, line: u32, col: u32) -> Option<(ResolvedDefinition<'a>, Vec<WorkspaceReference<'a>>)> {
        let (exact, fallback_wide_ref, fallback_mod) = self.find_indexed_definition_candidates(file, line, col);
        let used_cursor_fallback = exact.is_none();
        let def = match exact {
            Some(d) => d,
            None => self.resolve_cursor_path_at(file, line, col).or(fallback_wide_ref).or(fallback_mod)?,
        };
        let explicit = Some((def.file, def.start_line, def.kind, def.name));
        let mut refs = self.find_references_for_unit_with_target(def.path, def.unit_key, explicit);
        if refs.is_empty() && def.kind == DefKind::Local && def.name != def.path {
            refs = self.find_references_for_unit_with_target(def.name, def.unit_key, explicit);
        }
        if refs.is_empty() && def.kind == DefKind::Macro && used_cursor_fallback {
            if let (_, Some(wide), _) = self.find_indexed_definition_candidates(file, line, col) {
                let wide_explicit = Some((wide.file, wide.start_line, DefKind::Macro, wide.name));
                refs = self.find_references_for_unit_with_target(wide.path, wide.unit_key, wide_explicit);
            }
        }
        // Ensure the cursor position itself is included when resolved via path/assoc fallback
        if used_cursor_fallback {
            if let Some((ident, sc, ec)) = self.cursor_ident_at(file, line, col) {
                if ident == def.name || def.path.ends_with(&format!("::{ident}")) {
                    for i in self.candidate_reader_indices_for_file(file) {
                        let reader = &self.readers[i];
                        let Some(tf) = self.target_file_for_reader(i, file) else {
                            continue;
                        };
                        let Some(fid) = reader.find_file_id(tf.as_ref()) else {
                            continue;
                        };
                        let fstr = reader.get_file(fid);
                        let on_decl = def.start_line == line && (def.file == fstr || file.ends_with(def.file));
                        let on_ref = refs.iter().any(|r| r.span.start_line() == line && (r.file == fstr || file.ends_with(r.file)));
                        if !on_decl && !on_ref {
                            refs.push(WorkspaceReference {
                                crate_name: reader.crate_name(),
                                file: fstr,
                                span: crate::format::Span::new(fid, line, sc, line, ec),
                                kind: def.kind,
                                is_write: false,
                                is_macro_expansion: false,
                                source_root: self.source_roots.get(i).and_then(|r| r.as_deref()).or(self.workspace_root.as_deref()),
                                unit_key: self.unit_keys.get(i).and_then(|u| u.as_deref()),
                            });
                        }
                        break;
                    }
                }
            }
        }
        Some((def, refs))
    }

    /// Complete hover information (definition + signature + inferred type) at cursor location.
    pub fn hover_at<'a>(&'a self, file: &str, line: u32, col: u32) -> Option<WorkspaceHover<'a>> {
        let pairs = self.resolved_reader_file_ids(file);
        let (exact, fallback_wide_ref, fallback_mod) = self.find_indexed_definition_candidates_in_pairs(&pairs, file, line, col);
        let def = if let Some(resolved) = exact {
            Some(resolved)
        } else if let Some(cursor_def) = self.resolve_cursor_path_at(file, line, col) {
            Some(cursor_def)
        } else if self.type_entry_at_in_pairs(&pairs, line, col).is_some() {
            // A reference spanning more than the name under the cursor, and named otherwise (a `for` loop's
            // `into_iter`, an `async` body's `ResumeTy`), encloses a binding here that has its own type: the
            // hover is the binding's.
            None
        } else {
            fallback_wide_ref.or(fallback_mod)
        };
        let skip_type_at = def.as_ref().is_some_and(|d| {
            matches!(
                d.kind,
                DefKind::Mod
                    | DefKind::Struct
                    | DefKind::Enum
                    | DefKind::Union
                    | DefKind::Trait
                    | DefKind::TyAlias
                    | DefKind::AssocTy
                    | DefKind::Variant
                    | DefKind::VariantCtor
                    | DefKind::Macro
                    | DefKind::Use
                    | DefKind::TyParam
                    | DefKind::ConstParam
                    | DefKind::LifetimeParam
            )
        });
        let ty = if skip_type_at { None } else { self.type_entry_at_in_pairs(&pairs, line, col).map(|(t, _)| t) };
        if def.is_none() && ty.is_none() {
            return None;
        }
        Some(WorkspaceHover { definition: def, inferred_type: ty })
    }
}