diff options
| author | Matthew Toohey <contact@mtoohey.com> | 2024-04-01 16:22:54 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-01 20:22:54 +0000 |
| commit | dee8ccf04810ee4032fd28b366a4f796b7bf3062 (patch) | |
| tree | c1a93c99c762527e0181508037318ced1bd82e46 /crates/typst-ide | |
| parent | 16c3af7c92de97d14f52319bd375f842ce37a949 (diff) | |
Add side parameter to leaf_at (#3767)
Diffstat (limited to 'crates/typst-ide')
| -rw-r--r-- | crates/typst-ide/src/complete.rs | 4 | ||||
| -rw-r--r-- | crates/typst-ide/src/jump.rs | 13 | ||||
| -rw-r--r-- | crates/typst-ide/src/tooltip.rs | 5 |
3 files changed, 14 insertions, 8 deletions
diff --git a/crates/typst-ide/src/complete.rs b/crates/typst-ide/src/complete.rs index 4e4b8918..be28a431 100644 --- a/crates/typst-ide/src/complete.rs +++ b/crates/typst-ide/src/complete.rs @@ -10,7 +10,7 @@ use typst::foundations::{ }; use typst::model::Document; use typst::syntax::{ - ast, is_id_continue, is_id_start, is_ident, LinkedNode, Source, SyntaxKind, + ast, is_id_continue, is_id_start, is_ident, LinkedNode, Side, Source, SyntaxKind, }; use typst::text::RawElem; use typst::visualize::Color; @@ -1033,7 +1033,7 @@ impl<'a> CompletionContext<'a> { ) -> Option<Self> { let text = source.text(); let library = world.library(); - let leaf = LinkedNode::new(source.root()).leaf_at(cursor)?; + let leaf = LinkedNode::new(source.root()).leaf_at(cursor, Side::Before)?; Some(Self { world, document, diff --git a/crates/typst-ide/src/jump.rs b/crates/typst-ide/src/jump.rs index c8b7343a..7c3c7569 100644 --- a/crates/typst-ide/src/jump.rs +++ b/crates/typst-ide/src/jump.rs @@ -4,7 +4,7 @@ use ecow::EcoString; use typst::introspection::Meta; use typst::layout::{Frame, FrameItem, Point, Position, Size}; use typst::model::{Destination, Document}; -use typst::syntax::{FileId, LinkedNode, Source, Span, SyntaxKind}; +use typst::syntax::{FileId, LinkedNode, Side, Source, Span, SyntaxKind}; use typst::visualize::Geometry; use typst::World; @@ -115,11 +115,16 @@ pub fn jump_from_cursor( source: &Source, cursor: usize, ) -> Option<Position> { - let node = LinkedNode::new(source.root()).leaf_at(cursor)?; - if node.kind() != SyntaxKind::Text { - return None; + fn is_text(node: &LinkedNode) -> bool { + node.get().kind() == SyntaxKind::Text } + let root = LinkedNode::new(source.root()); + let node = root + .leaf_at(cursor, Side::Before) + .filter(is_text) + .or_else(|| root.leaf_at(cursor, Side::After).filter(is_text))?; + let span = node.span(); for (i, page) in document.pages.iter().enumerate() { if let Some(pos) = find_in_frame(&page.frame, span) { diff --git a/crates/typst-ide/src/tooltip.rs b/crates/typst-ide/src/tooltip.rs index 2f04be87..3416e5f8 100644 --- a/crates/typst-ide/src/tooltip.rs +++ b/crates/typst-ide/src/tooltip.rs @@ -6,7 +6,7 @@ use typst::eval::{CapturesVisitor, Tracer}; use typst::foundations::{repr, Capturer, CastInfo, Repr, Value}; use typst::layout::Length; use typst::model::Document; -use typst::syntax::{ast, LinkedNode, Source, SyntaxKind}; +use typst::syntax::{ast, LinkedNode, Side, Source, SyntaxKind}; use typst::util::{round_2, Numeric}; use typst::World; @@ -23,8 +23,9 @@ pub fn tooltip( document: Option<&Document>, source: &Source, cursor: usize, + side: Side, ) -> Option<Tooltip> { - let leaf = LinkedNode::new(source.root()).leaf_at(cursor)?; + let leaf = LinkedNode::new(source.root()).leaf_at(cursor, side)?; if leaf.kind().is_trivia() { return None; } |
