Skip to main content

fresh/services/
async_bridge.rs

1//! Async Bridge: Communication between async Tokio runtime and sync main loop
2//!
3//! This module implements the hybrid architecture described in TOKIO_ANALYSIS.md:
4//! - Tokio runtime handles I/O tasks (LSP, file watching, git, etc.)
5//! - Main UI loop stays synchronous (rendering, input, buffer manipulation)
6//! - std::sync::mpsc channels bridge the two worlds
7//!
8//! Philosophy:
9//! - I/O should be async (LSP, filesystem, network)
10//! - Computation should be sync (editing, rendering)
11//! - Main loop remains responsive and simple
12
13use crate::services::terminal::TerminalId;
14use crate::view::file_tree::{FileTreeView, NodeId};
15use lsp_types::{
16    CodeActionOrCommand, CompletionItem, Diagnostic, FoldingRange, InlayHint, Location,
17    SemanticTokensFullDeltaResult, SemanticTokensRangeResult, SemanticTokensResult, SignatureHelp,
18};
19use serde_json::Value;
20use std::sync::mpsc;
21
22/// Semantic token responses grouped by request type.
23#[derive(Debug)]
24pub enum LspSemanticTokensResponse {
25    Full(Result<Option<SemanticTokensResult>, String>),
26    FullDelta(Result<Option<SemanticTokensFullDeltaResult>, String>),
27    Range(Result<Option<SemanticTokensRangeResult>, String>),
28}
29
30/// Messages sent from async tasks to the synchronous main loop
31#[derive(Debug)]
32pub enum AsyncMessage {
33    /// LSP diagnostics received for a file
34    LspDiagnostics {
35        uri: String,
36        diagnostics: Vec<Diagnostic>,
37        /// Name of the server that sent these diagnostics (for per-server tracking)
38        server_name: String,
39    },
40
41    /// LSP server initialized successfully
42    LspInitialized {
43        language: String,
44        /// Name of the specific server (for per-server capability tracking)
45        server_name: String,
46        /// Capabilities reported by this server
47        capabilities: crate::services::lsp::manager::ServerCapabilitySummary,
48    },
49
50    /// LSP server crashed or failed
51    LspError {
52        language: String,
53        error: String,
54        /// Path to the stderr log file for this LSP session
55        stderr_log_path: Option<std::path::PathBuf>,
56    },
57
58    /// LSP completion response
59    LspCompletion {
60        request_id: u64,
61        items: Vec<CompletionItem>,
62    },
63
64    /// LSP go-to-definition response
65    LspGotoDefinition {
66        request_id: u64,
67        locations: Vec<Location>,
68    },
69
70    /// LSP rename response
71    LspRename {
72        request_id: u64,
73        result: Result<lsp_types::WorkspaceEdit, String>,
74    },
75
76    /// LSP hover response
77    LspHover {
78        request_id: u64,
79        /// Hover contents as a single string (joined if multiple parts)
80        contents: String,
81        /// Whether the content is markdown (true) or plaintext (false)
82        is_markdown: bool,
83        /// Optional range of the symbol that was hovered over (LSP line/character positions)
84        /// Used to highlight the hovered symbol
85        range: Option<((u32, u32), (u32, u32))>,
86    },
87
88    /// LSP find references response
89    LspReferences {
90        request_id: u64,
91        locations: Vec<Location>,
92    },
93
94    /// LSP signature help response
95    LspSignatureHelp {
96        request_id: u64,
97        signature_help: Option<SignatureHelp>,
98    },
99
100    /// LSP code actions response
101    LspCodeActions {
102        request_id: u64,
103        actions: Vec<CodeActionOrCommand>,
104    },
105
106    /// LSP completionItem/resolve response
107    LspCompletionResolved {
108        request_id: u64,
109        item: Result<lsp_types::CompletionItem, String>,
110    },
111
112    /// LSP textDocument/formatting response
113    LspFormatting {
114        request_id: u64,
115        uri: String,
116        edits: Vec<lsp_types::TextEdit>,
117    },
118
119    /// LSP textDocument/prepareRename response
120    LspPrepareRename {
121        request_id: u64,
122        result: Result<serde_json::Value, String>,
123    },
124
125    /// LSP pulled diagnostics response (textDocument/diagnostic)
126    LspPulledDiagnostics {
127        request_id: u64,
128        uri: String,
129        /// New result_id for incremental updates (None if server doesn't support)
130        result_id: Option<String>,
131        /// Diagnostics (empty if unchanged)
132        diagnostics: Vec<Diagnostic>,
133        /// True if diagnostics haven't changed since previous_result_id
134        unchanged: bool,
135    },
136
137    /// LSP inlay hints response (textDocument/inlayHint)
138    LspInlayHints {
139        request_id: u64,
140        uri: String,
141        /// Inlay hints for the requested range
142        hints: Vec<InlayHint>,
143    },
144
145    /// LSP folding ranges response (textDocument/foldingRange)
146    LspFoldingRanges {
147        request_id: u64,
148        uri: String,
149        ranges: Vec<FoldingRange>,
150    },
151
152    /// LSP semantic tokens response (full, full/delta, or range)
153    LspSemanticTokens {
154        request_id: u64,
155        uri: String,
156        response: LspSemanticTokensResponse,
157    },
158
159    /// LSP server status became quiescent (project fully loaded)
160    /// This is a rust-analyzer specific notification (experimental/serverStatus)
161    LspServerQuiescent { language: String },
162
163    /// LSP server requests diagnostic refresh (workspace/diagnostic/refresh)
164    /// Client should re-pull diagnostics for all open documents
165    LspDiagnosticRefresh { language: String },
166
167    /// File changed externally (future: file watching)
168    FileChanged { path: String },
169
170    /// Git status updated (future: git integration)
171    GitStatusChanged { status: String },
172
173    /// File explorer initialized with tree view
174    FileExplorerInitialized(FileTreeView),
175
176    /// File explorer node toggle completed
177    FileExplorerToggleNode(NodeId),
178
179    /// File explorer node refresh completed
180    FileExplorerRefreshNode(NodeId),
181
182    /// File explorer expand to path completed
183    /// Contains the updated FileTreeView with the path expanded and selected
184    FileExplorerExpandedToPath(FileTreeView),
185
186    /// Plugin-related async messages
187    Plugin(fresh_core::api::PluginAsyncMessage),
188
189    /// File open dialog: directory listing completed
190    FileOpenDirectoryLoaded(std::io::Result<Vec<crate::services::fs::DirEntry>>),
191
192    /// File open dialog: async shortcuts (Windows drive letters) loaded
193    FileOpenShortcutsLoaded(Vec<crate::app::file_open::NavigationShortcut>),
194
195    /// Terminal output received (triggers redraw)
196    TerminalOutput { terminal_id: TerminalId },
197
198    /// Terminal process exited
199    TerminalExited { terminal_id: TerminalId },
200
201    /// LSP progress notification ($/progress)
202    LspProgress {
203        language: String,
204        token: String,
205        value: LspProgressValue,
206    },
207
208    /// LSP window message (window/showMessage)
209    LspWindowMessage {
210        language: String,
211        message_type: LspMessageType,
212        message: String,
213    },
214
215    /// LSP log message (window/logMessage)
216    LspLogMessage {
217        language: String,
218        message_type: LspMessageType,
219        message: String,
220    },
221
222    /// LSP workspace/applyEdit (server -> client request)
223    /// Server asks client to apply a workspace edit (during executeCommand, etc.)
224    LspApplyEdit {
225        edit: lsp_types::WorkspaceEdit,
226        label: Option<String>,
227    },
228
229    /// LSP codeAction/resolve response
230    LspCodeActionResolved {
231        request_id: u64,
232        action: Result<lsp_types::CodeAction, String>,
233    },
234
235    /// LSP server request (server -> client)
236    /// Used for custom/extension methods that plugins can handle
237    LspServerRequest {
238        language: String,
239        server_command: String,
240        method: String,
241        params: Option<Value>,
242    },
243
244    /// Response for a plugin-initiated LSP request
245    PluginLspResponse {
246        language: String,
247        request_id: u64,
248        result: Result<Value, String>,
249    },
250
251    /// Plugin process completed with output
252    PluginProcessOutput {
253        /// Unique ID for this process (to match with callback)
254        process_id: u64,
255        /// Standard output
256        stdout: String,
257        /// Standard error
258        stderr: String,
259        /// Exit code
260        exit_code: i32,
261    },
262
263    /// LSP server status update (progress, messages, etc.)
264    LspStatusUpdate {
265        language: String,
266        /// Name of the specific server (for multi-server status tracking)
267        server_name: String,
268        status: LspServerStatus,
269        message: Option<String>,
270    },
271
272    /// Background grammar build completed — swap in the new registry.
273    /// `callback_ids` contains plugin callbacks to resolve (empty for the
274    /// initial startup build).
275    GrammarRegistryBuilt {
276        registry: std::sync::Arc<crate::primitives::grammar::GrammarRegistry>,
277        callback_ids: Vec<fresh_core::api::JsCallbackId>,
278    },
279
280    /// Quick Open file list loaded by a background task.
281    /// `complete` is `true` when the scan is finished, `false` for incremental
282    /// partial updates sent while the walk is still in progress.
283    QuickOpenFilesLoaded {
284        files: std::sync::Arc<Vec<crate::input::quick_open::providers::FileEntry>>,
285        complete: bool,
286    },
287}
288
289/// LSP progress value types
290#[derive(Debug, Clone)]
291pub enum LspProgressValue {
292    Begin {
293        title: String,
294        message: Option<String>,
295        percentage: Option<u32>,
296    },
297    Report {
298        message: Option<String>,
299        percentage: Option<u32>,
300    },
301    End {
302        message: Option<String>,
303    },
304}
305
306/// LSP message type (corresponds to MessageType in LSP spec)
307#[derive(Debug, Clone, Copy, PartialEq, Eq)]
308pub enum LspMessageType {
309    Error = 1,
310    Warning = 2,
311    Info = 3,
312    Log = 4,
313}
314
315/// LSP server status
316#[derive(Debug, Clone, Copy, PartialEq, Eq)]
317pub enum LspServerStatus {
318    Starting,
319    Initializing,
320    Running,
321    Error,
322    Shutdown,
323}
324
325/// Bridge between async Tokio runtime and sync main loop
326///
327/// Design:
328/// - Lightweight, cloneable sender that can be passed to async tasks
329/// - Non-blocking receiver checked each frame in main loop
330/// - No locks needed in main loop (channel handles synchronization)
331#[derive(Clone)]
332pub struct AsyncBridge {
333    sender: mpsc::Sender<AsyncMessage>,
334    // Receiver wrapped in Arc<Mutex<>> to allow cloning
335    receiver: std::sync::Arc<std::sync::Mutex<mpsc::Receiver<AsyncMessage>>>,
336}
337
338impl AsyncBridge {
339    /// Create a new async bridge with an unbounded channel
340    ///
341    /// Unbounded is appropriate here because:
342    /// 1. Main loop processes messages every 16ms (60fps)
343    /// 2. LSP messages are infrequent (< 100/sec typically)
344    /// 3. Memory usage is bounded by message rate × frame time
345    pub fn new() -> Self {
346        let (sender, receiver) = mpsc::channel();
347        Self {
348            sender,
349            receiver: std::sync::Arc::new(std::sync::Mutex::new(receiver)),
350        }
351    }
352
353    /// Get a cloneable sender for async tasks
354    ///
355    /// This sender can be:
356    /// - Cloned freely (cheap Arc internally)
357    /// - Sent to async tasks
358    /// - Stored in LspClient instances
359    pub fn sender(&self) -> mpsc::Sender<AsyncMessage> {
360        self.sender.clone()
361    }
362
363    /// Try to receive pending messages (non-blocking)
364    ///
365    /// Called each frame in the main loop to process async messages.
366    /// Returns all pending messages without blocking.
367    pub fn try_recv_all(&self) -> Vec<AsyncMessage> {
368        let mut messages = Vec::new();
369
370        // Lock the receiver and drain all pending messages
371        if let Ok(receiver) = self.receiver.lock() {
372            while let Ok(msg) = receiver.try_recv() {
373                messages.push(msg);
374            }
375        }
376
377        messages
378    }
379
380    /// Check if there are pending messages (non-blocking)
381    pub fn has_messages(&self) -> bool {
382        // Note: This is racy but safe - only used for optimization
383        if let Ok(receiver) = self.receiver.lock() {
384            receiver.try_recv().is_ok()
385        } else {
386            false
387        }
388    }
389}
390
391impl Default for AsyncBridge {
392    fn default() -> Self {
393        Self::new()
394    }
395}
396
397#[cfg(test)]
398mod tests {
399    use super::*;
400
401    #[test]
402    fn test_async_bridge_send_receive() {
403        let bridge = AsyncBridge::new();
404        let sender = bridge.sender();
405
406        // Send a message
407        sender
408            .send(AsyncMessage::LspInitialized {
409                language: "rust".to_string(),
410                server_name: "test".to_string(),
411                capabilities: Default::default(),
412            })
413            .unwrap();
414
415        // Receive it
416        let messages = bridge.try_recv_all();
417        assert_eq!(messages.len(), 1);
418
419        match &messages[0] {
420            AsyncMessage::LspInitialized {
421                language,
422                server_name,
423                ..
424            } => {
425                assert_eq!(language, "rust");
426                assert_eq!(server_name, "test");
427            }
428            _ => panic!("Wrong message type"),
429        }
430    }
431
432    #[test]
433    fn test_async_bridge_multiple_messages() {
434        let bridge = AsyncBridge::new();
435        let sender = bridge.sender();
436
437        // Send multiple messages
438        sender
439            .send(AsyncMessage::LspInitialized {
440                language: "rust".to_string(),
441                server_name: "test".to_string(),
442                capabilities: Default::default(),
443            })
444            .unwrap();
445        sender
446            .send(AsyncMessage::LspInitialized {
447                language: "typescript".to_string(),
448                server_name: "test".to_string(),
449                capabilities: Default::default(),
450            })
451            .unwrap();
452
453        // Receive all at once
454        let messages = bridge.try_recv_all();
455        assert_eq!(messages.len(), 2);
456    }
457
458    #[test]
459    fn test_async_bridge_no_messages() {
460        let bridge = AsyncBridge::new();
461
462        // Try to receive with no messages
463        let messages = bridge.try_recv_all();
464        assert_eq!(messages.len(), 0);
465    }
466
467    #[test]
468    fn test_async_bridge_clone_sender() {
469        let bridge = AsyncBridge::new();
470        let sender1 = bridge.sender();
471        let sender2 = sender1.clone();
472
473        // Both senders work
474        sender1
475            .send(AsyncMessage::LspInitialized {
476                language: "rust".to_string(),
477                server_name: "test".to_string(),
478                capabilities: Default::default(),
479            })
480            .unwrap();
481        sender2
482            .send(AsyncMessage::LspInitialized {
483                language: "typescript".to_string(),
484                server_name: "test".to_string(),
485                capabilities: Default::default(),
486            })
487            .unwrap();
488
489        let messages = bridge.try_recv_all();
490        assert_eq!(messages.len(), 2);
491    }
492
493    #[test]
494    fn test_async_bridge_diagnostics() {
495        let bridge = AsyncBridge::new();
496        let sender = bridge.sender();
497
498        // Send diagnostic message
499        let diagnostics = vec![lsp_types::Diagnostic {
500            range: lsp_types::Range {
501                start: lsp_types::Position {
502                    line: 0,
503                    character: 0,
504                },
505                end: lsp_types::Position {
506                    line: 0,
507                    character: 5,
508                },
509            },
510            severity: Some(lsp_types::DiagnosticSeverity::ERROR),
511            code: None,
512            code_description: None,
513            source: Some("rust-analyzer".to_string()),
514            message: "test error".to_string(),
515            related_information: None,
516            tags: None,
517            data: None,
518        }];
519
520        sender
521            .send(AsyncMessage::LspDiagnostics {
522                uri: "file:///test.rs".to_string(),
523                diagnostics: diagnostics.clone(),
524                server_name: "rust-analyzer".to_string(),
525            })
526            .unwrap();
527
528        let messages = bridge.try_recv_all();
529        assert_eq!(messages.len(), 1);
530
531        match &messages[0] {
532            AsyncMessage::LspDiagnostics {
533                uri,
534                diagnostics: diags,
535                server_name,
536            } => {
537                assert_eq!(uri, "file:///test.rs");
538                assert_eq!(diags.len(), 1);
539                assert_eq!(diags[0].message, "test error");
540                assert_eq!(server_name, "rust-analyzer");
541            }
542            _ => panic!("Expected LspDiagnostics message"),
543        }
544    }
545
546    #[test]
547    fn test_async_bridge_error_message() {
548        let bridge = AsyncBridge::new();
549        let sender = bridge.sender();
550
551        sender
552            .send(AsyncMessage::LspError {
553                language: "rust".to_string(),
554                error: "Failed to initialize".to_string(),
555                stderr_log_path: None,
556            })
557            .unwrap();
558
559        let messages = bridge.try_recv_all();
560        assert_eq!(messages.len(), 1);
561
562        match &messages[0] {
563            AsyncMessage::LspError {
564                language,
565                error,
566                stderr_log_path,
567            } => {
568                assert_eq!(language, "rust");
569                assert_eq!(error, "Failed to initialize");
570                assert!(stderr_log_path.is_none());
571            }
572            _ => panic!("Expected LspError message"),
573        }
574    }
575
576    #[test]
577    fn test_async_bridge_clone_bridge() {
578        let bridge = AsyncBridge::new();
579        let bridge_clone = bridge.clone();
580        let sender = bridge.sender();
581
582        // Send via original bridge's sender
583        sender
584            .send(AsyncMessage::LspInitialized {
585                language: "rust".to_string(),
586                server_name: "test".to_string(),
587                capabilities: Default::default(),
588            })
589            .unwrap();
590
591        // Receive via cloned bridge
592        let messages = bridge_clone.try_recv_all();
593        assert_eq!(messages.len(), 1);
594    }
595
596    #[test]
597    fn test_async_bridge_multiple_calls_to_try_recv_all() {
598        let bridge = AsyncBridge::new();
599        let sender = bridge.sender();
600
601        sender
602            .send(AsyncMessage::LspInitialized {
603                language: "rust".to_string(),
604                server_name: "test".to_string(),
605                capabilities: Default::default(),
606            })
607            .unwrap();
608
609        // First call gets the message
610        let messages1 = bridge.try_recv_all();
611        assert_eq!(messages1.len(), 1);
612
613        // Second call gets nothing
614        let messages2 = bridge.try_recv_all();
615        assert_eq!(messages2.len(), 0);
616    }
617
618    #[test]
619    fn test_async_bridge_ordering() {
620        let bridge = AsyncBridge::new();
621        let sender = bridge.sender();
622
623        // Send messages in order
624        sender
625            .send(AsyncMessage::LspInitialized {
626                language: "rust".to_string(),
627                server_name: "test".to_string(),
628                capabilities: Default::default(),
629            })
630            .unwrap();
631        sender
632            .send(AsyncMessage::LspInitialized {
633                language: "typescript".to_string(),
634                server_name: "test".to_string(),
635                capabilities: Default::default(),
636            })
637            .unwrap();
638        sender
639            .send(AsyncMessage::LspInitialized {
640                language: "python".to_string(),
641                server_name: "test".to_string(),
642                capabilities: Default::default(),
643            })
644            .unwrap();
645
646        // Messages should be received in same order
647        let messages = bridge.try_recv_all();
648        assert_eq!(messages.len(), 3);
649
650        match (&messages[0], &messages[1], &messages[2]) {
651            (
652                AsyncMessage::LspInitialized { language: l1, .. },
653                AsyncMessage::LspInitialized { language: l2, .. },
654                AsyncMessage::LspInitialized { language: l3, .. },
655            ) => {
656                assert_eq!(l1, "rust");
657                assert_eq!(l2, "typescript");
658                assert_eq!(l3, "python");
659            }
660            _ => panic!("Expected ordered LspInitialized messages"),
661        }
662    }
663}