use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use crate::domain::{ReferenceKind, ReferenceRecord};
use super::search::{
DEFAULT_MAX_PER_FILE, SymbolMatchTier, SymbolSearchOptions, TextSearchError, TextSearchOptions,
TextSearchResult, compute_test_ranges, current_code_search_keeps_file,
search_symbols_with_options, search_text_with_options, truncate_display_line,
};
use super::store::{IndexedFile, LiveIndex};
use crate::live_index::query::is_filtered_name;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum CommitId {
Sha(String),
Dirtyless,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct BaseKey {
pub canonical_root: PathBuf,
pub commit: CommitId,
}
impl BaseKey {
pub fn new(canonical_root: impl Into<PathBuf>, commit: CommitId) -> Self {
Self {
canonical_root: canonical_root.into(),
commit,
}
}
}
#[derive(Clone)]
pub struct IndexBase {
pub key: BaseKey,
pub index: Arc<LiveIndex>,
pub base_generation: u64,
}
impl IndexBase {
pub fn new(key: BaseKey, index: Arc<LiveIndex>, base_generation: u64) -> Self {
Self {
key,
index,
base_generation,
}
}
}
#[derive(Clone)]
pub enum FileDelta {
Upsert(Arc<IndexedFile>),
Tombstone,
}
#[derive(Clone)]
pub struct Overlay {
pub base_key: BaseKey,
pub base_generation: u64,
pub deltas: HashMap<String, FileDelta>,
}
impl Overlay {
pub fn fresh(base: &IndexBase) -> Self {
Self {
base_key: base.key.clone(),
base_generation: base.base_generation,
deltas: HashMap::new(),
}
}
pub fn delta_count(&self) -> usize {
self.deltas.len()
}
pub fn is_valid_against(&self, base: &IndexBase) -> bool {
self.base_key == base.key && self.base_generation == base.base_generation
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StaleOverlay {
pub base_generation: u64,
pub overlay_generation: u64,
}
impl std::fmt::Display for StaleOverlay {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"stale overlay: base generation {} != overlay generation {}",
self.base_generation, self.overlay_generation
)
}
}
impl std::error::Error for StaleOverlay {}
pub struct IndexView<'a> {
base: &'a LiveIndex,
overlay: Option<&'a Overlay>,
}
impl<'a> IndexView<'a> {
pub fn new(base: &'a IndexBase, overlay: Option<&'a Overlay>) -> Result<Self, StaleOverlay> {
if let Some(ov) = overlay {
if ov.base_generation != base.base_generation || ov.base_key != base.key {
return Err(StaleOverlay {
base_generation: base.base_generation,
overlay_generation: ov.base_generation,
});
}
}
Ok(Self {
base: base.index.as_ref(),
overlay,
})
}
pub fn base_only(base: &'a LiveIndex) -> Self {
Self {
base,
overlay: None,
}
}
pub fn get_file(&self, relative_path: &str) -> Option<&IndexedFile> {
if let Some(ov) = self.overlay
&& let Some(delta) = ov.deltas.get(relative_path)
{
return match delta {
FileDelta::Upsert(file) => Some(file.as_ref()),
FileDelta::Tombstone => None,
};
}
self.base.get_file(relative_path)
}
pub fn all_files(&self) -> Vec<(String, &IndexedFile)> {
match self.overlay {
None => self.base.all_files().map(|(p, f)| (p.clone(), f)).collect(),
Some(ov) => {
let mut out: Vec<(String, &IndexedFile)> = Vec::new();
for (path, file) in self.base.all_files() {
match ov.deltas.get(path) {
Some(FileDelta::Tombstone) => continue,
Some(FileDelta::Upsert(_)) => continue, None => out.push((path.clone(), file)),
}
}
for (path, delta) in &ov.deltas {
if let FileDelta::Upsert(file) = delta {
out.push((path.clone(), file.as_ref()));
}
}
out
}
}
}
fn overlay_deltas(&self) -> Option<&'a HashMap<String, FileDelta>> {
self.overlay.map(|ov| &ov.deltas)
}
pub fn search_symbols(
&self,
query: &str,
kind_filter: Option<&str>,
options: &SymbolSearchOptions,
) -> Vec<ViewSymbolHit> {
let no_deltas = match self.overlay_deltas() {
None => true,
Some(deltas) => deltas.is_empty(),
};
if no_deltas {
let result = search_symbols_with_options(self.base, query, kind_filter, options);
return result
.hits
.into_iter()
.map(|hit| ViewSymbolHit {
name: hit.name,
path: hit.path,
kind: hit.kind,
line: hit.line,
tier: hit.tier,
})
.collect();
}
let query_lower = query.to_lowercase();
let mut hits: Vec<ViewSymbolHit> = Vec::new();
let mut resolved = self.all_files();
resolved.sort_by(|a, b| a.0.cmp(&b.0));
for (path, file) in resolved {
for sym in &file.symbols {
if let Some(filter) = kind_filter
&& !filter.eq_ignore_ascii_case("all")
&& !symbol_kind_matches(filter, &sym.kind)
{
continue;
}
let name_lower = sym.name.to_lowercase();
if !name_lower.contains(&query_lower) {
continue;
}
let tier = if name_lower == query_lower {
SymbolMatchTier::Exact
} else if name_lower.starts_with(&query_lower) {
SymbolMatchTier::Prefix
} else {
SymbolMatchTier::Substring
};
hits.push(ViewSymbolHit {
name: sym.name.clone(),
path: path.clone(),
kind: sym.kind.to_string(),
line: sym.line_range.0 + 1,
tier,
});
}
}
hits.sort_by(|a, b| {
a.tier
.cmp(&b.tier)
.then(a.name.cmp(&b.name))
.then(a.path.cmp(&b.path))
});
hits
}
pub fn search_text(
&self,
query: Option<&str>,
terms: Option<&[String]>,
regex: bool,
options: &TextSearchOptions,
) -> Result<TextSearchResult, TextSearchError> {
let mut result = search_text_with_options(self.base, query, terms, regex, options)?;
let Some(deltas) = self.overlay_deltas() else {
return Ok(result);
};
if deltas.is_empty() {
return Ok(result);
}
result.files.retain(|file| !deltas.contains_key(&file.path));
let matcher = overlay_text_matcher(query, terms, regex)?;
let mut overlay_suppressed = 0usize;
if let Some(matcher) = matcher {
let mut appended: Vec<super::search::TextFileMatches> = Vec::new();
for (path, delta) in deltas {
let FileDelta::Upsert(file) = delta else {
continue;
};
if !current_code_search_keeps_file(path, &file.classification) {
continue;
}
let scan = scan_file_lines(file, &matcher);
overlay_suppressed += scan.suppressed;
if !scan.matches.is_empty() {
appended.push(super::search::TextFileMatches {
path: path.clone(),
matches: scan.matches,
rendered_lines: None,
callers: None,
});
}
}
appended.sort_by(|a, b| a.path.cmp(&b.path));
result.files.extend(appended);
}
result.total_matches = result.files.iter().map(|f| f.matches.len()).sum();
result.overflow_count = 0;
result.suppressed_by_noise = result
.suppressed_by_noise
.saturating_add(overlay_suppressed);
Ok(result)
}
pub fn find_references(
&self,
name: &str,
kind_filter: Option<ReferenceKind>,
include_filtered: bool,
) -> Vec<(String, &'a ReferenceRecord)> {
let mut out: Vec<(String, &'a ReferenceRecord)> = self
.base
.find_references_for_name(name, kind_filter, include_filtered)
.into_iter()
.map(|(path, reference)| (path.to_string(), reference))
.collect();
let Some(deltas) = self.overlay_deltas() else {
return out;
};
if deltas.is_empty() {
return out;
}
out.retain(|(path, _)| !deltas.contains_key(path));
let is_qualified = name.contains("::") || name.contains('.');
let mut appended: Vec<(String, &'a ReferenceRecord)> = Vec::new();
for (path, delta) in deltas {
let FileDelta::Upsert(file) = delta else {
continue;
};
if !current_code_search_keeps_file(path, &file.classification) {
continue;
}
for reference in &file.references {
let name_matches = if is_qualified {
reference.qualified_name.as_deref() == Some(name)
} else {
reference.name == name
};
if !name_matches {
continue;
}
if let Some(kf) = kind_filter
&& reference.kind != kf
{
continue;
}
if !include_filtered && is_filtered_name(&reference.name, &file.language) {
continue;
}
appended.push((path.clone(), reference));
}
}
appended.sort_by(|a, b| a.0.cmp(&b.0));
out.extend(appended);
out
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ViewSymbolHit {
pub name: String,
pub path: String,
pub kind: String,
pub line: u32,
pub tier: SymbolMatchTier,
}
fn symbol_kind_matches(filter: &str, kind: &crate::domain::SymbolKind) -> bool {
if kind.to_string().eq_ignore_ascii_case(filter) {
return true;
}
matches!(
(filter.to_ascii_lowercase().as_str(), kind),
("variable", crate::domain::SymbolKind::Variable)
| ("function", crate::domain::SymbolKind::Function)
| ("method", crate::domain::SymbolKind::Method)
| ("module", crate::domain::SymbolKind::Module)
| ("constant", crate::domain::SymbolKind::Constant)
)
}
enum OverlayTextMatcher {
Terms { terms: Vec<String>, fold: bool },
Regex(regex::Regex),
}
impl OverlayTextMatcher {
fn is_match(&self, line: &str) -> bool {
match self {
Self::Terms { terms, fold } => {
if *fold {
let line_lower = line.to_lowercase();
terms.iter().any(|t| line_lower.contains(&t.to_lowercase()))
} else {
terms.iter().any(|t| line.contains(t))
}
}
Self::Regex(re) => re.is_match(line),
}
}
}
fn overlay_text_matcher(
query: Option<&str>,
terms: Option<&[String]>,
regex: bool,
) -> Result<Option<OverlayTextMatcher>, TextSearchError> {
if regex {
let pattern = query
.map(str::trim)
.filter(|text| !text.is_empty())
.unwrap_or("");
if pattern.is_empty() {
return Err(TextSearchError::EmptyRegexQuery);
}
let re = regex::RegexBuilder::new(pattern)
.case_insensitive(true)
.build()
.map_err(|error| TextSearchError::InvalidRegex {
pattern: pattern.to_string(),
error: error.to_string(),
})?;
return Ok(Some(OverlayTextMatcher::Regex(re)));
}
let collected: Vec<String> = match terms {
Some(raw) if !raw.is_empty() => raw
.iter()
.map(|t| t.trim())
.filter(|t| !t.is_empty())
.map(ToOwned::to_owned)
.collect(),
_ => query
.map(str::trim)
.filter(|text| !text.is_empty())
.map(|text| vec![text.to_string()])
.unwrap_or_default(),
};
if collected.is_empty() {
return Ok(None);
}
Ok(Some(OverlayTextMatcher::Terms {
terms: collected,
fold: true,
}))
}
struct OverlayFileScan {
matches: Vec<super::search::TextLineMatch>,
suppressed: usize,
}
fn scan_file_lines(file: &IndexedFile, matcher: &OverlayTextMatcher) -> OverlayFileScan {
let test_ranges: Vec<(u32, u32)> = if file.language == crate::domain::LanguageId::Rust {
compute_test_ranges(file)
} else {
Vec::new()
};
let content = String::from_utf8_lossy(&file.content);
let mut matches = Vec::new();
let mut suppressed = 0usize;
for (line_idx, line) in content.lines().enumerate() {
let line = line.trim_end_matches('\r');
if !matcher.is_match(line) {
continue;
}
if !test_ranges.is_empty() {
let line_num = line_idx as u32;
if test_ranges
.iter()
.any(|&(start, end)| line_num >= start && line_num <= end)
{
suppressed += 1;
continue;
}
}
if matches.len() < DEFAULT_MAX_PER_FILE {
matches.push(super::search::TextLineMatch {
line_number: line_idx + 1,
line: truncate_display_line(line),
enclosing_symbol: None,
});
}
}
OverlayFileScan {
matches,
suppressed,
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Targets {
One(String),
Subset(Vec<String>),
All,
}
impl Targets {
fn selects(&self, project_id: &str) -> bool {
match self {
Self::One(id) => id == project_id,
Self::Subset(ids) => ids.iter().any(|id| id == project_id),
Self::All => true,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProjectHit<T> {
pub project_id: String,
pub hit: T,
}
#[derive(Clone)]
pub struct WorkingSetEntry {
pub project_id: String,
pub base: Arc<IndexBase>,
pub overlay: Overlay,
}
#[derive(Clone, Default)]
pub struct WorkingSet {
entries: Vec<WorkingSetEntry>,
}
impl WorkingSet {
pub fn new() -> Self {
Self {
entries: Vec::new(),
}
}
pub fn add(&mut self, project_id: impl Into<String>, base: Arc<IndexBase>) {
let project_id = project_id.into();
let overlay = Overlay::fresh(&base);
let entry = WorkingSetEntry {
project_id: project_id.clone(),
base,
overlay,
};
if let Some(slot) = self.entries.iter_mut().find(|e| e.project_id == project_id) {
*slot = entry;
} else {
self.entries.push(entry);
}
}
pub fn remove(&mut self, project_id: &str) -> Option<WorkingSetEntry> {
let pos = self
.entries
.iter()
.position(|e| e.project_id == project_id)?;
Some(self.entries.remove(pos))
}
pub fn get(&self, project_id: &str) -> Option<&WorkingSetEntry> {
self.entries.iter().find(|e| e.project_id == project_id)
}
pub fn get_mut(&mut self, project_id: &str) -> Option<&mut WorkingSetEntry> {
self.entries.iter_mut().find(|e| e.project_id == project_id)
}
pub fn iter(&self) -> impl Iterator<Item = &WorkingSetEntry> {
self.entries.iter()
}
pub fn len(&self) -> usize {
self.entries.len()
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub fn shares_base_with(&self, project_a: &str, project_b: &str) -> bool {
match (self.get(project_a), self.get(project_b)) {
(Some(a), Some(b)) => Arc::ptr_eq(&a.base, &b.base),
_ => false,
}
}
fn view_for<'a>(entry: &'a WorkingSetEntry) -> Result<IndexView<'a>, StaleOverlay> {
IndexView::new(&entry.base, Some(&entry.overlay))
}
pub fn search_symbols(
&self,
targets: &Targets,
query: &str,
kind_filter: Option<&str>,
options: &SymbolSearchOptions,
) -> Vec<ProjectHit<ViewSymbolHit>> {
let mut out = Vec::new();
for entry in self
.entries
.iter()
.filter(|e| targets.selects(&e.project_id))
{
let Ok(view) = Self::view_for(entry) else {
continue;
};
for hit in view.search_symbols(query, kind_filter, options) {
out.push(ProjectHit {
project_id: entry.project_id.clone(),
hit,
});
}
}
out
}
pub fn search_text(
&self,
targets: &Targets,
query: Option<&str>,
terms: Option<&[String]>,
regex: bool,
options: &TextSearchOptions,
) -> Result<Vec<ProjectHit<TextSearchResult>>, TextSearchError> {
let mut out = Vec::new();
for entry in self
.entries
.iter()
.filter(|e| targets.selects(&e.project_id))
{
let Ok(view) = Self::view_for(entry) else {
continue;
};
let result = view.search_text(query, terms, regex, options)?;
out.push(ProjectHit {
project_id: entry.project_id.clone(),
hit: result,
});
}
Ok(out)
}
pub fn find_references(
&self,
targets: &Targets,
name: &str,
kind_filter: Option<ReferenceKind>,
include_filtered: bool,
) -> Vec<ProjectHit<(String, ReferenceRecord)>> {
let mut out = Vec::new();
for entry in self
.entries
.iter()
.filter(|e| targets.selects(&e.project_id))
{
let Ok(view) = Self::view_for(entry) else {
continue;
};
for (path, reference) in view.find_references(name, kind_filter, include_filtered) {
out.push(ProjectHit {
project_id: entry.project_id.clone(),
hit: (path, reference.clone()),
});
}
}
out
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::{FileClassification, LanguageId};
use crate::live_index::store::{IndexedFile, LiveIndex, ParseStatus};
use std::time::Instant;
fn make_file(path: &str, content: &str) -> IndexedFile {
IndexedFile {
relative_path: path.to_string(),
language: LanguageId::Rust,
classification: FileClassification::for_code_path(path),
content: content.as_bytes().to_vec(),
symbols: Vec::new(),
parse_status: ParseStatus::Parsed,
parse_diagnostic: None,
byte_len: content.len() as u64,
content_hash: format!("h:{}", content.len()),
references: Vec::new(),
alias_map: HashMap::new(),
mtime_secs: 0,
}
}
fn synthetic_index(n: usize) -> LiveIndex {
let mut idx = LiveIndex::empty_live_index();
idx.is_empty = false;
idx.loaded_at = Instant::now();
let mut files: HashMap<String, Arc<IndexedFile>> = HashMap::with_capacity(n);
for i in 0..n {
let path = format!("f{i}.rs");
let file = make_file(&path, &format!("fn f{i}() {{}}"));
files.insert(path, Arc::new(file));
}
idx.files = files;
idx
}
fn base_key(commit: &str) -> BaseKey {
BaseKey::new("/repo/root", CommitId::Sha(commit.to_string()))
}
#[test]
fn proof1_shared_base_no_clone() {
let live = synthetic_index(100);
let probe = Arc::clone(live.files.get("f0.rs").expect("f0"));
let inner_count_baseline = Arc::strong_count(&probe);
let base = Arc::new(IndexBase::new(base_key("c0"), Arc::new(live), 1));
let overlay_1 = Overlay::fresh(&base);
let base_2 = Arc::clone(&base);
let overlay_2 = Overlay::fresh(&base_2);
assert!(
Arc::ptr_eq(&base, &base_2),
"consumers must share one IndexBase allocation"
);
assert!(Arc::ptr_eq(&base.index, &base_2.index));
let _view_1 = IndexView::new(&base, Some(&overlay_1)).expect("v1");
let _view_2 = IndexView::new(&base_2, Some(&overlay_2)).expect("v2");
let inner_count_after = Arc::strong_count(&probe);
assert_eq!(
inner_count_after, inner_count_baseline,
"adding consumer #2 must not clone the base files map \
(inner file Arc strong_count must not increase): {inner_count_baseline} -> {inner_count_after}"
);
}
#[test]
fn proof3_no_forced_reload() {
let live = synthetic_index(3);
let base_v1 = Arc::new(IndexBase::new(base_key("c0"), Arc::new(live), 1));
let overlay_b = Overlay::fresh(&base_v1);
let view_b = IndexView::new(&base_v1, Some(&overlay_b)).expect("B view v1");
assert_eq!(view_b.get_file("f0.rs").unwrap().content, b"fn f0() {}");
let mut live_v2 = synthetic_index(3);
live_v2.files.insert(
"f0.rs".to_string(),
Arc::new(make_file("f0.rs", "REINDEXED")),
);
let _base_v2 = Arc::new(IndexBase::new(base_key("c1"), Arc::new(live_v2), 2));
assert_eq!(
view_b.get_file("f0.rs").unwrap().content,
b"fn f0() {}",
"B's view over the prior snapshot must be unaffected by A's reindex"
);
}
#[test]
fn proof4_stale_fence() {
let live = synthetic_index(2);
let base_v1 = IndexBase::new(base_key("c0"), Arc::new(live), 1);
let overlay = Overlay::fresh(&base_v1);
assert!(overlay.is_valid_against(&base_v1));
let base_v2 = IndexBase::new(
base_key("c1"),
Arc::clone(&base_v1.index),
base_v1.base_generation + 1,
);
match IndexView::new(&base_v2, Some(&overlay)) {
Err(err) => assert_eq!(
err,
StaleOverlay {
base_generation: 2,
overlay_generation: 1
},
"reading a gen-1 overlay against a gen-2 base must be StaleOverlay"
),
Ok(_) => panic!("expected StaleOverlay, got a valid view"),
}
assert!(!overlay.is_valid_against(&base_v2));
}
use crate::domain::{SymbolKind, SymbolRecord};
fn make_file_with_symbol(path: &str, symbol_name: &str, content: &str) -> IndexedFile {
let mut file = make_file(path, content);
file.symbols = vec![SymbolRecord {
name: symbol_name.to_string(),
kind: SymbolKind::Function,
depth: 0,
sort_order: 0,
byte_range: (0, content.len() as u32),
line_range: (0, 0),
doc_byte_range: None,
item_byte_range: None,
}];
file
}
fn base_with_symbol(
root: &str,
commit: &str,
symbol_name: &str,
content: &str,
) -> Arc<IndexBase> {
let mut idx = LiveIndex::empty_live_index();
idx.is_empty = false;
idx.loaded_at = Instant::now();
let mut files: HashMap<String, Arc<IndexedFile>> = HashMap::new();
files.insert(
"f.rs".to_string(),
Arc::new(make_file_with_symbol("f.rs", symbol_name, content)),
);
idx.trigram_index = crate::live_index::trigram::TrigramIndex::build_from_files(&files);
idx.files = files;
idx.rebuild_reverse_index();
Arc::new(IndexBase::new(
BaseKey::new(root, CommitId::Sha(commit.to_string())),
Arc::new(idx),
1,
))
}
#[test]
fn working_set_add_remove_get() {
let mut ws = WorkingSet::new();
assert!(ws.is_empty());
let base_a = base_with_symbol("/a", "c0", "alpha_fn", "fn alpha_fn() {}");
let base_b = base_with_symbol("/b", "c0", "beta_fn", "fn beta_fn() {}");
ws.add("A", Arc::clone(&base_a));
ws.add("B", Arc::clone(&base_b));
assert_eq!(ws.len(), 2);
assert!(ws.get("A").is_some());
assert!(ws.get("B").is_some());
assert!(ws.get("missing").is_none());
let removed = ws.remove("A").expect("A present");
assert_eq!(removed.project_id, "A");
assert!(ws.get("A").is_none());
assert_eq!(ws.len(), 1);
assert!(ws.remove("A").is_none());
let ids: Vec<&str> = ws.iter().map(|e| e.project_id.as_str()).collect();
assert_eq!(ids, vec!["B"]);
}
#[test]
fn cross_project_search_is_source_attributed() {
let mut ws = WorkingSet::new();
ws.add(
"A",
base_with_symbol("/a", "c0", "shared_a", "fn shared_a() {}"),
);
ws.add(
"B",
base_with_symbol("/b", "c0", "shared_b", "fn shared_b() {}"),
);
ws.add(
"C",
base_with_symbol("/c", "c0", "shared_c", "fn shared_c() {}"),
);
let hits = ws.search_symbols(
&Targets::All,
"shared",
None,
&SymbolSearchOptions::for_current_code_search(usize::MAX),
);
assert_eq!(hits.len(), 3, "one hit per project");
let mut by_project: HashMap<&str, &str> = HashMap::new();
for h in &hits {
by_project.insert(h.project_id.as_str(), h.hit.name.as_str());
}
assert_eq!(by_project.get("A"), Some(&"shared_a"));
assert_eq!(by_project.get("B"), Some(&"shared_b"));
assert_eq!(by_project.get("C"), Some(&"shared_c"));
}
#[test]
fn cross_project_search_respects_targets() {
let mut ws = WorkingSet::new();
ws.add(
"A",
base_with_symbol("/a", "c0", "shared_a", "fn shared_a() {}"),
);
ws.add(
"B",
base_with_symbol("/b", "c0", "shared_b", "fn shared_b() {}"),
);
ws.add(
"C",
base_with_symbol("/c", "c0", "shared_c", "fn shared_c() {}"),
);
let one = ws.search_symbols(
&Targets::One("B".to_string()),
"shared",
None,
&SymbolSearchOptions::for_current_code_search(usize::MAX),
);
assert_eq!(one.len(), 1);
assert_eq!(one[0].project_id, "B");
assert_eq!(one[0].hit.name, "shared_b");
let subset = ws.search_symbols(
&Targets::Subset(vec!["A".to_string(), "C".to_string()]),
"shared",
None,
&SymbolSearchOptions::for_current_code_search(usize::MAX),
);
let projects: std::collections::BTreeSet<&str> =
subset.iter().map(|h| h.project_id.as_str()).collect();
assert_eq!(
projects,
["A", "C"]
.into_iter()
.collect::<std::collections::BTreeSet<_>>()
);
let none = ws.search_symbols(
&Targets::One("Z".to_string()),
"shared",
None,
&SymbolSearchOptions::for_current_code_search(usize::MAX),
);
assert!(none.is_empty());
}
#[test]
fn working_set_shares_base_arc() {
let mut ws = WorkingSet::new();
let shared = base_with_symbol("/repo", "c0", "f", "fn f() {}");
ws.add("view1", Arc::clone(&shared));
ws.add("view2", Arc::clone(&shared));
let a = ws.get("view1").unwrap();
let b = ws.get("view2").unwrap();
assert!(
Arc::ptr_eq(&a.base, &b.base),
"two entries on the same shared Arc<IndexBase> must be one allocation (SC-002)"
);
assert!(Arc::ptr_eq(&a.base.index, &b.base.index));
assert!(ws.shares_base_with("view1", "view2"));
ws.add("other", base_with_symbol("/other", "c0", "g", "fn g() {}"));
assert!(!ws.shares_base_with("view1", "other"));
}
#[test]
fn cross_project_text_search_attribution() {
let mut ws = WorkingSet::new();
ws.add("A", base_with_symbol("/a", "c0", "f", "let NEEDLE = 1;"));
ws.add("B", base_with_symbol("/b", "c0", "g", "let other = 2;"));
ws.add("C", base_with_symbol("/c", "c0", "h", "let NEEDLE = 3;"));
let results = ws
.search_text(
&Targets::All,
Some("NEEDLE"),
None,
false,
&TextSearchOptions::for_current_code_search(),
)
.expect("cross-project text search");
let with_matches: Vec<&str> = results
.iter()
.filter(|r| r.hit.total_matches > 0)
.map(|r| r.project_id.as_str())
.collect();
assert_eq!(with_matches, vec!["A", "C"]);
}
#[test]
fn working_set_skips_stale_overlay_entry() {
let mut ws = WorkingSet::new();
ws.add(
"A",
base_with_symbol("/a", "c0", "shared_a", "fn shared_a() {}"),
);
ws.get_mut("A").unwrap().overlay.base_generation = 999;
let hits = ws.search_symbols(
&Targets::All,
"shared",
None,
&SymbolSearchOptions::for_current_code_search(usize::MAX),
);
assert!(
hits.is_empty(),
"a stale-overlay entry must be skipped, not served"
);
}
fn base_with_files(
root: &str,
commit: &str,
files: &[(&str, LanguageId, &str)],
) -> Arc<IndexBase> {
let mut idx = LiveIndex::empty_live_index();
idx.is_empty = false;
idx.loaded_at = Instant::now();
let mut map: HashMap<String, Arc<IndexedFile>> = HashMap::new();
for (path, lang, sym) in files {
let mut file = make_file_with_symbol(path, sym, &format!("fn {sym}() {{}}"));
file.language = lang.clone();
map.insert((*path).to_string(), Arc::new(file));
}
idx.trigram_index = crate::live_index::trigram::TrigramIndex::build_from_files(&map);
idx.files = map;
idx.rebuild_reverse_index();
Arc::new(IndexBase::new(
BaseKey::new(root, CommitId::Sha(commit.to_string())),
Arc::new(idx),
1,
))
}
#[test]
fn cross_project_search_honors_scoping_options() {
use crate::live_index::search::PathScope;
let mut ws = WorkingSet::new();
ws.add(
"A",
base_with_files(
"/a",
"c0",
&[
("src/keep.rs", LanguageId::Rust, "widget"),
("other/skip.rs", LanguageId::Rust, "widget"),
("src/also.py", LanguageId::Python, "widget"),
],
),
);
let mut scoped = SymbolSearchOptions::for_current_code_search(usize::MAX);
scoped.path_scope = PathScope::prefix("src");
let hits = ws.search_symbols(&Targets::All, "widget", None, &scoped);
let paths: std::collections::BTreeSet<&str> =
hits.iter().map(|h| h.hit.path.as_str()).collect();
assert!(
paths.contains("src/keep.rs") && paths.contains("src/also.py"),
"src/-scoped search must include the src/ hits: {paths:?}"
);
assert!(
!paths.contains("other/skip.rs"),
"src/-scoped search must EXCLUDE other/ hits (path scoping honored): {paths:?}"
);
let mut rust_only = SymbolSearchOptions::for_current_code_search(usize::MAX);
rust_only.language_filter = Some(LanguageId::Rust);
let rust_hits = ws.search_symbols(&Targets::All, "widget", None, &rust_only);
let rust_paths: std::collections::BTreeSet<&str> =
rust_hits.iter().map(|h| h.hit.path.as_str()).collect();
assert!(
rust_paths.contains("src/keep.rs") && rust_paths.contains("other/skip.rs"),
"Rust-filtered search keeps the Rust files: {rust_paths:?}"
);
assert!(
!rust_paths.contains("src/also.py"),
"Rust-filtered search must EXCLUDE the Python file (language scoping honored): {rust_paths:?}"
);
let bounded = SymbolSearchOptions::for_current_code_search(1);
let bounded_hits = ws.search_symbols(&Targets::All, "widget", None, &bounded);
assert_eq!(
bounded_hits.len(),
1,
"result_limit=1 bounds the per-project hits (D14): got {}",
bounded_hits.len()
);
}
#[test]
fn overlay_preserves_base_test_suppression_count() {
let content = "fn real() {}\n#[cfg(test)]\nmod tests {\n let needle = 1;\n}\n";
let mut lib = make_file("src/lib.rs", content);
lib.symbols = vec![SymbolRecord {
name: "tests".to_string(),
kind: SymbolKind::Module,
depth: 0,
sort_order: 0,
byte_range: (0, content.len() as u32),
line_range: (1, 4),
doc_byte_range: None,
item_byte_range: None,
}];
let mut map: HashMap<String, Arc<IndexedFile>> = HashMap::new();
map.insert("src/lib.rs".to_string(), Arc::new(lib));
let mut idx = LiveIndex::empty_live_index();
idx.is_empty = false;
idx.loaded_at = Instant::now();
idx.trigram_index = crate::live_index::trigram::TrigramIndex::build_from_files(&map);
idx.files = map;
let base = Arc::new(IndexBase::new(base_key("c0"), Arc::new(idx), 1));
let opts = TextSearchOptions::for_current_code_search();
let base_view = IndexView::new(&base, None).expect("base view");
let base_result = base_view
.search_text(Some("needle"), None, false, &opts)
.expect("base search");
assert!(
base_result.files.is_empty(),
"no visible source hits in base"
);
assert_eq!(
base_result.suppressed_by_noise, 1,
"base must count the #[cfg(test)] match as suppressed"
);
let mut overlay = Overlay::fresh(&base);
overlay.deltas.insert(
"src/other.rs".to_string(),
FileDelta::Upsert(Arc::new(make_file("src/other.rs", "fn other() {}\n"))),
);
let view = IndexView::new(&base, Some(&overlay)).expect("overlay view");
let result = view
.search_text(Some("needle"), None, false, &opts)
.expect("overlay search");
assert!(
result.files.is_empty(),
"still no visible source hits after the overlay merge"
);
assert!(
result.suppressed_by_noise >= 1,
"dirty-tree overlay must preserve the base test-match suppression count, got {}",
result.suppressed_by_noise
);
}
}