mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Fix autocomplete and jumps in math (#5849)
This commit is contained in:
parent
93fe02b457
commit
024bbb2b46
@ -306,7 +306,10 @@ fn complete_math(ctx: &mut CompletionContext) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Behind existing atom or identifier: "$a|$" or "$abc|$".
|
// Behind existing atom or identifier: "$a|$" or "$abc|$".
|
||||||
if matches!(ctx.leaf.kind(), SyntaxKind::Text | SyntaxKind::MathIdent) {
|
if matches!(
|
||||||
|
ctx.leaf.kind(),
|
||||||
|
SyntaxKind::Text | SyntaxKind::MathText | SyntaxKind::MathIdent
|
||||||
|
) {
|
||||||
ctx.from = ctx.leaf.offset();
|
ctx.from = ctx.leaf.offset();
|
||||||
math_completions(ctx);
|
math_completions(ctx);
|
||||||
return true;
|
return true;
|
||||||
@ -358,7 +361,7 @@ fn complete_field_accesses(ctx: &mut CompletionContext) -> bool {
|
|||||||
// Behind an expression plus dot: "emoji.|".
|
// Behind an expression plus dot: "emoji.|".
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if ctx.leaf.kind() == SyntaxKind::Dot
|
if ctx.leaf.kind() == SyntaxKind::Dot
|
||||||
|| (ctx.leaf.kind() == SyntaxKind::Text
|
|| (matches!(ctx.leaf.kind(), SyntaxKind::Text | SyntaxKind::MathText)
|
||||||
&& ctx.leaf.text() == ".");
|
&& ctx.leaf.text() == ".");
|
||||||
if ctx.leaf.range().end == ctx.cursor;
|
if ctx.leaf.range().end == ctx.cursor;
|
||||||
if let Some(prev) = ctx.leaf.prev_sibling();
|
if let Some(prev) = ctx.leaf.prev_sibling();
|
||||||
@ -1768,4 +1771,14 @@ mod tests {
|
|||||||
test("#show outline.entry: it => it.\n#outline()\n= Hi", 30)
|
test("#show outline.entry: it => it.\n#outline()\n= Hi", 30)
|
||||||
.must_include(["indented", "body", "page"]);
|
.must_include(["indented", "body", "page"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_autocomplete_symbol_variants() {
|
||||||
|
test("#sym.arrow.", -1)
|
||||||
|
.must_include(["r", "dashed"])
|
||||||
|
.must_exclude(["cases"]);
|
||||||
|
test("$ arrow. $", -3)
|
||||||
|
.must_include(["r", "dashed"])
|
||||||
|
.must_exclude(["cases"]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,10 @@ pub fn jump_from_click(
|
|||||||
let Some(id) = span.id() else { continue };
|
let Some(id) = span.id() else { continue };
|
||||||
let source = world.source(id).ok()?;
|
let source = world.source(id).ok()?;
|
||||||
let node = source.find(span)?;
|
let node = source.find(span)?;
|
||||||
let pos = if node.kind() == SyntaxKind::Text {
|
let pos = if matches!(
|
||||||
|
node.kind(),
|
||||||
|
SyntaxKind::Text | SyntaxKind::MathText
|
||||||
|
) {
|
||||||
let range = node.range();
|
let range = node.range();
|
||||||
let mut offset = range.start + usize::from(span_offset);
|
let mut offset = range.start + usize::from(span_offset);
|
||||||
if (click.x - pos.x) > width / 2.0 {
|
if (click.x - pos.x) > width / 2.0 {
|
||||||
@ -115,7 +118,7 @@ pub fn jump_from_cursor(
|
|||||||
cursor: usize,
|
cursor: usize,
|
||||||
) -> Vec<Position> {
|
) -> Vec<Position> {
|
||||||
fn is_text(node: &LinkedNode) -> bool {
|
fn is_text(node: &LinkedNode) -> bool {
|
||||||
node.get().kind() == SyntaxKind::Text
|
matches!(node.kind(), SyntaxKind::Text | SyntaxKind::MathText)
|
||||||
}
|
}
|
||||||
|
|
||||||
let root = LinkedNode::new(source.root());
|
let root = LinkedNode::new(source.root());
|
||||||
@ -261,6 +264,11 @@ mod tests {
|
|||||||
test_click(s, point(21.0, 12.0), cursor(56));
|
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]
|
#[test]
|
||||||
fn test_jump_from_cursor() {
|
fn test_jump_from_cursor() {
|
||||||
let s = "*Hello* #box[ABC] World";
|
let s = "*Hello* #box[ABC] World";
|
||||||
@ -268,6 +276,11 @@ mod tests {
|
|||||||
test_cursor(s, 14, pos(1, 37.55, 16.58));
|
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]
|
#[test]
|
||||||
fn test_backlink() {
|
fn test_backlink() {
|
||||||
let s = "#footnote[Hi]";
|
let s = "#footnote[Hi]";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user