From 0451a1089dbe3dc34cd6d8d564c36c33b889f15b Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin <35292584+Myriad-Dreamin@users.noreply.github.com> Date: Thu, 30 Jan 2025 00:25:59 +0800 Subject: [PATCH] dev: handle compilation notifications for multiple projects (#1229) * dev: handle multiple-project notifications * dev: remove todo --- crates/tinymist-project/src/compiler.rs | 5 +++++ crates/tinymist/src/state/project.rs | 10 ++++++++-- crates/tinymist/src/state/server.rs | 5 +++-- crates/tinymist/src/tool/preview.rs | 3 ++- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/crates/tinymist-project/src/compiler.rs b/crates/tinymist-project/src/compiler.rs index b862d42be..6e1435f8b 100644 --- a/crates/tinymist-project/src/compiler.rs +++ b/crates/tinymist-project/src/compiler.rs @@ -210,6 +210,8 @@ pub trait CompileHandler: Send + Sync + 'static { // todo: notify project specific compile /// Called when a compilation is done. fn notify_compile(&self, res: &CompiledArtifact, rep: CompileReport); + /// Called when a project is removed. + fn notify_removed(&self, _id: &ProjectInsId) {} /// Called when the compilation status is changed. fn status(&self, revision: usize, id: &ProjectInsId, rep: CompileReport); } @@ -487,6 +489,9 @@ impl ProjectCom fn remove_dedicates(&mut self, id: &ProjectInsId) { let proj = self.dedicates.iter().position(|e| e.id == *id); if let Some(idx) = proj { + // Resets the handle state, e.g. notified revision + self.handler.notify_removed(id); + let _proj = self.dedicates.remove(idx); // todo: kill compilations } else { diff --git a/crates/tinymist/src/state/project.rs b/crates/tinymist/src/state/project.rs index 0a46b5e76..cd40a4536 100644 --- a/crates/tinymist/src/state/project.rs +++ b/crates/tinymist/src/state/project.rs @@ -147,7 +147,7 @@ pub struct CompileHandlerImpl { pub(crate) editor_tx: EditorSender, pub(crate) client: Box, - pub(crate) notified_revision: parking_lot::Mutex, + pub(crate) notified_revision: Mutex>, } pub trait ProjectClient: Send + Sync + 'static { @@ -295,10 +295,16 @@ impl CompileHandler for CompileHandlerImpl } } + fn notify_removed(&self, id: &ProjectInsId) { + let n_revs = &mut self.notified_revision.lock(); + n_revs.remove(id); + } + fn notify_compile(&self, snap: &LspCompiledArtifact, rep: CompileReport) { // todo: we need to manage the revision for fn status() as well { - let mut n_rev = self.notified_revision.lock(); + let mut n_revs = self.notified_revision.lock(); + let n_rev = n_revs.entry(snap.id.clone()).or_default(); if *n_rev >= snap.world.revision().get() { log::info!( "Project: already notified for revision {} <= {n_rev}", diff --git a/crates/tinymist/src/state/server.rs b/crates/tinymist/src/state/server.rs index 8cd48c915..306fb321c 100644 --- a/crates/tinymist/src/state/server.rs +++ b/crates/tinymist/src/state/server.rs @@ -5,6 +5,7 @@ use std::sync::Arc; use log::{error, info}; use lsp_types::*; +use parking_lot::Mutex; use sync_lsp::*; use task::ExportUserConfig; use tinymist_project::{EntryResolver, Interrupt, LspCompileSnapshot, ProjectInsId}; @@ -20,7 +21,6 @@ use tokio::sync::mpsc; use typst::diag::FileResult; use typst::layout::Position as TypstPosition; use typst::syntax::Source; -use vfs::{Bytes, FileChangeSet, MemoryEvent}; use crate::actor::editor::{EditorActor, EditorRequest}; use crate::project::{ @@ -32,6 +32,7 @@ use crate::route::ProjectRouteState; use crate::state::query::OnEnter; use crate::stats::CompilerQueryStats; use crate::task::{ExportTask, FormatTask, UserActionTask}; +use crate::vfs::{Bytes, FileChangeSet, MemoryEvent}; use crate::world::{LspUniverseBuilder, TaskInputs}; use crate::{init::*, *}; @@ -434,7 +435,7 @@ impl ServerState { stats: Arc::default(), }), - notified_revision: parking_lot::Mutex::new(0), + notified_revision: Mutex::default(), }); let default_path = config.compile.entry_resolver.resolve_default(); diff --git a/crates/tinymist/src/tool/preview.rs b/crates/tinymist/src/tool/preview.rs index 47578c07c..eb13e1ba3 100644 --- a/crates/tinymist/src/tool/preview.rs +++ b/crates/tinymist/src/tool/preview.rs @@ -13,6 +13,7 @@ use hyper_tungstenite::{tungstenite::Message, HyperWebsocket, HyperWebsocketStre use hyper_util::rt::TokioIo; use hyper_util::server::graceful::GracefulShutdown; use lsp_types::notification::Notification; +use parking_lot::Mutex; use reflexo_typst::debug_loc::SourceSpanOffset; use reflexo_typst::{error::prelude::*, Error, TypstDocument}; use serde::Serialize; @@ -646,7 +647,7 @@ pub async fn preview_main(args: PreviewCliArgs) -> Result<()> { client: Box::new(intr_tx.clone()), analysis: Arc::default(), - notified_revision: parking_lot::Mutex::new(0), + notified_revision: Mutex::default(), }); let mut server = ProjectCompiler::new(