fresh/app/window_resources.rs
1//! Editor-global resources shared into every `Window` by `Arc` clone.
2//!
3//! ## Why this exists
4//!
5//! `Window` was originally constructed with only its own per-project state
6//! (buffers, splits, file_explorer, lsp, …) and reached back to `Editor`
7//! for everything else (`config`, `theme`, `plugin_manager`, registries,
8//! filesystem authority). The reach-back forced almost every handler to
9//! live on `impl Editor` rather than `impl Window`, because the body
10//! needed `&self` *as Editor* to read those resources.
11//!
12//! `WindowResources` flips that: every editor-global service a handler
13//! could plausibly need is shared into `Window` as an `Arc<…>` clone (or
14//! `Clone`-by-value for handles that already carry their own `Arc`s,
15//! like `Authority`). A `Window` method now has direct access to
16//! `self.config.editor.line_wrap`, `self.authority.path_translation`,
17//! etc., without any `Editor` reference. Methods that previously had to
18//! sit on `impl Editor` to read these can move to `impl Window`.
19//!
20//! ## What stays on `Editor` (not in `WindowResources`)
21//!
22//! - `next_buffer_id` allocator (separate concept — see
23//! [`BufferIdAllocator`])
24//! - `theme: Theme` — direct value (not `Arc`); pending Tier-2 migration
25//! - `clipboard: Clipboard` — owned value; pending Tier-2 migration
26//! - `mode_registry: ModeRegistry` — owned value; pending wrap
27//! - `quick_open_registry: QuickOpenRegistry` — owned value; pending wrap
28//! - `event_broadcaster: EventBroadcaster` — owned value; needs check
29//! - `plugin_manager: PluginManager` — needs `Arc<Mutex<…>>` wrapping
30//! when the first hook-firing handler migrates to `impl Window`
31//! - All `*_registry` types currently owned by value
32//!
33//! These are deliberately deferred: they can be added to `WindowResources`
34//! incrementally as method migrations surface the need. Foundation PR
35//! lands what's cheap to share today; later PRs widen the surface as
36//! needed by each `impl Window` move.
37
38use crate::config::Config;
39use crate::config_io::DirectoryContext;
40use crate::input::command_registry::CommandRegistry;
41use crate::input::keybindings::KeybindingResolver;
42use crate::model::filesystem::FileSystem;
43use crate::primitives::grammar::GrammarRegistry;
44use crate::services::authority::Authority;
45use crate::services::fs::FsManager;
46use crate::services::time_source::SharedTimeSource;
47use crate::view::theme::ThemeRegistry;
48use fresh_core::BufferId;
49use std::collections::HashMap;
50use std::sync::atomic::{AtomicUsize, Ordering};
51use std::sync::{Arc, RwLock};
52
53/// Globally-unique `BufferId` allocator shared across every `Window`.
54///
55/// `BufferId`s must be unique editor-wide so plugin APIs that thread
56/// ids around (`editor.openFile(...) -> BufferId`, `setActiveBuffer(id)`,
57/// terminal `terminalId` correlation, recovery files keyed by id) don't
58/// have to disambiguate by `WindowId`. The allocator is a single
59/// `Arc<AtomicUsize>` cloned into every `Window` — concurrent
60/// `next()` calls return distinct ids without locking.
61#[derive(Debug, Clone)]
62pub struct BufferIdAllocator(Arc<AtomicUsize>);
63
64impl BufferIdAllocator {
65 /// Construct an allocator starting at `start`. The first `next()`
66 /// call returns `BufferId(start)`.
67 pub fn new(start: usize) -> Self {
68 Self(Arc::new(AtomicUsize::new(start)))
69 }
70
71 /// Allocate the next `BufferId`. Thread-safe and lock-free.
72 pub fn next(&self) -> BufferId {
73 BufferId(self.0.fetch_add(1, Ordering::Relaxed))
74 }
75
76 /// Peek at the value the next `next()` call would return without
77 /// advancing. Test-only — production code should always use `next()`.
78 #[doc(hidden)]
79 pub fn peek(&self) -> usize {
80 self.0.load(Ordering::Relaxed)
81 }
82
83 /// Restore the counter to a specific value (used by workspace
84 /// rehydration so persisted ids don't collide with freshly-allocated
85 /// ones after restart).
86 pub fn set(&self, value: usize) {
87 self.0.store(value, Ordering::Relaxed);
88 }
89}
90
91/// Editor-global resources that every `Window` holds an `Arc`-cloned
92/// reference to.
93///
94/// One instance is constructed in `editor_init` and cloned into the base
95/// `Window`. `Editor::create_window` and the first-dive seed path in
96/// `set_active_window` clone it into every subsequent `Window`. All
97/// fields are cheap to clone (`Arc` increment or `Clone`-by-value where
98/// the inner type already carries `Arc`s like `Authority`).
99///
100/// A `Window` handler that needs any of these reads it directly:
101/// `self.resources.config.editor.line_wrap`,
102/// `self.resources.authority.path_translation`, etc. The
103/// [`Window::config()`] / `Window::authority()` accessors are the
104/// canonical reading API; the field itself stays `pub(crate)` so call
105/// sites can split-borrow disjoint sub-fields when the borrow checker
106/// needs it.
107#[derive(Clone)]
108pub struct WindowResources {
109 /// Read-only editor configuration. Hot-reloaded by swapping the
110 /// `Arc`'s pointee when the user edits their config file.
111 pub config: Arc<Config>,
112
113 /// Tree-sitter grammar registry. `Arc` because grammar loading
114 /// can be expensive and is shared across windows.
115 pub grammar_registry: Arc<GrammarRegistry>,
116
117 /// Theme registry (the *catalogue* of available themes, not the
118 /// active theme — that's `Editor::theme` for now, pending the
119 /// Tier-2 wrap).
120 pub theme_registry: Arc<ThemeRegistry>,
121
122 /// Cache of plugin-supplied theme JSONs, populated by plugin
123 /// commands and read by the theme loader.
124 pub theme_cache: Arc<RwLock<HashMap<String, serde_json::Value>>>,
125
126 /// Keybinding resolver (mode → key → command map). `RwLock` because
127 /// plugin commands can mutate the resolver at runtime.
128 pub keybindings: Arc<RwLock<KeybindingResolver>>,
129
130 /// Command registry (named commands a plugin or user can invoke).
131 /// `RwLock` for the same reason as `keybindings`.
132 pub command_registry: Arc<RwLock<CommandRegistry>>,
133
134 /// Filesystem operation manager (background renames, deletes, etc.)
135 pub fs_manager: Arc<FsManager>,
136
137 /// Direct host-filesystem handle. Held alongside the active
138 /// authority's filesystem because some operations (recovery file
139 /// IO, history persistence) intentionally bypass the authority's
140 /// translation.
141 pub local_filesystem: Arc<dyn FileSystem + Send + Sync>,
142
143 /// Globally-unique `BufferId` allocator (see [`BufferIdAllocator`]).
144 pub buffer_id_alloc: BufferIdAllocator,
145
146 /// Active filesystem authority (local / devcontainer / remote).
147 /// `Authority` is `Clone` because it internally holds `Arc`s for
148 /// the filesystem and path-translation handles; cloning here gives
149 /// each window an independent handle that points at the same
150 /// underlying authority.
151 pub authority: Authority,
152
153 /// Editor-wide time source (real clock in production, controllable
154 /// in tests). Already `Arc`-internal.
155 pub time_source: SharedTimeSource,
156
157 /// Directory context (config dir, themes dir, plugins dir, etc.).
158 /// Cloned by value because it's a small struct of `PathBuf`s.
159 pub dir_context: DirectoryContext,
160
161 /// Tokio runtime for async I/O tasks (LSP, file watchers, git, etc.).
162 /// Single runtime shared across all windows via `Arc`. `None` means
163 /// the editor was constructed without async support (rare).
164 pub tokio_runtime: Option<Arc<tokio::runtime::Runtime>>,
165
166 /// Async-message bridge (Sender + Arc'd Receiver). Windows clone this
167 /// to publish messages back to the editor's main loop. The Receiver
168 /// is `Arc<Mutex<>>` internally, so all clones drain the same queue.
169 pub async_bridge: Option<crate::services::async_bridge::AsyncBridge>,
170
171 /// Plugin manager (single QuickJS instance), wrapped in
172 /// `Arc<RwLock<>>` so windows can fire hooks and read state
173 /// without going through `Editor`. Reads take a read lock; the
174 /// few `&mut self` methods (process_commands, check_thread_health,
175 /// test_inject_command) take a write lock.
176 pub plugin_manager: Arc<RwLock<crate::services::plugins::manager::PluginManager>>,
177
178 /// Active resolved theme, mirrored from `Editor.theme`. Each
179 /// `set_theme` / theme-reload pushes the new value into this
180 /// `Arc<RwLock<>>` so Window methods can read colors without
181 /// going through Editor.
182 pub theme: Arc<RwLock<crate::view::theme::Theme>>,
183
184 /// Editor-wide event broadcaster (cloneable, Arc inside).
185 pub event_broadcaster: crate::model::control_event::EventBroadcaster,
186
187 /// Hot-exit / crash recovery service. `Arc<Mutex>` because it carries
188 /// mutable session state (`session_started`, per-buffer save times)
189 /// and is shared by every window — per-window restore and auto-save
190 /// reach it directly (no active-window flip).
191 pub recovery_service: Arc<std::sync::Mutex<crate::services::recovery::RecoveryService>>,
192}