summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2025-01-23 11:16:04 +0100
committerGitHub <noreply@github.com>2025-01-23 10:16:04 +0000
commit1bd8ff0e0fa7966f4bd2a4426241781bed168df7 (patch)
treed3013460b54663cb803b0d5702c1bbfd4aab0f63
parent6fcc4322845482c1810c26ee7f6fc8f6fed20d7d (diff)
Methods on elements (#5733)
-rw-r--r--crates/typst-eval/src/call.rs24
-rw-r--r--tests/suite/scripting/methods.typ2
2 files changed, 23 insertions, 3 deletions
diff --git a/crates/typst-eval/src/call.rs b/crates/typst-eval/src/call.rs
index 0a9e1c48..69b274bb 100644
--- a/crates/typst-eval/src/call.rs
+++ b/crates/typst-eval/src/call.rs
@@ -325,6 +325,13 @@ fn eval_field_call(
} else if let Some(callee) = target.ty().scope().get(&field) {
args.insert(0, target_expr.span(), target);
Ok(FieldCall::Normal(callee.clone(), args))
+ } else if let Value::Content(content) = &target {
+ if let Some(callee) = content.elem().scope().get(&field) {
+ args.insert(0, target_expr.span(), target);
+ Ok(FieldCall::Normal(callee.clone(), args))
+ } else {
+ bail!(missing_field_call_error(target, field))
+ }
} else if matches!(
target,
Value::Symbol(_) | Value::Func(_) | Value::Type(_) | Value::Module(_)
@@ -341,8 +348,20 @@ fn eval_field_call(
/// Produce an error when we cannot call the field.
fn missing_field_call_error(target: Value, field: Ident) -> SourceDiagnostic {
- let mut error =
- error!(field.span(), "type {} has no method `{}`", target.ty(), field.as_str());
+ let mut error = match &target {
+ Value::Content(content) => error!(
+ field.span(),
+ "element {} has no method `{}`",
+ content.elem().name(),
+ field.as_str(),
+ ),
+ _ => error!(
+ field.span(),
+ "type {} has no method `{}`",
+ target.ty(),
+ field.as_str()
+ ),
+ };
match target {
Value::Dict(ref dict) if matches!(dict.get(&field), Ok(Value::Func(_))) => {
@@ -360,6 +379,7 @@ fn missing_field_call_error(target: Value, field: Ident) -> SourceDiagnostic {
}
_ => {}
}
+
error
}
diff --git a/tests/suite/scripting/methods.typ b/tests/suite/scripting/methods.typ
index 5deea2cf..566e9d9a 100644
--- a/tests/suite/scripting/methods.typ
+++ b/tests/suite/scripting/methods.typ
@@ -31,7 +31,7 @@
#numbers.fun()
--- method-unknown-but-field-exists ---
-// Error: 2:4-2:10 type content has no method `stroke`
+// Error: 2:4-2:10 element line has no method `stroke`
// Hint: 2:4-2:10 did you mean to access the field `stroke`?
#let l = line(stroke: red)
#l.stroke()