mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Improve unknown variable diagnostics (#4858)
This commit is contained in:
parent
39b47060cc
commit
799eb8004e
@ -73,7 +73,12 @@ impl<'a> Scopes<'a> {
|
|||||||
None => None,
|
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.
|
/// Try to access a variable mutably.
|
||||||
@ -109,13 +114,28 @@ fn cannot_mutate_constant(var: &str) -> HintedString {
|
|||||||
fn unknown_variable(var: &str) -> HintedString {
|
fn unknown_variable(var: &str) -> HintedString {
|
||||||
let mut res = HintedString::new(eco_format!("unknown variable: {}", var));
|
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") {
|
if matches!(var, "none" | "auto" | "false" | "true") {
|
||||||
res.hint(eco_format!(
|
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!(
|
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}`",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,3 +95,8 @@ $ mat(
|
|||||||
,1, ;
|
,1, ;
|
||||||
, ,1;
|
, ,1;
|
||||||
) $
|
) $
|
||||||
|
|
||||||
|
--- issue-2885-math-var-only-in-global ---
|
||||||
|
// Error: 7-10 unknown variable: rgb
|
||||||
|
// Hint: 7-10 `rgb` is not available directly in math, try adding a hash before it: `#rgb`
|
||||||
|
$text(rgb(0, 0, 0), "foo")$
|
||||||
|
@ -414,7 +414,7 @@
|
|||||||
--- ops-assign-unknown-var-lhs ---
|
--- ops-assign-unknown-var-lhs ---
|
||||||
#{
|
#{
|
||||||
// Error: 3-6 unknown variable: a-1
|
// Error: 3-6 unknown variable: a-1
|
||||||
// Hint: 3-6 if you meant to use subtraction, try adding spaces around the minus sign
|
// Hint: 3-6 if you meant to use subtraction, try adding spaces around the minus sign: `a - 1`
|
||||||
a-1 = 2
|
a-1 = 2
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -425,7 +425,7 @@
|
|||||||
a = a -1
|
a = a -1
|
||||||
|
|
||||||
// Error: 7-10 unknown variable: a-1
|
// Error: 7-10 unknown variable: a-1
|
||||||
// Hint: 7-10 if you meant to use subtraction, try adding spaces around the minus sign
|
// Hint: 7-10 if you meant to use subtraction, try adding spaces around the minus sign: `a - 1`
|
||||||
a = a-1
|
a = a-1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user