use ecow::EcoVec;
use typst::WorldExt;
use typst::foundations::AsOutput;
use typst::introspection::{DocumentPosition, HtmlPosition, PagedPosition};
use typst::layout::{Frame, FrameItem, Point, Size};
use typst::model::{Destination, Url};
use typst::syntax::{FileId, LinkedNode, Side, Source, Span, SyntaxKind};
use typst::visualize::{Curve, CurveItem, FillRule, Geometry};
use typst_html::{HtmlDocument, HtmlElement, HtmlNode, HtmlSliceExt};
use typst_layout::PagedDocument;
use crate::IdeWorld;
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Jump {
File(FileId, usize),
Url(Url),
Position(PagedPosition),
}
impl Jump {
fn from_span(world: &dyn IdeWorld, span: Span) -> Option<Self> {
let id = span.id()?;
let offset = world.range(span)?.start;
Some(Self::File(id, offset))
}
}
pub fn jump_from_click<D: JumpFromDocument>(
world: &dyn IdeWorld,
document: &D,
position: &D::Position,
) -> Option<Jump> {
document.resolve_position(world, position)
}
pub trait JumpFromDocument: jump_from_document_sealed::JumpFromDocument {}
impl JumpFromDocument for PagedDocument {}
impl JumpFromDocument for HtmlDocument {}
mod jump_from_document_sealed {
use typst::introspection::{HtmlPosition, InnerHtmlPosition, PagedPosition};
use typst::syntax::SyntaxKind;
use typst_html::{HtmlDocument, HtmlNode, HtmlSliceExt};
use typst_layout::PagedDocument;
use super::{Jump, jump_from_click_in_frame};
use crate::IdeWorld;
pub trait JumpFromDocument {
type Position;
fn resolve_position(
&self,
world: &dyn IdeWorld,
position: &Self::Position,
) -> Option<Jump>;
}
impl JumpFromDocument for PagedDocument {
type Position = PagedPosition;
fn resolve_position(
&self,
world: &dyn IdeWorld,
position: &Self::Position,
) -> Option<Jump> {
let page = self.pages().get(position.page.get() - 1)?;
let click = position.point;
jump_from_click_in_frame(world, self, &page.frame, click)
}
}
impl JumpFromDocument for HtmlDocument {
type Position = HtmlPosition;
fn resolve_position(
&self,
world: &dyn IdeWorld,
position: &Self::Position,
) -> Option<Jump> {
let mut current_node = self.root_node();
let mut prefix_len = 0;
let indices_count = position.element().count();
for (i, index) in position.element().enumerate() {
let reached_leaf_node = i == indices_count - 1;
match current_node {
HtmlNode::Element(html_element) => {
let (child_index, (mut child, _)) = html_element
.children
.iter_with_dom_indices()
.enumerate()
.find(|&(_, (child, dom_index))| {
!matches!(child, HtmlNode::Tag(_)) && dom_index == index
})?;
if reached_leaf_node
&& let HtmlNode::Text(_, _) = child
&& let Some(InnerHtmlPosition::Character(offset)) =
position.details()
{
let mut text_char_count = 0;
let mut text_node_part = child;
let mut text_node_offset = 0;
while text_char_count < *offset {
prefix_len = text_char_count;
text_node_part = html_element
.children
.get(child_index + text_node_offset)?;
let text_node_part_len =
if let HtmlNode::Text(text, _) = text_node_part {
text.chars().count()
} else {
0
};
text_char_count += text_node_part_len;
text_node_offset += 1;
}
child = text_node_part
}
current_node = child;
}
HtmlNode::Tag(_) | HtmlNode::Text(_, _) | HtmlNode::Frame(_) => {
return None;
}
}
}
let span = current_node.span();
let id = span.id()?;
let source = world.source(id).ok()?;
let ast_node = source.find(span)?;
let is_text_node = ast_node.kind() == SyntaxKind::Text;
if let (HtmlNode::Frame(frame), Some(InnerHtmlPosition::Frame(point))) =
(current_node, &position.details())
{
return jump_from_click_in_frame(world, self, &frame.inner, *point);
}
let source_range = ast_node.range();
Some(Jump::File(
id,
source_range.start
+ match (is_text_node, &position.details()) {
(true, Some(InnerHtmlPosition::Character(i))) => {
let source_text = &source.text()[source_range];
let slice: String = source_text
.chars()
.take(i.saturating_sub(prefix_len))
.collect();
slice.len()
}
_ => 0,
},
))
}
}
}
pub fn jump_from_click_in_frame(
world: &dyn IdeWorld,
output: impl AsOutput,
frame: &Frame,
click: Point,
) -> Option<Jump> {
let output = output.as_output();
for (pos, item) in frame.items() {
if let FrameItem::Link(dest, size) = item
&& is_in_rect(*pos, *size, click)
{
match dest {
Destination::Url(url) => return Some(Jump::Url(url.clone())),
Destination::Position(pos) => return Some(Jump::Position(*pos)),
Destination::Location(loc) => {
if let Some(DocumentPosition::Paged(pos)) =
output.introspector().position(*loc)
{
return Some(Jump::Position(pos));
}
}
}
}
}
for &(mut pos, ref item) in frame.items().rev() {
match item {
FrameItem::Group(group) => {
let pos = click - pos;
if let Some(clip) = &group.clip
&& !clip.contains(FillRule::NonZero, pos)
{
continue;
}
let Some(inv_transform) = group.transform.invert() else {
continue;
};
let pos = pos.transform_inf(inv_transform);
if let Some(span) =
jump_from_click_in_frame(world, output, &group.frame, pos)
{
return Some(span);
}
}
FrameItem::Text(text) => {
for glyph in &text.glyphs {
let width = glyph.x_advance.at(text.size);
if is_in_rect(
Point::new(pos.x, pos.y - text.size),
Size::new(width, text.size),
click,
) {
let (span, span_offset) = glyph.span;
let Some(id) = span.id() else { continue };
let source = world.source(id).ok()?;
let node = source.find(span)?;
let pos = if matches!(
node.kind(),
SyntaxKind::Text | SyntaxKind::MathText
) {
let range = node.range();
let mut offset = range.start + usize::from(span_offset);
if (click.x - pos.x) > width / 2.0 {
offset += glyph.range().len();
}
offset.min(range.end)
} else {
node.offset()
};
return Some(Jump::File(source.id(), pos));
}
pos.x += width;
}
}
FrameItem::Shape(shape, span) => {
if shape.fill.is_some() {
let within = match &shape.geometry {
Geometry::Line(..) => false,
Geometry::Rect(size) => is_in_rect(pos, *size, click),
Geometry::Curve(curve) => {
curve.contains(shape.fill_rule, click - pos)
}
};
if within {
return Jump::from_span(world, *span);
}
}
if let Some(stroke) = &shape.stroke {
let within = !stroke.thickness.approx_empty() && {
let base_curve = match &shape.geometry {
Geometry::Line(to) => &Curve(vec![CurveItem::Line(*to)]),
Geometry::Rect(size) => &Curve::rect(*size),
Geometry::Curve(curve) => curve,
};
base_curve.stroke_contains(stroke, click - pos)
};
if within {
return Jump::from_span(world, *span);
}
}
}
FrameItem::Image(_, size, span) if is_in_rect(pos, *size, click) => {
return Jump::from_span(world, *span);
}
_ => {}
}
}
None
}
fn is_in_rect(pos: Point, size: Size, click: Point) -> bool {
pos.x <= click.x
&& pos.x + size.x >= click.x
&& pos.y <= click.y
&& pos.y + size.y >= click.y
}
pub fn jump_from_cursor<D: JumpInDocument>(
document: &D,
source: &Source,
cursor: usize,
) -> Vec<D::Position> {
fn is_text(node: &LinkedNode) -> bool {
matches!(node.kind(), SyntaxKind::Text | SyntaxKind::MathText)
}
let root = LinkedNode::new(source.root());
let Some(node) = root
.leaf_at(cursor, Side::Before)
.filter(is_text)
.or_else(|| root.leaf_at(cursor, Side::After).filter(is_text))
else {
return vec![];
};
let span = node.span();
document.find_span(span)
}
pub trait JumpInDocument: jump_in_document_sealed::JumpInDocument {}
impl JumpInDocument for PagedDocument {}
impl JumpInDocument for HtmlDocument {}
mod jump_in_document_sealed {
use std::num::NonZeroUsize;
use ecow::EcoVec;
use typst::introspection::{HtmlPosition, PagedPosition};
use typst::syntax::Span;
use typst_html::HtmlDocument;
use typst_layout::PagedDocument;
use super::{find_in_elem, find_in_frame};
pub trait JumpInDocument {
type Position;
fn find_span(&self, span: Span) -> Vec<Self::Position>;
}
impl JumpInDocument for PagedDocument {
type Position = PagedPosition;
fn find_span(&self, span: Span) -> Vec<Self::Position> {
self.pages()
.iter()
.enumerate()
.filter_map(|(i, page)| {
find_in_frame(&page.frame, span).map(|point| PagedPosition {
page: NonZeroUsize::new(i + 1).unwrap(),
point,
})
})
.collect()
}
}
impl JumpInDocument for HtmlDocument {
type Position = HtmlPosition;
fn find_span(&self, span: Span) -> Vec<Self::Position> {
find_in_elem(self.root(), span, &mut EcoVec::new())
}
}
}
fn find_in_frame(frame: &Frame, span: Span) -> Option<Point> {
for &(mut pos, ref item) in frame.items() {
if let FrameItem::Group(group) = item
&& let Some(point) = find_in_frame(&group.frame, span)
{
return Some(pos + point.transform(group.transform));
}
if let FrameItem::Text(text) = item {
for glyph in &text.glyphs {
if glyph.span.0 == span {
return Some(pos);
}
pos.x += glyph.x_advance.at(text.size);
}
}
}
None
}
fn find_in_elem(
elem: &HtmlElement,
span: Span,
current_position: &mut EcoVec<usize>,
) -> Vec<HtmlPosition> {
let mut result = Vec::new();
for (child, dom_index) in elem.children.iter_with_dom_indices() {
match child {
HtmlNode::Tag(_) => {}
HtmlNode::Element(e) => {
current_position.push(dom_index);
result.extend(find_in_elem(e, span, current_position));
current_position.pop();
}
HtmlNode::Text(_, node_span) => {
if *node_span == span {
return vec![HtmlPosition::new(current_position.clone())];
}
}
HtmlNode::Frame(frame) => {
if let Some(frame_pos) = find_in_frame(&frame.inner, span) {
let mut position = current_position.clone();
position.push(dom_index);
return vec![HtmlPosition::new(position).in_frame(frame_pos)];
}
}
}
}
result
}
#[cfg(test)]
mod tests {
use std::borrow::Borrow;
use std::num::NonZeroUsize;
use ecow::eco_vec;
use typst::introspection::{HtmlPosition, PagedPosition};
use typst::layout::{Abs, Point};
use typst::utils::NonZeroExt;
use typst_html::HtmlDocument;
use typst_layout::PagedDocument;
use super::{Jump, jump_from_click, jump_from_cursor};
use crate::tests::{FilePos, TestWorld, WorldLike};
fn point(x: f64, y: f64) -> Point {
Point::new(Abs::pt(x), Abs::pt(y))
}
fn cursor(cursor: usize) -> Option<Jump> {
Some(Jump::File(TestWorld::main_id(), cursor))
}
fn pos(page: usize, x: f64, y: f64) -> Option<PagedPosition> {
Some(PagedPosition {
page: NonZeroUsize::new(page).unwrap(),
point: point(x, y),
})
}
macro_rules! assert_approx_eq {
($l:expr, $r:expr) => {
assert!(($l - $r).abs() < Abs::pt(0.1), "{:?} ≉ {:?}", $l, $r);
};
}
#[track_caller]
fn assert_jump_eq(jump: Option<Jump>, expected: Option<Jump>) {
if let (Some(Jump::Position(pos)), Some(Jump::Position(expected))) =
(&jump, &expected)
{
assert_eq!(pos.page, expected.page);
assert_approx_eq!(pos.point.x, expected.point.x);
assert_approx_eq!(pos.point.y, expected.point.y);
} else {
assert_eq!(jump, expected);
}
}
#[track_caller]
fn test_click(world: impl WorldLike, click: Point, expected: Option<Jump>) {
let world = world.acquire();
let world = world.borrow();
let doc: PagedDocument = typst::compile(world).output.unwrap();
let jump = jump_from_click(
world,
&doc,
&PagedPosition { page: NonZeroUsize::ONE, point: click },
);
assert_jump_eq(jump, expected);
}
#[track_caller]
fn test_click_html(
world: impl WorldLike,
click: HtmlPosition,
expected: Option<Jump>,
) {
let world = world.acquire();
let world = world.borrow();
let doc: HtmlDocument = typst::compile(world).output.unwrap();
let jump = jump_from_click(world, &doc, &click);
assert_jump_eq(jump, expected);
}
#[track_caller]
fn test_cursor(
world: impl WorldLike,
pos: impl FilePos,
expected: Option<PagedPosition>,
) {
let world = world.acquire();
let world = world.borrow();
let doc: PagedDocument = typst::compile(world).output.unwrap();
let (source, cursor) = pos.resolve(world);
let pos = jump_from_cursor(&doc, &source, cursor);
assert_eq!(!pos.is_empty(), expected.is_some());
if let (Some(pos), Some(expected)) = (pos.first(), expected) {
assert_eq!(pos.page, expected.page);
assert_approx_eq!(pos.point.x, expected.point.x);
assert_approx_eq!(pos.point.y, expected.point.y);
}
}
#[test]
fn test_jump_from_click() {
let s = "*Hello* #box[ABC] World";
test_click(s, point(0.0, 0.0), None);
test_click(s, point(70.0, 5.0), None);
test_click(s, point(45.0, 15.0), cursor(14));
test_click(s, point(48.0, 15.0), cursor(15));
test_click(s, point(72.0, 10.0), cursor(20));
}
#[test]
fn test_jump_from_click_par_indents() {
let s = "#set par(first-line-indent: 1cm, hanging-indent: 1cm);Hello";
test_click(s, point(21.0, 12.0), cursor(56));
}
#[test]
fn test_jump_from_click_math() {
test_click("$a + b$", point(28.0, 14.0), cursor(5));
}
#[test]
fn test_jump_from_click_transform_clip() {
let margin = point(10.0, 10.0);
test_click(
"#rect(width: 20pt, height: 20pt, fill: black)",
point(10.0, 10.0) + margin,
cursor(1),
);
test_click(
"#rect(width: 60pt, height: 10pt, fill: black)",
point(5.0, 30.0) + margin,
None,
);
test_click(
"#rotate(90deg, origin: bottom + left, rect(width: 60pt, height: 10pt, fill: black))",
point(5.0, 30.0) + margin,
cursor(38),
);
test_click(
"#scale(x: 300%, y: 300%, origin: top + left, rect(width: 10pt, height: 10pt, fill: black))",
point(20.0, 20.0) + margin,
cursor(45),
);
test_click(
"#box(width: 10pt, height: 10pt, clip: true, scale(x: 300%, y: 300%, \
origin: top + left, rect(width: 10pt, height: 10pt, fill: black)))",
point(20.0, 20.0) + margin,
None,
);
test_click(
"#box(width: 10pt, height: 10pt, clip: false, rect(width: 30pt, height: 30pt, fill: black))",
point(20.0, 20.0) + margin,
cursor(45),
);
test_click(
"#box(width: 10pt, height: 10pt, clip: true, rect(width: 30pt, height: 30pt, fill: black))",
point(20.0, 20.0) + margin,
None,
);
test_click(
"#rotate(90deg, origin: bottom + left)[hello world]",
point(5.0, 15.0) + margin,
cursor(40),
);
}
#[test]
fn test_jump_from_click_shapes() {
let margin = point(10.0, 10.0);
test_click(
"#rect(width: 30pt, height: 30pt, fill: black)",
point(15.0, 15.0) + margin,
cursor(1),
);
let circle = "#circle(width: 30pt, height: 30pt, fill: black)";
test_click(circle, point(15.0, 15.0) + margin, cursor(1));
test_click(circle, point(1.0, 1.0) + margin, None);
let bowtie =
"#polygon(fill: black, (0pt, 0pt), (20pt, 20pt), (20pt, 0pt), (0pt, 20pt))";
test_click(bowtie, point(1.0, 2.0) + margin, cursor(1));
test_click(bowtie, point(2.0, 1.0) + margin, None);
test_click(bowtie, point(19.0, 10.0) + margin, cursor(1));
let evenodd = r#"#polygon(fill: black, fill-rule: "even-odd",
(0pt, 10pt), (30pt, 10pt), (30pt, 20pt), (20pt, 20pt),
(20pt, 0pt), (10pt, 0pt), (10pt, 30pt), (20pt, 30pt),
(20pt, 20pt), (0pt, 20pt))"#;
test_click(evenodd, point(15.0, 15.0) + margin, None);
test_click(evenodd, point(5.0, 15.0) + margin, cursor(1));
test_click(evenodd, point(15.0, 5.0) + margin, cursor(1));
}
#[test]
fn test_jump_from_click_shapes_stroke() {
let margin = point(10.0, 10.0);
let rect =
"#place(dx: 10pt, dy: 10pt, rect(width: 10pt, height: 10pt, stroke: 5pt))";
test_click(rect, point(15.0, 15.0) + margin, None);
test_click(rect, point(10.0, 15.0) + margin, cursor(27));
test_click(
"#line(angle: 45deg, length: 10pt, stroke: 2pt)",
point(2.0, 2.0) + margin,
cursor(1),
);
}
#[test]
fn test_jump_from_click_html() {
test_click_html(
"This is a test.\n\nWith multiple elements.\n\nAnd some *formatting*.",
HtmlPosition::new(eco_vec![1, 2, 0]).at_char(6),
cursor(48),
);
}
#[test]
fn test_jump_from_click_html_introspection() {
test_click_html(
"This is a test.\n\n```\nwith some code\n```\n\nAnd `some` *formatting*.",
HtmlPosition::new(eco_vec![1, 2, 1, 0]).at_char(2),
cursor(48),
);
}
#[test]
fn test_jump_from_click_html_frame() {
test_click_html(
"A math formula:\n\n#html.frame($a x + b = 0$)",
HtmlPosition::new(eco_vec![1, 1]).in_frame(point(27.0, 5.0)),
cursor(37),
);
}
#[test]
fn test_jump_from_click_html_bindings() {
let src = "#let a = [This]; #let b = [exists]; #a#b";
test_click_html(
src,
HtmlPosition::new(eco_vec![1, 0, 0]).at_char(8),
cursor(src.find("ts];").unwrap()),
);
}
#[test]
fn test_jump_from_click_html_figcaption() {
let src = "#figure([Hello, world!], caption: [Output of the program.])";
test_click_html(
src,
HtmlPosition::new(eco_vec![1, 0, 1, 0])
.at_char("Fig. 1 — Out".chars().count()),
cursor(src.find("tput of").unwrap()),
);
}
#[test]
fn test_jump_from_click_html_offset_units() {
test_click_html(
"Ça va ?",
HtmlPosition::new(eco_vec![1, 0]).at_char(1),
cursor(2),
);
}
#[test]
fn test_jump_from_cursor() {
let s = "*Hello* #box[ABC] World";
test_cursor(s, 12, None);
test_cursor(s, 14, pos(1, 37.55, 16.58));
}
#[test]
fn test_jump_from_cursor_math() {
test_cursor("$a + b$", -3, pos(1, 27.51, 16.83));
}
#[test]
fn test_jump_from_cursor_transform() {
test_cursor(
r#"#rotate(90deg, origin: bottom + left, [hello world])"#,
-5,
pos(1, 10.0, 16.58),
);
}
#[test]
fn test_footnote_links() {
let s = "#footnote[Hi]";
test_click(s, point(10.0, 10.0), pos(1, 10.0, 31.58).map(Jump::Position));
test_click(s, point(19.0, 33.0), pos(1, 10.0, 16.58).map(Jump::Position));
}
#[test]
fn test_footnote_link_entry_customized() {
let s = "#show footnote.entry: [Replaced]; #footnote[Hi]";
test_click(s, point(10.0, 10.0), pos(1, 10.0, 31.58).map(Jump::Position));
}
#[test]
fn test_text_show_rule_jump() {
test_click(
r#"#show "word": underline; This is \ my word."#,
point(33.0, 27.0),
cursor(36),
);
}
}