Mountain/ApplicationState/State/ExtensionState/ProviderRegistration/
ProviderRegistration.rs

1//! # ProviderRegistration Module (ApplicationState)
2//!
3//! ## RESPONSIBILITIES
4//! Manages language providers registration state including completion,
5//! hover, document symbol, and other language feature providers.
6//!
7//! ## ARCHITECTURAL ROLE
8//! ProviderRegistration is part of the **ExtensionState** module, representing
9//! language provider registration state.
10//!
11//! ## KEY COMPONENTS
12//! - Registration: Main struct containing language providers map
13//! - Default: Initialization implementation
14//! - Helper methods: Provider manipulation utilities
15//!
16//! ## ERROR HANDLING
17//! - Thread-safe access via `Arc<Mutex<...>>`
18//! - Proper lock error handling with `MapLockError` helpers
19//!
20//! ## LOGGING
21//! State changes are logged at appropriate levels (debug, info, warn, error).
22//!
23//! ## PERFORMANCE CONSIDERATIONS
24//! - Lock mutexes briefly and release immediately
25//! - Avoid nested locks to prevent deadlocks
26//! - Use Arc for shared ownership across threads
27//!
28//! ## TODO
29//! - [ ] Add provider validation invariants
30//! - [ ] Implement provider lifecycle events
31//! - [ ] Add provider metrics collection
32
33use std::{
34	collections::HashMap,
35	sync::{Arc, Mutex as StandardMutex},
36};
37
38use log::debug;
39
40use crate::ApplicationState::DTO::ProviderRegistrationDTO::ProviderRegistrationDTO;
41
42/// Language provider registration state.
43#[derive(Clone)]
44pub struct Registration {
45	/// Registered language providers by handle.
46	pub LanguageProviders:Arc<StandardMutex<HashMap<u32, ProviderRegistrationDTO>>>,
47}
48
49impl Default for Registration {
50	fn default() -> Self {
51		debug!("[ProviderRegistration] Initializing default provider registration...");
52
53		Self { LanguageProviders:Arc::new(StandardMutex::new(HashMap::new())) }
54	}
55}
56
57impl Registration {
58	/// Gets all registered language providers.
59	pub fn GetProviders(&self) -> HashMap<u32, ProviderRegistrationDTO> {
60		self.LanguageProviders
61			.lock()
62			.ok()
63			.map(|guard| guard.clone())
64			.unwrap_or_default()
65	}
66
67	/// Gets a provider by its handle.
68	pub fn GetProvider(&self, handle:u32) -> Option<ProviderRegistrationDTO> {
69		self.LanguageProviders.lock().ok().and_then(|guard| guard.get(&handle).cloned())
70	}
71
72	/// Registers a language provider.
73	pub fn RegisterProvider(&self, handle:u32, provider:ProviderRegistrationDTO) {
74		if let Ok(mut guard) = self.LanguageProviders.lock() {
75			guard.insert(handle, provider);
76			debug!("[ProviderRegistration] Provider registered with handle: {}", handle);
77		}
78	}
79
80	/// Unregisters a language provider.
81	pub fn UnregisterProvider(&self, handle:u32) {
82		if let Ok(mut guard) = self.LanguageProviders.lock() {
83			guard.remove(&handle);
84			debug!("[ProviderRegistration] Provider unregistered with handle: {}", handle);
85		}
86	}
87
88	/// Gets all providers for a specific language.
89	pub fn GetProvidersForLanguage(&self, language:&str) -> Vec<ProviderRegistrationDTO> {
90		self.LanguageProviders
91			.lock()
92			.ok()
93			.map(|guard| guard.values().filter(|p| p.MatchesSelector("", language)).cloned().collect())
94			.unwrap_or_default()
95	}
96}