summaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authortingerrr <me@tinger.dev>2024-09-02 14:45:50 +0200
committerGitHub <noreply@github.com>2024-09-02 12:45:50 +0000
commit799eb8004eeafd758ed53c79c4e1ce34afb268dd (patch)
tree8744b8eedf9edba27bb876d6b019c42dc941d8f9 /crates
parent39b47060ccb214290d49d9767372231877e2921f (diff)
Improve unknown variable diagnostics (#4858)
Diffstat (limited to 'crates')
-rw-r--r--crates/typst/src/foundations/scope.rs28
1 files changed, 24 insertions, 4 deletions
diff --git a/crates/typst/src/foundations/scope.rs b/crates/typst/src/foundations/scope.rs
index b118540e..b64c478c 100644
--- a/crates/typst/src/foundations/scope.rs
+++ b/crates/typst/src/foundations/scope.rs
@@ -73,7 +73,12 @@ impl<'a> Scopes<'a> {
None => None,
})
})
- .ok_or_else(|| unknown_variable(var))
+ .ok_or_else(|| {
+ unknown_variable_math(
+ var,
+ self.base.is_some_and(|base| base.global.scope().get(var).is_some()),
+ )
+ })
}
/// Try to access a variable mutably.
@@ -109,13 +114,28 @@ fn cannot_mutate_constant(var: &str) -> HintedString {
fn unknown_variable(var: &str) -> HintedString {
let mut res = HintedString::new(eco_format!("unknown variable: {}", var));
+ if var.contains('-') {
+ res.hint(eco_format!(
+ "if you meant to use subtraction, try adding spaces around the minus sign{}: `{}`",
+ if var.matches('-').count() > 1 { "s" } else { "" },
+ var.replace('-', " - ")
+ ));
+ }
+
+ res
+}
+
+#[cold]
+fn unknown_variable_math(var: &str, in_global: bool) -> HintedString {
+ let mut res = HintedString::new(eco_format!("unknown variable: {}", var));
+
if matches!(var, "none" | "auto" | "false" | "true") {
res.hint(eco_format!(
- "if you meant to use a literal, try adding a hash before it"
+ "if you meant to use a literal, try adding a hash before it: `#{var}`",
));
- } else if var.contains('-') {
+ } else if in_global {
res.hint(eco_format!(
- "if you meant to use subtraction, try adding spaces around the minus sign",
+ "`{var}` is not available directly in math, try adding a hash before it: `#{var}`",
));
}