hint for unknown variable containing dash (#924)

This commit is contained in:
Marmare314 2023-04-24 19:08:07 +02:00 committed by GitHub
parent 83c11f1ee4
commit 12129f0170
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 3 deletions

View File

@ -42,7 +42,7 @@ impl<'a> Scopes<'a> {
.chain(self.scopes.iter().rev())
.chain(self.base.map(|base| base.global.scope()))
.find_map(|scope| scope.get(var))
.ok_or_else(|| eco_format!("unknown variable: {}", var))
.ok_or_else(|| unknown_variable(var))
}
/// Try to access a variable immutably in math.
@ -62,12 +62,22 @@ impl<'a> Scopes<'a> {
.ok_or_else(|| {
match self.base.and_then(|base| base.global.scope().get(var)) {
Some(_) => eco_format!("cannot mutate a constant: {}", var),
_ => eco_format!("unknown variable: {}", var),
_ => unknown_variable(var),
}
})?
}
}
/// The error message when a variable is not found.
#[cold]
fn unknown_variable(var: &str) -> EcoString {
if var.contains('-') {
eco_format!("unknown variable: {} if you meant to use subtraction, try adding spaces around the minus sign.", var)
} else {
eco_format!("unknown variable: {}", var)
}
}
/// A map from binding names to values.
#[derive(Default, Clone, Hash)]
pub struct Scope(BTreeMap<EcoString, Slot>, bool);

View File

@ -4,3 +4,19 @@
---
// Error: 1:17-1:19 expected length, found integer: a length needs a unit did you mean 12pt?
#set text(size: 12)
---
#{
let a = 2
a = 1-a
a = a -1
// Error: 7-10 unknown variable: a-1 if you meant to use subtraction, try adding spaces around the minus sign.
a = a-1
}
---
#{
// Error: 3-6 unknown variable: a-1 if you meant to use subtraction, try adding spaces around the minus sign.
a-1 = 2
}