From 1b10d19d76e2ddf09a63d00a6fc56556d2bbfe08 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin <35292584+Myriad-Dreamin@users.noreply.github.com> Date: Mon, 16 Dec 2024 22:09:38 +0800 Subject: [PATCH] Consider parameters when iterating items in scope (#5517) --- crates/typst-ide/src/matchers.rs | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/crates/typst-ide/src/matchers.rs b/crates/typst-ide/src/matchers.rs index 4aeba29be..18262f701 100644 --- a/crates/typst-ide/src/matchers.rs +++ b/crates/typst-ide/src/matchers.rs @@ -123,6 +123,36 @@ pub fn named_items( } } + if let Some(v) = parent.cast::().filter(|v| { + // Check if the node is in the body of the closure. + let body = parent.find(v.body().span()); + body.is_some_and(|n| n.find(node.span()).is_some()) + }) { + for param in v.params().children() { + match param { + ast::Param::Pos(pattern) => { + for ident in pattern.bindings() { + if let Some(t) = recv(NamedItem::Var(ident)) { + return Some(t); + } + } + } + ast::Param::Named(n) => { + if let Some(t) = recv(NamedItem::Var(n.name())) { + return Some(t); + } + } + ast::Param::Spread(s) => { + if let Some(sink_ident) = s.sink_ident() { + if let Some(t) = recv(NamedItem::Var(sink_ident)) { + return Some(t); + } + } + } + } + } + } + ancestor = Some(parent.clone()); continue; } @@ -269,6 +299,17 @@ mod tests { assert!(!has_named_items(r#"#let a = 1;#let b = 2;"#, 8, "b")); } + #[test] + fn test_param_named_items() { + // Has named items + assert!(has_named_items(r#"#let f(a) = 1;#let b = 2;"#, 12, "a")); + assert!(has_named_items(r#"#let f(a: b) = 1;#let b = 2;"#, 15, "a")); + + // Doesn't have named items + assert!(!has_named_items(r#"#let f(a) = 1;#let b = 2;"#, 19, "a")); + assert!(!has_named_items(r#"#let f(a: b) = 1;#let b = 2;"#, 15, "b")); + } + #[test] fn test_import_named_items() { // Cannot test much.