lsp: No error when sending messages to the Preview fails
Some checks are pending
autofix.ci / format_fix (push) Waiting to run
autofix.ci / lint_typecheck (push) Waiting to run
CI / python_test (windows-2022) (push) Blocked by required conditions
CI / wasm_demo (push) Blocked by required conditions
CI / files-changed (push) Waiting to run
CI / build_and_test (--exclude ffmpeg --exclude gstreamer-player, windows-2022, beta) (push) Blocked by required conditions
CI / build_and_test (--exclude ffmpeg --exclude gstreamer-player, windows-2022, stable) (push) Blocked by required conditions
CI / build_and_test (ubuntu-22.04, nightly) (push) Blocked by required conditions
CI / node_test (macos-14) (push) Blocked by required conditions
CI / node_test (ubuntu-22.04) (push) Blocked by required conditions
CI / node_test (windows-2022) (push) Blocked by required conditions
CI / python_test (macos-14) (push) Blocked by required conditions
CI / python_test (ubuntu-22.04) (push) Blocked by required conditions
CI / cpp_test_driver (macos-13) (push) Blocked by required conditions
CI / cpp_test_driver (ubuntu-22.04) (push) Blocked by required conditions
CI / cpp_test_driver (windows-2022) (push) Blocked by required conditions
CI / cpp_cmake (macos-14, 1.85) (push) Blocked by required conditions
CI / cpp_cmake (ubuntu-22.04, stable) (push) Blocked by required conditions
CI / cpp_cmake (windows-2022, nightly) (push) Blocked by required conditions
CI / cpp_package_test (push) Blocked by required conditions
CI / vsce_build_test (push) Blocked by required conditions
CI / tree-sitter (push) Blocked by required conditions
CI / mcu (pico-st7789, thumbv6m-none-eabi) (push) Blocked by required conditions
CI / mcu (pico2-st7789, thumbv8m.main-none-eabihf) (push) Blocked by required conditions
CI / mcu (stm32h735g, thumbv7em-none-eabihf) (push) Blocked by required conditions
CI / build_and_test (--exclude bevy-example, ubuntu-22.04, 1.85) (push) Blocked by required conditions
CI / build_and_test (--exclude ffmpeg --exclude gstreamer-player, --exclude bevy-example, windows-2022, 1.85) (push) Blocked by required conditions
CI / build_and_test (--exclude ffmpeg --exclude gstreamer-player, macos-14, stable) (push) Blocked by required conditions
CI / mcu-embassy (push) Blocked by required conditions
CI / ffi_32bit_build (push) Blocked by required conditions
CI / docs (push) Blocked by required conditions
CI / wasm (push) Blocked by required conditions
CI / updater_test (0.3.0) (push) Blocked by required conditions
CI / fmt_test (push) Blocked by required conditions
CI / esp-idf-quick (push) Blocked by required conditions
CI / android (push) Blocked by required conditions
CI / miri (push) Blocked by required conditions
CI / test-figma-inspector (push) Blocked by required conditions

The live-preview can get killed at any moment, so do not fail
if sending messages to it fails.
This commit is contained in:
Tobias Hunger 2025-09-10 12:15:26 +00:00 committed by Olivier Goffart
parent bdb323754e
commit 017e02e623
6 changed files with 55 additions and 74 deletions

View file

@ -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

View file

@ -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<Context>) {
}
let version = document_cache.document_version(&url);
ctx.to_preview
.send(&common::LspToPreviewMessage::SetContents {
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::SetConfiguration {
ctx.to_preview.send(&common::LspToPreviewMessage::SetConfiguration {
config: ctx.preview_config.borrow().clone(),
})
.unwrap();
});
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 {
ctx.to_preview.send(&common::LspToPreviewMessage::HighlightFromEditor {
url: Some(uri),
offset: element.text_range().start().into(),
})
.unwrap();
});
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 {
ctx.to_preview.send(&common::LspToPreviewMessage::HighlightFromEditor {
url: Some(uri),
offset: gp.unwrap().text_range().start().into(),
})
.unwrap();
});
}
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 {
ctx.to_preview.send(&common::LspToPreviewMessage::HighlightFromEditor {
url: None,
offset: 0,
})
.unwrap();
});
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::<Rename, _>(|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 {
ctx.to_preview.send(&common::LspToPreviewMessage::SetContents {
url: common::VersionedUrl::new(url.clone(), version),
contents: content.clone(),
})
.unwrap();
});
}
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<Context>, 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<Context>, 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,

View file

@ -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 {
to_preview.send(&common::LspToPreviewMessage::SetContents {
url: common::VersionedUrl::new(url, None),
contents: contents.clone(),
})
.unwrap();
});
} else {
to_preview.send(&common::LspToPreviewMessage::ForgetFile { url }).unwrap();
to_preview.send(&common::LspToPreviewMessage::ForgetFile { url });
}
}
Some(contents.map(|c| (None, c)))

View file

@ -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::<common::LspToPreviewMessage>(message.clone())
fn send(&self, message: &common::LspToPreviewMessage) {
let _ =
self.server_notifier.send_notification::<common::LspToPreviewMessage>(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 {

View file

@ -240,8 +240,9 @@ impl WasmLspToPreview {
}
impl common::LspToPreview for WasmLspToPreview {
fn send(&self, message: &common::LspToPreviewMessage) -> common::Result<()> {
self.server_notifier.send_notification::<common::LspToPreviewMessage>(message.clone())
fn send(&self, message: &common::LspToPreviewMessage) {
let _ =
self.server_notifier.send_notification::<common::LspToPreviewMessage>(message.clone());
}
fn preview_target(&self) -> common::PreviewTarget {

View file

@ -202,12 +202,10 @@ pub fn create(
return Some(contents.map(|c| (None, c)));
};
if let Ok(contents) = &contents {
to_preview
.send(&LspToPreviewMessage::SetContents {
to_preview.send(&LspToPreviewMessage::SetContents {
url: VersionedUrl::new(url, None),
contents: contents.clone(),
})
.unwrap()
});
}
Some(contents.map(|c| (None, c)))
})