summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-02-02 14:27:31 +0100
committerLaurenz <laurmaedje@gmail.com>2023-02-02 14:27:31 +0100
commitbb12624e8e970915e41363809139f168d4c545c6 (patch)
tree33787a13e8c1e03377d7044d9b5d703d791fc6a0
parent5f5c65927982447133b625edea3f5c9bab4f9e3d (diff)
Fix heading and list markers
-rw-r--r--src/syntax/lexer.rs17
-rw-r--r--src/syntax/parser.rs14
-rw-r--r--tests/ref/basics/enum.pngbin29431 -> 30313 bytes
-rw-r--r--tests/ref/basics/heading.pngbin24759 -> 26270 bytes
-rw-r--r--tests/ref/basics/list.pngbin20300 -> 20936 bytes
-rw-r--r--tests/ref/basics/terms.pngbin16887 -> 17233 bytes
-rw-r--r--tests/typ/basics/enum.typ5
-rw-r--r--tests/typ/basics/heading.typ13
-rw-r--r--tests/typ/basics/list.typ10
-rw-r--r--tests/typ/basics/terms.typ14
10 files changed, 38 insertions, 35 deletions
diff --git a/src/syntax/lexer.rs b/src/syntax/lexer.rs
index 0bf7966f..b79b4c7f 100644
--- a/src/syntax/lexer.rs
+++ b/src/syntax/lexer.rs
@@ -191,15 +191,15 @@ impl Lexer<'_> {
':' => SyntaxKind::Colon,
'=' => {
self.s.eat_while('=');
- if self.space_and_more() {
+ if self.space_or_end() {
SyntaxKind::HeadingMarker
} else {
self.text()
}
}
- '-' if self.space_and_more() => SyntaxKind::ListMarker,
- '+' if self.space_and_more() => SyntaxKind::EnumMarker,
- '/' if self.space_and_more() => SyntaxKind::TermMarker,
+ '-' if self.space_or_end() => SyntaxKind::ListMarker,
+ '+' if self.space_or_end() => SyntaxKind::EnumMarker,
+ '/' if self.space_or_end() => SyntaxKind::TermMarker,
_ => self.text(),
}
@@ -363,13 +363,8 @@ impl Lexer<'_> {
alphanum(prev) && alphanum(next)
}
- fn space_and_more(&self) -> bool {
- let mut s = self.s;
- if !s.at(char::is_whitespace) {
- return false;
- }
- s.eat_while(|c: char| c.is_whitespace() && !is_newline(c));
- !s.done() && !s.at(is_newline)
+ fn space_or_end(&self) -> bool {
+ self.s.done() || self.s.at(char::is_whitespace)
}
}
diff --git a/src/syntax/parser.rs b/src/syntax/parser.rs
index afb207b6..5933d481 100644
--- a/src/syntax/parser.rs
+++ b/src/syntax/parser.rs
@@ -157,7 +157,7 @@ fn emph(p: &mut Parser) {
fn heading(p: &mut Parser) {
let m = p.marker();
p.assert(SyntaxKind::HeadingMarker);
- whitespace(p);
+ whitespace_line(p);
markup(p, false, usize::MAX, |kind| {
kind == SyntaxKind::Label || kind == SyntaxKind::RightBracket
});
@@ -168,7 +168,7 @@ fn list_item(p: &mut Parser) {
let m = p.marker();
p.assert(SyntaxKind::ListMarker);
let min_indent = p.column(p.prev_end());
- whitespace(p);
+ whitespace_line(p);
markup(p, false, min_indent, |kind| kind == SyntaxKind::RightBracket);
p.wrap(m, SyntaxKind::ListItem);
}
@@ -177,7 +177,7 @@ fn enum_item(p: &mut Parser) {
let m = p.marker();
p.assert(SyntaxKind::EnumMarker);
let min_indent = p.column(p.prev_end());
- whitespace(p);
+ whitespace_line(p);
markup(p, false, min_indent, |kind| kind == SyntaxKind::RightBracket);
p.wrap(m, SyntaxKind::EnumItem);
}
@@ -186,18 +186,18 @@ fn term_item(p: &mut Parser) {
let m = p.marker();
p.assert(SyntaxKind::TermMarker);
let min_indent = p.column(p.prev_end());
- whitespace(p);
+ whitespace_line(p);
markup(p, false, usize::MAX, |kind| {
kind == SyntaxKind::Colon || kind == SyntaxKind::RightBracket
});
p.expect(SyntaxKind::Colon);
- whitespace(p);
+ whitespace_line(p);
markup(p, false, min_indent, |kind| kind == SyntaxKind::RightBracket);
p.wrap(m, SyntaxKind::TermItem);
}
-fn whitespace(p: &mut Parser) {
- while p.current().is_trivia() {
+fn whitespace_line(p: &mut Parser) {
+ while !p.newline() && p.current().is_trivia() {
p.eat();
}
}
diff --git a/tests/ref/basics/enum.png b/tests/ref/basics/enum.png
index 5ccdb1ad..8e48076b 100644
--- a/tests/ref/basics/enum.png
+++ b/tests/ref/basics/enum.png
Binary files differ
diff --git a/tests/ref/basics/heading.png b/tests/ref/basics/heading.png
index 9cb4d098..b4f99665 100644
--- a/tests/ref/basics/heading.png
+++ b/tests/ref/basics/heading.png
Binary files differ
diff --git a/tests/ref/basics/list.png b/tests/ref/basics/list.png
index b6b8ed3e..6db5fb71 100644
--- a/tests/ref/basics/list.png
+++ b/tests/ref/basics/list.png
Binary files differ
diff --git a/tests/ref/basics/terms.png b/tests/ref/basics/terms.png
index e186ec8c..5c695795 100644
--- a/tests/ref/basics/terms.png
+++ b/tests/ref/basics/terms.png
Binary files differ
diff --git a/tests/typ/basics/enum.typ b/tests/typ/basics/enum.typ
index 02eb03c2..fd845c2b 100644
--- a/tests/typ/basics/enum.typ
+++ b/tests/typ/basics/enum.typ
@@ -55,9 +55,10 @@
+ B
---
-// Lone plus is not an enum.
+// Edge cases.
+
-No enum
+Empty
++Nope
---
// Error: 22-24 invalid numbering pattern
diff --git a/tests/typ/basics/heading.typ b/tests/typ/basics/heading.typ
index d843a2e8..3d637036 100644
--- a/tests/typ/basics/heading.typ
+++ b/tests/typ/basics/heading.typ
@@ -1,12 +1,6 @@
// Test headings.
---
-#show heading: it => text(blue, it.title)
-
-=
-No heading
-
----
// Different number of equals signs.
= Level 1
@@ -50,3 +44,10 @@ multiline.
= Heading
===== Heading 🌍
#heading(level: 5)[Heading]
+
+---
+// Edge cases.
+#set heading(numbering: "1.")
+=
+Not in heading
+=Nope
diff --git a/tests/typ/basics/list.typ b/tests/typ/basics/list.typ
index 1c111dcb..3fd9ddb1 100644
--- a/tests/typ/basics/list.typ
+++ b/tests/typ/basics/list.typ
@@ -1,10 +1,6 @@
// Test bullet lists.
---
--
-No list
-
----
_Shopping list_
#list[Apples][Potatoes][Juice]
@@ -52,3 +48,9 @@ _Shopping list_
#set list(marker: [-])
- Bare hyphen
- is not a list
+
+---
+// Edge cases.
+-
+Not in list
+-Nope
diff --git a/tests/typ/basics/terms.typ b/tests/typ/basics/terms.typ
index 204defbf..57864423 100644
--- a/tests/typ/basics/terms.typ
+++ b/tests/typ/basics/terms.typ
@@ -1,11 +1,6 @@
// Test term list.
---
-/
-No: list \
-/No: list
-
----
// Test with constructor.
#terms(
([One], [First]),
@@ -46,3 +41,12 @@ No: list \
/ A: One letter
/ BB: Two letters
/ CCC: Three letters
+
+---
+/ Term:
+Not in list
+/Nope
+
+---
+// Error: 8 expected colon
+/ Hello