From 4ae376f2c7ba9621479640afbe45936c6c859cff Mon Sep 17 00:00:00 2001 From: Wenzhuo Liu Date: Fri, 17 May 2024 16:35:51 +0800 Subject: [PATCH] Fix raw block has extra space when end with backtick (#4162) Co-authored-by: Laurenz --- crates/typst-syntax/src/lexer.rs | 19 +++++++++++------- ...-3820-raw-space-when-end-with-backtick.png | Bin 0 -> 351 bytes tests/suite/text/raw.typ | 15 +++++++++++++- 3 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 tests/ref/issue-3820-raw-space-when-end-with-backtick.png 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 0000000000000000000000000000000000000000..1ba3fb182ee428cebac1edda1a99578f9bc54435 GIT binary patch literal 351 zcmV-l0igbgP);y3{%=H8F z!S_Y@qaqsCu!jE?+-7y1McD8KOLYYR^hzPae11Q>1&w;Z?6EGG)jEKyV8-sIb3@rW zEn?m!MUbdvo&g2$!#NO>7=Da~=&qG>1vRj93~Wso(ZIkFg9Fc#YG8lIDq@?56$<%; zAX5-eYG8Qxz#RiaDPi=XH^eE_gWA;Z(N)%RGIReW_VZ86SfhO|?b zpp~|KH8XxQ=VMaM-Y&+ZB)s_i{z?sedkt*By4u4{QVrZr;gcM=aiT#*xCxMI xU{3b4^iA^>XD?U9PNrY<0@1LBHLT&Ez5wwYdw>#T?^*x=002ovPDHLkV1l;?n#lkF literal 0 HcmV?d00001 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