summaryrefslogtreecommitdiff
path: root/src/model
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-03-17 18:43:20 +0100
committerLaurenz <laurmaedje@gmail.com>2023-03-17 18:43:20 +0100
commit59903270dc5a21c65df8a4d88068662287918b4c (patch)
tree0ad087fcaa3ad84efe24cf316549655ea9162430 /src/model
parentaf7fe4d76083c597ec2198a73383b9e3899d75ea (diff)
Methods for page and location on queried content
Diffstat (limited to 'src/model')
-rw-r--r--src/model/content.rs21
-rw-r--r--src/model/typeset.rs15
2 files changed, 32 insertions, 4 deletions
diff --git a/src/model/content.rs b/src/model/content.rs
index 5317236e..3c661929 100644
--- a/src/model/content.rs
+++ b/src/model/content.rs
@@ -2,6 +2,7 @@ use std::any::TypeId;
use std::fmt::{self, Debug, Formatter, Write};
use std::hash::{Hash, Hasher};
use std::iter::{self, Sum};
+use std::num::NonZeroUsize;
use std::ops::{Add, AddAssign, Deref};
use ecow::{eco_format, EcoString, EcoVec};
@@ -9,10 +10,10 @@ use once_cell::sync::Lazy;
use super::{
node, Behave, Behaviour, Fold, Guard, Locatable, Recipe, StableId, Style, StyleMap,
- Synthesize,
+ Synthesize, Vt,
};
use crate::diag::{SourceResult, StrResult};
-use crate::doc::Meta;
+use crate::doc::{Location, Meta};
use crate::eval::{
cast_from_value, cast_to_value, Args, Cast, Func, FuncInfo, Str, Value, Vm,
};
@@ -185,6 +186,22 @@ impl Content {
self.field(field).ok_or_else(|| missing_field(field))
}
+ /// Determine the page of this content.
+ pub fn page(&self, vt: &Vt) -> StrResult<NonZeroUsize> {
+ match self.stable_id() {
+ Some(id) => Ok(vt.introspector.page(id)),
+ None => Err("this method can only be called on queried content".into()),
+ }
+ }
+
+ /// Determine the location of this content.
+ pub fn location(&self, vt: &Vt) -> StrResult<Location> {
+ match self.stable_id() {
+ Some(id) => Ok(vt.introspector.location(id)),
+ None => Err("this method can only be called on queried content".into()),
+ }
+ }
+
/// The content's label.
pub fn label(&self) -> Option<&Label> {
match self.field("label")? {
diff --git a/src/model/typeset.rs b/src/model/typeset.rs
index 683f2846..162a2a67 100644
--- a/src/model/typeset.rs
+++ b/src/model/typeset.rs
@@ -53,8 +53,7 @@ pub fn typeset(
/// A virtual typesetter.
///
-/// Holds the state needed to [typeset] content. This is the equivalent to the
-/// [Vm](crate::eval::Vm) for typesetting.
+/// Holds the state needed to [typeset] content.
pub struct Vt<'a> {
/// The compilation environment.
pub world: Tracked<'a, dyn World>,
@@ -66,6 +65,18 @@ pub struct Vt<'a> {
pub introspector: Tracked<'a, Introspector>,
}
+impl Vt<'_> {
+ /// Mutably reborrow with a shorter lifetime.
+ pub fn reborrow_mut(&mut self) -> Vt<'_> {
+ Vt {
+ world: self.world,
+ tracer: TrackedMut::reborrow_mut(&mut self.tracer),
+ provider: TrackedMut::reborrow_mut(&mut self.provider),
+ introspector: self.introspector,
+ }
+ }
+}
+
/// Provides stable identities to nodes.
#[derive(Clone)]
pub struct StabilityProvider {