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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
//! Plain accessor methods on `Editor`.
//!
//! Configuration getters, key-translator/time-source/event-broadcaster
//! handles, LSP / completion / update query helpers, mode registry
//! access, status/warning log setup, and the per-frame timer-check
//! methods (mouse hover / semantic highlight / diagnostic pull /
//! completion trigger).
//!
//! These are mostly small `&self` queries that read a single field;
//! grouping them together keeps mod.rs focused on the central
//! orchestration.
use super::*;
impl Editor {
/// Get a reference to the async bridge (if available)
pub fn async_bridge(&self) -> Option<&AsyncBridge> {
self.async_bridge.as_ref()
}
/// Get a reference to the config
pub fn config(&self) -> &Config {
&self.config
}
/// Get a mutable reference to the config.
///
/// Routes through `Arc::make_mut`: if the plugin state snapshot (or any
/// other reader) still holds an `Arc` to the current value, this
/// CoW-clones so existing readers observe a stable value and the next
/// snapshot refresh sees a new pointer. `Arc<T>` has no `DerefMut`, so
/// the only way to mutate through `self.config` is via this accessor —
/// there is no code path that can silently leave a reader with stale
/// data.
///
/// Window-side reads (`Window::config()`) read a *separate* Arc clone
/// stashed in `WindowResources`. Mutations through `config_mut`
/// therefore leave window clones stale until [`Editor::sync_windows_config`]
/// runs. Callers that mutate a config field which Window code reads
/// (e.g. `editor.line_wrap`, `editor.enable_inlay_hints`,
/// `languages`, etc.) must call `sync_windows_config()` afterwards.
/// `set_config` does this automatically.
pub fn config_mut(&mut self) -> &mut Config {
Arc::make_mut(&mut self.config)
}
/// Replace the config wholesale. Used by the "reload config" path and
/// by tests that want to swap in a freshly-parsed file. Constructs a
/// fresh `Arc`, so any snapshot that still holds the old value sees
/// the pointer move and will reserialize on the next refresh.
///
/// Also propagates the new `Arc` to every window's
/// `resources.config`, so window-scoped reads see the swap.
pub fn set_config(&mut self, new_config: Config) {
self.config = Arc::new(new_config);
self.sync_windows_config();
}
/// Propagate `self.config` to every window's `resources.config` so
/// window-side reads (`Window::config()`) see the latest value.
/// `Arc::clone` is cheap, so this is a constant-time fanout per
/// window. Called from `set_config` and at the end of any
/// `config_mut()`-driven mutation that affects window-read fields.
pub(crate) fn sync_windows_config(&mut self) {
let cfg = self.config.clone();
for w in self.windows.values_mut() {
w.resources.config = cfg.clone();
}
}
/// Replace the cached raw user config. Like `set_config`, constructs
/// a fresh `Arc` so the plugin snapshot notices the change.
pub(crate) fn set_user_config_raw(&mut self, value: serde_json::Value) {
self.user_config_raw = Arc::new(value);
}
/// Mutable access to the active window's merged diagnostics map.
/// Routes through `Arc::make_mut`, which CoW-clones while the
/// plugin snapshot still holds the old map — readers never
/// observe an in-place mutation.
pub(crate) fn stored_diagnostics_mut(
&mut self,
) -> &mut HashMap<String, Vec<lsp_types::Diagnostic>> {
Arc::make_mut(&mut self.active_window_mut().stored_diagnostics)
}
/// Mutable access to the active window's folding-ranges map.
/// Same `Arc::make_mut` CoW pattern as `stored_diagnostics_mut`.
pub(crate) fn stored_folding_ranges_mut(
&mut self,
) -> &mut HashMap<String, Vec<lsp_types::FoldingRange>> {
Arc::make_mut(&mut self.active_window_mut().stored_folding_ranges)
}
/// Get a reference to the key translator (for input calibration)
pub fn key_translator(&self) -> &crate::input::key_translator::KeyTranslator {
&self.key_translator
}
/// Get a reference to the time source
pub fn time_source(&self) -> &SharedTimeSource {
&self.time_source
}
/// Emit a control event
pub fn emit_event(&self, name: impl Into<String>, data: serde_json::Value) {
self.event_broadcaster.emit_named(name, data);
}
/// Send a response to a plugin for an async operation
pub(super) fn send_plugin_response(&self, response: fresh_core::api::PluginResponse) {
self.plugin_manager
.read()
.unwrap()
.deliver_response(response);
}
// `take_pending_semantic_token_request` and
// `take_pending_semantic_token_range_request` live on `impl Window`
// — call them via `self.active_window_mut()`.
/// Get all keybindings as (key, action) pairs
pub fn get_all_keybindings(&self) -> Vec<(String, String)> {
self.keybindings.read().unwrap().get_all_bindings()
}
/// Get the formatted keybinding for a specific action (for display in messages)
/// Returns None if no keybinding is found for the action
pub fn get_keybinding_for_action(&self, action_name: &str) -> Option<String> {
self.keybindings
.read()
.unwrap()
.find_keybinding_for_action(action_name, self.active_window().key_context.clone())
}
/// Raw-event counterpart: return the `(KeyCode, KeyModifiers)` currently
/// bound to `action` in `context`. Intended for callers that need to
/// simulate the user pressing the bound key (e2e tests, some hotkey-
/// chaining code) without hardcoding a default that a user's rebind
/// would invalidate.
pub fn keybinding_event_for_action(
&self,
action: &crate::input::keybindings::Action,
context: crate::input::keybindings::KeyContext,
) -> Option<(crossterm::event::KeyCode, crossterm::event::KeyModifiers)> {
self.keybindings
.read()
.unwrap()
.get_keybinding_event_for_action(action, context)
}
/// Get mutable access to the mode registry
pub fn mode_registry_mut(&mut self) -> &mut ModeRegistry {
&mut self.mode_registry
}
/// Get immutable access to the mode registry
pub fn mode_registry(&self) -> &ModeRegistry {
&self.mode_registry
}
/// Get the currently active buffer ID.
///
/// This is derived from the split manager (single source of truth).
/// The editor always has at least one buffer, so this never fails.
///
/// When the active split has a buffer-group tab as its active target
/// (i.e., `active_group_tab.is_some()`), this returns the buffer of the
/// currently-focused inner panel — so that input routing, command palette
/// context, buffer mode, and other "what is the user looking at" queries
/// resolve to the panel the user is actually interacting with rather than
/// the split's background leaf buffer.
///
/// The override only takes effect if the inner panel's buffer is still
/// live in `self.buffers`; otherwise it falls back to the main split's
/// leaf buffer so callers never see a stale/freed buffer id.
#[inline]
pub fn active_buffer(&self) -> BufferId {
let (_, buf) = self.effective_active_pair();
buf
}
/// The split id whose `SplitViewState` owns the currently-focused
/// cursors/viewport/buffer state. For a regular split this is just
/// `split_manager.active_split()`. For a split that has a group tab
/// active, this returns the focused inner panel's leaf id (which
/// lives in `split_view_states` even though it's not in the main
/// split tree).
#[inline]
pub fn effective_active_split(&self) -> crate::model::event::LeafId {
let (split, _) = self.effective_active_pair();
split
}
/// Resolve the effective (split, buffer) pair for the currently-focused
/// target. This is the single source of truth — both `active_buffer` and
/// `effective_active_split` derive from it so they can never disagree.
///
/// Returned invariant: `split_view_states[split]` exists, its
/// `active_buffer` equals the returned buffer id, `self.buffers`
/// contains the returned buffer id, and `split.keyed_states` contains
/// an entry for the returned buffer id. Consequently the mutation path
/// in `apply_event_to_active_buffer` (which indexes into
/// `keyed_states[buffer]`) is always well-defined for the returned pair.
///
/// If a buffer-group panel is focused but any of the invariants above
/// is not satisfied for the inner leaf (for example because the panel
/// buffer was freed without clearing `focused_group_leaf`), the helper
/// falls back to the outer split's own leaf. The fallback is also
/// validated before being returned.
#[inline]
fn effective_active_pair(&self) -> (crate::model::event::LeafId, BufferId) {
self.active_window().effective_active_pair()
}
/// Get the mode name for the active buffer.
///
/// Resolution order:
/// 1. The buffer's own virtual-buffer mode, if it has one.
/// 2. The mode declared by the buffer-group containing this
/// buffer (e.g. a `git-log` group's keybindings apply to its
/// panels regardless of whether each panel is a virtual
/// buffer or a file-backed one — `openFileStreaming` produces
/// the latter for streaming detail panels).
pub fn active_buffer_mode(&self) -> Option<&str> {
let buffer_id = self.active_buffer();
let win = self.active_window();
if let Some(mode) = win
.buffer_metadata
.get(&buffer_id)
.and_then(|meta| meta.virtual_mode())
{
return Some(mode);
}
let group_id = win.buffer_to_group.get(&buffer_id).copied()?;
win.buffer_groups.get(&group_id).map(|g| g.mode.as_str())
}
/// Check if the active buffer is read-only
pub fn is_active_buffer_read_only(&self) -> bool {
if let Some(metadata) = self
.active_window()
.buffer_metadata
.get(&self.active_buffer())
{
if metadata.read_only {
return true;
}
// Also check if the mode is read-only
if let Some(mode_name) = metadata.virtual_mode() {
return self.mode_registry.is_read_only(mode_name);
}
}
false
}
// `mark_buffer_read_only` lives on `impl Window` — call it via
// `self.active_window_mut().mark_buffer_read_only(buffer_id, ro)`.
/// Get the effective mode for the active buffer.
///
/// Buffer-local mode (virtual buffers) takes precedence over the global
/// editor mode, so that e.g. a search-replace panel isn't hijacked by
/// a markdown-source or vi-mode global mode.
pub fn effective_mode(&self) -> Option<&str> {
// When a floating widget panel is mounted, its plugin-defined
// mode (`editor.setEditorMode(...)`) takes precedence over the
// underlying buffer's virtual mode. Without this, opening the
// Orchestrator picker from a python3 terminal session would
// resolve mode-keybindings against `"terminal"` instead of
// `"orchestrator-open"`, so picker-specific shortcuts like
// `Alt+N` never reached their handlers.
if self.floating_widget_panel.is_some() {
if let Some(mode) = self.active_window().editor_mode.as_deref() {
return Some(mode);
}
}
self.active_buffer_mode()
.or(self.active_window().editor_mode.as_deref())
}
// `has_active_lsp_progress`, `get_lsp_progress`, and
// `is_lsp_server_ready` live on `impl Window` — call them via
// `self.active_window().has_active_lsp_progress()` etc.
/// Read-only view of the editor-wide popup stack.
///
/// `global_popups` itself is `pub(crate)` so its internals stay
/// private to the app module; tests need to inspect its depth /
/// contents to verify "no two popups stacked across the buffer-
/// local and global stacks" invariants (e.g. issue 1 of the LSP
/// indicator-click bugs), so we expose an immutable accessor here.
pub fn global_popups(&self) -> &crate::view::popup::PopupManager {
&self.global_popups
}
/// The earliest wall-clock deadline at which the main event loop
/// needs to wake up and re-render, *purely because of internal
/// time-driven UI elements* (animations, the LSP status-bar
/// spinner). Returns `None` when no time-driven UI is in flight —
/// the loop can sleep until the next user / async event without
/// missing a frame.
///
/// The `Some` case includes the LSP-progress spinner: its glyph
/// is computed from `SystemTime::now() / 100ms`, so the loop has
/// to wake at ~100ms cadence to actually advance it. Without
/// this signal, the indicator would only tick when an unrelated
/// event caused a frame, and the user would see a "frozen"
/// spinner whenever the server stopped emitting `$/progress`
/// (e.g. died externally — see #1941 issue 3).
pub fn next_periodic_redraw_deadline(&self) -> Option<std::time::Instant> {
let lsp_progress_deadline = if self.active_window().has_active_lsp_progress() {
// 100ms matches the spinner-glyph period in
// `lsp_status::compose_lsp_status`.
Some(std::time::Instant::now() + std::time::Duration::from_millis(100))
} else {
None
};
let anim_deadline = self.active_window().animations.next_deadline();
[lsp_progress_deadline, anim_deadline]
.into_iter()
.flatten()
.min()
}
/// Get stored LSP diagnostics (for testing and external access)
/// Returns a reference to the diagnostics map keyed by file URI
pub fn get_stored_diagnostics(&self) -> &HashMap<String, Vec<lsp_types::Diagnostic>> {
&self.active_window().stored_diagnostics
}
/// Check if an update is available
pub fn is_update_available(&self) -> bool {
self.update_checker
.as_ref()
.map(|c| c.is_update_available())
.unwrap_or(false)
}
/// Get the latest version string if an update is available
pub fn latest_version(&self) -> Option<&str> {
self.update_checker
.as_ref()
.and_then(|c| c.latest_version())
}
/// Get the cached release check result (for shutdown notification)
pub fn get_update_result(
&self,
) -> Option<&crate::services::release_checker::ReleaseCheckResult> {
self.update_checker
.as_ref()
.and_then(|c| c.get_cached_result())
}
/// Set a custom update checker (for testing)
///
/// This allows injecting a custom PeriodicUpdateChecker that points to a mock server,
/// enabling E2E tests for the update notification UI.
#[doc(hidden)]
pub fn set_update_checker(
&mut self,
checker: crate::services::release_checker::PeriodicUpdateChecker,
) {
self.update_checker = Some(checker);
}
/// Configure LSP server for a specific language
pub fn set_lsp_config(&mut self, language: String, config: Vec<LspServerConfig>) {
let __active_id = self.active_window;
if let Some(lsp) = self
.windows
.get_mut(&__active_id)
.and_then(|w| w.lsp.as_mut())
{
lsp.set_language_configs(language, config);
}
}
// `running_lsp_servers`, `pending_completion_requests_count`,
// `completion_items_count`, `initialized_lsp_server_count`, and
// `shutdown_lsp_server` live on `impl Window` — call them via
// `self.active_window()` / `self.active_window_mut()`.
/// Set up warning log monitoring
///
/// When warnings/errors are logged, they will be written to the specified path
/// and the editor will be notified via the receiver.
pub fn set_warning_log(&mut self, receiver: std::sync::mpsc::Receiver<()>, path: PathBuf) {
self.warning_log = Some((receiver, path));
}
/// Take the warning-log receiver+path out of this editor.
///
/// The receiver is single-consumer and lives for the process's
/// lifetime; on a destructive editor restart (e.g. authority swap)
/// `main.rs` lifts it from the old editor and re-installs it on the
/// new one so warnings keep flowing post-restart instead of vanishing
/// with the dropped editor.
pub fn take_warning_log(&mut self) -> Option<(std::sync::mpsc::Receiver<()>, PathBuf)> {
self.warning_log.take()
}
/// Set the status message log path
pub fn set_status_log_path(&mut self, path: PathBuf) {
self.status_log_path = Some(path);
}
/// Queue a new authority and restart the editor.
///
/// Per the design decision in `docs/internal/AUTHORITY_DESIGN.md`,
/// authority transitions piggy-back on the existing
/// `change_working_dir` restart path. The caller never sees an
/// editor that is half-transitioned: the current `Editor` is
/// dropped, `main.rs` rebuilds a fresh one with the queued
/// authority, and session restore reopens buffers against the new
/// backend. This is slower than an in-place pointer swap but is
/// far more robust — every cached `Arc<dyn FileSystem>`, LSP
/// handle, terminal PTY, plugin state, and in-flight task is
/// dropped cleanly by the existing restart machinery.
pub fn install_authority(&mut self, authority: crate::services::authority::Authority) {
self.pending_authority = Some(authority);
// Re-open the same working directory; `main.rs` picks up the
// pending authority from the old editor just before dropping it.
self.request_restart(self.working_dir().to_path_buf());
}
/// Restore the default local authority. Same destructive-restart
/// semantics as `install_authority` — the caller never observes a
/// half-transitioned editor.
pub fn clear_authority(&mut self) {
// Reuse the editor's live trust handle so the restored local authority
// is gated by the same workspace-trust state.
let trust = std::sync::Arc::clone(&self.authority.workspace_trust);
let env = std::sync::Arc::clone(&self.authority.env_provider);
self.install_authority(crate::services::authority::Authority::local(trust, env));
}
/// Take the queued authority (if any). Called by `main.rs` on
/// restart to move the queued authority into the fresh editor.
pub fn take_pending_authority(&mut self) -> Option<crate::services::authority::Authority> {
self.pending_authority.take()
}
/// Directly replace the active authority without triggering a
/// restart. Intended for the post-construction wiring in `main.rs`
/// only, where the editor is still being set up and there is no
/// user-visible state to preserve. Do not call this from the event
/// loop — use `install_authority` for that.
///
/// Also refreshes the plugin state snapshot so hooks that fire after
/// this call (notably `plugins_loaded`, fired by `main.rs` right
/// after `set_boot_authority`) see the real `authority_label` instead
/// of the empty string the temporary `Authority::local()` carried
/// during construction.
pub fn set_boot_authority(&mut self, authority: crate::services::authority::Authority) {
self.authority = authority;
// Propagate the new authority to every window's resources so
// window-side filesystem/path-translation reads (`Window::authority()`)
// see the swap. `Authority` carries internal `Arc`s, so this just
// clones cheap handles.
let auth = self.authority.clone();
for w in self.windows.values_mut() {
w.resources.authority = auth.clone();
}
// Propagate the authority's long-running spawner into the LSP
// manager so `force_spawn` can route server processes through
// the right backend. The editor rebuilds on every authority
// transition (AUTHORITY_DESIGN.md principle 7), so this is the
// single wiring point — no need for a hot-swap API. Path
// translation rides along for the same reason — LSP URIs need
// to be host↔container-translated under the new authority.
let __active_id = self.active_window;
if let Some(lsp) = self
.windows
.get_mut(&__active_id)
.and_then(|w| w.lsp.as_mut())
{
lsp.set_long_running_spawner(self.authority.long_running_spawner.clone());
lsp.set_path_translation(self.authority.path_translation.clone());
lsp.set_workspace_trust(self.authority.workspace_trust.clone());
}
#[cfg(feature = "plugins")]
{
self.update_plugin_state_snapshot();
// Notify plugins so they can re-register state-gated
// commands (e.g. devcontainer `Attach` only when not
// attached). Production transitions also trigger a full
// editor restart that re-runs plugin init, but firing
// here keeps in-process transitions and the test harness
// (which simulates the restart inline) consistent.
let label = self.authority.display_label.clone();
self.plugin_manager.read().unwrap().run_hook(
"authority_changed",
crate::services::plugins::hooks::HookArgs::AuthorityChanged { label },
);
}
}
/// Read-only access to the active authority.
pub fn authority(&self) -> &crate::services::authority::Authority {
&self.authority
}
/// The editor's current working directory — the active window's
/// project root. Derived, not stored: there is no separate
/// `working_dir` field that could drift out of sync with the active
/// window (issue #2056). Individual buffers may live elsewhere.
pub fn working_dir(&self) -> &std::path::Path {
&self.active_window().root
}
/// The currently active `Session`. Always `WindowId(1)` until
/// the multi-session migration step lands; until then this is
/// effectively a typed wrapper around `working_dir`. New code
/// should prefer this accessor so the eventual migration is a
/// no-op for the call site.
///
/// Panics if the active session id is not present in the
/// `sessions` map. That invariant is upheld by the constructor
/// and `setActiveWindow` (when added) — if the panic ever fires
/// it indicates a bug in session lifecycle code.
pub fn active_window(&self) -> &crate::app::window::Window {
self.windows
.get(&self.active_window)
.expect("active_window id must be a member of sessions")
}
/// The active session's id.
pub fn active_session_id(&self) -> fresh_core::WindowId {
self.active_window
}
/// Allocate the next globally-unique `BufferId`. Use this in
/// `impl Editor` handler bodies that mint new buffer ids. Handlers
/// that have already moved to `impl Window` use
/// `Window::alloc_buffer_id` (which delegates to the same
/// `Arc<BufferIdAllocator>` shared via `WindowResources`).
///
/// Keeps `next_buffer_id` in sync with the allocator's high-water
/// mark so workspace snapshots that read the `next_buffer_id`
/// counter directly continue to see a correct value. The
/// allocator's atomic is the source of truth; this counter mirrors
/// it for serialization compatibility.
pub(crate) fn alloc_buffer_id(&mut self) -> fresh_core::BufferId {
let id = self.buffer_id_alloc.next();
// Bump the legacy counter past the freshly-issued id so
// workspace serialization snapshots see a value at least one
// greater than every issued id.
if id.0 + 1 > self.next_buffer_id {
self.next_buffer_id = id.0 + 1;
}
id
}
/// Number of sessions currently in the editor. Always 1 until
/// the multi-session step lands.
pub fn session_count(&self) -> usize {
self.windows.len()
}
/// Look up a session by id. Returns `None` if `id` is not in
/// the sessions map. Useful for tests; production code that
/// needs the active session should use `active_window()`.
pub fn session(&self, id: fresh_core::WindowId) -> Option<&crate::app::window::Window> {
self.windows.get(&id)
}
/// Active session's utility-dock panel-id → buffer-id map.
/// Used by tests to assert that the active window's dock
/// occupancy is what was set on it. (Pre-0b this asserted
/// "warm-swap restored the stash"; post-0b every window owns
/// its own dock, so the assertion is just "this window's
/// `panel_ids` map matches expectations.")
#[doc(hidden)]
pub fn panel_ids_for_test(&self) -> &std::collections::HashMap<String, fresh_core::BufferId> {
self.panel_ids()
}
/// Inject a panel_ids entry. Used by tests to populate the
/// active session's dock occupancy without going through the
/// async plugin command path.
#[doc(hidden)]
pub fn insert_panel_id_for_test(&mut self, key: String, buffer_id: fresh_core::BufferId) {
self.panel_ids_mut().insert(key, buffer_id);
}
/// True iff the active session has an LSP manager attached.
/// Used by tests to assert that the active window's `lsp`
/// slot is populated. (Pre-0b this exercised the warm-swap
/// code; post-0b the LSP manager lives directly on `Window`,
/// so the assertion is just "this window's `lsp` is `Some`.")
#[doc(hidden)]
pub fn has_lsp_for_test(&self) -> bool {
self.lsp().is_some()
}
/// Inject an LspManager so tests can prove the swap routes
/// it through the session stash without depending on real
/// LSP server spawn.
#[doc(hidden)]
pub fn install_dummy_lsp_for_test(&mut self) {
let active = self.active_window;
self.active_window_mut().lsp =
Some(crate::services::lsp::manager::LspManager::new(active, None));
}
/// Most-recent `path_changed` event the editor received.
/// Test-only — used by `watch_path` e2e tests to assert
/// kernel events surfaced to the editor.
#[doc(hidden)]
pub fn last_path_change_for_test(&self) -> Option<&(u64, std::path::PathBuf, &'static str)> {
self.last_path_change_for_test.as_ref()
}
/// Most-recent `WatchPathRegistered` plugin response, paired
/// with its request_id. Test-only.
#[doc(hidden)]
pub fn last_watch_response_for_test(&self) -> Option<&(u64, Result<u64, String>)> {
self.last_watch_response_for_test.as_ref()
}
/// Inject an mtime entry into the active session's mod-time
/// cache. Used by tests to populate `Window.file_mod_times`
/// without going through real file I/O. (Pre-0b this was
/// reaching the warm-swap stash; post-0b it's a direct
/// insert into the active window's cache.)
#[doc(hidden)]
pub fn insert_mtime_for_test(&mut self, path: std::path::PathBuf, t: std::time::SystemTime) {
self.file_mod_times_mut().insert(path, t);
}
/// Whether the active session's mtime cache contains `path`.
#[doc(hidden)]
pub fn has_mtime_for_test(&self, path: &std::path::Path) -> bool {
self.file_mod_times().contains_key(path)
}
/// Mutable access to the active session. Used by lifecycle code
/// that re-targets per-session state (renaming, etc.). Same
/// panic invariant as `active_window()`.
pub fn active_window_mut(&mut self) -> &mut crate::app::window::Window {
let id = self.active_window;
self.windows
.get_mut(&id)
.expect("active_window id must be a member of sessions")
}
/// The active window's layout-cache (split-leaf rects, tab rects,
/// file-explorer rect, view-line mappings). Mouse hit-testing and
/// visual-line motion read from here.
pub(crate) fn active_layout(&self) -> &crate::app::types::WindowLayoutCache {
&self.active_window().layout_cache
}
/// Mutable handle to the active window's layout cache. Renderer
/// writes split / tab / file-explorer hit-test rects here at the
/// end of each frame.
pub(crate) fn active_layout_mut(&mut self) -> &mut crate::app::types::WindowLayoutCache {
&mut self.active_window_mut().layout_cache
}
/// The active window's editor-chrome layout cache (status bar,
/// menu, popups, prompt overlay, full-frame cell-theme map).
/// Mouse hit-testing reads from here.
pub(crate) fn active_chrome(&self) -> &crate::app::types::ChromeLayout {
&self.active_window().chrome_layout
}
/// Mutable handle to the active window's chrome-layout cache.
/// Renderer writes status-bar / menu / popup / prompt-overlay
/// hit-test rects here at the end of each frame.
pub(crate) fn active_chrome_mut(&mut self) -> &mut crate::app::types::ChromeLayout {
&mut self.active_window_mut().chrome_layout
}
/// Active window's utility-dock panel-id → buffer-id map.
/// Each window owns its own dock; switching windows shows a
/// different (possibly empty) dock.
pub(crate) fn panel_ids(&self) -> &std::collections::HashMap<String, BufferId> {
&self.active_window().panel_ids
}
/// Mutable handle to the active window's panel-id map.
pub(crate) fn panel_ids_mut(&mut self) -> &mut std::collections::HashMap<String, BufferId> {
&mut self.active_window_mut().panel_ids
}
/// Active window's open-file mtime cache. Auto-revert only
/// fires for files in the active window — dormant windows
/// keep their mtime snapshot until the next dive.
pub(crate) fn file_mod_times(
&self,
) -> &std::collections::HashMap<std::path::PathBuf, std::time::SystemTime> {
&self.active_window().file_mod_times
}
/// Mutable handle to the active window's mtime cache.
pub(crate) fn file_mod_times_mut(
&mut self,
) -> &mut std::collections::HashMap<std::path::PathBuf, std::time::SystemTime> {
&mut self.active_window_mut().file_mod_times
}
/// Active window's file-explorer view (`None` if it's never been
/// opened in this window). Each window has its own tree;
/// switching windows shows that window's view (or none).
pub fn file_explorer(&self) -> Option<&FileTreeView> {
self.active_window().file_explorer.as_ref()
}
/// Mutable handle to the active window's file-explorer view.
/// Holds `&mut self` for the call's lifetime — for sites that
/// also need to read other Editor fields, use direct
/// `self.windows.get_mut(&self.active_window).and_then(|w| w.file_explorer.as_mut())`
/// instead so the borrow on `self.windows` stays disjoint.
pub fn file_explorer_mut(&mut self) -> Option<&mut FileTreeView> {
self.active_window_mut().file_explorer.as_mut()
}
/// Active window's buffer storage. Each window owns its
/// `EditorState` map outright; closing the window drops them.
/// Cross-window iteration goes through `self.windows.values()`
/// directly.
pub(crate) fn buffers(&self) -> &crate::app::window::WindowBuffers {
&self.active_window().buffers
}
/// Mutable handle to the active window's buffer storage.
/// Holds `&mut self` for the call's lifetime — at sites that
/// need a concurrent mutable borrow on another Window field
/// (`splits`, `event_logs`, etc.) take a single
/// `let window = self.windows.get_mut(&self.active_window).unwrap()`
/// and split-access the disjoint sub-fields directly.
pub(crate) fn buffers_mut(&mut self) -> &mut crate::app::window::WindowBuffers {
&mut self.active_window_mut().buffers
}
/// Active window's LSP manager (`None` if no LSP has been spawned
/// for this window yet). Each window has its own LSP set rooted
/// at its project root.
pub(crate) fn lsp(&self) -> Option<&crate::services::lsp::manager::LspManager> {
self.active_window().lsp.as_ref()
}
/// Mutable handle to the active window's LSP manager. Same
/// borrow caveat as `file_explorer_mut()`: at sites that also
/// need to read other Editor fields, prefer direct
/// `self.windows.get_mut(&self.active_window).and_then(|w| w.lsp.as_mut())`.
pub(crate) fn lsp_mut(&mut self) -> Option<&mut crate::services::lsp::manager::LspManager> {
self.active_window_mut().lsp.as_mut()
}
/// Active window's split tree. Panics if the window has no
/// layout yet — the invariant is "the active window always has
/// `splits` populated", upheld by `set_active_window` (which
/// seeds the layout on first dive) and by editor init (which
/// hands the initial layout to the base window).
pub(crate) fn split_manager(&self) -> &crate::view::split::SplitManager {
&self
.active_window()
.buffers
.splits()
.expect("active window must have a populated split layout")
.0
}
/// Mutable handle to the active window's split tree.
pub(crate) fn split_manager_mut(&mut self) -> &mut crate::view::split::SplitManager {
&mut self
.active_window_mut()
.buffers
.splits_mut()
.expect("active window must have a populated split layout")
.0
}
/// Active window's per-leaf view state map.
#[cfg(test)]
pub(crate) fn split_view_states(
&self,
) -> &std::collections::HashMap<crate::model::event::LeafId, crate::view::split::SplitViewState>
{
&self
.active_window()
.buffers
.splits()
.expect("active window must have a populated split layout")
.1
}
/// Mutable handle to the active window's per-leaf view state map.
pub(crate) fn split_view_states_mut(
&mut self,
) -> &mut std::collections::HashMap<
crate::model::event::LeafId,
crate::view::split::SplitViewState,
> {
&mut self
.active_window_mut()
.buffers
.splits_mut()
.expect("active window must have a populated split layout")
.1
}
/// Return buffer ids whose on-disk path sits at or under `root`.
/// Used by file-explorer operations that need to react when a file
/// or directory on disk goes away or moves.
pub fn buffer_ids_under_path(&self, root: &std::path::Path) -> Vec<BufferId> {
self.windows
.get(&self.active_window)
.map(|w| &w.buffers)
.expect("active window present")
.iter()
.filter_map(|(id, state)| {
let p = state.buffer.file_path()?;
if p == root || p.starts_with(root) {
Some(*id)
} else {
None
}
})
.collect()
}
/// Get remote connection info if editing remote files
///
/// Returns `Some("user@host")` for remote editing, `None` for local.
pub fn remote_connection_info(&self) -> Option<&str> {
self.authority.filesystem.remote_connection_info()
}
/// Get connection string for display in status bar and file explorer.
///
/// Per principle 9, identity lives in the authority. The label set
/// by whoever constructed the authority wins; if it is empty (the
/// SSH constructor leaves it that way) we fall back to the
/// filesystem's `remote_connection_info()`, which knows how to
/// annotate disconnected SSH sessions.
pub fn connection_display_string(&self) -> Option<String> {
if !self.authority.display_label.is_empty() {
return Some(self.authority.display_label.clone());
}
self.remote_connection_info().map(|conn| {
if self.authority.filesystem.is_remote_connected() {
conn.to_string()
} else {
format!("{} (Disconnected)", conn)
}
})
}
/// Get the status log path
pub fn get_status_log_path(&self) -> Option<&PathBuf> {
self.status_log_path.as_ref()
}
/// Open the status log file (user clicked on status message)
pub fn open_status_log(&mut self) {
if let Some(path) = self.status_log_path.clone() {
// Use open_local_file since log files are always local
match self.active_window_mut().open_local_file(&path) {
Ok(buffer_id) => {
self.active_window_mut()
.mark_buffer_read_only(buffer_id, true);
}
Err(e) => {
tracing::error!("Failed to open status log: {}", e);
}
}
} else {
self.set_status_message("Status log not available".to_string());
}
}
/// Check for and handle any new warnings in the warning log
///
/// Updates the general warning domain for the status bar.
/// Returns true if new warnings were found.
pub fn check_warning_log(&mut self) -> bool {
let path = match &self.warning_log {
Some((receiver, path)) => {
let mut new_warning_count = 0usize;
while receiver.try_recv().is_ok() {
new_warning_count += 1;
}
if new_warning_count == 0 {
return false;
}
(path.clone(), new_warning_count)
}
None => return false,
};
let (path, new_warning_count) = path;
self.active_window_mut()
.warning_domains
.general
.add_warnings(new_warning_count);
self.active_window_mut()
.warning_domains
.general
.set_log_path(path);
true
}
/// Get the warning domain registry
// Warning-domain accessors live on `impl Window`:
// - `clear_warnings` — call as `self.active_window_mut().clear_warnings()`.
// - Read access via `active_window().warning_domains` directly
// (and its `.general` / `.lsp` sub-registries).
// `has_lsp_error`, `get_effective_warning_level`,
// `get_general_warning_level`, `get_general_warning_count`,
// `get_warning_domains`, `get_warning_log_path`,
// `clear_warning_indicator` were thin getters with no remaining
// callers and have been removed.
/// Open the warning log file (user-initiated action). Stays on
/// `impl Editor` because it calls editor-orchestration helpers
/// (`open_local_file`, `mark_buffer_read_only`).
pub fn open_warning_log(&mut self) {
if let Some(path) = self
.active_window_mut()
.warning_domains
.general
.log_path
.clone()
{
// Use open_local_file since log files are always local
match self.active_window_mut().open_local_file(&path) {
Ok(buffer_id) => {
self.active_window_mut()
.mark_buffer_read_only(buffer_id, true);
}
Err(e) => {
tracing::error!("Failed to open warning log: {}", e);
}
}
}
}
// `update_lsp_warning_domain` lives on `impl Window` — call it via
// `self.active_window_mut().update_lsp_warning_domain()`.
/// Check if mouse hover timer has expired and trigger LSP hover request
///
/// This implements debounced hover - we wait for the configured delay before
/// sending the request to avoid spamming the LSP server on every mouse move.
/// Returns true if a hover request was triggered.
pub fn check_mouse_hover_timer(&mut self) -> bool {
// Check if mouse hover is enabled
if !self.config.editor.mouse_hover_enabled {
return false;
}
let hover_delay = std::time::Duration::from_millis(self.config.editor.mouse_hover_delay_ms);
// Get hover state without borrowing self
let hover_info = match self.active_window_mut().mouse_state.lsp_hover_state {
Some((byte_pos, start_time, screen_x, screen_y)) => {
if self.active_window_mut().mouse_state.lsp_hover_request_sent {
return false; // Already sent request for this position
}
if start_time.elapsed() < hover_delay {
return false; // Timer hasn't expired yet
}
Some((byte_pos, screen_x, screen_y))
}
None => return false,
};
let Some((byte_pos, screen_x, screen_y)) = hover_info else {
return false;
};
// Store mouse position for popup positioning
self.active_window_mut()
.hover
.set_screen_position((screen_x, screen_y));
// Request hover at the byte position — only mark as sent if dispatched
match self.request_hover_at_position(byte_pos) {
Ok(true) => {
self.active_window_mut().mouse_state.lsp_hover_request_sent = true;
true
}
Ok(false) => false, // no server ready, timer will retry
Err(e) => {
tracing::debug!("Failed to request hover: {}", e);
false
}
}
}
// `check_semantic_highlight_timer` lives on `impl Window` — call it
// via `self.active_window().check_semantic_highlight_timer()`.
// `check_diagnostic_pull_timer` lives on `impl Window` — call it via
// `self.active_window_mut().check_diagnostic_pull_timer()`. Pulls
// run against the active window's LSP manager and its per-window
// `scheduled_diagnostic_pull` debounce slot.
/// Check if completion trigger timer has expired and trigger completion if so
///
/// This implements debounced completion - we wait for quick_suggestions_delay_ms
/// before sending the completion request to avoid spamming the LSP server.
/// Returns true if a completion request was triggered.
pub fn check_completion_trigger_timer(&mut self) -> bool {
// Check if we have a scheduled completion trigger
let Some(trigger_time) = self.active_window_mut().scheduled_completion_trigger else {
return false;
};
// Check if the timer has expired
if Instant::now() < trigger_time {
return false;
}
// Clear the scheduled trigger
self.active_window_mut().scheduled_completion_trigger = None;
// Don't trigger if a popup is already visible
if self.active_state().popups.is_visible() {
return false;
}
// Trigger the completion request
self.request_completion();
true
}
}