mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Merge f06e591144d2c1093e9cc21b545bbea30708b6c5 into 9b09146a6b5e936966ed7ee73bce9dd2df3810ae
This commit is contained in:
commit
3ae2a362f4
@ -62,7 +62,7 @@ fn eval_markup<'a>(
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
*elem = std::mem::take(elem).labelled(label);
|
*elem = std::mem::take(elem).labelled(label, expr.span());
|
||||||
} else {
|
} else {
|
||||||
vm.engine.sink.warn(warning!(
|
vm.engine.sink.warn(warning!(
|
||||||
expr.span(),
|
expr.span(),
|
||||||
|
@ -75,7 +75,10 @@ pub fn definition(
|
|||||||
let label = Label::new(PicoStr::intern(node.cast::<ast::Ref>()?.target()));
|
let label = Label::new(PicoStr::intern(node.cast::<ast::Ref>()?.target()));
|
||||||
let selector = Selector::Label(label);
|
let selector = Selector::Label(label);
|
||||||
let elem = document?.introspector.query_first(&selector)?;
|
let elem = document?.introspector.query_first(&selector)?;
|
||||||
return Some(Definition::Span(elem.span()));
|
let labelled_at = elem.labelled_at().or(elem.span());
|
||||||
|
if !labelled_at.is_detached() {
|
||||||
|
return Some(Definition::Span(labelled_at));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {}
|
_ => {}
|
||||||
@ -181,7 +184,13 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_definition_ref() {
|
fn test_definition_ref() {
|
||||||
test("#figure[] <hi> See @hi", -2, Side::After).must_be_at("main.typ", 1..9);
|
test("#figure[] <hi> See @hi", -2, Side::After).must_be_at("main.typ", 10..14);
|
||||||
|
let source =
|
||||||
|
r#"#let test1(body) = figure(body); #test1([Test1]) <fig:test1> @fig:test1"#;
|
||||||
|
test(source, -2, Side::After).must_be_at("main.typ", 49..60);
|
||||||
|
let source = r#"#let test1(body) = figure(body); #test1([Test1]) <fig:test1> @fig:test1
|
||||||
|
#let test2(body) = test1(body); #test2([Test2]) <fig:test2>; @fig:test2"#;
|
||||||
|
test(source, -2, Side::After).must_be_at("main.typ", 120..131);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -82,6 +82,8 @@ pub struct Content {
|
|||||||
struct Inner<T: ?Sized + 'static> {
|
struct Inner<T: ?Sized + 'static> {
|
||||||
/// An optional label attached to the element.
|
/// An optional label attached to the element.
|
||||||
label: Option<Label>,
|
label: Option<Label>,
|
||||||
|
/// The span where the label is attached.
|
||||||
|
labelled_at: Span,
|
||||||
/// The element's location which identifies it in the layouted output.
|
/// The element's location which identifies it in the layouted output.
|
||||||
location: Option<Location>,
|
location: Option<Location>,
|
||||||
/// Manages the element during realization.
|
/// Manages the element during realization.
|
||||||
@ -99,6 +101,7 @@ impl Content {
|
|||||||
Self {
|
Self {
|
||||||
inner: Arc::new(Inner {
|
inner: Arc::new(Inner {
|
||||||
label: None,
|
label: None,
|
||||||
|
labelled_at: Span::detached(),
|
||||||
location: None,
|
location: None,
|
||||||
lifecycle: SmallBitSet::new(),
|
lifecycle: SmallBitSet::new(),
|
||||||
elem: elem.into(),
|
elem: elem.into(),
|
||||||
@ -135,9 +138,16 @@ impl Content {
|
|||||||
self.inner.label
|
self.inner.label
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the span where the label is attached.
|
||||||
|
pub fn labelled_at(&self) -> Span {
|
||||||
|
self.inner.labelled_at
|
||||||
|
}
|
||||||
|
|
||||||
/// Attach a label to the content.
|
/// Attach a label to the content.
|
||||||
pub fn labelled(mut self, label: Label) -> Self {
|
pub fn labelled(mut self, label: Label, labelled_at: Span) -> Self {
|
||||||
self.set_label(label);
|
let m = self.make_mut();
|
||||||
|
m.label = Some(label);
|
||||||
|
m.labelled_at = labelled_at;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -540,8 +550,7 @@ impl Content {
|
|||||||
/// The content's element function. This function can be used to create the element
|
/// The content's element function. This function can be used to create the element
|
||||||
/// contained in this content. It can be used in set and show rules for the
|
/// contained in this content. It can be used in set and show rules for the
|
||||||
/// element. Can be compared with global functions to check whether you have
|
/// element. Can be compared with global functions to check whether you have
|
||||||
/// a specific
|
/// a specific kind of element.
|
||||||
/// kind of element.
|
|
||||||
#[func]
|
#[func]
|
||||||
pub fn func(&self) -> Element {
|
pub fn func(&self) -> Element {
|
||||||
self.elem()
|
self.elem()
|
||||||
@ -742,6 +751,7 @@ impl<T: NativeElement> Bounds for T {
|
|||||||
location: inner.location,
|
location: inner.location,
|
||||||
lifecycle: inner.lifecycle.clone(),
|
lifecycle: inner.lifecycle.clone(),
|
||||||
elem: LazyHash::reuse(self.clone(), &inner.elem),
|
elem: LazyHash::reuse(self.clone(), &inner.elem),
|
||||||
|
labelled_at: inner.labelled_at,
|
||||||
}),
|
}),
|
||||||
span,
|
span,
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user