fresh/app/editor_accessors.rs
1//! Plain accessor methods on `Editor`.
2//!
3//! Configuration getters, key-translator/time-source/event-broadcaster
4//! handles, LSP / completion / update query helpers, mode registry
5//! access, status/warning log setup, and the per-frame timer-check
6//! methods (mouse hover / semantic highlight / diagnostic pull /
7//! completion trigger).
8//!
9//! These are mostly small `&self` queries that read a single field;
10//! grouping them together keeps mod.rs focused on the central
11//! orchestration.
12
13use super::*;
14
15impl Editor {
16 /// Get a reference to the async bridge (if available)
17 pub fn async_bridge(&self) -> Option<&AsyncBridge> {
18 self.async_bridge.as_ref()
19 }
20
21 /// Get a reference to the config
22 pub fn config(&self) -> &Config {
23 &self.config
24 }
25
26 /// Get a mutable reference to the config.
27 ///
28 /// Routes through `Arc::make_mut`: if the plugin state snapshot (or any
29 /// other reader) still holds an `Arc` to the current value, this
30 /// CoW-clones so existing readers observe a stable value and the next
31 /// snapshot refresh sees a new pointer. `Arc<T>` has no `DerefMut`, so
32 /// the only way to mutate through `self.config` is via this accessor —
33 /// there is no code path that can silently leave a reader with stale
34 /// data.
35 pub fn config_mut(&mut self) -> &mut Config {
36 Arc::make_mut(&mut self.config)
37 }
38
39 /// Replace the config wholesale. Used by the "reload config" path and
40 /// by tests that want to swap in a freshly-parsed file. Constructs a
41 /// fresh `Arc`, so any snapshot that still holds the old value sees
42 /// the pointer move and will reserialize on the next refresh.
43 pub fn set_config(&mut self, new_config: Config) {
44 self.config = Arc::new(new_config);
45 }
46
47 /// Replace the cached raw user config. Like `set_config`, constructs
48 /// a fresh `Arc` so the plugin snapshot notices the change.
49 pub(crate) fn set_user_config_raw(&mut self, value: serde_json::Value) {
50 self.user_config_raw = Arc::new(value);
51 }
52
53 /// Mutable access to the merged diagnostics map. Routes through
54 /// `Arc::make_mut`, which CoW-clones while the plugin snapshot still
55 /// holds the old map — readers never observe an in-place mutation.
56 pub(crate) fn stored_diagnostics_mut(
57 &mut self,
58 ) -> &mut HashMap<String, Vec<lsp_types::Diagnostic>> {
59 Arc::make_mut(&mut self.stored_diagnostics)
60 }
61
62 /// Mutable access to the folding-ranges map. CoW-clones through
63 /// `Arc::make_mut` for the same reason as `stored_diagnostics_mut`.
64 pub(crate) fn stored_folding_ranges_mut(
65 &mut self,
66 ) -> &mut HashMap<String, Vec<lsp_types::FoldingRange>> {
67 Arc::make_mut(&mut self.stored_folding_ranges)
68 }
69
70 /// Get a reference to the key translator (for input calibration)
71 pub fn key_translator(&self) -> &crate::input::key_translator::KeyTranslator {
72 &self.key_translator
73 }
74
75 /// Get a reference to the time source
76 pub fn time_source(&self) -> &SharedTimeSource {
77 &self.time_source
78 }
79
80 /// Emit a control event
81 pub fn emit_event(&self, name: impl Into<String>, data: serde_json::Value) {
82 self.event_broadcaster.emit_named(name, data);
83 }
84
85 /// Send a response to a plugin for an async operation
86 pub(super) fn send_plugin_response(&self, response: fresh_core::api::PluginResponse) {
87 self.plugin_manager.deliver_response(response);
88 }
89
90 /// Remove a pending semantic token request from tracking maps.
91 pub(super) fn take_pending_semantic_token_request(
92 &mut self,
93 request_id: u64,
94 ) -> Option<SemanticTokenFullRequest> {
95 if let Some(request) = self.pending_semantic_token_requests.remove(&request_id) {
96 self.semantic_tokens_in_flight.remove(&request.buffer_id);
97 Some(request)
98 } else {
99 None
100 }
101 }
102
103 /// Remove a pending semantic token range request from tracking maps.
104 pub(super) fn take_pending_semantic_token_range_request(
105 &mut self,
106 request_id: u64,
107 ) -> Option<SemanticTokenRangeRequest> {
108 if let Some(request) = self
109 .pending_semantic_token_range_requests
110 .remove(&request_id)
111 {
112 self.semantic_tokens_range_in_flight
113 .remove(&request.buffer_id);
114 Some(request)
115 } else {
116 None
117 }
118 }
119
120 /// Get all keybindings as (key, action) pairs
121 pub fn get_all_keybindings(&self) -> Vec<(String, String)> {
122 self.keybindings.read().unwrap().get_all_bindings()
123 }
124
125 /// Get the formatted keybinding for a specific action (for display in messages)
126 /// Returns None if no keybinding is found for the action
127 pub fn get_keybinding_for_action(&self, action_name: &str) -> Option<String> {
128 self.keybindings
129 .read()
130 .unwrap()
131 .find_keybinding_for_action(action_name, self.key_context.clone())
132 }
133
134 /// Raw-event counterpart: return the `(KeyCode, KeyModifiers)` currently
135 /// bound to `action` in `context`. Intended for callers that need to
136 /// simulate the user pressing the bound key (e2e tests, some hotkey-
137 /// chaining code) without hardcoding a default that a user's rebind
138 /// would invalidate.
139 pub fn keybinding_event_for_action(
140 &self,
141 action: &crate::input::keybindings::Action,
142 context: crate::input::keybindings::KeyContext,
143 ) -> Option<(crossterm::event::KeyCode, crossterm::event::KeyModifiers)> {
144 self.keybindings
145 .read()
146 .unwrap()
147 .get_keybinding_event_for_action(action, context)
148 }
149
150 /// Get mutable access to the mode registry
151 pub fn mode_registry_mut(&mut self) -> &mut ModeRegistry {
152 &mut self.mode_registry
153 }
154
155 /// Get immutable access to the mode registry
156 pub fn mode_registry(&self) -> &ModeRegistry {
157 &self.mode_registry
158 }
159
160 /// Get the currently active buffer ID.
161 ///
162 /// This is derived from the split manager (single source of truth).
163 /// The editor always has at least one buffer, so this never fails.
164 ///
165 /// When the active split has a buffer-group tab as its active target
166 /// (i.e., `active_group_tab.is_some()`), this returns the buffer of the
167 /// currently-focused inner panel — so that input routing, command palette
168 /// context, buffer mode, and other "what is the user looking at" queries
169 /// resolve to the panel the user is actually interacting with rather than
170 /// the split's background leaf buffer.
171 ///
172 /// The override only takes effect if the inner panel's buffer is still
173 /// live in `self.buffers`; otherwise it falls back to the main split's
174 /// leaf buffer so callers never see a stale/freed buffer id.
175 #[inline]
176 pub fn active_buffer(&self) -> BufferId {
177 let (_, buf) = self.effective_active_pair();
178 buf
179 }
180
181 /// The split id whose `SplitViewState` owns the currently-focused
182 /// cursors/viewport/buffer state. For a regular split this is just
183 /// `split_manager.active_split()`. For a split that has a group tab
184 /// active, this returns the focused inner panel's leaf id (which
185 /// lives in `split_view_states` even though it's not in the main
186 /// split tree).
187 #[inline]
188 pub fn effective_active_split(&self) -> crate::model::event::LeafId {
189 let (split, _) = self.effective_active_pair();
190 split
191 }
192
193 /// Resolve the effective (split, buffer) pair for the currently-focused
194 /// target. This is the single source of truth — both `active_buffer` and
195 /// `effective_active_split` derive from it so they can never disagree.
196 ///
197 /// Returned invariant: `split_view_states[split]` exists, its
198 /// `active_buffer` equals the returned buffer id, `self.buffers`
199 /// contains the returned buffer id, and `split.keyed_states` contains
200 /// an entry for the returned buffer id. Consequently the mutation path
201 /// in `apply_event_to_active_buffer` (which indexes into
202 /// `keyed_states[buffer]`) is always well-defined for the returned pair.
203 ///
204 /// If a buffer-group panel is focused but any of the invariants above
205 /// is not satisfied for the inner leaf (for example because the panel
206 /// buffer was freed without clearing `focused_group_leaf`), the helper
207 /// falls back to the outer split's own leaf. The fallback is also
208 /// validated before being returned.
209 #[inline]
210 fn effective_active_pair(&self) -> (crate::model::event::LeafId, BufferId) {
211 let active_split = self.split_manager.active_split();
212 if let Some(vs) = self.split_view_states.get(&active_split) {
213 if vs.active_group_tab.is_some() {
214 if let Some(inner_leaf) = vs.focused_group_leaf {
215 if let Some(inner_vs) = self.split_view_states.get(&inner_leaf) {
216 let inner_buf = inner_vs.active_buffer;
217 if self.buffers.contains_key(&inner_buf)
218 && inner_vs.keyed_states.contains_key(&inner_buf)
219 {
220 return (inner_leaf, inner_buf);
221 }
222 }
223 }
224 }
225 }
226 let outer_buf = self
227 .split_manager
228 .active_buffer_id()
229 .expect("Editor always has at least one buffer");
230 (active_split, outer_buf)
231 }
232
233 /// Get the mode name for the active buffer (if it's a virtual buffer)
234 pub fn active_buffer_mode(&self) -> Option<&str> {
235 self.buffer_metadata
236 .get(&self.active_buffer())
237 .and_then(|meta| meta.virtual_mode())
238 }
239
240 /// Check if the active buffer is read-only
241 pub fn is_active_buffer_read_only(&self) -> bool {
242 if let Some(metadata) = self.buffer_metadata.get(&self.active_buffer()) {
243 if metadata.read_only {
244 return true;
245 }
246 // Also check if the mode is read-only
247 if let Some(mode_name) = metadata.virtual_mode() {
248 return self.mode_registry.is_read_only(mode_name);
249 }
250 }
251 false
252 }
253
254 /// Check if editing should be disabled for the active buffer
255 /// This returns true when editing_disabled is true (e.g., for read-only virtual buffers)
256 pub fn is_editing_disabled(&self) -> bool {
257 self.active_state().editing_disabled
258 }
259
260 /// Mark a buffer as read-only, setting both metadata and editor state consistently.
261 /// This is the single entry point for making a buffer read-only.
262 pub fn mark_buffer_read_only(&mut self, buffer_id: BufferId, read_only: bool) {
263 if let Some(metadata) = self.buffer_metadata.get_mut(&buffer_id) {
264 metadata.read_only = read_only;
265 }
266 if let Some(state) = self.buffers.get_mut(&buffer_id) {
267 state.editing_disabled = read_only;
268 }
269 }
270
271 /// Get the effective mode for the active buffer.
272 ///
273 /// Buffer-local mode (virtual buffers) takes precedence over the global
274 /// editor mode, so that e.g. a search-replace panel isn't hijacked by
275 /// a markdown-source or vi-mode global mode.
276 pub fn effective_mode(&self) -> Option<&str> {
277 self.active_buffer_mode().or(self.editor_mode.as_deref())
278 }
279
280 /// Check if LSP has any active progress tasks (e.g., indexing)
281 pub fn has_active_lsp_progress(&self) -> bool {
282 !self.lsp_progress.is_empty()
283 }
284
285 /// Get the current LSP progress info (if any)
286 pub fn get_lsp_progress(&self) -> Vec<(String, String, Option<String>)> {
287 self.lsp_progress
288 .iter()
289 .map(|(token, info)| (token.clone(), info.title.clone(), info.message.clone()))
290 .collect()
291 }
292
293 /// Check if any LSP server for a given language is running (ready)
294 pub fn is_lsp_server_ready(&self, language: &str) -> bool {
295 use crate::services::async_bridge::LspServerStatus;
296 self.lsp_server_statuses
297 .iter()
298 .any(|((lang, server_name), status)| {
299 if !matches!(status, LspServerStatus::Running) {
300 return false;
301 }
302 if lang == language {
303 return true;
304 }
305 // Check if this server's scope accepts the queried language
306 self.lsp
307 .as_ref()
308 .and_then(|lsp| lsp.server_scope(server_name))
309 .map(|scope| scope.accepts(language))
310 .unwrap_or(false)
311 })
312 }
313
314 /// Get stored LSP diagnostics (for testing and external access)
315 /// Returns a reference to the diagnostics map keyed by file URI
316 pub fn get_stored_diagnostics(&self) -> &HashMap<String, Vec<lsp_types::Diagnostic>> {
317 &self.stored_diagnostics
318 }
319
320 /// Check if an update is available
321 pub fn is_update_available(&self) -> bool {
322 self.update_checker
323 .as_ref()
324 .map(|c| c.is_update_available())
325 .unwrap_or(false)
326 }
327
328 /// Get the latest version string if an update is available
329 pub fn latest_version(&self) -> Option<&str> {
330 self.update_checker
331 .as_ref()
332 .and_then(|c| c.latest_version())
333 }
334
335 /// Get the cached release check result (for shutdown notification)
336 pub fn get_update_result(
337 &self,
338 ) -> Option<&crate::services::release_checker::ReleaseCheckResult> {
339 self.update_checker
340 .as_ref()
341 .and_then(|c| c.get_cached_result())
342 }
343
344 /// Set a custom update checker (for testing)
345 ///
346 /// This allows injecting a custom PeriodicUpdateChecker that points to a mock server,
347 /// enabling E2E tests for the update notification UI.
348 #[doc(hidden)]
349 pub fn set_update_checker(
350 &mut self,
351 checker: crate::services::release_checker::PeriodicUpdateChecker,
352 ) {
353 self.update_checker = Some(checker);
354 }
355
356 /// Configure LSP server for a specific language
357 pub fn set_lsp_config(&mut self, language: String, config: Vec<LspServerConfig>) {
358 if let Some(ref mut lsp) = self.lsp {
359 lsp.set_language_configs(language, config);
360 }
361 }
362
363 /// Get a list of currently running LSP server languages
364 pub fn running_lsp_servers(&self) -> Vec<String> {
365 self.lsp
366 .as_ref()
367 .map(|lsp| lsp.running_servers())
368 .unwrap_or_default()
369 }
370
371 /// Return the number of pending completion requests.
372 pub fn pending_completion_requests_count(&self) -> usize {
373 self.pending_completion_requests.len()
374 }
375
376 /// Return the number of stored completion items.
377 pub fn completion_items_count(&self) -> usize {
378 self.completion_items.as_ref().map_or(0, |v| v.len())
379 }
380
381 /// Return the number of initialized LSP servers for a given language.
382 pub fn initialized_lsp_server_count(&self, language: &str) -> usize {
383 self.lsp
384 .as_ref()
385 .map(|lsp| {
386 lsp.get_handles(language)
387 .iter()
388 .filter(|sh| sh.capabilities.initialized)
389 .count()
390 })
391 .unwrap_or(0)
392 }
393
394 /// Shutdown an LSP server by language (marks it as disabled until manual restart)
395 ///
396 /// Returns true if the server was found and shutdown, false otherwise
397 pub fn shutdown_lsp_server(&mut self, language: &str) -> bool {
398 if let Some(ref mut lsp) = self.lsp {
399 lsp.shutdown_server(language)
400 } else {
401 false
402 }
403 }
404
405 /// Enable event log streaming to a file
406 pub fn enable_event_streaming<P: AsRef<Path>>(&mut self, path: P) -> AnyhowResult<()> {
407 // Enable streaming for all existing event logs
408 for event_log in self.event_logs.values_mut() {
409 event_log.enable_streaming(&path)?;
410 }
411 Ok(())
412 }
413
414 /// Log keystroke for debugging
415 pub fn log_keystroke(&mut self, key_code: &str, modifiers: &str) {
416 if let Some(event_log) = self.event_logs.get_mut(&self.active_buffer()) {
417 event_log.log_keystroke(key_code, modifiers);
418 }
419 }
420
421 /// Set up warning log monitoring
422 ///
423 /// When warnings/errors are logged, they will be written to the specified path
424 /// and the editor will be notified via the receiver.
425 pub fn set_warning_log(&mut self, receiver: std::sync::mpsc::Receiver<()>, path: PathBuf) {
426 self.warning_log = Some((receiver, path));
427 }
428
429 /// Take the warning-log receiver+path out of this editor.
430 ///
431 /// The receiver is single-consumer and lives for the process's
432 /// lifetime; on a destructive editor restart (e.g. authority swap)
433 /// `main.rs` lifts it from the old editor and re-installs it on the
434 /// new one so warnings keep flowing post-restart instead of vanishing
435 /// with the dropped editor.
436 pub fn take_warning_log(&mut self) -> Option<(std::sync::mpsc::Receiver<()>, PathBuf)> {
437 self.warning_log.take()
438 }
439
440 /// Set the status message log path
441 pub fn set_status_log_path(&mut self, path: PathBuf) {
442 self.status_log_path = Some(path);
443 }
444
445 /// Queue a new authority and restart the editor.
446 ///
447 /// Per the design decision in `docs/internal/AUTHORITY_DESIGN.md`,
448 /// authority transitions piggy-back on the existing
449 /// `change_working_dir` restart path. The caller never sees an
450 /// editor that is half-transitioned: the current `Editor` is
451 /// dropped, `main.rs` rebuilds a fresh one with the queued
452 /// authority, and session restore reopens buffers against the new
453 /// backend. This is slower than an in-place pointer swap but is
454 /// far more robust — every cached `Arc<dyn FileSystem>`, LSP
455 /// handle, terminal PTY, plugin state, and in-flight task is
456 /// dropped cleanly by the existing restart machinery.
457 pub fn install_authority(&mut self, authority: crate::services::authority::Authority) {
458 self.pending_authority = Some(authority);
459 // Re-open the same working directory; `main.rs` picks up the
460 // pending authority from the old editor just before dropping it.
461 self.request_restart(self.working_dir.clone());
462 }
463
464 /// Restore the default local authority. Same destructive-restart
465 /// semantics as `install_authority` — the caller never observes a
466 /// half-transitioned editor.
467 pub fn clear_authority(&mut self) {
468 self.install_authority(crate::services::authority::Authority::local());
469 }
470
471 /// Take the queued authority (if any). Called by `main.rs` on
472 /// restart to move the queued authority into the fresh editor.
473 pub fn take_pending_authority(&mut self) -> Option<crate::services::authority::Authority> {
474 self.pending_authority.take()
475 }
476
477 /// Directly replace the active authority without triggering a
478 /// restart. Intended for the post-construction wiring in `main.rs`
479 /// only, where the editor is still being set up and there is no
480 /// user-visible state to preserve. Do not call this from the event
481 /// loop — use `install_authority` for that.
482 ///
483 /// Also refreshes the plugin state snapshot so hooks that fire after
484 /// this call (notably `plugins_loaded`, fired by `main.rs` right
485 /// after `set_boot_authority`) see the real `authority_label` instead
486 /// of the empty string the temporary `Authority::local()` carried
487 /// during construction.
488 pub fn set_boot_authority(&mut self, authority: crate::services::authority::Authority) {
489 self.authority = authority;
490 // Propagate the authority's long-running spawner into the LSP
491 // manager so `force_spawn` can route server processes through
492 // the right backend. The editor rebuilds on every authority
493 // transition (AUTHORITY_DESIGN.md principle 7), so this is the
494 // single wiring point — no need for a hot-swap API. Path
495 // translation rides along for the same reason — LSP URIs need
496 // to be host↔container-translated under the new authority.
497 if let Some(lsp) = self.lsp.as_mut() {
498 lsp.set_long_running_spawner(self.authority.long_running_spawner.clone());
499 lsp.set_path_translation(self.authority.path_translation.clone());
500 }
501 #[cfg(feature = "plugins")]
502 {
503 self.update_plugin_state_snapshot();
504 // Notify plugins so they can re-register state-gated
505 // commands (e.g. devcontainer `Attach` only when not
506 // attached). Production transitions also trigger a full
507 // editor restart that re-runs plugin init, but firing
508 // here keeps in-process transitions and the test harness
509 // (which simulates the restart inline) consistent.
510 let label = self.authority.display_label.clone();
511 self.plugin_manager.run_hook(
512 "authority_changed",
513 crate::services::plugins::hooks::HookArgs::AuthorityChanged { label },
514 );
515 }
516 }
517
518 /// Read-only access to the active authority.
519 pub fn authority(&self) -> &crate::services::authority::Authority {
520 &self.authority
521 }
522
523 /// The editor's current working directory. This is the project
524 /// root; individual buffers may live elsewhere.
525 pub fn working_dir(&self) -> &std::path::Path {
526 &self.working_dir
527 }
528
529 /// Return buffer ids whose on-disk path sits at or under `root`.
530 /// Used by file-explorer operations that need to react when a file
531 /// or directory on disk goes away or moves.
532 pub fn buffer_ids_under_path(&self, root: &std::path::Path) -> Vec<BufferId> {
533 self.buffers
534 .iter()
535 .filter_map(|(id, state)| {
536 let p = state.buffer.file_path()?;
537 if p == root || p.starts_with(root) {
538 Some(*id)
539 } else {
540 None
541 }
542 })
543 .collect()
544 }
545
546 /// Get remote connection info if editing remote files
547 ///
548 /// Returns `Some("user@host")` for remote editing, `None` for local.
549 pub fn remote_connection_info(&self) -> Option<&str> {
550 self.authority.filesystem.remote_connection_info()
551 }
552
553 /// Get connection string for display in status bar and file explorer.
554 ///
555 /// Per principle 9, identity lives in the authority. The label set
556 /// by whoever constructed the authority wins; if it is empty (the
557 /// SSH constructor leaves it that way) we fall back to the
558 /// filesystem's `remote_connection_info()`, which knows how to
559 /// annotate disconnected SSH sessions.
560 pub fn connection_display_string(&self) -> Option<String> {
561 if !self.authority.display_label.is_empty() {
562 return Some(self.authority.display_label.clone());
563 }
564 self.remote_connection_info().map(|conn| {
565 if self.authority.filesystem.is_remote_connected() {
566 conn.to_string()
567 } else {
568 format!("{} (Disconnected)", conn)
569 }
570 })
571 }
572
573 /// Get the status log path
574 pub fn get_status_log_path(&self) -> Option<&PathBuf> {
575 self.status_log_path.as_ref()
576 }
577
578 /// Open the status log file (user clicked on status message)
579 pub fn open_status_log(&mut self) {
580 if let Some(path) = self.status_log_path.clone() {
581 // Use open_local_file since log files are always local
582 match self.open_local_file(&path) {
583 Ok(buffer_id) => {
584 self.mark_buffer_read_only(buffer_id, true);
585 }
586 Err(e) => {
587 tracing::error!("Failed to open status log: {}", e);
588 }
589 }
590 } else {
591 self.set_status_message("Status log not available".to_string());
592 }
593 }
594
595 /// Check for and handle any new warnings in the warning log
596 ///
597 /// Updates the general warning domain for the status bar.
598 /// Returns true if new warnings were found.
599 pub fn check_warning_log(&mut self) -> bool {
600 let Some((receiver, path)) = &self.warning_log else {
601 return false;
602 };
603
604 // Non-blocking check for any warnings
605 let mut new_warning_count = 0usize;
606 while receiver.try_recv().is_ok() {
607 new_warning_count += 1;
608 }
609
610 if new_warning_count > 0 {
611 // Update general warning domain (don't auto-open file)
612 self.warning_domains.general.add_warnings(new_warning_count);
613 self.warning_domains.general.set_log_path(path.clone());
614 }
615
616 new_warning_count > 0
617 }
618
619 /// Get the warning domain registry
620 pub fn get_warning_domains(&self) -> &WarningDomainRegistry {
621 &self.warning_domains
622 }
623
624 /// Get the warning log path (for opening when user clicks indicator)
625 pub fn get_warning_log_path(&self) -> Option<&PathBuf> {
626 self.warning_domains.general.log_path.as_ref()
627 }
628
629 /// Open the warning log file (user-initiated action)
630 pub fn open_warning_log(&mut self) {
631 if let Some(path) = self.warning_domains.general.log_path.clone() {
632 // Use open_local_file since log files are always local
633 match self.open_local_file(&path) {
634 Ok(buffer_id) => {
635 self.mark_buffer_read_only(buffer_id, true);
636 }
637 Err(e) => {
638 tracing::error!("Failed to open warning log: {}", e);
639 }
640 }
641 }
642 }
643
644 /// Clear the general warning indicator (user dismissed)
645 pub fn clear_warning_indicator(&mut self) {
646 self.warning_domains.general.clear();
647 }
648
649 /// Clear all warning indicators (user dismissed via command)
650 pub fn clear_warnings(&mut self) {
651 self.warning_domains.general.clear();
652 self.warning_domains.lsp.clear();
653 self.status_message = Some("Warnings cleared".to_string());
654 }
655
656 /// Check if any LSP server is in error state
657 pub fn has_lsp_error(&self) -> bool {
658 self.warning_domains.lsp.level() == WarningLevel::Error
659 }
660
661 /// Get the effective warning level for the status bar (LSP indicator)
662 /// Returns Error if LSP has errors, Warning if there are warnings, None otherwise
663 pub fn get_effective_warning_level(&self) -> WarningLevel {
664 self.warning_domains.lsp.level()
665 }
666
667 /// Get the general warning level (for the general warning badge)
668 pub fn get_general_warning_level(&self) -> WarningLevel {
669 self.warning_domains.general.level()
670 }
671
672 /// Get the general warning count
673 pub fn get_general_warning_count(&self) -> usize {
674 self.warning_domains.general.count
675 }
676
677 /// Update LSP warning domain from server statuses
678 pub fn update_lsp_warning_domain(&mut self) {
679 self.warning_domains
680 .lsp
681 .update_from_statuses(&self.lsp_server_statuses);
682 }
683
684 /// Check if mouse hover timer has expired and trigger LSP hover request
685 ///
686 /// This implements debounced hover - we wait for the configured delay before
687 /// sending the request to avoid spamming the LSP server on every mouse move.
688 /// Returns true if a hover request was triggered.
689 pub fn check_mouse_hover_timer(&mut self) -> bool {
690 // Check if mouse hover is enabled
691 if !self.config.editor.mouse_hover_enabled {
692 return false;
693 }
694
695 let hover_delay = std::time::Duration::from_millis(self.config.editor.mouse_hover_delay_ms);
696
697 // Get hover state without borrowing self
698 let hover_info = match self.mouse_state.lsp_hover_state {
699 Some((byte_pos, start_time, screen_x, screen_y)) => {
700 if self.mouse_state.lsp_hover_request_sent {
701 return false; // Already sent request for this position
702 }
703 if start_time.elapsed() < hover_delay {
704 return false; // Timer hasn't expired yet
705 }
706 Some((byte_pos, screen_x, screen_y))
707 }
708 None => return false,
709 };
710
711 let Some((byte_pos, screen_x, screen_y)) = hover_info else {
712 return false;
713 };
714
715 // Store mouse position for popup positioning
716 self.hover.set_screen_position((screen_x, screen_y));
717
718 // Request hover at the byte position — only mark as sent if dispatched
719 match self.request_hover_at_position(byte_pos) {
720 Ok(true) => {
721 self.mouse_state.lsp_hover_request_sent = true;
722 true
723 }
724 Ok(false) => false, // no server ready, timer will retry
725 Err(e) => {
726 tracing::debug!("Failed to request hover: {}", e);
727 false
728 }
729 }
730 }
731
732 /// Check if semantic highlight debounce timer has expired
733 ///
734 /// Returns true if a redraw is needed because the debounce period has elapsed
735 /// and semantic highlights need to be recomputed.
736 pub fn check_semantic_highlight_timer(&self) -> bool {
737 // Check all buffers for pending semantic highlight redraws
738 for state in self.buffers.values() {
739 if let Some(remaining) = state.reference_highlight_overlay.needs_redraw() {
740 if remaining.is_zero() {
741 return true;
742 }
743 }
744 }
745 false
746 }
747
748 /// Check if diagnostic pull timer has expired and trigger re-pull if so.
749 ///
750 /// Debounced diagnostic re-pull after document changes — waits 500ms after
751 /// the last edit before requesting fresh diagnostics from the LSP server.
752 pub fn check_diagnostic_pull_timer(&mut self) -> bool {
753 let Some((buffer_id, trigger_time)) = self.scheduled_diagnostic_pull else {
754 return false;
755 };
756
757 if Instant::now() < trigger_time {
758 return false;
759 }
760
761 self.scheduled_diagnostic_pull = None;
762
763 // Get URI and language for this buffer
764 let Some(metadata) = self.buffer_metadata.get(&buffer_id) else {
765 return false;
766 };
767 let Some(uri) = metadata.file_uri().cloned() else {
768 return false;
769 };
770 let Some(language) = self.buffers.get(&buffer_id).map(|s| s.language.clone()) else {
771 return false;
772 };
773
774 let Some(lsp) = self.lsp.as_mut() else {
775 return false;
776 };
777 let Some(sh) = lsp.handle_for_feature_mut(&language, crate::types::LspFeature::Diagnostics)
778 else {
779 return false;
780 };
781 let client = &mut sh.handle;
782
783 let request_id = self.next_lsp_request_id;
784 self.next_lsp_request_id += 1;
785 let previous_result_id = self.diagnostic_result_ids.get(uri.as_str()).cloned();
786 if let Err(e) =
787 client.document_diagnostic(request_id, uri.as_uri().clone(), previous_result_id)
788 {
789 tracing::debug!(
790 "Failed to pull diagnostics after edit for {}: {}",
791 uri.as_str(),
792 e
793 );
794 } else {
795 tracing::debug!(
796 "Pulling diagnostics after edit for {} (request_id={})",
797 uri.as_str(),
798 request_id
799 );
800 }
801
802 false // no immediate redraw needed; diagnostics arrive asynchronously
803 }
804
805 /// Check if completion trigger timer has expired and trigger completion if so
806 ///
807 /// This implements debounced completion - we wait for quick_suggestions_delay_ms
808 /// before sending the completion request to avoid spamming the LSP server.
809 /// Returns true if a completion request was triggered.
810 pub fn check_completion_trigger_timer(&mut self) -> bool {
811 // Check if we have a scheduled completion trigger
812 let Some(trigger_time) = self.scheduled_completion_trigger else {
813 return false;
814 };
815
816 // Check if the timer has expired
817 if Instant::now() < trigger_time {
818 return false;
819 }
820
821 // Clear the scheduled trigger
822 self.scheduled_completion_trigger = None;
823
824 // Don't trigger if a popup is already visible
825 if self.active_state().popups.is_visible() {
826 return false;
827 }
828
829 // Trigger the completion request
830 self.request_completion();
831
832 true
833 }
834}