diff --git a/src/syntax/parsing.rs b/src/syntax/parsing.rs index fc19d61e5..85a8880d7 100644 --- a/src/syntax/parsing.rs +++ b/src/syntax/parsing.rs @@ -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::>::into($lang).map(|s| Ident(s.to_string()))), + lang: $lang, block: lines.len() > 1, lines, }) }}; } + fn Lang<'a, T: Into>>(lang: T) -> Option> { + Some(Into::>::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")); diff --git a/src/syntax/tokens.rs b/src/syntax/tokens.rs index 23bad7c8e..0c37e9922 100644 --- a/src/syntax/tokens.rs +++ b/src/syntax/tokens.rs @@ -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>, + raw: &'a str, + terminated: bool, + ) -> Token<'a> { + Token::Code { lang, raw, terminated } + } + fn Lang<'a, T: Into>>(lang: T) -> Option> { + Some(Into::>::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("_`")); }