diff --git a/crates/lsp/src/lsp.rs b/crates/lsp/src/lsp.rs index 86352d225d64b5986911a620e76b7f5231ef71e1..f4f538b061c892bb07cedc10304233936ee9c079 100644 --- a/crates/lsp/src/lsp.rs +++ b/crates/lsp/src/lsp.rs @@ -59,14 +59,13 @@ pub struct LanguageServer { pub struct LanguageServerId(pub usize); pub enum Subscription { - Detached, Notification { method: &'static str, - notification_handlers: Arc>>, + notification_handlers: Option>>>, }, Io { id: usize, - io_handlers: Weak>>, + io_handlers: Option>>>, }, } @@ -501,7 +500,7 @@ impl LanguageServer { self.io_handlers.lock().insert(id, Box::new(f)); Subscription::Io { id, - io_handlers: Arc::downgrade(&self.io_handlers), + io_handlers: Some(Arc::downgrade(&self.io_handlers)), } } @@ -533,7 +532,7 @@ impl LanguageServer { ); Subscription::Notification { method, - notification_handlers: self.notification_handlers.clone(), + notification_handlers: Some(self.notification_handlers.clone()), } } @@ -615,7 +614,7 @@ impl LanguageServer { ); Subscription::Notification { method, - notification_handlers: self.notification_handlers.clone(), + notification_handlers: Some(self.notification_handlers.clone()), } } @@ -726,8 +725,14 @@ impl Drop for LanguageServer { } impl Subscription { - pub fn detach(mut self) { - *(&mut self) = Self::Detached; + pub fn detach(&mut self) { + match self { + Subscription::Notification { + notification_handlers, + .. + } => *notification_handlers = None, + Subscription::Io { io_handlers, .. } => *io_handlers = None, + } } } @@ -740,15 +745,16 @@ impl fmt::Display for LanguageServerId { impl Drop for Subscription { fn drop(&mut self) { match self { - Subscription::Detached => {} Subscription::Notification { method, notification_handlers, } => { - notification_handlers.lock().remove(method); + if let Some(handlers) = notification_handlers { + handlers.lock().remove(method); + } } Subscription::Io { id, io_handlers } => { - if let Some(io_handlers) = io_handlers.upgrade() { + if let Some(io_handlers) = io_handlers.as_ref().and_then(|h| h.upgrade()) { io_handlers.lock().remove(id); } }