|
| 1 | +use std::collections::HashSet; |
| 2 | + |
| 3 | +use socketioxide::extract::{SocketRef, State}; |
| 4 | +use tracing::{error, info}; |
| 5 | + |
| 6 | +use crate::app_state::{AppState, is_language_opened}; |
| 7 | +use crate::handlers::{ |
| 8 | + acp_handler::*, |
| 9 | + git_handler::*, |
| 10 | + io_handler::*, |
| 11 | + lsp_handler::*, |
| 12 | + search_handler::*, |
| 13 | + terminal_handler::*, |
| 14 | + theme_handler::*, |
| 15 | +}; |
| 16 | + |
| 17 | +pub async fn handle_connect(socket: SocketRef, _state: State<AppState>) { |
| 18 | + info!("Socket.IO connected: {:?} {:?}", socket.ns(), socket.id); |
| 19 | + |
| 20 | + socket.on("file:open", handle_file_open); |
| 21 | + socket.on("dir:list", handle_dir_list); |
| 22 | + socket.on("file:change", handle_file_change); |
| 23 | + socket.on("file:save", handle_file_save); |
| 24 | + socket.on("file:create", handle_create); |
| 25 | + socket.on("file:close", handle_file_close); |
| 26 | + |
| 27 | + socket.on("lsp:completion", handle_completion); |
| 28 | + socket.on("lsp:definition", handle_definition); |
| 29 | + socket.on("lsp:references", handle_references); |
| 30 | + socket.on("lsp:hover", handle_hover); |
| 31 | + |
| 32 | + socket.on("search:start", handle_search); |
| 33 | + socket.on("search:cancel", handle_search_cancel); |
| 34 | + |
| 35 | + socket.on("terminal:start", handle_terminal_start); |
| 36 | + socket.on("terminal:input", handle_terminal_input); |
| 37 | + socket.on("terminal:resize", handle_terminal_resize); |
| 38 | + socket.on("terminal:close", handle_terminal_close); |
| 39 | + socket.on("terminal:reconnect", handle_terminal_reconnect); |
| 40 | + |
| 41 | + socket.on("acp:start", handle_acp_start); |
| 42 | + socket.on("acp:prompt", handle_acp_prompt); |
| 43 | + socket.on("acp:stop", handle_acp_stop); |
| 44 | + socket.on("acp:cancel", handle_acp_cancel); |
| 45 | + socket.on("acp:set_model", handle_acp_set_model); |
| 46 | + socket.on("acp:set_reasoning", handle_acp_set_reasoning); |
| 47 | + socket.on("acp:list", handle_acp_list); |
| 48 | + socket.on("acp:sessions_list", handle_acp_sessions_list); |
| 49 | + socket.on("acp:reconnect", handle_acp_reconnect); |
| 50 | + socket.on("acp:permission_response", handle_acp_permission_response); |
| 51 | + socket.on("acp:undo", handle_acp_undo); |
| 52 | + |
| 53 | + socket.on("git:status", handle_git_status); |
| 54 | + socket.on("git:file-original", handle_git_file_original); |
| 55 | + socket.on("git:commit", handle_git_commit); |
| 56 | + socket.on("git:push", handle_git_push); |
| 57 | + socket.on("git:pull", handle_git_pull); |
| 58 | + socket.on("git:branches", handle_git_branches); |
| 59 | + socket.on("git:checkout", handle_git_checkout); |
| 60 | + socket.on("git:revert", handle_git_revert); |
| 61 | + |
| 62 | + socket.on("theme:list", handle_theme_list); |
| 63 | + socket.on("theme:get", handle_theme_get); |
| 64 | + socket.on_disconnect(handle_disconnect) |
| 65 | +} |
| 66 | + |
| 67 | +pub async fn handle_disconnect(socket: SocketRef, state: State<AppState>) { |
| 68 | + info!("Socket.IO disconnected: {}", socket.id); |
| 69 | + |
| 70 | + let sid = socket.id.as_str().to_string(); |
| 71 | + |
| 72 | + // Get opened files for this socket before removing socket data |
| 73 | + let opened_files = { |
| 74 | + let sockets_data = state.socket2data.lock().await; |
| 75 | + match sockets_data.get(&sid) { |
| 76 | + Some(socket_data) => socket_data.opened_files.clone(), |
| 77 | + None => return, |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + // Get languages for files opened by this socket |
| 82 | + let languages = { |
| 83 | + let f2c = state.file2code.lock().await; |
| 84 | + opened_files |
| 85 | + .iter() |
| 86 | + .filter_map(|path| f2c.get(path).map(|code| code.lang.clone())) |
| 87 | + .collect::<HashSet<_>>() |
| 88 | + }; |
| 89 | + |
| 90 | + // Remove socket data immediately |
| 91 | + { |
| 92 | + let mut sockets_data = state.socket2data.lock().await; |
| 93 | + sockets_data.remove(&sid); |
| 94 | + } |
| 95 | + |
| 96 | + // Spawn a delayed task to clean up files and LSP servers (5-second grace period) |
| 97 | + tokio::spawn(async move { |
| 98 | + tokio::time::sleep(tokio::time::Duration::from_secs(5)).await; |
| 99 | + |
| 100 | + // Get all opened files from remaining active sockets |
| 101 | + let all_opened_files: HashSet<String> = { |
| 102 | + let sockets_data = state.socket2data.lock().await; |
| 103 | + sockets_data |
| 104 | + .values() |
| 105 | + .flat_map(|data| data.opened_files.iter()) |
| 106 | + .cloned() |
| 107 | + .collect() |
| 108 | + }; |
| 109 | + |
| 110 | + // Clean up files that are no longer opened by any socket |
| 111 | + let files_to_close: Vec<(String, String)> = { |
| 112 | + let f2c = state.file2code.lock().await; |
| 113 | + opened_files |
| 114 | + .iter() |
| 115 | + .filter(|path| !all_opened_files.contains(*path)) |
| 116 | + .filter_map(|path| f2c.get(path).map(|code| (path.clone(), code.lang.clone()))) |
| 117 | + .collect() |
| 118 | + }; |
| 119 | + |
| 120 | + // Close files (notify LSP didClose) |
| 121 | + if !files_to_close.is_empty() { |
| 122 | + let mut lsp_manager = state.lsp_manager.lock().await; |
| 123 | + for (file_path, lang) in &files_to_close { |
| 124 | + if let Some(lsp) = lsp_manager.get(lang).await { |
| 125 | + if let Err(e) = lsp.did_close(file_path) { |
| 126 | + error!("Failed to notify LSP didClose for {}: {:?}", file_path, e); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // Stop LSP servers for languages that have no files opened by other active sockets |
| 133 | + for lang in languages { |
| 134 | + if !is_language_opened(&lang, &state).await { |
| 135 | + info!("Lsp autoclose after grace period: '{}'", lang); |
| 136 | + let mut lsp_manager = state.lsp_manager.lock().await; |
| 137 | + lsp_manager.stop(&lang).await; |
| 138 | + } |
| 139 | + } |
| 140 | + }); |
| 141 | +} |
0 commit comments