Skip to content

Commit 345c253

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 a650906 commit 345c253

File tree

7 files changed

+23
-28
lines changed

7 files changed

+23
-28
lines changed

grammar.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ module.exports = grammar({
6464
$.taglink,
6565
$.codespan,
6666
$.argument,
67-
$.optional_arg,
67+
$.optional,
6868
$.keycode,
6969
$.note,
7070
),
@@ -114,6 +114,10 @@ module.exports = grammar({
114114
/ALT-./,
115115
),
116116

117+
// Optional argument: [arg] (no whitespace allowed).
118+
// This is the vimdoc style optional arg, as opposed to {arg}? (LuaLS style).
119+
optional: () => /\[[^\]{\n\t ]+\]/,
120+
117121
// First part (minus tags) of h3 or column_heading.
118122
uppercase_name: () => seq(
119123
token.immediate(_uppercase_word), // No whitespace before heading.
@@ -237,10 +241,8 @@ module.exports = grammar({
237241
taglink: ($) => _word($, prec(1, /[^|\n\t ]+/), '|', '|'),
238242
// Inline code (may contain whitespace!): `foo bar`
239243
codespan: ($) => _word($, /[^``\n]+/, '`', '`'),
240-
// Argument: {arg} (no whitespace allowed), also {arg}? for LuaLS-style optional args.
244+
// Argument: {arg} (no whitespace allowed), also {arg}? (LuaLS style "optional arg").
241245
argument: ($) => seq(_word($, /[^}\n\t ]+/, '{', '}'), optional(token.immediate('?'))),
242-
// Optional argument: [arg] (no whitespace allowed)
243-
optional_arg: ($) => _word($, /[^\]\n\t ]+/, '[', ']'),
244246
},
245247
});
246248

queries/vimdoc/highlights.scm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050

5151
(argument) @variable.parameter
5252

53+
(optional) @variable.parameter
54+
5355
(keycode) @string.special
5456

5557
(url) @string.special.url

test/corpus/argument.txt

Lines changed: 4 additions & 2 deletions
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

Lines changed: 8 additions & 16 deletions
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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,7 @@ codeblock stop and start on same line
402402
(line
403403
(word)
404404
(word)
405-
(optional_arg
406-
(word)))
405+
(optional))
407406
(line
408407
(argument
409408
(word))))

test/corpus/taglink.txt

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 2 deletions
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)