1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! Initial mode provider for cross-personality support (#623).
//!
//! Personality modules (vim, emacs) register their initial mode during
//! `init()`. Bootstrap reads it after all modules have initialized to
//! determine which mode to start in.
//!
//! This follows the established `ServiceRegistry` pattern used by
//! `LookupPolicyStore`, `KeybindingStore`, `ModeBridgeStore`, etc.
use reovim_kernel::api::v1::{ModeId, Service};
/// Provider for the initial editor mode.
///
/// Personality modules call [`set`](Self::set) during `init()` to declare
/// their initial mode. Bootstrap calls [`get`](Self::get) after all modules
/// have loaded to determine the startup mode.
///
/// If multiple personality modules are loaded (e.g., both vim and emacs),
/// the last writer wins and a warning is logged.
pub struct InitialModeProvider {
mode: parking_lot::RwLock<Option<ModeId>>,
}
impl InitialModeProvider {
/// Create a new empty provider.
#[must_use]
pub const fn new() -> Self {
Self {
mode: parking_lot::RwLock::new(None),
}
}
/// Set the initial mode. Called by personality modules during `init()`.
///
/// If a mode was already set (another personality module registered first),
/// the previous value is overwritten. The caller (personality module) is
/// responsible for logging if needed.
///
/// Returns the previous mode if one was already set.
pub fn set(&self, mode: ModeId) -> Option<ModeId> {
self.mode.write().replace(mode)
}
/// Get the registered initial mode, if any.
#[must_use]
pub fn get(&self) -> Option<ModeId> {
self.mode.read().clone()
}
}
impl Default for InitialModeProvider {
fn default() -> Self {
Self::new()
}
}
impl Service for InitialModeProvider {}
#[cfg(test)]
#[path = "initial_mode_tests.rs"]
mod tests;