summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeedehai <18319900+Leedehai@users.noreply.github.com>2024-04-04 04:18:37 -0400
committerGitHub <noreply@github.com>2024-04-04 08:18:37 +0000
commit8c28f67504256998584d89bab7e3805599f72e00 (patch)
tree1698cb96a624569363d421a82e08e883fb51814f
parentd4b3ae0925748ef37a778b56ee26b63bf4585b06 (diff)
Let the lexer respect linebreaks within inline raw (#3756)
-rw-r--r--crates/typst-syntax/src/kind.rs2
-rw-r--r--crates/typst-syntax/src/lexer.rs26
-rw-r--r--tests/ref/text/raw-code.pngbin40290 -> 55071 bytes
-rw-r--r--tests/typ/text/raw-code.typ15
4 files changed, 37 insertions, 6 deletions
diff --git a/crates/typst-syntax/src/kind.rs b/crates/typst-syntax/src/kind.rs
index c34f6002..c84e5358 100644
--- a/crates/typst-syntax/src/kind.rs
+++ b/crates/typst-syntax/src/kind.rs
@@ -32,7 +32,7 @@ pub enum SyntaxKind {
RawLang,
/// A raw delimiter consisting of 1 or 3+ backticks: `` ` ``.
RawDelim,
- /// A sequence of whitespace to ignore in a raw block: ` `.
+ /// A sequence of whitespace to ignore in a raw text: ` `.
RawTrimmed,
/// A hyperlink: `https://typst.org`.
Link,
diff --git a/crates/typst-syntax/src/lexer.rs b/crates/typst-syntax/src/lexer.rs
index f1d29fb3..20cd0d60 100644
--- a/crates/typst-syntax/src/lexer.rs
+++ b/crates/typst-syntax/src/lexer.rs
@@ -275,10 +275,7 @@ impl Lexer<'_> {
if backticks >= 3 {
self.blocky_raw(start, end, backticks);
} else {
- // Single backtick needs no trimming or extra fancyness.
- self.s.jump(end - backticks);
- self.push_raw(SyntaxKind::Text);
- self.s.jump(end);
+ self.inline_raw(start, end, backticks);
}
// Closing delimiter.
@@ -356,6 +353,25 @@ impl Lexer<'_> {
self.s.jump(end);
}
+ fn inline_raw(&mut self, start: usize, end: usize, backticks: usize) {
+ self.s.jump(start + backticks);
+
+ while self.s.cursor() < end - backticks {
+ if self.s.at(is_newline) {
+ self.push_raw(SyntaxKind::Text);
+ self.s.eat_newline();
+ self.push_raw(SyntaxKind::RawTrimmed);
+ continue;
+ }
+ self.s.eat();
+ }
+ self.push_raw(SyntaxKind::Text);
+
+ self.s.jump(end);
+ }
+
+ /// Push the current cursor that marks the end of a raw segment of
+ /// the given `kind`.
fn push_raw(&mut self, kind: SyntaxKind) {
let end = self.s.cursor();
self.raw.push((kind, end));
@@ -821,7 +837,7 @@ pub fn link_prefix(text: &str) -> (&str, bool) {
(s.before(), brackets.is_empty())
}
-/// Split text at newlines.
+/// Split text at newlines. These newline characters are not kept.
pub fn split_newlines(text: &str) -> Vec<&str> {
let mut s = Scanner::new(text);
let mut lines = Vec::new();
diff --git a/tests/ref/text/raw-code.png b/tests/ref/text/raw-code.png
index d3373f5f..682c7c48 100644
--- a/tests/ref/text/raw-code.png
+++ b/tests/ref/text/raw-code.png
Binary files differ
diff --git a/tests/typ/text/raw-code.typ b/tests/typ/text/raw-code.typ
index ca7b247d..3ac72a05 100644
--- a/tests/typ/text/raw-code.typ
+++ b/tests/typ/text/raw-code.typ
@@ -69,3 +69,18 @@ end
</body>
</html>
```
+
+---
+#set page(width: 180pt)
+#set text(6pt)
+#set raw(lang:"python")
+
+Inline raws, multiline e.g. `for i in range(10):
+ # Only this line is a comment.
+ print(i)` or otherwise e.g. `print(j)`, are colored properly.
+
+Inline raws, multiline e.g. `
+# Appears blocky due to linebreaks at the boundary.
+for i in range(10):
+ print(i)
+` or otherwise e.g. `print(j)`, are colored properly.