mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Make jump_from_cursor
return a vector of Position
(#4886)
This commit is contained in:
parent
57895ad725
commit
a5b506c424
@ -113,25 +113,30 @@ pub fn jump_from_cursor(
|
|||||||
document: &Document,
|
document: &Document,
|
||||||
source: &Source,
|
source: &Source,
|
||||||
cursor: usize,
|
cursor: usize,
|
||||||
) -> Option<Position> {
|
) -> Vec<Position> {
|
||||||
fn is_text(node: &LinkedNode) -> bool {
|
fn is_text(node: &LinkedNode) -> bool {
|
||||||
node.get().kind() == SyntaxKind::Text
|
node.get().kind() == SyntaxKind::Text
|
||||||
}
|
}
|
||||||
|
|
||||||
let root = LinkedNode::new(source.root());
|
let root = LinkedNode::new(source.root());
|
||||||
let node = root
|
let Some(node) = root
|
||||||
.leaf_at(cursor, Side::Before)
|
.leaf_at(cursor, Side::Before)
|
||||||
.filter(is_text)
|
.filter(is_text)
|
||||||
.or_else(|| root.leaf_at(cursor, Side::After).filter(is_text))?;
|
.or_else(|| root.leaf_at(cursor, Side::After).filter(is_text))
|
||||||
|
else {
|
||||||
|
return vec![];
|
||||||
|
};
|
||||||
|
|
||||||
let span = node.span();
|
let span = node.span();
|
||||||
for (i, page) in document.pages.iter().enumerate() {
|
document
|
||||||
if let Some(point) = find_in_frame(&page.frame, span) {
|
.pages
|
||||||
return Some(Position { page: NonZeroUsize::new(i + 1).unwrap(), point });
|
.iter()
|
||||||
}
|
.enumerate()
|
||||||
}
|
.filter_map(|(i, page)| {
|
||||||
|
find_in_frame(&page.frame, span)
|
||||||
None
|
.map(|point| Position { page: NonZeroUsize::new(i + 1).unwrap(), point })
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Find the position of a span in a frame.
|
/// Find the position of a span in a frame.
|
||||||
@ -226,8 +231,8 @@ mod tests {
|
|||||||
let world = TestWorld::new(text);
|
let world = TestWorld::new(text);
|
||||||
let doc = typst::compile(&world).output.unwrap();
|
let doc = typst::compile(&world).output.unwrap();
|
||||||
let pos = jump_from_cursor(&doc, &world.main, cursor);
|
let pos = jump_from_cursor(&doc, &world.main, cursor);
|
||||||
assert_eq!(pos.is_some(), expected.is_some());
|
assert_eq!(!pos.is_empty(), expected.is_some());
|
||||||
if let (Some(pos), Some(expected)) = (pos, expected) {
|
if let (Some(pos), Some(expected)) = (pos.first(), expected) {
|
||||||
assert_eq!(pos.page, expected.page);
|
assert_eq!(pos.page, expected.page);
|
||||||
assert_approx_eq!(pos.point.x, expected.point.x);
|
assert_approx_eq!(pos.point.x, expected.point.x);
|
||||||
assert_approx_eq!(pos.point.y, expected.point.y);
|
assert_approx_eq!(pos.point.y, expected.point.y);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user