use std::path::{Path, PathBuf};
use std::sync::{Mutex, OnceLock};
use tracing::warn;
use crate::command_safety::shell_parser::prewarm_bash_parser;
use crate::utils::common::detect_workspace_languages;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchToolReadiness {
Ready,
Missing,
Error,
}
impl SearchToolReadiness {
#[must_use]
pub fn is_ready(self) -> bool {
matches!(self, Self::Ready)
}
#[must_use]
pub fn label(self) -> &'static str {
match self {
Self::Ready => "ready",
Self::Missing => "missing",
Self::Error => "error",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SearchToolBundleStatus {
pub content_search: SearchToolReadiness,
pub declaration_search: SearchToolReadiness,
}
impl SearchToolBundleStatus {
#[must_use]
pub fn all_ready(self) -> bool {
self.content_search.is_ready() && self.declaration_search.is_ready()
}
#[must_use]
pub fn all_unavailable(self) -> bool {
!self.content_search.is_ready() && !self.declaration_search.is_ready()
}
#[must_use]
pub fn has_errors(self) -> bool {
matches!(self.content_search, SearchToolReadiness::Error)
|| matches!(self.declaration_search, SearchToolReadiness::Error)
}
#[must_use]
pub fn header_summary(self) -> String {
if self.all_ready() {
return "Search: native".to_string();
}
format!(
"Search: content {} \u{00b7} declarations {}",
self.content_search.label(),
self.declaration_search.label()
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct SearchRuntimeSnapshot {
pub(crate) workspace_languages: Vec<String>,
pub(crate) search_tools: SearchToolBundleStatus,
pub(crate) bash_tree_sitter_ready: bool,
}
const SEARCH_RUNTIME_CACHE_CAP: usize = 16;
static SEARCH_RUNTIME_CACHE: OnceLock<Mutex<lru::LruCache<PathBuf, SearchRuntimeSnapshot>>> = OnceLock::new();
pub(crate) fn snapshot_for_workspace(workspace_root: &Path) -> SearchRuntimeSnapshot {
let workspace_root = workspace_root.to_path_buf();
let cache = SEARCH_RUNTIME_CACHE.get_or_init(|| {
let capacity = std::num::NonZeroUsize::new(SEARCH_RUNTIME_CACHE_CAP)
.expect("SEARCH_RUNTIME_CACHE_CAP is a non-zero const");
Mutex::new(lru::LruCache::new(capacity))
});
{
let mut cache_guard = cache.lock().unwrap_or_else(|poisoned| {
warn!("search runtime cache mutex poisoned; recovering");
poisoned.into_inner()
});
if let Some(snapshot) = cache_guard.get(&workspace_root).cloned() {
return snapshot;
}
}
let workspace_languages = detect_workspace_languages(&workspace_root);
let search_tools = SearchToolBundleStatus {
content_search: SearchToolReadiness::Ready,
declaration_search: SearchToolReadiness::Ready,
};
let snapshot = SearchRuntimeSnapshot {
workspace_languages,
search_tools,
bash_tree_sitter_ready: prewarm_bash_parser().is_ok(),
};
let mut guard = cache.lock().unwrap_or_else(|poisoned| {
warn!("search runtime cache mutex poisoned; recovering");
poisoned.into_inner()
});
guard.get_or_insert(workspace_root, || snapshot.clone()).clone()
}
pub fn dominant_workspace_language(workspace_root: &Path) -> Option<String> {
snapshot_for_workspace(workspace_root).workspace_languages.into_iter().next()
}
pub fn search_tool_bundle_status(workspace_root: &Path) -> SearchToolBundleStatus {
snapshot_for_workspace(workspace_root).search_tools
}