1use anyhow::{Context, Result};
4
5use crate::content_store::ContentReader;
6use crate::trigram::TrigramIndex;
7
8pub fn find_file_id(content_reader: &ContentReader, target_path: &str) -> Option<u32> {
10 for file_id in 0..content_reader.file_count() {
11 if let Some(path) = content_reader.get_file_path(file_id as u32) {
12 if path.to_string_lossy() == target_path {
13 return Some(file_id as u32);
14 }
15 }
16 }
17 None
18}
19
20pub fn rebuild_trigram_index(content_reader: &ContentReader) -> Result<TrigramIndex> {
22 log::debug!(
23 "Rebuilding trigram index from {} files",
24 content_reader.file_count()
25 );
26 let mut trigram_index = TrigramIndex::new();
27
28 for file_id in 0..content_reader.file_count() {
29 let file_path = content_reader
30 .get_file_path(file_id as u32)
31 .context("Invalid file_id")?
32 .to_path_buf();
33 let content = content_reader.get_file_content(file_id as u32)?;
34
35 let idx = trigram_index.add_file(file_path);
36 trigram_index.index_file(idx, content);
37 }
38
39 trigram_index.finalize();
40 log::debug!(
41 "Trigram index rebuilt with {} trigrams",
42 trigram_index.trigram_count()
43 );
44
45 Ok(trigram_index)
46}
47
48pub fn normalize_glob_pattern(pattern: &str) -> String {
55 if pattern.starts_with('.') || pattern.starts_with('/') || pattern.starts_with('*') {
56 pattern.to_string()
57 } else {
58 format!("./{}", pattern)
59 }
60}