Skip to content

Commit 2a569e7

Browse files
committed
[optional]
Problem: Using _word() to define an optional arg leads to a lot of syntax errors in real-world :help files because [brackets] are commonly used for non-args. Solution: Define (optional) with a regex, like (keycode) instead of using the more "formal" mechanism. Regex has "best-effort" behavior, while seq() behaves more strictly. Don't treat `[{arg}]` as (optional), else it clobbers the nested `{arg}`. fix #1
1 parent d76e6b7 commit 2a569e7

File tree

7 files changed

+23
-28
lines changed

7 files changed

+23
-28
lines changed

grammar.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module.exports = grammar({
5757
$.taglink,
5858
$.codespan,
5959
$.argument,
60-
$.optional_arg,
60+
$.optional,
6161
$.keycode,
6262
$.note,
6363
),
@@ -107,6 +107,10 @@ module.exports = grammar({
107107
/ALT-./,
108108
),
109109

110+
// Optional argument: [arg] (no whitespace allowed).
111+
// This is the vimdoc style optional arg, as opposed to {arg}? (LuaLS style).
112+
optional: () => /\[[^\]{\n\t ]+\]/,
113+
110114
// First part (minus tags) of h3 or column_heading.
111115
uppercase_name: () => seq(
112116
token.immediate(_uppercase_word), // No whitespace before heading.
@@ -223,10 +227,8 @@ module.exports = grammar({
223227
taglink: ($) => _word($, prec(1, /[^|\n\t ]+/), '|', '|'),
224228
// Inline code (may contain whitespace!): `foo bar`
225229
codespan: ($) => _word($, /[^``\n]+/, '`', '`'),
226-
// Argument: {arg} (no whitespace allowed), also {arg}? for LuaLS-style optional args.
230+
// Argument: {arg} (no whitespace allowed), also {arg}? (LuaLS style "optional arg").
227231
argument: ($) => seq(_word($, /[^}\n\t ]+/, '{', '}'), optional(token.immediate('?'))),
228-
// Optional argument: [arg] (no whitespace allowed)
229-
optional_arg: ($) => _word($, /[^\]\n\t ]+/, '[', ']'),
230232
},
231233
});
232234

queries/vimdoc/highlights.scm

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444

4545
(argument) @variable.parameter
4646

47+
(optional) @variable.parameter
48+
4749
(keycode) @string.special
4850

4951
(url) @string.special.url

test/corpus/argument.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ nvim_buf_detach_event[{buf}]
123123
(word))
124124
(line
125125
(word)
126-
(optional_arg
127-
(word)))))
126+
(word)
127+
(argument
128+
(word))
129+
(word))))
128130

129131
================================================================================
130132
NOT an argument

test/corpus/argument_optional.txt

+8-16
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,22 @@ optional [argument]
1111
(block
1212
(line
1313
(word)
14-
(optional_arg
15-
(word))
14+
(optional)
1615
(word)
17-
(optional_arg
18-
(word))
19-
(optional_arg
20-
(word))
16+
(optional)
17+
(optional)
2118
(argument
2219
(word))
2320
(tag
2421
(word)))
2522
(line
2623
(word)
27-
(optional_arg
28-
(word))
24+
(optional)
2925
(word)
30-
(optional_arg
31-
(word))
32-
(optional_arg
33-
(word))
34-
(optional_arg
35-
(word))
36-
(optional_arg
37-
(word))
26+
(optional)
27+
(optional)
28+
(optional)
29+
(optional)
3830
(argument
3931
(word))
4032
(word))))

test/corpus/codeblock.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,7 @@ codeblock stop and start on same line
398398
(line
399399
(word)
400400
(word)
401-
(optional_arg
402-
(word)))
401+
(optional))
403402
(line
404403
(argument
405404
(word))))

test/corpus/taglink.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ Hello |world| hello
5656
(taglink
5757
(word))
5858
(word)
59-
(optional_arg
60-
(word))
59+
(optional)
6160
(word))
6261
(line
6362
(taglink

test/corpus/url.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ markdown: [https://neovim.io/doc/user/#yay](https://neovim.io/doc/user/#yay).
5454
(word))
5555
(line
5656
(word)
57-
(optional_arg
58-
(word))
57+
(optional)
5958
(word)
6059
(url
6160
(word))

0 commit comments

Comments
 (0)