diff --git a/tools/lsp/common.rs b/tools/lsp/common.rs index a707cbd38..788a65c76 100644 --- a/tools/lsp/common.rs +++ b/tools/lsp/common.rs @@ -6,7 +6,6 @@ use std::{ collections::HashMap, path::{Path, PathBuf}, - rc::Rc, }; pub type Error = Box; @@ -16,7 +15,6 @@ pub type Result = std::result::Result; /// ServerNotifier pub trait PreviewApi { fn set_use_external_previewer(&self, use_external: bool); - fn request_state(&self, ctx: &Rc); fn set_contents(&self, path: &Path, contents: &str); fn load_preview(&self, component: PreviewComponent); fn config_changed( @@ -26,6 +24,9 @@ pub trait PreviewApi { library_paths: &HashMap, ); fn highlight(&self, path: Option, offset: u32) -> Result<()>; + + /// What is the current component to preview? + fn current_component(&self) -> Option; } /// The Component to preview diff --git a/tools/lsp/language.rs b/tools/lsp/language.rs index 979316144..ec7e03f76 100644 --- a/tools/lsp/language.rs +++ b/tools/lsp/language.rs @@ -77,6 +77,30 @@ fn create_show_preview_command( ) } +pub fn request_state(ctx: &std::rc::Rc) { + #[cfg(feature = "preview-external")] + { + let documents = &ctx.document_cache.borrow().documents; + + for (p, d) in documents.all_file_documents() { + let Some(node) = &d.node else { + continue; + }; + ctx.preview.set_contents(p, &node.text().to_string()); + } + let style = documents.compiler_config.style.clone().unwrap_or_default(); + ctx.preview.config_changed( + &style, + &documents.compiler_config.include_paths, + &documents.compiler_config.library_paths, + ); + + if let Some(c) = ctx.preview.current_component() { + ctx.preview.load_preview(c); + } + } +} + /// A cache of loaded documents pub struct DocumentCache { pub(crate) documents: TypeLoader, diff --git a/tools/lsp/main.rs b/tools/lsp/main.rs index 3b0e93e07..62387e603 100644 --- a/tools/lsp/main.rs +++ b/tools/lsp/main.rs @@ -53,31 +53,6 @@ impl PreviewApi for Previewer { } } - fn request_state(&self, _ctx: &Rc) { - #[cfg(any(feature = "preview-builtin", feature = "preview-external"))] - { - let documents = &_ctx.document_cache.borrow().documents; - - for (p, d) in documents.all_file_documents() { - let Some(node) = &d.node else { - continue; - }; - self.set_contents(p, &node.text().to_string()); - } - let cc = &documents.compiler_config; - let empty = String::new(); - self.config_changed( - cc.style.as_ref().unwrap_or(&empty), - &cc.include_paths, - &cc.library_paths, - ); - - if let Some(c) = self.to_show.take() { - self.load_preview(c); - } - } - } - fn set_contents(&self, _path: &std::path::Path, _contents: &str) { if *self.use_external_previewer.borrow() { #[cfg(feature = "preview-external")] @@ -173,6 +148,10 @@ impl PreviewApi for Previewer { } } } + + fn current_component(&self) -> Option { + self.to_show.borrow().clone() + } } #[derive(Clone, clap::Parser)] @@ -492,7 +471,7 @@ async fn handle_notification(req: lsp_server::Notification, ctx: &Rc) - ctx.preview.set_use_external_previewer(is_external); } M::RequestState { .. } => { - ctx.preview.request_state(ctx); + crate::language::request_state(ctx); } } } diff --git a/tools/lsp/wasm_main.rs b/tools/lsp/wasm_main.rs index bed0a116e..62c4933dc 100644 --- a/tools/lsp/wasm_main.rs +++ b/tools/lsp/wasm_main.rs @@ -53,30 +53,6 @@ impl PreviewApi for Previewer { // The WASM LSP always needs to use the WASM preview! } - fn request_state(&self, ctx: &std::rc::Rc) { - #[cfg(feature = "preview-external")] - { - let documents = &ctx.document_cache.borrow().documents; - - for (p, d) in documents.all_file_documents() { - let Some(node) = &d.node else { - continue; - }; - self.set_contents(p, &node.text().to_string()); - } - let style = documents.compiler_config.style.clone().unwrap_or_default(); - self.config_changed( - &style, - &documents.compiler_config.include_paths, - &documents.compiler_config.library_paths, - ); - - if let Some(c) = self.to_show.take() { - self.load_preview(c); - } - } - } - fn set_contents(&self, path: &std::path::Path, contents: &str) { #[cfg(feature = "preview-external")] let _ = self.server_notifier.send_notification( @@ -145,6 +121,10 @@ impl PreviewApi for Previewer { }, ) } + + fn current_component(&self) -> Option { + self.to_show.borrow().clone() + } } #[derive(Clone)] @@ -152,6 +132,7 @@ pub struct ServerNotifier { send_notification: Function, send_request: Function, } + impl ServerNotifier { pub fn send_notification(&self, method: String, params: impl Serialize) -> Result<()> { self.send_notification @@ -346,7 +327,7 @@ impl SlintServer { // Nothing to do! } M::RequestState { .. } => { - // Nothing to do! + crate::language::request_state(&self.ctx); } } Ok(())