From d6e58ea961c679ca93fa1d085faa2e2248f5aea6 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin <35292584+Myriad-Dreamin@users.noreply.github.com> Date: Mon, 1 Apr 2024 10:35:48 +0800 Subject: [PATCH] fix: perform correct dynamic analysis on imports (#143) * dev: perform correct dynamic analysis on imports * dev: review e2e snapshot --- .../src/analysis/track_values.rs | 3 +- .../src/upstream/complete/ext.rs | 54 +++++++++---------- tests/e2e/main.rs | 2 +- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/crates/tinymist-query/src/analysis/track_values.rs b/crates/tinymist-query/src/analysis/track_values.rs index 3612b5d2..1f0efa05 100644 --- a/crates/tinymist-query/src/analysis/track_values.rs +++ b/crates/tinymist-query/src/analysis/track_values.rs @@ -49,6 +49,7 @@ pub fn analyze_expr(world: &dyn World, node: &LinkedNode) -> EcoVec<(Value, Opti /// Try to load a module from the current source file. pub fn analyze_import(world: &dyn World, source: &LinkedNode) -> Option { + let source_span = source.span(); let (source, _) = analyze_expr(world, source).into_iter().next()?; if source.scope().is_some() { return Some(source); @@ -72,7 +73,7 @@ pub fn analyze_import(world: &dyn World, source: &LinkedNode) -> Option { Scopes::new(Some(world.library())), Span::detached(), ); - typst::eval::import(&mut vm, source, Span::detached(), true) + typst::eval::import(&mut vm, source, source_span, true) .ok() .map(Value::Module) } diff --git a/crates/tinymist-query/src/upstream/complete/ext.rs b/crates/tinymist-query/src/upstream/complete/ext.rs index bf814b96..09ba61bb 100644 --- a/crates/tinymist-query/src/upstream/complete/ext.rs +++ b/crates/tinymist-query/src/upstream/complete/ext.rs @@ -27,36 +27,34 @@ impl<'a> CompletionContext<'a> { } } + // todo: cache if let Some(v) = node.cast::() { let imports = v.imports(); - match imports { - None | Some(ast::Imports::Wildcard) => { - if let Some(value) = node - .children() - .find(|child| child.is::()) - .and_then(|source| analyze_import(self.world, &source)) - { - if imports.is_none() { - // todo: correct kind - defined.extend( - value - .name() - .map(Into::into) - .map(|e| (e, CompletionKind::Variable)), - ); - } else if let Some(scope) = value.scope() { - for (name, _) in scope.iter() { - defined.insert(name.clone(), CompletionKind::Variable); - } - } - } - } - Some(ast::Imports::Items(items)) => { - for item in items.iter() { - defined.insert( - item.bound_name().get().clone(), - CompletionKind::Variable, - ); + let anaylyze = node.children().find(|child| child.is::()); + let analyzed = anaylyze + .as_ref() + .and_then(|source| analyze_import(self.world, source)); + if analyzed.is_none() { + log::info!("failed to analyze import: {:?}", anaylyze); + } + if let Some(value) = analyzed { + if imports.is_none() { + // todo: correct kind + defined.extend( + value + .name() + .map(Into::into) + .map(|e| (e, CompletionKind::Module)), + ); + } else if let Some(scope) = value.scope() { + for (name, v) in scope.iter() { + let kind = match v { + Value::Func(..) => CompletionKind::Func, + Value::Module(..) => CompletionKind::Module, + Value::Type(..) => CompletionKind::Type, + _ => CompletionKind::Constant, + }; + defined.insert(name.clone(), kind); } } } diff --git a/tests/e2e/main.rs b/tests/e2e/main.rs index dca32bda..05dd40e9 100644 --- a/tests/e2e/main.rs +++ b/tests/e2e/main.rs @@ -386,7 +386,7 @@ fn e2e() { }); let hash = replay_log(&tinymist_binary, &root.join("vscode")); - insta::assert_snapshot!(hash, @"siphash128_13:118679388281974949c659d7287827e2"); + insta::assert_snapshot!(hash, @"siphash128_13:ae577b1e07a6119b995d0d82df55a4b6"); } }