lsp: Clean up PreviewApi

request_state went the wrong way around wrt. communication flow, so
remove and deduplicate it.
This commit is contained in:
Tobias Hunger 2023-11-02 18:55:24 +01:00 committed by Olivier Goffart
parent fbb6d70ff9
commit 5bf5b7b02d
4 changed files with 38 additions and 53 deletions

View file

@ -6,7 +6,6 @@
use std::{
collections::HashMap,
path::{Path, PathBuf},
rc::Rc,
};
pub type Error = Box<dyn std::error::Error>;
@ -16,7 +15,6 @@ pub type Result<T> = std::result::Result<T, Error>;
/// ServerNotifier
pub trait PreviewApi {
fn set_use_external_previewer(&self, use_external: bool);
fn request_state(&self, ctx: &Rc<crate::language::Context>);
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<String, PathBuf>,
);
fn highlight(&self, path: Option<PathBuf>, offset: u32) -> Result<()>;
/// What is the current component to preview?
fn current_component(&self) -> Option<PreviewComponent>;
}
/// The Component to preview

View file

@ -77,6 +77,30 @@ fn create_show_preview_command(
)
}
pub fn request_state(ctx: &std::rc::Rc<Context>) {
#[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,

View file

@ -53,31 +53,6 @@ impl PreviewApi for Previewer {
}
}
fn request_state(&self, _ctx: &Rc<crate::language::Context>) {
#[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<crate::common::PreviewComponent> {
self.to_show.borrow().clone()
}
}
#[derive(Clone, clap::Parser)]
@ -492,7 +471,7 @@ async fn handle_notification(req: lsp_server::Notification, ctx: &Rc<Context>) -
ctx.preview.set_use_external_previewer(is_external);
}
M::RequestState { .. } => {
ctx.preview.request_state(ctx);
crate::language::request_state(ctx);
}
}
}

View file

@ -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<crate::language::Context>) {
#[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<crate::common::PreviewComponent> {
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(())