diff --git a/tools/lsp/common.rs b/tools/lsp/common.rs index c1ccfdf59..e6fd59c6c 100644 --- a/tools/lsp/common.rs +++ b/tools/lsp/common.rs @@ -15,7 +15,7 @@ pub type Result = std::result::Result; /// ServerNotifier pub trait PreviewApi { fn set_contents(&self, path: &Path, contents: &str); - fn load_preview(&self, component: PreviewComponent, behavior: PostLoadBehavior); + fn load_preview(&self, component: PreviewComponent); fn config_changed( &self, style: &str, @@ -44,10 +44,3 @@ pub struct PreviewComponent { /// The style name for the preview pub style: String, } - -/// What to do after a preview is loaded -#[allow(unused)] -pub enum PostLoadBehavior { - ShowAfterLoad, - DoNothing, -} diff --git a/tools/lsp/language.rs b/tools/lsp/language.rs index 03ff404f6..0691a6a36 100644 --- a/tools/lsp/language.rs +++ b/tools/lsp/language.rs @@ -394,16 +394,13 @@ pub fn show_preview_command(params: &[serde_json::Value], ctx: &Rc) -> params.get(1).and_then(|v| v.as_str()).filter(|v| !v.is_empty()).map(|v| v.to_string()); let path = uri_to_file(&url).unwrap_or_default(); - ctx.preview.load_preview( - crate::common::PreviewComponent { - path, - component, - include_paths: config.include_paths.clone(), - library_paths: config.library_paths.clone(), - style: config.style.clone().unwrap_or_default(), - }, - crate::common::PostLoadBehavior::ShowAfterLoad, - ); + ctx.preview.load_preview(crate::common::PreviewComponent { + path, + component, + include_paths: config.include_paths.clone(), + library_paths: config.library_paths.clone(), + style: config.style.clone().unwrap_or_default(), + }); Ok(()) } diff --git a/tools/lsp/main.rs b/tools/lsp/main.rs index 3fba2e893..43ce00ed3 100644 --- a/tools/lsp/main.rs +++ b/tools/lsp/main.rs @@ -41,15 +41,11 @@ impl PreviewApi for Previewer { preview::set_contents(_path, _contents.to_string()); } - fn load_preview( - &self, - _component: common::PreviewComponent, - _behavior: common::PostLoadBehavior, - ) { + fn load_preview(&self, _component: common::PreviewComponent) { #[cfg(feature = "preview")] { preview::open_ui(&self.server_notifier); - preview::load_preview(_component, _behavior); + preview::load_preview(_component); } } diff --git a/tools/lsp/preview/native.rs b/tools/lsp/preview/native.rs index 1ed6043f1..28032e0ab 100644 --- a/tools/lsp/preview/native.rs +++ b/tools/lsp/preview/native.rs @@ -3,7 +3,7 @@ // cSpell: ignore condvar -use crate::common::{PostLoadBehavior, PreviewComponent}; +use crate::common::PreviewComponent; use crate::lsp_ext::{Health, ServerStatusNotification, ServerStatusParams}; use crate::ServerNotifier; @@ -177,7 +177,7 @@ fn close_ui_impl(preview_state: &mut PreviewState) { } } -pub fn load_preview(component: PreviewComponent, post_load_behavior: PostLoadBehavior) { +pub fn load_preview(component: PreviewComponent) { use std::sync::atomic::{AtomicU32, Ordering}; static PENDING_EVENTS: AtomicU32 = AtomicU32::new(0); if PENDING_EVENTS.load(Ordering::SeqCst) > 0 { @@ -186,7 +186,7 @@ pub fn load_preview(component: PreviewComponent, post_load_behavior: PostLoadBeh PENDING_EVENTS.fetch_add(1, Ordering::SeqCst); run_in_ui_thread(move || async move { PENDING_EVENTS.fetch_sub(1, Ordering::SeqCst); - reload_preview(component, post_load_behavior).await + reload_preview(component).await }); } @@ -212,9 +212,11 @@ pub fn set_contents(path: &Path, content: String) { drop(cache); let mut cache = CONTENT_CACHE.get_or_init(Default::default).lock().unwrap(); - cache.sender = sender.clone(); + cache.sender = sender; - load_preview(current, PostLoadBehavior::DoNothing); + if cache.sender.is_some() { + load_preview(current); + } } } @@ -238,9 +240,11 @@ pub fn config_changed( drop(cache); let mut cache = CONTENT_CACHE.get_or_init(Default::default).lock().unwrap(); - cache.sender = sender.clone(); + cache.sender = sender; - load_preview(current, PostLoadBehavior::DoNothing); + if cache.sender.is_some() { + load_preview(current); + } } }; } @@ -339,10 +343,7 @@ fn configure_design_mode(enabled: bool, sender: &crate::ServerNotifier) { }); } -async fn reload_preview( - preview_component: PreviewComponent, - _post_load_behavior: PostLoadBehavior, -) { +async fn reload_preview(preview_component: PreviewComponent) { let (design_mode, sender) = { let mut cache = CONTENT_CACHE.get_or_init(Default::default).lock().unwrap(); cache.dependency.clear(); diff --git a/tools/lsp/wasm_main.rs b/tools/lsp/wasm_main.rs index 39f1b5d64..5ab525dc4 100644 --- a/tools/lsp/wasm_main.rs +++ b/tools/lsp/wasm_main.rs @@ -52,11 +52,7 @@ impl PreviewApi for Previewer { // do nothing! } - fn load_preview( - &self, - _component: common::PreviewComponent, - _behavior: common::PostLoadBehavior, - ) { + fn load_preview(&self, _component: common::PreviewComponent) { // do nothing! }