Skip to content

Commit

Permalink
feat: use world implementation from tinymist (#636)
Browse files Browse the repository at this point in the history
* fix: warnings in clippy 1.84

* feat: use world implementation from tinymist

* build: update dep

* fix: fmt

* dev: delete reflexo-vfs

* fix: default features

* fix: compile error
  • Loading branch information
Myriad-Dreamin authored Feb 2, 2025
1 parent ed2eb0f commit 68b46b4
Show file tree
Hide file tree
Showing 148 changed files with 779 additions and 7,258 deletions.
286 changes: 207 additions & 79 deletions Cargo.lock

Large diffs are not rendered by default.

24 changes: 18 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ members = [
"benches/lowering",

"crates/reflexo",
"crates/reflexo-vfs",
"crates/reflexo-world",
"crates/reflexo-typst",
"crates/conversion/typst2vec",
"crates/conversion/vec2canvas",
Expand Down Expand Up @@ -193,12 +191,15 @@ vergen = { version = "9.0.1", features = [
] }
vergen-gitcl = { version = "1.0.1" }

# tinymist's world implementation
typst-shim = { git = "https://github.com/Myriad-Dreamin/tinymist.git", rev = "7b8739487ed959b295c4df39e5cc2ebd47b4e066" }
tinymist-analysis = { git = "https://github.com/Myriad-Dreamin/tinymist.git", rev = "7b8739487ed959b295c4df39e5cc2ebd47b4e066" }
tinymist-std = { git = "https://github.com/Myriad-Dreamin/tinymist.git", rev = "7b8739487ed959b295c4df39e5cc2ebd47b4e066", default-features = false }
tinymist-world = { git = "https://github.com/Myriad-Dreamin/tinymist.git", rev = "7b8739487ed959b295c4df39e5cc2ebd47b4e066", default-features = false }

# project core
reflexo = { version = "0.5.4", path = "crates/reflexo", default-features = false }
reflexo-vfs = { version = "0.5.4", path = "crates/reflexo-vfs" }
reflexo-world = { version = "0.5.4", path = "crates/reflexo-world" }
reflexo-typst = { version = "0.5.4", path = "crates/reflexo-typst", default-features = false }
reflexo-typst-shim = { version = "0.5.4", path = "crates/reflexo-typst-shim" }

# conversions
reflexo-typst2vec = { version = "0.5.4", path = "crates/conversion/typst2vec" }
Expand All @@ -214,7 +215,12 @@ typst-ts-dev-server = { version = "0.5.4", path = "server/dev" }
typst-ts-cli = { version = "0.5.4", path = "cli" }

[workspace.lints.rust]
missing_docs = "warn"
# missing_docs = "warn"
unexpected_cfgs = { level = "allow", check-cfg = [
'cfg(wasm_bindgen_unstable_test_coverage)',
'cfg(noop)',
'cfg(used_linker)',
] }

[workspace.lints.clippy]
uninlined_format_args = "warn"
Expand Down Expand Up @@ -245,3 +251,9 @@ typst-pdf = { git = "https://github.com/Myriad-Dreamin/typst", tag = "typst.ts/v
# typst-pdf = { path = "../typst/crates/typst-pdf" }

# fontdb = { path = "../fontdb" }

# [patch."https://github.com/Myriad-Dreamin/tinymist.git"]
# typst-shim = { path = "../tinymist/crates/typst-shim" }
# tinymist-analysis = { path = "../tinymist/crates/tinymist-analysis" }
# tinymist-std = { path = "../tinymist/crates/tinymist-std" }
# tinymist-world = { path = "../tinymist/crates/tinymist-world" }
4 changes: 2 additions & 2 deletions benches/lowering/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ static TEST_DOC: LazyLock<Arc<TypstDocument>> =

fn compile(driver: &CompileDriver, src: &str) -> Arc<TypstDocument> {
let mut driver = driver.lock().unwrap();
let e = driver.entry_file().to_owned().unwrap();
let e = driver.main_id();
driver
.with_shadow_file(&e, src.as_bytes().into(), |this| {
.with_shadow_file_by_id(e, src.as_bytes().into(), |this| {
std::marker::PhantomData.compile(&this.snapshot(), &mut Default::default())
})
.unwrap()
Expand Down
33 changes: 15 additions & 18 deletions cli/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use typst::foundations::{Bytes, Dict, IntoValue};
use typst::model::Document;

use crate::font::fonts;
use crate::utils::current_dir;
use crate::{
utils::{self, UnwrapOrExit},
CompileArgs, CompileOnceArgs,
Expand Down Expand Up @@ -82,7 +81,9 @@ pub fn create_driver(args: CompileOnceArgs) -> CompileDriver<PureCompiler<TypstS
let world = if is_stdin {
let mut u = universe;

let entry = u.entry_state().select_in_workspace(*MEMORY_MAIN_ENTRY);
let entry = u
.entry_state()
.select_in_workspace(MEMORY_MAIN_ENTRY.vpath().as_rooted_path());
u.mutate_entry(entry).unwrap();

let src = read_from_stdin()
Expand Down Expand Up @@ -114,31 +115,27 @@ pub fn create_driver(args: CompileOnceArgs) -> CompileDriver<PureCompiler<TypstS
}

pub fn compile_export(args: CompileArgs, exporter: GroupExporter<Document>) -> ! {
let is_stdin = args.compile.entry == "-";
let (intr_tx, intr_rx) = mpsc::unbounded_channel();

let driver = create_driver(args.compile.clone());

// todo: make dynamic layout exporter
let output_dir = {
// If output is specified, use it.
let dir = (!args.compile.output.is_empty()).then(|| Path::new(&args.compile.output));
// Otherwise, use the parent directory of the entry file.
let entry = driver.entry_file().expect("entry_file is not set");
let dir = dir.map(Path::to_owned).unwrap_or_else(|| {
if is_stdin {
current_dir()
} else {
entry.parent().expect("entry_file has no parent").to_owned()
}
});
if is_stdin {
let dir = args.compile.output_dir();
if args.compile.is_stdin() {
dir.join("main")
} else {
dir.join(entry.file_name().expect("entry_file has no file name"))
dir.join(
args.compile
.main_id()
.vpath()
.as_rooted_path()
.file_name()
.expect("entry_file has no file name"),
)
}
};

let driver = create_driver(args.compile);

let feature_set =
FeatureSet::default().configure(&DIAG_FMT_FEATURE, args.diagnostic_format.into());

Expand Down
100 changes: 98 additions & 2 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ pub mod utils;
pub mod version;

use core::fmt;
use std::{borrow::Cow, path::PathBuf};
use std::{
borrow::Cow,
path::{Path, PathBuf},
sync::OnceLock,
};

use chrono::{DateTime, Utc};
use clap::{builder::ValueParser, ArgAction, Args, Command, Parser, Subcommand, ValueEnum};
use reflexo_typst::build_info::VERSION;
use reflexo_typst::{
build_info::VERSION, vfs::WorkspaceResolver, ImmutPath, TypstFileId, MEMORY_MAIN_ENTRY,
};
use typst::syntax::VirtualPath;
use utils::current_dir;
use version::VersionFormat;

/// The character typically used to separate path components
Expand Down Expand Up @@ -147,6 +155,94 @@ pub struct CompileOnceArgs {

#[clap(skip)]
pub extra_embedded_fonts: Vec<Cow<'static, [u8]>>,

/// The root of workspace of the compilation.
#[clap(skip)]
pub parsed_entry: OnceLock<ImmutPath>,

/// The root of workspace of the compilation.
#[clap(skip)]
pub main_id: OnceLock<TypstFileId>,

/// The output directory of the compilation.
#[clap(skip)]
pub parsed_output: OnceLock<ImmutPath>,
}

impl CompileOnceArgs {
pub fn is_stdin(&self) -> bool {
self.entry == "-"
}

pub fn root(&self) -> &ImmutPath {
self.parsed_entry.get_or_init(|| {
let root = Path::new(&self.workspace);

if root.is_absolute() {
root.into()
} else {
current_dir().join(root).into()
}
})
}

pub fn main_id(&self) -> &TypstFileId {
self.main_id.get_or_init(|| {
if self.is_stdin() {
*MEMORY_MAIN_ENTRY
} else {
let root = self.root();
let entry = Path::new(&self.entry);

let entry = if entry.is_absolute() {
entry.to_owned()
} else {
current_dir().join(entry)
};

let path = match entry.strip_prefix(root) {
Ok(rel) => VirtualPath::new(rel),
Err(_) => clap::Error::raw(
clap::error::ErrorKind::InvalidValue,
format!(
"entry file path must be in workspace directory: {workspace_dir}\n",
workspace_dir = root.display()
),
)
.exit(),
};

WorkspaceResolver::workspace_file(Some(root), path)
}
})
}

pub fn output_dir(&self) -> &ImmutPath {
self.parsed_output.get_or_init(|| {
let input = self.main_id();

if self.output.is_empty() {
if self.is_stdin() {
current_dir().into()
} else {
input
.vpath()
.as_rooted_path()
.parent()
.unwrap_or_else(|| {
clap::Error::raw(
clap::error::ErrorKind::InvalidValue,
"entry file has no parent",
)
.exit()
})
.into()
}
} else {
Path::new(&self.output).into()
}
})
}
}

/// Parses key/value pairs split by the first equal sign.
Expand Down
1 change: 0 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::{

use clap::FromArgMatches;
use reflexo_typst::config::{entry::EntryOpts, CompileOpts};
use reflexo_typst::error::prelude::*;
use reflexo_typst::exporter_builtins::GroupExporter;
use reflexo_typst::exporter_utils::map_err;
use reflexo_typst::path::{unix_slash, PathClean};
Expand Down
2 changes: 1 addition & 1 deletion cli/src/query_repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Completer for ReplContext {

// commit line changes

let entry = driver.entry_file().unwrap();
let entry = driver.entry_file().unwrap().to_err().unwrap();
let content = std::fs::read_to_string(&entry).map_err(ReadlineError::Io)?;
let static_prefix = content + "\n#show ";
let static_prefix_len = static_prefix.len();
Expand Down
3 changes: 3 additions & 0 deletions crates/conversion/typst2vec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ no-content-hint = []
glyph2vec = []
debug-gc = []
item-dashmap = ["reflexo/item-dashmap"]

[lints]
workspace = true
17 changes: 8 additions & 9 deletions crates/conversion/typst2vec/src/font/glyph.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
pub use ttf_parser::GlyphId;

use std::fmt::Write;
use std::hash::{Hash, Hasher};
use std::{ops::Deref, sync::Arc};

pub use ttf_parser::GlyphId;
use reflexo::hash::{item_hash128, HashedTrait, StaticHash128};
use reflexo::ImmutStr;
use typst::text::Font;
// use typst::geom::Axes;
use typst::visualize::{Image as TypstImage, RasterFormat};

use reflexo::hash::item_hash128;
use reflexo::{HashedTrait, ImmutStr, StaticHash128};

use super::ligature::resolve_ligature;

/// IGlyphProvider extracts the font data from the font.
Expand Down Expand Up @@ -168,19 +167,19 @@ struct SvgOutlineBuilder(pub String);

impl ttf_parser::OutlineBuilder for SvgOutlineBuilder {
fn move_to(&mut self, x: f32, y: f32) {
write!(&mut self.0, "M {} {} ", x, y).unwrap();
write!(&mut self.0, "M {x} {y} ").unwrap();
}

fn line_to(&mut self, x: f32, y: f32) {
write!(&mut self.0, "L {} {} ", x, y).unwrap();
write!(&mut self.0, "L {x} {y} ").unwrap();
}

fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
write!(&mut self.0, "Q {} {} {} {} ", x1, y1, x, y).unwrap();
write!(&mut self.0, "Q {x1} {y1} {x} {y} ").unwrap();
}

fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
write!(&mut self.0, "C {} {} {} {} {} {} ", x1, y1, x2, y2, x, y).unwrap();
write!(&mut self.0, "C {x1} {y1} {x2} {y2} {x} {y} ").unwrap();
}

fn close(&mut self) {
Expand Down
4 changes: 2 additions & 2 deletions crates/conversion/typst2vec/src/font/ligature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl LigatureResolver {
.into_iter()
.map(|g| {
self.rev_cmap.get(&g).unwrap_or_else(|| {
println!("ligature component not found: {:?} {:?}", g, face);
eprintln!("ligature component not found: {g:?} {face:?}");
&' '
})
})
Expand Down Expand Up @@ -131,6 +131,6 @@ fn get_ligature_resolver(font: &Font) -> Arc<LigatureResolver> {
pub(super) fn resolve_ligature(font: &Font, id: GlyphId) -> Option<ImmutStr> {
let resolver = get_ligature_resolver(font);
// let res = resolver.resolve(font.ttf(), id);
// println!("resolve_ligature {:?} {:?} -> {:?}", font, id, res);
// eprintln!("resolve_ligature {:?} {:?} -> {:?}", font, id, res);
resolver.resolve(font.ttf(), id)
}
8 changes: 4 additions & 4 deletions crates/conversion/typst2vec/src/incr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl IncrDocServer {
.map(|i| i.1 .0)
.min()
.unwrap_or(0);
println!(
eprintln!(
"gc[{}]: max: {}, min: {}, remove: {}",
self.typst2vec.lifetime,
self.typst2vec
Expand All @@ -78,7 +78,7 @@ impl IncrDocServer {

// for (fg, (_, item)) in
// self.typst2vec.items.iter().filter(|(_, i)| i.0 == mi) {
// println!("mi {fg:?} => {item:#?}");
// eprintln!("mi {fg:?} => {item:#?}");
// }
}

Expand Down Expand Up @@ -135,15 +135,15 @@ impl IncrDocServer {
pub fn resolve_element_paths_by_span(
&mut self,
span_offset: SourceSpanOffset,
) -> ZResult<Vec<Vec<ElementPoint>>> {
) -> Result<Vec<Vec<ElementPoint>>> {
self.typst2vec.spans.query_element_paths(span_offset)
}

/// Gets the span range of the given element path.
pub fn resolve_span_by_element_path(
&mut self,
path: &[ElementPoint],
) -> ZResult<Option<(SourceSpanOffset, SourceSpanOffset)>> {
) -> Result<Option<(SourceSpanOffset, SourceSpanOffset)>> {
self.typst2vec.spans.query(path)
}
}
2 changes: 1 addition & 1 deletion crates/conversion/typst2vec/src/pass/glyph2vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ impl ConvertInnerImpl {
.as_ref()
.map(|s| {
WARN_VIEW_BOX.get_or_init(|| {
println!(
eprintln!(
"render_svg_glyph with viewBox, This should be helpful if you can help us verify the result: {:?} {:?}",
font.info().family,
doc_string
Expand Down
4 changes: 2 additions & 2 deletions crates/conversion/typst2vec/src/pass/span2vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl Span2VecPass {
pub fn query_element_paths(
&mut self,
span_offset: SourceSpanOffset,
) -> ZResult<Vec<Vec<ElementPoint>>> {
) -> Result<Vec<Vec<ElementPoint>>> {
self.span_tree.get_or_init(|| {
log::info!("lazy spans are initializing");
std::mem::take(&mut self.collector).into()
Expand Down Expand Up @@ -510,7 +510,7 @@ impl Span2VecPass {
pub fn query(
&mut self,
path: &[ElementPoint],
) -> ZResult<Option<(SourceSpanOffset, SourceSpanOffset)>> {
) -> Result<Option<(SourceSpanOffset, SourceSpanOffset)>> {
self.span_tree.get_or_init(|| {
log::info!("lazy spans are initializing");
std::mem::take(&mut self.collector).into()
Expand Down
Loading

0 comments on commit 68b46b4

Please sign in to comment.