Skip to content

Commit

Permalink
dev: introduce task concept and rewrite export actor as export task (#…
Browse files Browse the repository at this point in the history
…383)

* dev: introduce task concept and rewrite export actor as export task

* dev: delete useless code

* fix: export mode
  • Loading branch information
Myriad-Dreamin authored Jul 8, 2024
1 parent 4b1f8cd commit 692b87e
Show file tree
Hide file tree
Showing 11 changed files with 348 additions and 222 deletions.
21 changes: 8 additions & 13 deletions crates/tinymist-query/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,16 @@ mod polymorphic {
Merged,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub enum ExportKind {
#[default]
Pdf,
Svg { page: PageSelection },
Png { page: PageSelection },
Svg {
page: PageSelection,
},
Png {
page: PageSelection,
},
}

impl ExportKind {
Expand All @@ -166,11 +171,6 @@ mod polymorphic {
pub kind: ExportKind,
}

#[derive(Debug, Clone)]
pub struct OnSaveExportRequest {
pub path: PathBuf,
}

#[derive(Debug, Clone)]
pub struct FormattingRequest {
/// The path of the document to get semantic tokens for.
Expand Down Expand Up @@ -200,7 +200,6 @@ mod polymorphic {
#[derive(Debug, Clone)]
pub enum CompilerQueryRequest {
OnExport(OnExportRequest),
OnSaveExport(OnSaveExportRequest),
Hover(HoverRequest),
GotoDefinition(GotoDefinitionRequest),
GotoDeclaration(GotoDeclarationRequest),
Expand Down Expand Up @@ -235,7 +234,6 @@ mod polymorphic {
use FoldRequestFeature::*;
match self {
Self::OnExport(..) => Mergeable,
Self::OnSaveExport(..) => Mergeable,
Self::Hover(..) => PinnedFirst,
Self::GotoDefinition(..) => PinnedFirst,
Self::GotoDeclaration(..) => PinnedFirst,
Expand Down Expand Up @@ -269,7 +267,6 @@ mod polymorphic {
pub fn associated_path(&self) -> Option<&Path> {
Some(match self {
Self::OnExport(..) => return None,
Self::OnSaveExport(req) => &req.path,
Self::Hover(req) => &req.path,
Self::GotoDefinition(req) => &req.path,
Self::GotoDeclaration(req) => &req.path,
Expand Down Expand Up @@ -304,7 +301,6 @@ mod polymorphic {
#[derive(Debug, Clone)]
pub enum CompilerQueryResponse {
OnExport(Option<PathBuf>),
OnSaveExport(()),
Hover(Option<Hover>),
GotoDefinition(Option<GotoDefinitionResponse>),
GotoDeclaration(Option<GotoDeclarationResponse>),
Expand Down Expand Up @@ -338,7 +334,6 @@ mod polymorphic {
pub fn to_untyped(self) -> serde_json::Result<JsonValue> {
match self {
Self::OnExport(res) => serde_json::to_value(res),
Self::OnSaveExport(res) => serde_json::to_value(res),
Self::Hover(res) => serde_json::to_value(res),
Self::GotoDefinition(res) => serde_json::to_value(res),
Self::GotoDeclaration(res) => serde_json::to_value(res),
Expand Down
34 changes: 13 additions & 21 deletions crates/tinymist/src/actor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Bootstrap actors for Tinymist.
pub mod editor;
pub mod export;
pub mod format;
#[cfg(feature = "preview")]
pub mod preview;
Expand All @@ -19,13 +18,13 @@ use typst_ts_compiler::vfs::notify::{FileChangeSet, MemoryEvent};
use typst_ts_core::config::compiler::EntryState;

use self::{
export::{ExportActor, ExportConfig},
format::run_format_thread,
typ_client::{CompileClientActor, CompileHandler},
typ_server::CompileServerActor,
user_action::run_user_action_thread,
};
use crate::{
task::{ExportConfig, ExportTask, ExportTaskConf},
world::{ImmutDict, LspWorldBuilder},
LanguageState,
};
Expand All @@ -51,27 +50,20 @@ impl LanguageState {
inputs: ImmutDict,
snapshot: FileChangeSet,
) -> CompileClientActor {
let (doc_tx, doc_rx) = watch::channel(None);
let (export_tx, export_rx) = mpsc::unbounded_channel();
let (doc_tx, _) = watch::channel(None);
let (intr_tx, intr_rx) = mpsc::unbounded_channel();

// Run Export actors before preparing cluster to avoid loss of events
self.client.handle.spawn(
ExportActor {
group: editor_group.clone(),
editor_tx: self.editor_tx.clone(),
export_rx,
doc_rx,
entry: entry.clone(),
config: ExportConfig {
substitute_pattern: self.compile_config().output_path.clone(),
mode: self.compile_config().export_pdf,
},
kind: ExportKind::Pdf,
count_words: self.config.notify_compile_status,
}
.run(),
);
let export = ExportTask::new(ExportTaskConf {
group: editor_group.clone(),
editor_tx: Some(self.editor_tx.clone()),
config: ExportConfig {
substitute_pattern: self.compile_config().output_path.clone(),
mode: self.compile_config().export_pdf,
},
kind: ExportKind::Pdf,
count_words: self.config.notify_compile_status,
});

log::info!(
"TypstActor: creating server for {editor_group}, entry: {entry:?}, inputs: {inputs:?}"
Expand All @@ -87,7 +79,7 @@ impl LanguageState {
diag_group: editor_group.clone(),
intr_tx: intr_tx.clone(),
doc_tx,
export_tx: export_tx.clone(),
export: export.clone(),
editor_tx: self.editor_tx.clone(),
analysis: Analysis {
position_encoding,
Expand Down
60 changes: 29 additions & 31 deletions crates/tinymist/src/actor/typ_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use std::{
use anyhow::{anyhow, bail};
use log::{error, info, trace};
use parking_lot::Mutex;
use sync_lsp::{just_future, QueryFuture};
use tinymist_query::{
analysis::{Analysis, AnalysisContext, AnalysisResources},
DiagnosticsMap, ExportKind, ServerInfoResponse, VersionedDocument,
Expand All @@ -56,11 +57,10 @@ use typst_ts_core::{

use super::{
editor::{EditorRequest, TinymistCompileStatusEnum},
export::ExportConfig,
typ_server::{CompilationHandle, CompileSnapshot, CompiledArtifact, Interrupt},
};
use crate::{
actor::export::ExportRequest,
task::{ExportConfig, ExportSignal, ExportTask},
tool::preview::CompileStatus,
utils::{self, threaded_receive},
world::{LspCompilerFeat, LspWorld},
Expand All @@ -79,7 +79,7 @@ pub struct CompileHandler {

pub(crate) intr_tx: mpsc::UnboundedSender<Interrupt<LspCompilerFeat>>,
pub(crate) doc_tx: watch::Sender<Option<Arc<TypstDocument>>>,
pub(crate) export_tx: mpsc::UnboundedSender<ExportRequest>,
pub(crate) export: ExportTask,
pub(crate) editor_tx: EditorSender,
}

Expand Down Expand Up @@ -253,7 +253,6 @@ impl CompileHandler {
) {
if let Ok(doc) = res.clone() {
let _ = self.doc_tx.send(Some(doc.clone()));
let _ = self.export_tx.send(ExportRequest::OnTyped);
}

self.editor_tx
Expand Down Expand Up @@ -306,6 +305,14 @@ impl CompilationHandle<LspCompilerFeat> for CompileHandler {
snap.env.tracer.as_ref().map(|e| e.clone().warnings()),
);

if snap.flags.triggered_by_mem_events && snap.flags.triggered_by_fs_events {
self.export.signal(snap, ExportSignal::TypedAndSaved);
} else if snap.flags.triggered_by_mem_events {
self.export.signal(snap, ExportSignal::Typed);
} else if snap.flags.triggered_by_fs_events {
self.export.signal(snap, ExportSignal::Saved);
}

self.preview_notify_compile(res, snap.flags.triggered_by_fs_events);
}
}
Expand Down Expand Up @@ -352,33 +359,29 @@ impl CompileClientActor {
self.config = config;
}

pub(crate) fn change_export_pdf(&mut self, config: ExportConfig) {
let _ = self
.handle
.export_tx
.send(ExportRequest::ChangeConfig(config));
pub(crate) fn change_export_config(&mut self, config: ExportConfig) {
self.handle.export.change_config(config);
}

pub fn on_export(&self, kind: ExportKind, path: PathBuf) -> anyhow::Result<Option<PathBuf>> {
// todo: we currently doesn't respect the path argument...
info!("CompileActor: on export: {}", path.display());
pub fn on_export(&self, kind: ExportKind, path: PathBuf) -> QueryFuture {
let snap = self.snapshot()?;
let export = self.handle.export.task();

let (tx, rx) = oneshot::channel();
let _ = self
.handle
.export_tx
.send(ExportRequest::Oneshot(Some(kind), tx));
let res: Option<PathBuf> = utils::threaded_receive(rx)?;

info!("CompileActor: on export end: {path:?} as {res:?}");
Ok(res)
}
let entry = self.config.determine_entry(Some(path.as_path().into()));

just_future!(async move {
let snap = snap.snapshot().await?;
let snap = snap.task(TaskInputs {
entry: Some(entry),
..Default::default()
});

pub fn on_save_export(&self, path: PathBuf) -> anyhow::Result<()> {
info!("CompileActor: on save export: {}", path.display());
let _ = self.handle.export_tx.send(ExportRequest::OnSaved);
let artifact = snap.compile().await;
let res = export.oneshot(&artifact, kind).await;

Ok(())
log::info!("CompileActor: on export end: {path:?} as {res:?}");
Ok(tinymist_query::CompilerQueryResponse::OnExport(res))
})
}
}

Expand Down Expand Up @@ -417,11 +420,6 @@ impl CompileClientActor {
entry: Some(next_entry.clone()),
..Default::default()
});
// todo: let export request accept compiled artifact
let _ = self
.handle
.export_tx
.send(ExportRequest::ChangeExportPath(next_entry.clone()));

self.entry = next_entry;

Expand Down
Loading

0 comments on commit 692b87e

Please sign in to comment.