From 45c866fbb947715cfee1937c85d6d6d6f393d7a8 Mon Sep 17 00:00:00 2001 From: Yifan Song <41675306+Eric-Song-Nop@users.noreply.github.com> Date: Wed, 18 Dec 2024 19:11:13 +0100 Subject: [PATCH] Fix autocompletion of half-completed import item (#5531) Co-authored-by: Laurenz --- crates/typst-ide/src/complete.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/typst-ide/src/complete.rs b/crates/typst-ide/src/complete.rs index 5c2b500a0..c731165d7 100644 --- a/crates/typst-ide/src/complete.rs +++ b/crates/typst-ide/src/complete.rs @@ -521,11 +521,13 @@ fn complete_imports(ctx: &mut CompletionContext) -> bool { if_chain! { if ctx.leaf.kind() == SyntaxKind::Ident; if let Some(parent) = ctx.leaf.parent(); - if parent.kind() == SyntaxKind::ImportItems; + if parent.kind() == SyntaxKind::ImportItemPath; if let Some(grand) = parent.parent(); - if let Some(ast::Expr::Import(import)) = grand.get().cast(); + if grand.kind() == SyntaxKind::ImportItems; + if let Some(great) = grand.parent(); + if let Some(ast::Expr::Import(import)) = great.get().cast(); if let Some(ast::Imports::Items(items)) = import.imports(); - if let Some(source) = grand.children().find(|child| child.is::()); + if let Some(source) = great.children().find(|child| child.is::()); then { ctx.from = ctx.leaf.offset(); import_item_completions(ctx, items, &source); @@ -1743,4 +1745,18 @@ mod tests { test("#figure(cap)", -1).must_apply("caption", "caption: [${}]"); } + + #[test] + fn test_autocomplete_import_items() { + let world = TestWorld::new("#import \"other.typ\": ") + .with_source("second.typ", "#import \"other.typ\": th") + .with_source("other.typ", "#let this = 1; #let that = 2"); + + test_with_path(&world, "main.typ", 21) + .must_include(["*", "this", "that"]) + .must_exclude(["figure"]); + test_with_path(&world, "second.typ", 23) + .must_include(["this", "that"]) + .must_exclude(["*", "figure"]); + } }