diff options
| author | Laurenz <laurmaedje@gmail.com> | 2024-11-10 13:35:49 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2024-11-13 10:21:40 +0100 |
| commit | 5625914872b2824388ce65f3fb184d913f29cbff (patch) | |
| tree | e8ca84034dea217686ed0f4b179e1e391a3a46e3 /crates/typst-ide/src | |
| parent | 6c22ba1cbd21dae100f25b0268389f993f075626 (diff) | |
Simplify import handling
Diffstat (limited to 'crates/typst-ide/src')
| -rw-r--r-- | crates/typst-ide/src/analyze.rs | 35 | ||||
| -rw-r--r-- | crates/typst-ide/src/utils.rs | 25 |
2 files changed, 32 insertions, 28 deletions
diff --git a/crates/typst-ide/src/analyze.rs b/crates/typst-ide/src/analyze.rs index ce7fe478..5e3dfd70 100644 --- a/crates/typst-ide/src/analyze.rs +++ b/crates/typst-ide/src/analyze.rs @@ -1,11 +1,8 @@ use comemo::Track; use ecow::{eco_vec, EcoString, EcoVec}; -use typst::engine::{Engine, Route, Sink, Traced}; -use typst::foundations::{Context, Label, Scopes, Styles, Value}; -use typst::introspection::Introspector; +use typst::foundations::{Label, Styles, Value}; use typst::model::{BibliographyElem, Document}; -use typst::syntax::{ast, LinkedNode, Span, SyntaxKind}; -use typst_eval::Vm; +use typst::syntax::{ast, LinkedNode, SyntaxKind}; use crate::IdeWorld; @@ -46,7 +43,7 @@ pub fn analyze_expr( eco_vec![(val, None)] } -/// Try to load a module from the current source file. +/// Tries to load a module from the given `source` node. pub fn analyze_import(world: &dyn IdeWorld, source: &LinkedNode) -> Option<Value> { // Use span in the node for resolving imports with relative paths. let source_span = source.span(); @@ -55,29 +52,11 @@ pub fn analyze_import(world: &dyn IdeWorld, source: &LinkedNode) -> Option<Value return Some(source); } - let introspector = Introspector::default(); - let traced = Traced::default(); - let mut sink = Sink::new(); - let engine = Engine { - routines: &typst::ROUTINES, - world: world.upcast().track(), - introspector: introspector.track(), - traced: traced.track(), - sink: sink.track_mut(), - route: Route::default(), - }; - - let context = Context::none(); - let mut vm = Vm::new( - engine, - context.track(), - Scopes::new(Some(world.library())), - Span::detached(), - ); + let Value::Str(path) = source else { return None }; - typst_eval::import(&mut vm, source, source_span, true) - .ok() - .map(Value::Module) + crate::utils::with_engine(world, |engine| { + typst_eval::import(engine, &path, source_span).ok().map(Value::Module) + }) } /// Find all labels and details for them. diff --git a/crates/typst-ide/src/utils.rs b/crates/typst-ide/src/utils.rs index e5252d91..903fb2f3 100644 --- a/crates/typst-ide/src/utils.rs +++ b/crates/typst-ide/src/utils.rs @@ -1,8 +1,33 @@ use std::fmt::Write; +use comemo::Track; use ecow::{eco_format, EcoString}; +use typst::engine::{Engine, Route, Sink, Traced}; +use typst::introspection::Introspector; use typst::text::{FontInfo, FontStyle}; +use crate::IdeWorld; + +/// Create a temporary engine and run a task on it. +pub fn with_engine<F, T>(world: &dyn IdeWorld, f: F) -> T +where + F: FnOnce(&mut Engine) -> T, +{ + let introspector = Introspector::default(); + let traced = Traced::default(); + let mut sink = Sink::new(); + let mut engine = Engine { + routines: &typst::ROUTINES, + world: world.upcast().track(), + introspector: introspector.track(), + traced: traced.track(), + sink: sink.track_mut(), + route: Route::default(), + }; + + f(&mut engine) +} + /// Extract the first sentence of plain text of a piece of documentation. /// /// Removes Markdown formatting. |
