mirror of
https://github.com/typst/typst
synced 2025-05-13 20:46:23 +08:00
Deduplicate and flexibilize code token & node building 🧺
This commit is contained in:
parent
862f1ccad8
commit
b2f3730013
@ -866,24 +866,20 @@ mod tests {
|
||||
}
|
||||
|
||||
macro_rules! C {
|
||||
(None, $($line:expr),* $(,)?) => {{
|
||||
($lang:expr, $($line:expr),* $(,)?) => {{
|
||||
let lines = vec![$($line.to_string()) ,*];
|
||||
SyntaxNode::Code(Code {
|
||||
lang: None,
|
||||
block: lines.len() > 1,
|
||||
lines,
|
||||
})
|
||||
}};
|
||||
(Some($lang:expr), $($line:expr),* $(,)?) => {{
|
||||
let lines = vec![$($line.to_string()) ,*];
|
||||
SyntaxNode::Code(Code {
|
||||
lang: Some(Into::<Spanned<&str>>::into($lang).map(|s| Ident(s.to_string()))),
|
||||
lang: $lang,
|
||||
block: lines.len() > 1,
|
||||
lines,
|
||||
})
|
||||
}};
|
||||
}
|
||||
|
||||
fn Lang<'a, T: Into<Spanned<&'a str>>>(lang: T) -> Option<Spanned<Ident>> {
|
||||
Some(Into::<Spanned<&str>>::into(lang).map(|s| Ident(s.to_string())))
|
||||
}
|
||||
|
||||
macro_rules! F {
|
||||
($($tts:tt)*) => { SyntaxNode::Call(Call!(@$($tts)*)) }
|
||||
}
|
||||
@ -1086,10 +1082,10 @@ mod tests {
|
||||
e!("`hi\nyou" => s(1,3, 1,3, "expected backtick"));
|
||||
t!("`hi\\`du`" => R!["hi`du"]);
|
||||
|
||||
t!("```java System.out.print```" => C![Some("java"), "System.out.print"]);
|
||||
ts!("```java out```" => s(0,0, 0,14, C![Lang(s(0,3, 0,7, "java")), "out"]));
|
||||
t!("``` console.log(\n\"alert\"\n)" => C![None, "console.log(", "\"alert\"", ")"]);
|
||||
t!("```typst \r\n Typst uses `\\`` to indicate code blocks" => C![
|
||||
Some("typst"), " Typst uses ``` to indicate code blocks"
|
||||
Lang("typst"), " Typst uses ``` to indicate code blocks"
|
||||
]);
|
||||
|
||||
e!("``` hi\nyou" => s(1,3, 1,3, "expected backticks"));
|
||||
|
@ -619,12 +619,15 @@ mod tests {
|
||||
fn Raw(raw: &str, terminated: bool) -> Token {
|
||||
Token::Raw { raw, terminated }
|
||||
}
|
||||
fn Code<'a>(lang: Option<&'a str>, raw: &'a str, terminated: bool) -> Token<'a> {
|
||||
Token::Code {
|
||||
lang: lang.map(Spanned::zero),
|
||||
raw,
|
||||
terminated,
|
||||
}
|
||||
fn Code<'a>(
|
||||
lang: Option<Spanned<&'a str>>,
|
||||
raw: &'a str,
|
||||
terminated: bool,
|
||||
) -> Token<'a> {
|
||||
Token::Code { lang, raw, terminated }
|
||||
}
|
||||
fn Lang<'a, T: Into<Spanned<&'a str>>>(lang: T) -> Option<Spanned<&'a str>> {
|
||||
Some(Into::<Spanned<&str>>::into(lang))
|
||||
}
|
||||
fn UE(sequence: &str, terminated: bool) -> Token {
|
||||
Token::UnicodeEscape { sequence, terminated }
|
||||
@ -684,12 +687,12 @@ mod tests {
|
||||
t!(Body, "#()" => Hashtag, T("()"));
|
||||
t!(Body, "`[func]`" => Raw("[func]", true));
|
||||
t!(Body, "`]" => Raw("]", false));
|
||||
t!(Body, "\\ " => Backslash, S(0));
|
||||
t!(Body, "`\\``" => Raw("\\`", true));
|
||||
t!(Body, "``not code`" => Raw("", true), T("not"), S(0), T("code"), Raw("", false));
|
||||
t!(Body, "```rust hi```" => Code(Some("rust"), "hi", true));
|
||||
t!(Body, "```rust hi```" => Code(Lang("rust"), "hi", true));
|
||||
t!(Body, "``` hi`\\``" => Code(None, "hi`\\``", false));
|
||||
t!(Body, "```js \r\n document.write(\"go\")" => Code(Some("js"), " document.write(\"go\")", false));
|
||||
t!(Body, "\\ " => Backslash, S(0));
|
||||
t!(Body, "```js \r\n document.write(\"go\")" => Code(Lang("js"), " document.write(\"go\")", false));
|
||||
t!(Header, "_`" => Invalid("_`"));
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user