Skip to content

Commit

Permalink
dev: handle compilation notifications for multiple projects (#1229)
Browse files Browse the repository at this point in the history
* dev: handle multiple-project notifications

* dev: remove todo
  • Loading branch information
Myriad-Dreamin authored Jan 29, 2025
1 parent eac275c commit 0451a10
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
5 changes: 5 additions & 0 deletions crates/tinymist-project/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ pub trait CompileHandler<F: CompilerFeat, Ext>: Send + Sync + 'static {
// todo: notify project specific compile
/// Called when a compilation is done.
fn notify_compile(&self, res: &CompiledArtifact<F>, 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);
}
Expand Down Expand Up @@ -487,6 +489,9 @@ impl<F: CompilerFeat + Send + Sync + 'static, Ext: Default + 'static> 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 {
Expand Down
10 changes: 8 additions & 2 deletions crates/tinymist/src/state/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub struct CompileHandlerImpl {
pub(crate) editor_tx: EditorSender,
pub(crate) client: Box<dyn ProjectClient>,

pub(crate) notified_revision: parking_lot::Mutex<usize>,
pub(crate) notified_revision: Mutex<FxHashMap<ProjectInsId, usize>>,
}

pub trait ProjectClient: Send + Sync + 'static {
Expand Down Expand Up @@ -295,10 +295,16 @@ impl CompileHandler<LspCompilerFeat, ProjectInsStateExt> 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}",
Expand Down
5 changes: 3 additions & 2 deletions crates/tinymist/src/state/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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::{
Expand All @@ -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::*, *};

Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion crates/tinymist/src/tool/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 0451a10

Please sign in to comment.