use std::sync::Arc;
use {parking_lot::RwLock, reovim_kernel::api::v1::Service};
use crate::SyntaxDriverFactory;
#[derive(Default)]
pub struct SyntaxFactoryStore {
factories: RwLock<Vec<Arc<dyn SyntaxDriverFactory>>>,
}
impl SyntaxFactoryStore {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn add(&self, factory: Arc<dyn SyntaxDriverFactory>) {
self.factories.write().push(factory);
}
pub fn take_factories(&self) -> Vec<Arc<dyn SyntaxDriverFactory>> {
std::mem::take(&mut *self.factories.write())
}
#[must_use]
pub fn find(&self, language_id: &str) -> Option<Arc<dyn SyntaxDriverFactory>> {
self.factories
.read()
.iter()
.find(|f| f.supports(language_id))
.cloned()
}
#[must_use]
pub fn len(&self) -> usize {
self.factories.read().len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.factories.read().is_empty()
}
}
impl Service for SyntaxFactoryStore {}
impl std::fmt::Debug for SyntaxFactoryStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SyntaxFactoryStore")
.field("count", &self.len())
.finish()
}
}
#[cfg(test)]
#[path = "store_tests.rs"]
mod tests;