use comemo::{Track, Tracked};
use crate::diag::{warning, HintedStrResult, SourceResult};
use crate::engine::Engine;
use crate::foundations::{
cast, elem, func, Content, Context, Func, LocatableSelector, NativeElement, Packed,
Show, StyleChain, Value,
};
use crate::introspection::{Locatable, Location};
use crate::syntax::Span;
#[func(contextual)]
pub fn locate(
engine: &mut Engine,
context: Tracked<Context>,
span: Span,
selector: LocateInput,
) -> HintedStrResult<LocateOutput> {
Ok(match selector {
LocateInput::Selector(selector) => {
LocateOutput::Location(selector.resolve_unique(engine.introspector, context)?)
}
LocateInput::Func(func) => {
engine.sink.warn(warning!(
span, "`locate` with callback function is deprecated";
hint: "use a `context` expression instead"
));
LocateOutput::Content(LocateElem::new(func).pack().spanned(span))
}
})
}
pub enum LocateInput {
Selector(LocatableSelector),
Func(Func),
}
cast! {
LocateInput,
v: Func => {
if v.element().is_some() {
Self::Selector(Value::Func(v).cast()?)
} else {
Self::Func(v)
}
},
v: LocatableSelector => Self::Selector(v),
}
pub enum LocateOutput {
Location(Location),
Content(Content),
}
cast! {
LocateOutput,
self => match self {
Self::Location(v) => v.into_value(),
Self::Content(v) => v.into_value(),
},
v: Location => Self::Location(v),
v: Content => Self::Content(v),
}
#[elem(Locatable, Show)]
struct LocateElem {
#[required]
func: Func,
}
impl Show for Packed<LocateElem> {
#[typst_macros::time(name = "locate", span = self.span())]
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
let location = self.location().unwrap();
let context = Context::new(Some(location), Some(styles));
Ok(self.func().call(engine, context.track(), [location])?.display())
}
}