Skip to content

Commit 9b814b6

Browse files
committed
feat: numbered listitems
- Capture numbered listiems in the existing `line_li` form. - Introduce `prefix` capture so that consumers can check if a listitem is ordered (numbered) or unordered.
1 parent e7c5565 commit 9b814b6

File tree

5 files changed

+159
-50
lines changed

5 files changed

+159
-50
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ Overview
2121
terminated by "implicit stop" (no terminating `<`) consumes blank lines, so
2222
`block` has no way to end.
2323
- `line_li` ("listitem")
24-
- Lines starting with `-`/`` (_not_ `+`/`*`) are listitems.
24+
- Lines starting with `-`/``/`[0-9].` (_not_ `+`/`*`) are listitems.
25+
- Use the `prefix` node to detect if the listitem is ordered (numbered) or
26+
unodered.
2527
- Consumes lines until blank line, codeblock, or next listitem.
2628
- Nesting is ignored: indented listitems are parsed as siblings. Consumers can
2729
check leading whitespace to decide nesting.
@@ -44,6 +46,12 @@ Known issues
4446

4547
- Input must end with newline/EOL (`\n`). Grammar does not support files without EOL.
4648
- Input must end with a blank line. Though this doesn't seem to matter in practice.
49+
- Any line starting with `1.` (or other number) is treated as a listitem, even
50+
if the first line of its `block` is not a listitem. Example:
51+
```
52+
Foo was 0, not
53+
1. Uh oh.
54+
```
4755
- Spec requires that `codeblock` delimiter ">" must be preceded by a space
4856
(" >"), not a tab. But currently the grammar doesn't enforce this. Example:
4957
`:help lcs-tab`.

grammar.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
// @ts-check
1414

1515
const _uppercase_word = /[A-Z0-9.()][-A-Z0-9.()_]+/;
16-
// List-item.
17-
const _li_token = /[-][ ]+/;
18-
// Numbered list-item.
19-
const _num_li_token = /[0-9]{1,3}\.[ ]+/;
16+
// Listitem (incl. numbered items).
17+
const _li_token = /([-]|([0-9]{1,3}\.))[ ]+/;
2018

2119
module.exports = grammar({
2220
name: 'vimdoc',
@@ -136,8 +134,7 @@ module.exports = grammar({
136134
choice(
137135
repeat1($.line),
138136
repeat1($.line_li),
139-
repeat1($.line_li_num),
140-
seq(repeat1($.line), repeat1(choice($.line_li, $.line_li_num))),
137+
seq(repeat1($.line), repeat1($.line_li)),
141138
),
142139
choice(
143140
token.immediate('<'), // Treat codeblock-terminating "<" as whitespace.
@@ -169,17 +166,7 @@ module.exports = grammar({
169166
// Listitem: consumes prefixed line and all adjacent non-prefixed lines.
170167
line_li: ($) => prec.right(1, seq(
171168
optional(token.immediate('<')), // Treat codeblock-terminating "<" as whitespace.
172-
_li_token,
173-
choice(
174-
alias(seq(repeat1($._atom), /\n/), $.line),
175-
seq(alias(repeat1($._atom), $.line), $.codeblock),
176-
),
177-
repeat(alias($._line_noli, $.line)),
178-
)),
179-
// Numbered listitem: consumes prefixed line and all adjacent non-prefixed lines.
180-
line_li_num: ($) => prec.right(1, seq(
181-
optional(token.immediate('<')), // Treat codeblock-terminating "<" as whitespace.
182-
_num_li_token,
169+
alias(_li_token, $.prefix),
183170
choice(
184171
alias(seq(repeat1($._atom), /\n/), $.line),
185172
seq(alias(repeat1($._atom), $.line), $.codeblock),

test/corpus/heading3-column_heading.txt

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -224,34 +224,38 @@ ABC not-h3
224224
(word)
225225
(word)))
226226
(block
227-
(line
228-
(word)
229-
(optionlink
230-
(word))
231-
(codespan
232-
(word))
233-
(word)
234-
(word))
235-
(line
236-
(codespan
227+
(line_li
228+
(prefix)
229+
(line
230+
(optionlink
231+
(word))
232+
(codespan
233+
(word))
234+
(word)
237235
(word))
238-
(word)
239-
(word))
240-
(line
241-
(word)
242-
(word)
243-
(word)
244-
(word)
245-
(word))
246-
(line
247-
(word)
248-
(word)
249-
(word)
250-
(codespan
236+
(line)
237+
(line
238+
(codespan
239+
(word))
240+
(word)
241+
(word)))
242+
(line_li
243+
(prefix)
244+
(line
245+
(word)
246+
(word)
247+
(word)
251248
(word))
252-
(word)
253-
(word)
254-
(word))))
249+
(line)
250+
(line
251+
(word)
252+
(word)
253+
(word)
254+
(codespan
255+
(word))
256+
(word)
257+
(word)
258+
(word)))))
255259

256260
================================================================================
257261
NOT column_heading

0 commit comments

Comments
 (0)