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::fs::FsManager;
45use crate::services::time_source::SharedTimeSource;
46use crate::view::theme::ThemeRegistry;
47use fresh_core::BufferId;
48use std::collections::HashMap;
49use std::sync::atomic::{AtomicUsize, Ordering};
50use std::sync::{Arc, RwLock};
51
52/// Globally-unique `BufferId` allocator shared across every `Window`.
53///
54/// `BufferId`s must be unique editor-wide so plugin APIs that thread
55/// ids around (`editor.openFile(...) -> BufferId`, `setActiveBuffer(id)`,
56/// terminal `terminalId` correlation, recovery files keyed by id) don't
57/// have to disambiguate by `WindowId`. The allocator is a single
58/// `Arc<AtomicUsize>` cloned into every `Window` — concurrent
59/// `next()` calls return distinct ids without locking.
60#[derive(Debug, Clone)]
61pub struct BufferIdAllocator(Arc<AtomicUsize>);
62
63impl BufferIdAllocator {
64 /// Construct an allocator starting at `start`. The first `next()`
65 /// call returns `BufferId(start)`.
66 pub fn new(start: usize) -> Self {
67 Self(Arc::new(AtomicUsize::new(start)))
68 }
69
70 /// Allocate the next `BufferId`. Thread-safe and lock-free.
71 pub fn next(&self) -> BufferId {
72 BufferId(self.0.fetch_add(1, Ordering::Relaxed))
73 }
74
75 /// Peek at the value the next `next()` call would return without
76 /// advancing. Test-only — production code should always use `next()`.
77 #[doc(hidden)]
78 pub fn peek(&self) -> usize {
79 self.0.load(Ordering::Relaxed)
80 }
81
82 /// Restore the counter to a specific value (used by workspace
83 /// rehydration so persisted ids don't collide with freshly-allocated
84 /// ones after restart).
85 pub fn set(&self, value: usize) {
86 self.0.store(value, Ordering::Relaxed);
87 }
88}
89
90/// Editor-global resources that every `Window` holds an `Arc`-cloned
91/// reference to.
92///
93/// One instance is constructed in `editor_init` and cloned into the base
94/// `Window`. `Editor::create_window` and the first-dive seed path in
95/// `set_active_window` clone it into every subsequent `Window`. All
96/// fields are cheap to clone (`Arc` increment or `Clone`-by-value where
97/// the inner type already carries `Arc`s like `Authority`).
98///
99/// A `Window` handler that needs any of these reads it directly:
100/// `self.resources.config.editor.line_wrap`,
101/// `self.authority().path_translation`, etc. The
102/// [`Window::config()`] / `Window::authority()` accessors are the
103/// canonical reading API; the field itself stays `pub(crate)` so call
104/// sites can split-borrow disjoint sub-fields when the borrow checker
105/// needs it.
106#[derive(Clone)]
107pub struct WindowResources {
108 /// Read-only editor configuration. Hot-reloaded by swapping the
109 /// `Arc`'s pointee when the user edits their config file.
110 pub config: Arc<Config>,
111
112 /// Tree-sitter grammar registry. `Arc` because grammar loading
113 /// can be expensive and is shared across windows.
114 pub grammar_registry: Arc<GrammarRegistry>,
115
116 /// Theme registry (the *catalogue* of available themes, not the
117 /// active theme — that's `Editor::theme` for now, pending the
118 /// Tier-2 wrap).
119 pub theme_registry: Arc<ThemeRegistry>,
120
121 /// Cache of plugin-supplied theme JSONs, populated by plugin
122 /// commands and read by the theme loader.
123 pub theme_cache: Arc<RwLock<HashMap<String, serde_json::Value>>>,
124
125 /// Keybinding resolver (mode → key → command map). `RwLock` because
126 /// plugin commands can mutate the resolver at runtime.
127 pub keybindings: Arc<RwLock<KeybindingResolver>>,
128
129 /// Command registry (named commands a plugin or user can invoke).
130 /// `RwLock` for the same reason as `keybindings`.
131 pub command_registry: Arc<RwLock<CommandRegistry>>,
132
133 /// Filesystem operation manager (background renames, deletes, etc.)
134 pub fs_manager: Arc<FsManager>,
135
136 /// Direct host-filesystem handle. Held alongside the active
137 /// authority's filesystem because some operations (recovery file
138 /// IO, history persistence) intentionally bypass the authority's
139 /// translation.
140 pub local_filesystem: Arc<dyn FileSystem + Send + Sync>,
141
142 /// Globally-unique `BufferId` allocator (see [`BufferIdAllocator`]).
143 pub buffer_id_alloc: BufferIdAllocator,
144
145 // NOTE: a window's `Authority` is **not** here — it lives directly on
146 // `Window` (owned, non-shared). Keeping it out of these `Clone`-fanned
147 // resources is what stops one session's backend/trust/env from leaking
148 // into another (issue #2280). See `Window::authority`.
149 /// Editor-wide time source (real clock in production, controllable
150 /// in tests). Already `Arc`-internal.
151 pub time_source: SharedTimeSource,
152
153 /// Directory context (config dir, themes dir, plugins dir, etc.).
154 /// Cloned by value because it's a small struct of `PathBuf`s.
155 pub dir_context: DirectoryContext,
156
157 /// Tokio runtime for async I/O tasks (LSP, file watchers, git, etc.).
158 /// Single runtime shared across all windows via `Arc`. `None` means
159 /// the editor was constructed without async support (rare).
160 pub tokio_runtime: Option<Arc<tokio::runtime::Runtime>>,
161
162 /// Async-message bridge (Sender + Arc'd Receiver). Windows clone this
163 /// to publish messages back to the editor's main loop. The Receiver
164 /// is `Arc<Mutex<>>` internally, so all clones drain the same queue.
165 pub async_bridge: Option<crate::services::async_bridge::AsyncBridge>,
166
167 /// Plugin manager (single QuickJS instance), wrapped in
168 /// `Arc<RwLock<>>` so windows can fire hooks and read state
169 /// without going through `Editor`. Reads take a read lock; the
170 /// few `&mut self` methods (process_commands, check_thread_health,
171 /// test_inject_command) take a write lock.
172 pub plugin_manager: Arc<RwLock<crate::services::plugins::manager::PluginManager>>,
173
174 /// Active resolved theme, mirrored from `Editor.theme`. Each
175 /// `set_theme` / theme-reload pushes the new value into this
176 /// `Arc<RwLock<>>` so Window methods can read colors without
177 /// going through Editor.
178 pub theme: Arc<RwLock<crate::view::theme::Theme>>,
179
180 /// Editor-wide event broadcaster (cloneable, Arc inside).
181 pub event_broadcaster: crate::model::control_event::EventBroadcaster,
182
183 /// Hot-exit / crash recovery service. `Arc<Mutex>` because it carries
184 /// mutable session state (`session_started`, per-buffer save times)
185 /// and is shared by every window — per-window restore and auto-save
186 /// reach it directly (no active-window flip).
187 pub recovery_service: Arc<std::sync::Mutex<crate::services::recovery::RecoveryService>>,
188}