Fix heading and list markers

This commit is contained in:
Laurenz 2023-02-02 14:27:31 +01:00
parent 5f5c659279
commit bb12624e8e
10 changed files with 38 additions and 35 deletions

View File

@ -191,15 +191,15 @@ impl Lexer<'_> {
':' => SyntaxKind::Colon, ':' => SyntaxKind::Colon,
'=' => { '=' => {
self.s.eat_while('='); self.s.eat_while('=');
if self.space_and_more() { if self.space_or_end() {
SyntaxKind::HeadingMarker SyntaxKind::HeadingMarker
} else { } else {
self.text() self.text()
} }
} }
'-' if self.space_and_more() => SyntaxKind::ListMarker, '-' if self.space_or_end() => SyntaxKind::ListMarker,
'+' if self.space_and_more() => SyntaxKind::EnumMarker, '+' if self.space_or_end() => SyntaxKind::EnumMarker,
'/' if self.space_and_more() => SyntaxKind::TermMarker, '/' if self.space_or_end() => SyntaxKind::TermMarker,
_ => self.text(), _ => self.text(),
} }
@ -363,13 +363,8 @@ impl Lexer<'_> {
alphanum(prev) && alphanum(next) alphanum(prev) && alphanum(next)
} }
fn space_and_more(&self) -> bool { fn space_or_end(&self) -> bool {
let mut s = self.s; self.s.done() || self.s.at(char::is_whitespace)
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)
} }
} }

View File

@ -157,7 +157,7 @@ fn emph(p: &mut Parser) {
fn heading(p: &mut Parser) { fn heading(p: &mut Parser) {
let m = p.marker(); let m = p.marker();
p.assert(SyntaxKind::HeadingMarker); p.assert(SyntaxKind::HeadingMarker);
whitespace(p); whitespace_line(p);
markup(p, false, usize::MAX, |kind| { markup(p, false, usize::MAX, |kind| {
kind == SyntaxKind::Label || kind == SyntaxKind::RightBracket kind == SyntaxKind::Label || kind == SyntaxKind::RightBracket
}); });
@ -168,7 +168,7 @@ fn list_item(p: &mut Parser) {
let m = p.marker(); let m = p.marker();
p.assert(SyntaxKind::ListMarker); p.assert(SyntaxKind::ListMarker);
let min_indent = p.column(p.prev_end()); let min_indent = p.column(p.prev_end());
whitespace(p); whitespace_line(p);
markup(p, false, min_indent, |kind| kind == SyntaxKind::RightBracket); markup(p, false, min_indent, |kind| kind == SyntaxKind::RightBracket);
p.wrap(m, SyntaxKind::ListItem); p.wrap(m, SyntaxKind::ListItem);
} }
@ -177,7 +177,7 @@ fn enum_item(p: &mut Parser) {
let m = p.marker(); let m = p.marker();
p.assert(SyntaxKind::EnumMarker); p.assert(SyntaxKind::EnumMarker);
let min_indent = p.column(p.prev_end()); let min_indent = p.column(p.prev_end());
whitespace(p); whitespace_line(p);
markup(p, false, min_indent, |kind| kind == SyntaxKind::RightBracket); markup(p, false, min_indent, |kind| kind == SyntaxKind::RightBracket);
p.wrap(m, SyntaxKind::EnumItem); p.wrap(m, SyntaxKind::EnumItem);
} }
@ -186,18 +186,18 @@ fn term_item(p: &mut Parser) {
let m = p.marker(); let m = p.marker();
p.assert(SyntaxKind::TermMarker); p.assert(SyntaxKind::TermMarker);
let min_indent = p.column(p.prev_end()); let min_indent = p.column(p.prev_end());
whitespace(p); whitespace_line(p);
markup(p, false, usize::MAX, |kind| { markup(p, false, usize::MAX, |kind| {
kind == SyntaxKind::Colon || kind == SyntaxKind::RightBracket kind == SyntaxKind::Colon || kind == SyntaxKind::RightBracket
}); });
p.expect(SyntaxKind::Colon); p.expect(SyntaxKind::Colon);
whitespace(p); whitespace_line(p);
markup(p, false, min_indent, |kind| kind == SyntaxKind::RightBracket); markup(p, false, min_indent, |kind| kind == SyntaxKind::RightBracket);
p.wrap(m, SyntaxKind::TermItem); p.wrap(m, SyntaxKind::TermItem);
} }
fn whitespace(p: &mut Parser) { fn whitespace_line(p: &mut Parser) {
while p.current().is_trivia() { while !p.newline() && p.current().is_trivia() {
p.eat(); p.eat();
} }
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -55,9 +55,10 @@
+ B + B
--- ---
// Lone plus is not an enum. // Edge cases.
+ +
No enum Empty
+Nope
--- ---
// Error: 22-24 invalid numbering pattern // Error: 22-24 invalid numbering pattern

View File

@ -1,11 +1,5 @@
// Test headings. // Test headings.
---
#show heading: it => text(blue, it.title)
=
No heading
--- ---
// Different number of equals signs. // Different number of equals signs.
@ -50,3 +44,10 @@ multiline.
= Heading = Heading
===== Heading 🌍 ===== Heading 🌍
#heading(level: 5)[Heading] #heading(level: 5)[Heading]
---
// Edge cases.
#set heading(numbering: "1.")
=
Not in heading
=Nope

View File

@ -1,9 +1,5 @@
// Test bullet lists. // Test bullet lists.
---
-
No list
--- ---
_Shopping list_ _Shopping list_
#list[Apples][Potatoes][Juice] #list[Apples][Potatoes][Juice]
@ -52,3 +48,9 @@ _Shopping list_
#set list(marker: [-]) #set list(marker: [-])
- Bare hyphen - Bare hyphen
- is not a list - is not a list
---
// Edge cases.
-
Not in list
-Nope

View File

@ -1,10 +1,5 @@
// Test term list. // Test term list.
---
/
No: list \
/No: list
--- ---
// Test with constructor. // Test with constructor.
#terms( #terms(
@ -46,3 +41,12 @@ No: list \
/ A: One letter / A: One letter
/ BB: Two letters / BB: Two letters
/ CCC: Three letters / CCC: Three letters
---
/ Term:
Not in list
/Nope
---
// Error: 8 expected colon
/ Hello