use std::collections::HashMap;
use crate::tools::code_language::CodeLanguage;
use crate::tools::code_search::{CodeSearchResult, CodeSearchResultType, ResolvedSearchScope};
use crate::tools::grep_file::LiteralSearchCandidate;
use crate::tools::outline_search::DeclarationFileRecord;
use crate::types::CompactStr;
use std::path::PathBuf;
use super::ranking::{DeclarationInventory, RankedCandidate};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SourceByteRange {
pub start: usize,
pub end: usize,
}
pub fn process_declaration_file(
scope: &ResolvedSearchScope,
languages: &[CodeLanguage],
file: &DeclarationFileRecord,
definition_enabled: bool,
source_cache: &mut HashMap<PathBuf, String>,
inventories: &mut HashMap<PathBuf, DeclarationInventory>,
candidates: &mut Vec<RankedCandidate>,
) {
let Some((canonical, relative)) =
super::scope::accepted_candidate_path(scope, &scope.workspace_root, &file.path, languages)
else {
return;
};
let Ok(source) = std::fs::read_to_string(&canonical) else {
inventories.insert(canonical, DeclarationInventory::default());
return;
};
let mut inventory = DeclarationInventory {
complete: file.complete,
exact_name_ranges: Vec::new(),
};
for declaration in &file.declarations {
let name_range = SourceByteRange {
start: declaration.range.byte_start,
end: declaration.range.byte_end,
};
inventory.exact_name_ranges.push(name_range);
if definition_enabled {
candidates.push(RankedCandidate {
result: CodeSearchResult {
result_type: CodeSearchResultType::Definition,
path: relative.clone(),
line: Some(declaration.line),
column: Some(declaration.column),
name: Some(CompactStr::from(declaration.name.as_str())),
snippet: Some(super::normalised_snippet(super::source_line(&source, declaration.line))),
},
backend_ordinal: 0,
});
}
}
inventories.insert(canonical.clone(), inventory);
source_cache.insert(canonical, source);
}
#[derive(Default)]
pub struct SourceTreeCache {
pub sources: HashMap<PathBuf, String>,
pub masks: HashMap<PathBuf, Vec<bool>>,
}
pub fn classify_literal_candidates(
scope: &ResolvedSearchScope,
languages: &[CodeLanguage],
literals: Vec<LiteralSearchCandidate>,
usage_enabled: bool,
text_enabled: bool,
outline_stream_complete: bool,
caches: &mut SourceTreeCache,
inventories: &HashMap<PathBuf, DeclarationInventory>,
candidates: &mut Vec<RankedCandidate>,
) {
for literal in literals {
let Some((canonical, relative)) =
super::scope::accepted_candidate_path(scope, &scope.workspace_root, &literal.path, languages)
else {
continue;
};
let range = SourceByteRange { start: literal.byte_start, end: literal.byte_end };
let language = CodeLanguage::from_path(&canonical);
let inventory = inventories.get(&canonical);
let is_definition = inventory.is_some_and(|inventory| inventory.exact_name_ranges.contains(&range));
if is_definition {
continue;
}
let can_classify_usage = usage_enabled
&& language.is_some_and(|language| language.supports_usage_classification())
&& inventory.map_or(outline_stream_complete, |inventory| inventory.complete);
let is_usage = if can_classify_usage {
let source = caches
.sources
.entry(canonical.clone())
.or_insert_with(|| std::fs::read_to_string(&canonical).unwrap_or_default())
.clone();
is_exact_identifier_hit(&literal, &source, language, caches)
} else {
false
};
if is_usage {
candidates.push(RankedCandidate {
result: CodeSearchResult {
result_type: CodeSearchResultType::Usage,
path: relative,
line: Some(literal.line),
column: Some(literal.column),
name: Some(CompactStr::from(literal.matched_text.as_str())),
snippet: Some(super::normalised_snippet(&literal.snippet)),
},
backend_ordinal: 1,
});
} else if text_enabled {
candidates.push(RankedCandidate {
result: CodeSearchResult {
result_type: CodeSearchResultType::Text,
path: relative,
line: Some(literal.line),
column: Some(literal.column),
name: None,
snippet: Some(super::normalised_snippet(&literal.snippet)),
},
backend_ordinal: 2,
});
}
}
}
fn is_exact_identifier_hit(
literal: &LiteralSearchCandidate,
source: &str,
language: Option<CodeLanguage>,
caches: &mut SourceTreeCache,
) -> bool {
if !literal.word_bounded || literal.matched_text.is_empty() {
return false;
}
let Some(language) = language else {
return false;
};
let mask = caches
.masks
.entry(literal.path.clone())
.or_insert_with(|| vtcode_commons::lexical::code_mask(source, &language.lexical_syntax()));
mask.get(literal.byte_start..literal.byte_end)
.is_some_and(|bytes| bytes.iter().all(|is_code| *is_code))
}