diff options
| author | Laurenz <laurmaedje@gmail.com> | 2024-07-11 16:24:28 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-11 14:24:28 +0000 |
| commit | be516867c834d1172e17b3b2b25d70428c3d918b (patch) | |
| tree | 7a930e38671d4b2bec7e26c756e143e20960e2be /crates/typst-ide/src | |
| parent | 36042ff222d70349f4c459384790ec40e1103bf9 (diff) | |
Spans for cross-file go-to-definition (#4539)
Diffstat (limited to 'crates/typst-ide/src')
| -rw-r--r-- | crates/typst-ide/src/analyze.rs | 2 | ||||
| -rw-r--r-- | crates/typst-ide/src/complete.rs | 8 | ||||
| -rw-r--r-- | crates/typst-ide/src/definition.rs | 14 | ||||
| -rw-r--r-- | crates/typst-ide/src/matchers.rs | 20 | ||||
| -rw-r--r-- | crates/typst-ide/src/tooltip.rs | 2 |
5 files changed, 24 insertions, 22 deletions
diff --git a/crates/typst-ide/src/analyze.rs b/crates/typst-ide/src/analyze.rs index ecb73dce..c3779556 100644 --- a/crates/typst-ide/src/analyze.rs +++ b/crates/typst-ide/src/analyze.rs @@ -49,7 +49,6 @@ pub fn analyze_expr( pub fn analyze_import(world: &dyn World, source: &LinkedNode) -> Option<Value> { // Use span in the node for resolving imports with relative paths. let source_span = source.span(); - let (source, _) = analyze_expr(world, source).into_iter().next()?; if source.scope().is_some() { return Some(source); @@ -73,6 +72,7 @@ pub fn analyze_import(world: &dyn World, source: &LinkedNode) -> Option<Value> { Scopes::new(Some(world.library())), Span::detached(), ); + typst::eval::import(&mut vm, source, source_span, true) .ok() .map(Value::Module) diff --git a/crates/typst-ide/src/complete.rs b/crates/typst-ide/src/complete.rs index c4f86d04..f6c96d00 100644 --- a/crates/typst-ide/src/complete.rs +++ b/crates/typst-ide/src/complete.rs @@ -386,12 +386,12 @@ fn field_access_completions( value: &Value, styles: &Option<Styles>, ) { - for (name, value) in value.ty().scope().iter() { + for (name, value, _) in value.ty().scope().iter() { ctx.value_completion(Some(name.clone()), value, true, None); } if let Some(scope) = value.scope() { - for (name, value) in scope.iter() { + for (name, value, _) in scope.iter() { ctx.value_completion(Some(name.clone()), value, true, None); } } @@ -557,7 +557,7 @@ fn import_item_completions<'a>( ctx.snippet_completion("*", "*", "Import everything."); } - for (name, value) in scope.iter() { + for (name, value, _) in scope.iter() { if existing.iter().all(|item| item.original_name().as_str() != name) { ctx.value_completion(Some(name.clone()), value, false, None); } @@ -1345,7 +1345,7 @@ impl<'a> CompletionContext<'a> { ); let scope = if in_math { self.math } else { self.global }; - for (name, value) in scope.iter() { + for (name, value, _) in scope.iter() { if filter(value) && !defined.contains(name) { self.value_completion(Some(name.clone()), value, parens, None); } diff --git a/crates/typst-ide/src/definition.rs b/crates/typst-ide/src/definition.rs index 45262781..4323226d 100644 --- a/crates/typst-ide/src/definition.rs +++ b/crates/typst-ide/src/definition.rs @@ -22,9 +22,7 @@ pub fn definition( let root = LinkedNode::new(source.root()); let leaf = root.leaf_at(cursor, side)?; - let target = deref_target(leaf.clone())?; - - let mut use_site = match target { + let mut use_site = match deref_target(leaf.clone())? { DerefTarget::VarAccess(node) | DerefTarget::Callee(node) => node, DerefTarget::IncludePath(path) | DerefTarget::ImportPath(path) => { let import_item = @@ -82,10 +80,10 @@ pub fn definition( .then_some(site.span()) .unwrap_or_else(Span::detached), )), - NamedItem::Import(name, span, value) => Some(Definition::item( + NamedItem::Import(name, name_span, value) => Some(Definition::item( name.clone(), Span::detached(), - span, + name_span, value.cloned(), )), } @@ -99,14 +97,14 @@ pub fn definition( | Some(SyntaxKind::MathFrac) | Some(SyntaxKind::MathAttach) ); - let library = world.library(); + let library = world.library(); let scope = if in_math { library.math.scope() } else { library.global.scope() }; - for (item_name, value) in scope.iter() { + for (item_name, value, span) in scope.iter() { if *item_name == name { return Some(Definition::item( name, - Span::detached(), + span, Span::detached(), Some(value.clone()), )); diff --git a/crates/typst-ide/src/matchers.rs b/crates/typst-ide/src/matchers.rs index 757e5ab6..1daec819 100644 --- a/crates/typst-ide/src/matchers.rs +++ b/crates/typst-ide/src/matchers.rs @@ -77,12 +77,8 @@ pub fn named_items<T>( // ``` Some(ast::Imports::Wildcard) => { if let Some(scope) = source.and_then(|(value, _)| value.scope()) { - for (name, value) in scope.iter() { - let item = NamedItem::Import( - name, - Span::detached(), - Some(value), - ); + for (name, value, span) in scope.iter() { + let item = NamedItem::Import(name, span, Some(value)); if let Some(res) = recv(item) { return Some(res); } @@ -94,9 +90,17 @@ pub fn named_items<T>( // ``` Some(ast::Imports::Items(items)) => { for item in items.iter() { - let name = item.bound_name(); + let original = item.original_name(); + let bound = item.bound_name(); + let scope = source.and_then(|(value, _)| value.scope()); + let span = scope + .and_then(|s| s.get_span(&original)) + .unwrap_or(Span::detached()) + .or(bound.span()); + + let value = scope.and_then(|s| s.get(&original)); if let Some(res) = - recv(NamedItem::Import(name.get(), name.span(), None)) + recv(NamedItem::Import(bound.get(), span, value)) { return Some(res); } diff --git a/crates/typst-ide/src/tooltip.rs b/crates/typst-ide/src/tooltip.rs index c78c02d8..02fb3ec6 100644 --- a/crates/typst-ide/src/tooltip.rs +++ b/crates/typst-ide/src/tooltip.rs @@ -126,7 +126,7 @@ fn closure_tooltip(leaf: &LinkedNode) -> Option<Tooltip> { let captures = visitor.finish(); let mut names: Vec<_> = - captures.iter().map(|(name, _)| eco_format!("`{name}`")).collect(); + captures.iter().map(|(name, ..)| eco_format!("`{name}`")).collect(); if names.is_empty() { return None; } |
