diff --git a/crates/typst-syntax/src/lexer.rs b/crates/typst-syntax/src/lexer.rs index 97aa94d2b..6719deafd 100644 --- a/crates/typst-syntax/src/lexer.rs +++ b/crates/typst-syntax/src/lexer.rs @@ -297,16 +297,12 @@ impl Lexer<'_> { self.push_raw(SyntaxKind::RawLang); } - // Determine inner content between backticks and with trimmed - // single spaces (line trimming comes later). + // Determine inner content between backticks. self.s.eat_if(' '); - let mut inner = self.s.to(end - backticks); - if inner.trim_end().ends_with('`') { - inner = inner.strip_suffix(' ').unwrap_or(inner); - } + let inner = self.s.to(end - backticks); // Determine dedent level. - let lines = split_newlines(inner); + let mut lines = split_newlines(inner); let dedent = lines .iter() .skip(1) @@ -317,6 +313,15 @@ impl Lexer<'_> { .min() .unwrap_or(0); + // Trim single space in last line if text ends with a backtick. The last + // line is the one directly before the closing backticks and if it is + // just whitespace, it will be completely trimmed below. + if inner.trim_end().ends_with('`') { + if let Some(last) = lines.last_mut() { + *last = last.strip_suffix(' ').unwrap_or(last); + } + } + let is_whitespace = |line: &&str| line.chars().all(char::is_whitespace); let starts_whitespace = lines.first().is_some_and(is_whitespace); let ends_whitespace = lines.last().is_some_and(is_whitespace); diff --git a/tests/ref/issue-3820-raw-space-when-end-with-backtick.png b/tests/ref/issue-3820-raw-space-when-end-with-backtick.png new file mode 100644 index 000000000..1ba3fb182 Binary files /dev/null and b/tests/ref/issue-3820-raw-space-when-end-with-backtick.png differ diff --git a/tests/suite/text/raw.typ b/tests/suite/text/raw.typ index dce77fdb5..6a3ea6bd7 100644 --- a/tests/suite/text/raw.typ +++ b/tests/suite/text/raw.typ @@ -624,7 +624,20 @@ fn main() { ~~~~The~spaces~on~this~line~shrink +--- issue-3820-raw-space-when-end-with-backtick --- +```typ +`code` +``` + + ```typ + `code` + ``` + --- raw-unclosed --- -// Unterminated. +// Test unterminated raw text. +// +// Note: This test should be the final one in the file because it messes up +// syntax highlighting. +// // Error: 1-2:1 unclosed raw text `endless