From 017e02e6236498da1eaaf8184610f7c6a9d90ddc Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Wed, 10 Sep 2025 12:15:26 +0000 Subject: [PATCH] lsp: No error when sending messages to the Preview fails The live-preview can get killed at any moment, so do not fail if sending messages to it fails. --- tools/lsp/common.rs | 6 +-- tools/lsp/language.rs | 78 +++++++++++---------------- tools/lsp/main.rs | 12 ++--- tools/lsp/preview/connector/native.rs | 18 ++++--- tools/lsp/preview/connector/wasm.rs | 5 +- tools/lsp/wasm_main.rs | 10 ++-- 6 files changed, 55 insertions(+), 74 deletions(-) diff --git a/tools/lsp/common.rs b/tools/lsp/common.rs index 96aabf233..49323b4b4 100644 --- a/tools/lsp/common.rs +++ b/tools/lsp/common.rs @@ -42,7 +42,7 @@ pub enum PreviewTarget { #[allow(dead_code)] pub trait LspToPreview { - fn send(&self, message: &LspToPreviewMessage) -> Result<()>; + fn send(&self, message: &LspToPreviewMessage); fn set_preview_target(&self, target: PreviewTarget) -> Result<()>; fn preview_target(&self) -> PreviewTarget; } @@ -52,9 +52,7 @@ pub trait LspToPreview { pub struct DummyLspToPreview {} impl LspToPreview for DummyLspToPreview { - fn send(&self, _message: &LspToPreviewMessage) -> Result<()> { - Ok(()) - } + fn send(&self, _message: &LspToPreviewMessage) {} fn preview_target(&self) -> PreviewTarget { PreviewTarget::Dummy diff --git a/tools/lsp/language.rs b/tools/lsp/language.rs index f7ef02275..18e3c7786 100644 --- a/tools/lsp/language.rs +++ b/tools/lsp/language.rs @@ -34,6 +34,7 @@ use lsp_types::{ SemanticTokensOptions, ServerCapabilities, ServerInfo, TextDocumentSyncCapability, TextEdit, Url, WorkDoneProgressOptions, }; + use std::cell::RefCell; use std::collections::HashMap; use std::collections::HashSet; @@ -90,22 +91,18 @@ pub fn request_state(ctx: &std::rc::Rc) { } let version = document_cache.document_version(&url); - ctx.to_preview - .send(&common::LspToPreviewMessage::SetContents { - url: common::VersionedUrl::new(url, version), - contents: node.text().to_string(), - }) - .unwrap(); + ctx.to_preview.send(&common::LspToPreviewMessage::SetContents { + url: common::VersionedUrl::new(url, version), + contents: node.text().to_string(), + }); } - ctx.to_preview - .send(&common::LspToPreviewMessage::SetConfiguration { - config: ctx.preview_config.borrow().clone(), - }) - .unwrap(); + ctx.to_preview.send(&common::LspToPreviewMessage::SetConfiguration { + config: ctx.preview_config.borrow().clone(), + }); if let Some(c) = ctx.to_show.borrow().clone() { - ctx.to_preview.send(&common::LspToPreviewMessage::ShowPreview(c)).unwrap(); + ctx.to_preview.send(&common::LspToPreviewMessage::ShowPreview(c)); } } @@ -431,12 +428,10 @@ pub fn register_request_handlers(rh: &mut RequestHandler) { { let element = gp.as_ref().unwrap().child_node(SyntaxKind::Element).unwrap(); - ctx.to_preview - .send(&common::LspToPreviewMessage::HighlightFromEditor { - url: Some(uri), - offset: element.text_range().start().into(), - }) - .unwrap(); + ctx.to_preview.send(&common::LspToPreviewMessage::HighlightFromEditor { + url: Some(uri), + offset: element.text_range().start().into(), + }); let range = util::node_to_lsp_range(&p); return Ok(Some(vec![lsp_types::DocumentHighlight { range, kind: None }])); @@ -454,23 +449,19 @@ pub fn register_request_handlers(rh: &mut RequestHandler) { .as_ref() .is_some_and(|n| n.kind() != SyntaxKind::Component) { - ctx.to_preview - .send(&common::LspToPreviewMessage::HighlightFromEditor { - url: Some(uri), - offset: gp.unwrap().text_range().start().into(), - }) - .unwrap(); + ctx.to_preview.send(&common::LspToPreviewMessage::HighlightFromEditor { + url: Some(uri), + offset: gp.unwrap().text_range().start().into(), + }); } return Ok(Some(vec![lsp_types::DocumentHighlight { range, kind: None }])); } if let Some(value) = common::rename_element_id::find_element_ids(&tk, &p) { - ctx.to_preview - .send(&common::LspToPreviewMessage::HighlightFromEditor { - url: None, - offset: 0, - }) - .unwrap(); + ctx.to_preview.send(&common::LspToPreviewMessage::HighlightFromEditor { + url: None, + offset: 0, + }); return Ok(Some( value .into_iter() @@ -483,8 +474,7 @@ pub fn register_request_handlers(rh: &mut RequestHandler) { } } ctx.to_preview - .send(&common::LspToPreviewMessage::HighlightFromEditor { url: None, offset: 0 }) - .unwrap(); + .send(&common::LspToPreviewMessage::HighlightFromEditor { url: None, offset: 0 }); Ok(None) }); rh.register::(|params, ctx| async move { @@ -580,7 +570,7 @@ pub fn show_preview_command( let c = common::PreviewComponent { url, component }; ctx.to_show.replace(Some(c.clone())); - ctx.to_preview.send(&common::LspToPreviewMessage::ShowPreview(c)).unwrap(); + ctx.to_preview.send(&common::LspToPreviewMessage::ShowPreview(c)); Ok(()) } @@ -742,12 +732,10 @@ pub(crate) async fn reload_document_impl( let dependencies = match action { FileAction::ProcessContent(content) => { if let Some(ctx) = ctx { - ctx.to_preview - .send(&common::LspToPreviewMessage::SetContents { - url: common::VersionedUrl::new(url.clone(), version), - contents: content.clone(), - }) - .unwrap(); + ctx.to_preview.send(&common::LspToPreviewMessage::SetContents { + url: common::VersionedUrl::new(url.clone(), version), + contents: content.clone(), + }); } let dependencies = document_cache.invalidate_url(&url); let _ = document_cache.load_url(&url, version, content, &mut diag).await; @@ -756,9 +744,7 @@ pub(crate) async fn reload_document_impl( FileAction::IgnoreFile => return Default::default(), FileAction::InvalidateFile => { if let Some(ctx) = ctx { - ctx.to_preview - .send(&common::LspToPreviewMessage::ForgetFile { url: url.clone() }) - .unwrap(); + ctx.to_preview.send(&common::LspToPreviewMessage::ForgetFile { url: url.clone() }); } document_cache.invalidate_url(&url) } @@ -854,16 +840,14 @@ fn send_diagnostics( pub async fn invalidate_document(ctx: &Rc, url: lsp_types::Url) -> common::Result<()> { // The preview cares about resources and slint files, so forward everything - ctx.to_preview - .send(&common::LspToPreviewMessage::InvalidateContents { url: url.clone() }) - .unwrap(); + ctx.to_preview.send(&common::LspToPreviewMessage::InvalidateContents { url: url.clone() }); ctx.document_cache.borrow_mut().drop_document(&url) } pub async fn delete_document(ctx: &Rc, url: lsp_types::Url) -> common::Result<()> { // The preview cares about resources and slint files, so forward everything - ctx.to_preview.send(&common::LspToPreviewMessage::ForgetFile { url: url.clone() }).unwrap(); + ctx.to_preview.send(&common::LspToPreviewMessage::ForgetFile { url: url.clone() }); ctx.document_cache.borrow_mut().drop_document(&url) } @@ -1456,7 +1440,7 @@ pub async fn load_configuration(ctx: &Context) -> common::Result<()> { document_cache.reload_cached_file(url, &mut diag).await; } - ctx.to_preview.send(&common::LspToPreviewMessage::SetConfiguration { config }).unwrap(); + ctx.to_preview.send(&common::LspToPreviewMessage::SetConfiguration { config }); send_diagnostics( &ctx.server_notifier, diff --git a/tools/lsp/main.rs b/tools/lsp/main.rs index 840879e8e..8f476c67d 100644 --- a/tools/lsp/main.rs +++ b/tools/lsp/main.rs @@ -342,14 +342,12 @@ fn main_loop(connection: Connection, init_param: InitializeParams, cli_args: Cli let contents = std::fs::read_to_string(&path); if let Ok(url) = Url::from_file_path(&path) { if let Ok(contents) = &contents { - to_preview - .send(&common::LspToPreviewMessage::SetContents { - url: common::VersionedUrl::new(url, None), - contents: contents.clone(), - }) - .unwrap(); + to_preview.send(&common::LspToPreviewMessage::SetContents { + url: common::VersionedUrl::new(url, None), + contents: contents.clone(), + }); } else { - to_preview.send(&common::LspToPreviewMessage::ForgetFile { url }).unwrap(); + to_preview.send(&common::LspToPreviewMessage::ForgetFile { url }); } } Some(contents.map(|c| (None, c))) diff --git a/tools/lsp/preview/connector/native.rs b/tools/lsp/preview/connector/native.rs index 157fc4480..d6fffb9ba 100644 --- a/tools/lsp/preview/connector/native.rs +++ b/tools/lsp/preview/connector/native.rs @@ -105,16 +105,17 @@ impl Drop for ChildProcessLspToPreview { } impl common::LspToPreview for ChildProcessLspToPreview { - fn send(&self, message: &common::LspToPreviewMessage) -> common::Result<()> { + fn send(&self, message: &common::LspToPreviewMessage) { if self.preview_is_running() { let mut inner = self.inner.borrow_mut(); let inner = inner.as_mut().unwrap(); - let message = serde_json::to_string(message).map_err(|e| e.to_string())?; - writeln!(inner.to_child, "{message}")?; + let Ok(message) = serde_json::to_string(message) else { + return; + }; + let _ = writeln!(inner.to_child, "{message}"); } else if let common::LspToPreviewMessage::ShowPreview(_) = message { self.start_preview().unwrap(); } - Ok(()) } fn preview_target(&self) -> common::PreviewTarget { @@ -137,8 +138,9 @@ impl EmbeddedLspToPreview { } impl common::LspToPreview for EmbeddedLspToPreview { - fn send(&self, message: &common::LspToPreviewMessage) -> common::Result<()> { - self.server_notifier.send_notification::(message.clone()) + fn send(&self, message: &common::LspToPreviewMessage) { + let _ = + self.server_notifier.send_notification::(message.clone()); } fn preview_target(&self) -> common::PreviewTarget { @@ -169,8 +171,8 @@ impl SwitchableLspToPreview { } impl common::LspToPreview for SwitchableLspToPreview { - fn send(&self, message: &common::LspToPreviewMessage) -> common::Result<()> { - self.lsp_to_previews.get(&self.current_target.borrow()).unwrap().send(message) + fn send(&self, message: &common::LspToPreviewMessage) { + let _ = self.lsp_to_previews.get(&self.current_target.borrow()).unwrap().send(message); } fn preview_target(&self) -> common::PreviewTarget { diff --git a/tools/lsp/preview/connector/wasm.rs b/tools/lsp/preview/connector/wasm.rs index 5d36de88f..7e0bfb02b 100644 --- a/tools/lsp/preview/connector/wasm.rs +++ b/tools/lsp/preview/connector/wasm.rs @@ -240,8 +240,9 @@ impl WasmLspToPreview { } impl common::LspToPreview for WasmLspToPreview { - fn send(&self, message: &common::LspToPreviewMessage) -> common::Result<()> { - self.server_notifier.send_notification::(message.clone()) + fn send(&self, message: &common::LspToPreviewMessage) { + let _ = + self.server_notifier.send_notification::(message.clone()); } fn preview_target(&self) -> common::PreviewTarget { diff --git a/tools/lsp/wasm_main.rs b/tools/lsp/wasm_main.rs index b90e27358..cc8ecaebd 100644 --- a/tools/lsp/wasm_main.rs +++ b/tools/lsp/wasm_main.rs @@ -202,12 +202,10 @@ pub fn create( return Some(contents.map(|c| (None, c))); }; if let Ok(contents) = &contents { - to_preview - .send(&LspToPreviewMessage::SetContents { - url: VersionedUrl::new(url, None), - contents: contents.clone(), - }) - .unwrap() + to_preview.send(&LspToPreviewMessage::SetContents { + url: VersionedUrl::new(url, None), + contents: contents.clone(), + }); } Some(contents.map(|c| (None, c))) })