Skip to content

Commit

Permalink
dev(core): change map_shadow api
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Sep 15, 2023
1 parent 007f541 commit 78c8bc4
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cli/src/query_repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<()>;
Expand All @@ -84,7 +84,7 @@ pub trait ShadowApi {
fn with_shadow_file<T>(
&mut self,
file_path: &Path,
content: &str,
content: Bytes,
f: impl FnOnce(&mut Self) -> SourceResult<T>,
) -> SourceResult<T> {
self.map_shadow(file_path, content).at(Span::detached())?;
Expand All @@ -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)
}
Expand All @@ -116,7 +116,7 @@ pub trait ShadowApi {
fn with_shadow_file_by_id<T>(
&mut self,
file_id: TypstFileId,
content: &str,
content: Bytes,
f: impl FnOnce(&mut Self) -> SourceResult<T>,
) -> SourceResult<T> {
let file_path = self._shadow_map_id(file_id).at(Span::detached())?;
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/service/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -109,7 +109,7 @@ impl<W: World + ShadowApi> ShadowApi for CompileDriverImpl<W> {
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)
}

Expand Down Expand Up @@ -285,7 +285,7 @@ impl<C: Compiler + ShadowApi> WrappedCompiler for DynamicLayoutCompiler<C> {
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);
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 2 additions & 4 deletions compiler/src/vfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,8 @@ impl<M: AccessModel + Sized> Vfs<M> {
})
}

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(())
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl<F: CompilerFeat> ShadowApi for CompilerWorld<F> {
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)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion tests/heap-profile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 78c8bc4

Please sign in to comment.