Files
nuwiki-rs/crates/nuwiki-core/src/syntax/registry.rs
T
gffranco 29a4efb6bc
CI / cargo fmt --check (push) Successful in 59s
CI / cargo clippy (push) Successful in 1m20s
CI / cargo test (push) Successful in 1m29s
CI / editor keymaps (push) Failing after 33s
feat: initial nuwiki Rust workspace (core, lsp, ls)
2026-06-24 00:01:59 +00:00

59 lines
1.6 KiB
Rust

//! Registry of syntax plugins.
//!
//! Plugins are stored behind `Arc<dyn SyntaxPlugin>` so the LSP layer can
//! hand a cheap clone to per-document tasks without re-locking the registry.
use std::sync::Arc;
use super::SyntaxPlugin;
#[derive(Default, Clone)]
pub struct SyntaxRegistry {
plugins: Vec<Arc<dyn SyntaxPlugin>>,
}
impl SyntaxRegistry {
pub fn new() -> Self {
Self::default()
}
/// Register a plugin owned directly. The most common shape — implementer
/// hands over a value, registry takes ownership.
pub fn register<P>(&mut self, plugin: P)
where
P: SyntaxPlugin + 'static,
{
self.plugins.push(Arc::new(plugin));
}
/// Look up a plugin by its `id`.
pub fn get(&self, id: &str) -> Option<&dyn SyntaxPlugin> {
self.plugins
.iter()
.find(|p| p.id() == id)
.map(|p| p.as_ref())
}
/// Look up a plugin by file extension. `ext` should include the leading
/// dot (e.g. `".wiki"`) — matches the plugin's `file_extensions()`.
pub fn detect_from_extension(&self, ext: &str) -> Option<&dyn SyntaxPlugin> {
self.plugins
.iter()
.find(|p| p.file_extensions().iter().any(|e| *e == ext))
.map(|p| p.as_ref())
}
/// Iterate over registered plugins in registration order.
pub fn iter(&self) -> impl Iterator<Item = &dyn SyntaxPlugin> + '_ {
self.plugins.iter().map(|p| p.as_ref())
}
pub fn len(&self) -> usize {
self.plugins.len()
}
pub fn is_empty(&self) -> bool {
self.plugins.is_empty()
}
}