Skip to content

Commit

Permalink
build: save dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Jan 11, 2025
1 parent 08b5d40 commit 8feefa1
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/tinymist-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typst.workspace = true

tinymist-assets = { workspace = true }
tinymist-project.workspace = true
tinymist-fs.workspace = true
typst-assets = { workspace = true, features = ["fonts"] }

dirs.workspace = true
Expand Down
62 changes: 58 additions & 4 deletions crates/tinymist-world/src/typ_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use std::{
};

use sync_lsp::LspClient;
use tinymist_project::{Id, ProjectInput, ProjectTask, ResourcePath};
use tinymist_project::{
Id, ProjectInput, ProjectMaterial, ProjectRoute, ProjectTask, ResourcePath,
};
use tokio::sync::{mpsc, oneshot};

use reflexo_typst::{
Expand All @@ -21,9 +23,9 @@ use reflexo_typst::{
vfs::notify::{FilesystemEvent, MemoryEvent, NotifyMessage, UpstreamUpdateEvent},
watch_deps, CompileEnv, CompileReport, Compiler, CompilerFeat, CompilerUniverse, CompilerWorld,
ConsoleDiagReporter, EntryReader, GenericExporter, Revising, TaskInputs, TypstDocument,
WorldDeps,
TypstFileId, WorldDeps,
};
use typst::diag::{SourceDiagnostic, SourceResult};
use typst::diag::{EcoString, SourceDiagnostic, SourceResult};

use crate::{LspCompilerFeat, LspWorld};

Expand Down Expand Up @@ -541,6 +543,8 @@ impl ProjectCompiler<LspCompilerFeat> {
enum LockUpdate {
Input(ProjectInput),
Task(ProjectTask),
Material(ProjectMaterial),
Route(ProjectRoute),
}

pub struct ProjectLockUpdater {
Expand All @@ -549,7 +553,7 @@ pub struct ProjectLockUpdater {
}

impl ProjectLockUpdater {
pub fn compile(&mut self, world: &LspWorld) -> Option<Id> {
pub fn compiled(&mut self, world: &LspWorld) -> Option<Id> {
let entry = world.entry_state();
log::info!("ProjectCompiler: record compile for {entry:?}");
// todo: correct root
Expand Down Expand Up @@ -601,8 +605,30 @@ impl ProjectLockUpdater {
self.updates.push(LockUpdate::Task(task));
}

pub fn update_materials(&mut self, doc_id: Id, ids: Vec<TypstFileId>) {
let mut files = ids
.into_iter()
.map(ResourcePath::from_file_id)
.collect::<Vec<_>>();
files.sort();
self.updates.push(LockUpdate::Material(ProjectMaterial {
root: EcoString::default(),
id: doc_id,
files,
}));
}

pub fn route(&mut self, doc_id: Id, priority: u32) {
self.updates.push(LockUpdate::Route(ProjectRoute {
id: doc_id,
priority,
}));
}

pub fn commit(self) {
let err = tinymist_project::LockFile::update(&self.root, |l| {
let root: EcoString = unix_slash(&self.root).into();
let root_hash = reflexo_typst::hash::hash128(&root);
for update in self.updates {
match update {
LockUpdate::Input(input) => {
Expand All @@ -611,6 +637,34 @@ impl ProjectLockUpdater {
LockUpdate::Task(task) => {
l.replace_task(task);
}
LockUpdate::Material(mut mat) => {
mat.root = root.clone();
let cache_dir = dirs::cache_dir();
if let Some(cache_dir) = cache_dir {
let id = reflexo_typst::hash::hash128(&mat.id);
let lower4096 = root_hash & 0xfff;
let upper4096 = root_hash >> 12;

// let hash_str = format!("{root:016x}/{id:016x}");
let hash_str = format!("{lower4096:03x}/{upper4096:013x}/{id:016x}");

let cache_dir = cache_dir.join("tinymist/projects").join(hash_str);
let _ = std::fs::create_dir_all(&cache_dir);

let data = serde_json::to_string(&mat).unwrap();
let path = cache_dir.join("material.json");
let result = tinymist_fs::paths::write_atomic(path, data);
if let Err(err) = result {
log::error!("ProjectCompiler: write material error: {err}");
}

// todo: clean up old cache
}
// l.replace_material(mat);
}
LockUpdate::Route(route) => {
l.replace_route(route);
}
}
}

Expand Down
38 changes: 35 additions & 3 deletions crates/tinymist/src/actor/typ_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ use log::{error, info, trace};
use reflexo::path::unix_slash;
use reflexo_typst::{
error::prelude::*, typst::prelude::*, vfs::notify::MemoryEvent, world::EntryState,
CompileReport, EntryReader, Error, ImmutPath, TaskInputs,
CompileReport, Compiler, EntryReader, Error, ImmutPath, TaskInputs,
};
use sync_lsp::just_future;
use tinymist_project::PROJECT_ROUTE_USER_ACTION_PRIORITY;
use tinymist_query::{
analysis::{Analysis, AnalysisRevLock, LocalContextGuard},
CompilerQueryRequest, CompilerQueryResponse, DiagnosticsMap, EntryResolver, OnExportRequest,
Expand All @@ -42,7 +43,7 @@ use tinymist_world::{
CompilationHandle, CompileSnapshot, CompiledArtifact, Interrupt, ProjectCompiler,
ProjectInterrupt, SucceededArtifact,
},
LspInterrupt,
LspInterrupt, LspProjectCompiler,
};
use tokio::sync::{mpsc, oneshot};
use typst::{diag::SourceDiagnostic, World};
Expand Down Expand Up @@ -475,11 +476,42 @@ impl CompileClientActor {
pub fn on_export(&mut self, req: OnExportRequest) -> QueryFuture {
let OnExportRequest { path, kind, open } = req;

let snap = self.snapshot()?;
let snap = self.query_snapshot()?;

let update_dep = async move {
let mut snap = snap.receive().await?;
let world = snap.world.clone();
let updater = LspProjectCompiler::update_lock(&world);

// todo: Introducing additional compilation here, but we must get accurate
// dependencies by compiling it again.
snap.snap.world.source_db.take_state();
let err = std::marker::PhantomData.compile(&snap.world, &mut Default::default());
if err.is_err() {
return ZResult::Ok(());
}

let ids = snap.run_analysis(|lg| lg.depended_files()).ok();

let _ = updater.zip(ids).and_then(|(mut updater, file_ids)| {
let doc_id = updater.compiled(&world)?;

updater.update_materials(doc_id.clone(), file_ids);
updater.route(doc_id, PROJECT_ROUTE_USER_ACTION_PRIORITY);

updater.commit();

Some(())
});

ZResult::Ok(())
};

let snap = self.snapshot()?;
let entry = self.entry_resolver().resolve(Some(path.as_path().into()));
let export = self.handle.export.factory.oneshot(snap, Some(entry), kind);
just_future(async move {
tokio::spawn(update_dep);
let res = export.await?;

// See https://github.com/Myriad-Dreamin/tinymist/issues/837
Expand Down
2 changes: 1 addition & 1 deletion crates/tinymist/src/task/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl ExportConfig {
let updater = LspProjectCompiler::update_lock(&snap.world);

let _ = updater.and_then(|mut updater| {
let doc_id = updater.compile(&snap.world)?;
let doc_id = updater.compiled(&snap.world)?;
let task_id = doc_id.clone();

let when = match self.config.mode {
Expand Down

0 comments on commit 8feefa1

Please sign in to comment.