summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/typst-syntax/src/lexer.rs19
-rw-r--r--tests/ref/issue-3820-raw-space-when-end-with-backtick.pngbin0 -> 351 bytes
-rw-r--r--tests/suite/text/raw.typ15
3 files changed, 26 insertions, 8 deletions
diff --git a/crates/typst-syntax/src/lexer.rs b/crates/typst-syntax/src/lexer.rs
index 20cd0d60..6e64beff 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 00000000..1ba3fb18
--- /dev/null
+++ b/tests/ref/issue-3820-raw-space-when-end-with-backtick.png
Binary files differ
diff --git a/tests/suite/text/raw.typ b/tests/suite/text/raw.typ
index dce77fdb..6a3ea6bd 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