summaryrefslogtreecommitdiff
path: root/crates/typst-ide/src/definition.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/typst-ide/src/definition.rs')
-rw-r--r--crates/typst-ide/src/definition.rs356
1 files changed, 143 insertions, 213 deletions
diff --git a/crates/typst-ide/src/definition.rs b/crates/typst-ide/src/definition.rs
index a8286554..94def1c1 100644
--- a/crates/typst-ide/src/definition.rs
+++ b/crates/typst-ide/src/definition.rs
@@ -1,13 +1,22 @@
-use ecow::EcoString;
-use typst::foundations::{Label, Module, Selector, Value};
+use typst::foundations::{Label, Selector, Value};
use typst::model::Document;
-use typst::syntax::ast::AstNode;
-use typst::syntax::{ast, LinkedNode, Side, Source, Span, SyntaxKind};
+use typst::syntax::{ast, LinkedNode, Side, Source, Span};
+use crate::utils::globals;
use crate::{
- analyze_import, deref_target, named_items, DerefTarget, IdeWorld, NamedItem,
+ analyze_expr, analyze_import, deref_target, named_items, DerefTarget, IdeWorld,
+ NamedItem,
};
+/// A definition of some item.
+#[derive(Debug, Clone)]
+pub enum Definition {
+ /// The item is defined at the given span.
+ Span(Span),
+ /// The item is defined in the standard library.
+ Std(Value),
+}
+
/// Find the definition of the item under the cursor.
///
/// Passing a `document` (from a previous compilation) is optional, but enhances
@@ -23,241 +32,162 @@ pub fn definition(
let root = LinkedNode::new(source.root());
let leaf = root.leaf_at(cursor, side)?;
- 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 =
- analyze_import(world, &path).and_then(|v| v.cast::<Module>().ok())?;
- return Some(Definition::module(&import_item, path.span(), Span::detached()));
+ match deref_target(leaf.clone())? {
+ // Try to find a named item (defined in this file or an imported file)
+ // or fall back to a standard library item.
+ DerefTarget::VarAccess(node) | DerefTarget::Callee(node) => {
+ let name = node.cast::<ast::Ident>()?.get().clone();
+ if let Some(src) = named_items(world, node.clone(), |item: NamedItem| {
+ (*item.name() == name).then(|| Definition::Span(item.span()))
+ }) {
+ return Some(src);
+ };
+
+ if let Some((value, _)) = analyze_expr(world, &node).first() {
+ let span = match value {
+ Value::Content(content) => content.span(),
+ Value::Func(func) => func.span(),
+ _ => Span::detached(),
+ };
+ if !span.is_detached() && span != node.span() {
+ return Some(Definition::Span(span));
+ }
+ }
+
+ if let Some(value) = globals(world, &leaf).get(&name) {
+ return Some(Definition::Std(value.clone()));
+ }
}
- DerefTarget::Ref(r) => {
- let label = Label::new(r.cast::<ast::Ref>()?.target());
- let sel = Selector::Label(label);
- let elem = document?.introspector.query_first(&sel)?;
- let span = elem.span();
- return Some(Definition {
- kind: DefinitionKind::Label,
- name: label.as_str().into(),
- value: Some(Value::Label(label)),
- span,
- name_span: Span::detached(),
- });
+
+ // Try to jump to the an imported file or package.
+ DerefTarget::ImportPath(node) | DerefTarget::IncludePath(node) => {
+ let Some(Value::Module(module)) = analyze_import(world, &node) else {
+ return None;
+ };
+ let id = module.file_id()?;
+ let span = Span::from_range(id, 0..0);
+ return Some(Definition::Span(span));
}
- DerefTarget::Label(..) | DerefTarget::Code(..) => {
- return None;
+
+ // Try to jump to the referenced content.
+ DerefTarget::Ref(node) => {
+ let label = Label::new(node.cast::<ast::Ref>()?.target());
+ let selector = Selector::Label(label);
+ let elem = document?.introspector.query_first(&selector)?;
+ return Some(Definition::Span(elem.span()));
}
- };
- let mut has_path = false;
- while let Some(node) = use_site.cast::<ast::FieldAccess>() {
- has_path = true;
- use_site = use_site.find(node.target().span())?;
+ _ => {}
}
- let name = use_site.cast::<ast::Ident>()?.get().clone();
- let src = named_items(world, use_site, |item: NamedItem| {
- if *item.name() != name {
- return None;
- }
+ None
+}
- match item {
- NamedItem::Var(name) => {
- let name_span = name.span();
- let span = find_let_binding(source, name_span);
- Some(Definition::item(name.get().clone(), span, name_span, None))
- }
- NamedItem::Fn(name) => {
- let name_span = name.span();
- let span = find_let_binding(source, name_span);
- Some(
- Definition::item(name.get().clone(), span, name_span, None)
- .with_kind(DefinitionKind::Function),
- )
- }
- NamedItem::Module(item, site) => Some(Definition::module(
- item,
- site.span(),
- matches!(site.kind(), SyntaxKind::Ident)
- .then_some(site.span())
- .unwrap_or_else(Span::detached),
- )),
- NamedItem::Import(name, name_span, value) => Some(Definition::item(
- name.clone(),
- Span::detached(),
- name_span,
- value.cloned(),
- )),
- }
- });
+#[cfg(test)]
+mod tests {
+ use std::ops::Range;
- let src = src.or_else(|| {
- let in_math = matches!(
- leaf.parent_kind(),
- Some(SyntaxKind::Equation)
- | Some(SyntaxKind::Math)
- | Some(SyntaxKind::MathFrac)
- | Some(SyntaxKind::MathAttach)
- );
+ use typst::foundations::{IntoValue, NativeElement};
+ use typst::syntax::Side;
+ use typst::WorldExt;
- let library = world.library();
- let scope = if in_math { library.math.scope() } else { library.global.scope() };
- for (item_name, value, span) in scope.iter() {
- if *item_name == name {
- return Some(Definition::item(
- name,
- span,
- Span::detached(),
- Some(value.clone()),
- ));
- }
- }
+ use super::{definition, Definition};
+ use crate::tests::{SourceExt, TestWorld};
- None
- })?;
+ type Response = (TestWorld, Option<Definition>);
- (!has_path).then_some(src)
-}
+ trait ResponseExt {
+ fn must_be_at(&self, path: &str, range: Range<usize>) -> &Self;
+ fn must_be_value(&self, value: impl IntoValue) -> &Self;
+ }
-/// A definition of some item.
-#[derive(Debug, Clone)]
-pub struct Definition {
- /// The name of the definition.
- pub name: EcoString,
- /// The kind of the definition.
- pub kind: DefinitionKind,
- /// An instance of the definition, if available.
- pub value: Option<Value>,
- /// The source span of the entire definition. May be detached if unknown.
- pub span: Span,
- /// The span of the definition's name. May be detached if unknown.
- pub name_span: Span,
-}
+ impl ResponseExt for Response {
+ #[track_caller]
+ fn must_be_at(&self, path: &str, expected: Range<usize>) -> &Self {
+ match self.1 {
+ Some(Definition::Span(span)) => {
+ let range = self.0.range(span);
+ assert_eq!(
+ span.id().unwrap().vpath().as_rootless_path().to_string_lossy(),
+ path
+ );
+ assert_eq!(range, Some(expected));
+ }
+ _ => panic!("expected span definition"),
+ }
+ self
+ }
-impl Definition {
- fn item(name: EcoString, span: Span, name_span: Span, value: Option<Value>) -> Self {
- Self {
- name,
- kind: match value {
- Some(Value::Func(_)) => DefinitionKind::Function,
- _ => DefinitionKind::Variable,
- },
- value,
- span,
- name_span,
+ #[track_caller]
+ fn must_be_value(&self, expected: impl IntoValue) -> &Self {
+ match &self.1 {
+ Some(Definition::Std(value)) => {
+ assert_eq!(*value, expected.into_value())
+ }
+ _ => panic!("expected std definition"),
+ }
+ self
}
}
- fn module(module: &Module, span: Span, name_span: Span) -> Self {
- Definition {
- name: module.name().clone(),
- kind: DefinitionKind::Module,
- value: Some(Value::Module(module.clone())),
- span,
- name_span,
- }
+ #[track_caller]
+ fn test(text: &str, cursor: isize, side: Side) -> Response {
+ let world = TestWorld::new(text);
+ test_with_world(world, cursor, side)
}
- fn with_kind(self, kind: DefinitionKind) -> Self {
- Self { kind, ..self }
+ #[track_caller]
+ fn test_with_world(world: TestWorld, cursor: isize, side: Side) -> Response {
+ let doc = typst::compile(&world).output.ok();
+ let source = &world.main;
+ let def = definition(&world, doc.as_ref(), source, source.cursor(cursor), side);
+ (world, def)
}
-}
-/// A kind of item that is definition.
-#[derive(Debug, Clone, PartialEq, Hash)]
-pub enum DefinitionKind {
- /// ```plain
- /// let foo;
- /// ^^^^^^^^ span
- /// ^^^ name_span
- /// ```
- Variable,
- /// ```plain
- /// let foo(it) = it;
- /// ^^^^^^^^^^^^^^^^^ span
- /// ^^^ name_span
- /// ```
- Function,
- /// Case 1
- /// ```plain
- /// import "foo.typ": *
- /// ^^^^^^^^^ span
- /// name_span is detached
- /// ```
- ///
- /// Case 2
- /// ```plain
- /// import "foo.typ" as bar: *
- /// span ^^^
- /// name_span ^^^
- /// ```
- Module,
- /// ```plain
- /// <foo>
- /// ^^^^^ span
- /// name_span is detached
- /// ```
- Label,
-}
+ #[test]
+ fn test_definition_let() {
+ test("#let x; #x", 9, Side::After).must_be_at("main.typ", 5..6);
+ test("#let x() = {}; #x", 16, Side::After).must_be_at("main.typ", 5..6);
+ }
-fn find_let_binding(source: &Source, name_span: Span) -> Span {
- let node = LinkedNode::new(source.root());
- std::iter::successors(node.find(name_span).as_ref(), |n| n.parent())
- .find(|n| matches!(n.kind(), SyntaxKind::LetBinding))
- .map(|s| s.span())
- .unwrap_or_else(Span::detached)
-}
+ #[test]
+ fn test_definition_field_access_function() {
+ let world = TestWorld::new("#import \"other.typ\"; #other.foo")
+ .with_source("other.typ", "#let foo(x) = x + 1");
-#[cfg(test)]
-mod tests {
- use std::ops::Range;
+ // The span is at the args here because that's what the function value's
+ // span is. Not ideal, but also not too big of a big deal.
+ test_with_world(world, -1, Side::Before).must_be_at("other.typ", 8..11);
+ }
- use typst::foundations::{IntoValue, Label, NativeElement, Value};
- use typst::syntax::Side;
- use typst::WorldExt;
+ #[test]
+ fn test_definition_cross_file() {
+ let world = TestWorld::new("#import \"other.typ\": x; #x")
+ .with_source("other.typ", "#let x = 1");
+ test_with_world(world, -1, Side::After).must_be_at("other.typ", 5..6);
+ }
+
+ #[test]
+ fn test_definition_import() {
+ let world = TestWorld::new("#import \"other.typ\" as o: x")
+ .with_source("other.typ", "#let x = 1");
+ test_with_world(world, 14, Side::Before).must_be_at("other.typ", 0..0);
+ }
- use super::{definition, DefinitionKind as Kind};
- use crate::tests::TestWorld;
+ #[test]
+ fn test_definition_include() {
+ let world = TestWorld::new("#include \"other.typ\"")
+ .with_source("other.typ", "Hello there");
+ test_with_world(world, 14, Side::Before).must_be_at("other.typ", 0..0);
+ }
- #[track_caller]
- fn test<T>(
- text: &str,
- cursor: usize,
- name: &str,
- kind: Kind,
- value: Option<T>,
- range: Option<Range<usize>>,
- ) where
- T: IntoValue,
- {
- let world = TestWorld::new(text);
- let doc = typst::compile(&world).output.ok();
- let actual = definition(&world, doc.as_ref(), &world.main, cursor, Side::After)
- .map(|d| (d.kind, d.name, world.range(d.span), d.value));
- assert_eq!(
- actual,
- Some((kind, name.into(), range, value.map(IntoValue::into_value)))
- );
+ #[test]
+ fn test_definition_ref() {
+ test("#figure[] <hi> See @hi", 21, Side::After).must_be_at("main.typ", 1..9);
}
#[test]
- fn test_definition() {
- test("#let x; #x", 9, "x", Kind::Variable, None::<Value>, Some(1..6));
- test("#let x() = {}; #x", 16, "x", Kind::Function, None::<Value>, Some(1..13));
- test(
- "#table",
- 1,
- "table",
- Kind::Function,
- Some(typst::model::TableElem::elem()),
- None,
- );
- test(
- "#figure[] <hi> See @hi",
- 21,
- "hi",
- Kind::Label,
- Some(Label::new("hi")),
- Some(1..9),
- );
+ fn test_definition_std() {
+ test("#table", 1, Side::After).must_be_value(typst::model::TableElem::elem());
}
}