From efb9d17b59d6266710a61c649a7026052cfc3321 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin <35292584+Myriad-Dreamin@users.noreply.github.com> Date: Sat, 16 Sep 2023 08:06:59 +0800 Subject: [PATCH] dev(core): change map_shadow api (#356) --- cli/src/query_repl.rs | 2 +- compiler/src/lib.rs | 10 +++++----- compiler/src/service/driver.rs | 6 +++--- compiler/src/service/mod.rs | 4 ++-- compiler/src/vfs/mod.rs | 6 ++---- compiler/src/world.rs | 2 +- packages/compiler/src/lib.rs | 2 +- tests/heap-profile/src/lib.rs | 4 +++- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/cli/src/query_repl.rs b/cli/src/query_repl.rs index 6b4441dd..4433330f 100644 --- a/cli/src/query_repl.rs +++ b/cli/src/query_repl.rs @@ -122,7 +122,7 @@ impl Completer for ReplContext { driver.world.reset(); let typst_completions = driver - .with_shadow_file_by_id(main_id, &dyn_content, |driver| { + .with_shadow_file_by_id(main_id, dyn_content.as_bytes().into(), |driver| { let frames = driver.compile().map(|d| d.pages); let frames = frames.as_ref().map(|v| v.as_slice()).unwrap_or_default(); let source = driver.world.main(); diff --git a/compiler/src/lib.rs b/compiler/src/lib.rs index 028d34df..b2d779d9 100644 --- a/compiler/src/lib.rs +++ b/compiler/src/lib.rs @@ -63,7 +63,7 @@ use typst::{ diag::{At, FileResult, SourceResult}, syntax::Span, }; -use typst_ts_core::TypstFileId; +use typst_ts_core::{Bytes, TypstFileId}; /// Latest version of the shadow api, which is in beta. pub trait ShadowApi { @@ -75,7 +75,7 @@ pub trait ShadowApi { fn reset_shadow(&mut self); /// Add a shadow file to the driver. - fn map_shadow(&self, path: &Path, content: &str) -> FileResult<()>; + fn map_shadow(&self, path: &Path, content: Bytes) -> FileResult<()>; /// Add a shadow file to the driver. fn unmap_shadow(&self, path: &Path) -> FileResult<()>; @@ -84,7 +84,7 @@ pub trait ShadowApi { fn with_shadow_file( &mut self, file_path: &Path, - content: &str, + content: Bytes, f: impl FnOnce(&mut Self) -> SourceResult, ) -> SourceResult { self.map_shadow(file_path, content).at(Span::detached())?; @@ -96,7 +96,7 @@ pub trait ShadowApi { /// Add a shadow file to the driver by file id. /// Note: to enable this function, `ShadowApi` must implement /// `_shadow_map_id`. - fn map_shadow_by_id(&self, file_id: TypstFileId, content: &str) -> FileResult<()> { + fn map_shadow_by_id(&self, file_id: TypstFileId, content: Bytes) -> FileResult<()> { let file_path = self._shadow_map_id(file_id)?; self.map_shadow(&file_path, content) } @@ -116,7 +116,7 @@ pub trait ShadowApi { fn with_shadow_file_by_id( &mut self, file_id: TypstFileId, - content: &str, + content: Bytes, f: impl FnOnce(&mut Self) -> SourceResult, ) -> SourceResult { let file_path = self._shadow_map_id(file_id).at(Span::detached())?; diff --git a/compiler/src/service/driver.rs b/compiler/src/service/driver.rs index e061103e..e1426399 100644 --- a/compiler/src/service/driver.rs +++ b/compiler/src/service/driver.rs @@ -6,7 +6,7 @@ use std::{ use crate::ShadowApi; use typst::{diag::SourceResult, syntax::VirtualPath, World}; use typst_ts_core::{ - exporter_builtins::GroupExporter, path::PathClean, Exporter, TakeAs, TypstFileId, + exporter_builtins::GroupExporter, path::PathClean, Bytes, Exporter, TakeAs, TypstFileId, }; use super::{Compiler, WorkspaceProvider, WrappedCompiler}; @@ -109,7 +109,7 @@ impl ShadowApi for CompileDriverImpl { self.world.reset_shadow() } - fn map_shadow(&self, path: &Path, content: &str) -> typst::diag::FileResult<()> { + fn map_shadow(&self, path: &Path, content: Bytes) -> typst::diag::FileResult<()> { self.world.map_shadow(path, content) } @@ -285,7 +285,7 @@ impl WrappedCompiler for DynamicLayoutCompiler { self.target, ); - self.with_shadow_file_by_id(variable_file, &variables, |this| { + self.with_shadow_file_by_id(variable_file, variables.as_bytes().into(), |this| { // compile and export document let output = Arc::new(this.inner_mut().compile()?); svg_exporter.render(current_width, output); diff --git a/compiler/src/service/mod.rs b/compiler/src/service/mod.rs index 1690580f..8aa547b4 100644 --- a/compiler/src/service/mod.rs +++ b/compiler/src/service/mod.rs @@ -12,7 +12,7 @@ use typst::{ syntax::Span, World, }; -use typst_ts_core::TypstFileId; +use typst_ts_core::{Bytes, TypstFileId}; #[cfg(feature = "system-compile")] pub(crate) mod diag; @@ -240,7 +240,7 @@ where } #[inline] - fn map_shadow(&self, path: &Path, content: &str) -> FileResult<()> { + fn map_shadow(&self, path: &Path, content: Bytes) -> FileResult<()> { self.inner().map_shadow(path, content) } diff --git a/compiler/src/vfs/mod.rs b/compiler/src/vfs/mod.rs index d3b45a68..0b600645 100644 --- a/compiler/src/vfs/mod.rs +++ b/compiler/src/vfs/mod.rs @@ -315,10 +315,8 @@ impl Vfs { }) } - pub fn map_shadow(&self, path: &Path, content: &str) -> FileResult<()> { - self.access_model - .inner() - .add_file(path.into(), content.as_bytes().into()); + pub fn map_shadow(&self, path: &Path, content: Bytes) -> FileResult<()> { + self.access_model.inner().add_file(path.into(), content); Ok(()) } diff --git a/compiler/src/world.rs b/compiler/src/world.rs index 52ba78ff..83c88ed3 100644 --- a/compiler/src/world.rs +++ b/compiler/src/world.rs @@ -231,7 +231,7 @@ impl ShadowApi for CompilerWorld { self.vfs.reset_shadow() } - fn map_shadow(&self, path: &Path, content: &str) -> FileResult<()> { + fn map_shadow(&self, path: &Path, content: Bytes) -> FileResult<()> { self.vfs.map_shadow(path, content) } diff --git a/packages/compiler/src/lib.rs b/packages/compiler/src/lib.rs index 890f6960..2b4b640f 100644 --- a/packages/compiler/src/lib.rs +++ b/packages/compiler/src/lib.rs @@ -74,7 +74,7 @@ impl TypstCompiler { pub fn add_source(&mut self, path: &str, content: &str, is_main: bool) -> bool { let path = Path::new(path).to_owned(); - match self.compiler.map_shadow(&path, content) { + match self.compiler.map_shadow(&path, content.as_bytes().into()) { Ok(_) => { if is_main { self.compiler.set_entry_file(path); diff --git a/tests/heap-profile/src/lib.rs b/tests/heap-profile/src/lib.rs index 9843dafd..e26dd027 100644 --- a/tests/heap-profile/src/lib.rs +++ b/tests/heap-profile/src/lib.rs @@ -44,7 +44,9 @@ pub fn test_compiler( let main_id = driver.main_id(); driver - .with_shadow_file_by_id(main_id, &content, |driver| driver.compile()) + .with_shadow_file_by_id(main_id, content.as_bytes().into(), |driver| { + driver.compile() + }) .unwrap(); comemo::evict(10);