From d76e6b7090b44790d9ed52b99a21902c9dc37430 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Mon, 26 Jun 2023 00:25:04 +0200 Subject: [PATCH 1/3] [optional_arg] fix #1 --- grammar.js | 5 ++- test/corpus/{arguments.txt => argument.txt} | 14 ++++---- test/corpus/argument_optional.txt | 40 +++++++++++++++++++++ test/corpus/codeblock.txt | 4 +-- test/corpus/taglink.txt | 4 +-- test/corpus/url.txt | 4 +-- 6 files changed, 55 insertions(+), 16 deletions(-) rename test/corpus/{arguments.txt => argument.txt} (97%) create mode 100644 test/corpus/argument_optional.txt diff --git a/grammar.js b/grammar.js index 832ff34..6f0aa95 100644 --- a/grammar.js +++ b/grammar.js @@ -57,6 +57,7 @@ module.exports = grammar({ $.taglink, $.codespan, $.argument, + $.optional_arg, $.keycode, $.note, ), @@ -222,8 +223,10 @@ module.exports = grammar({ taglink: ($) => _word($, prec(1, /[^|\n\t ]+/), '|', '|'), // Inline code (may contain whitespace!): `foo bar` codespan: ($) => _word($, /[^``\n]+/, '`', '`'), - // Argument: {arg} (no whitespace allowed) + // Argument: {arg} (no whitespace allowed), also {arg}? for LuaLS-style optional args. argument: ($) => seq(_word($, /[^}\n\t ]+/, '{', '}'), optional(token.immediate('?'))), + // Optional argument: [arg] (no whitespace allowed) + optional_arg: ($) => _word($, /[^\]\n\t ]+/, '[', ']'), }, }); diff --git a/test/corpus/arguments.txt b/test/corpus/argument.txt similarity index 97% rename from test/corpus/arguments.txt rename to test/corpus/argument.txt index 05351f7..91157c8 100644 --- a/test/corpus/arguments.txt +++ b/test/corpus/argument.txt @@ -123,22 +123,20 @@ nvim_buf_detach_event[{buf}] (word)) (line (word) - (word) - (argument - (word)) - (word)))) + (optional_arg + (word))))) ================================================================================ NOT an argument ================================================================================ a '{' '}' block {foo "{bar}" `{baz}` |{baz| } {} -foo { bar +foo { bar { {} foo{{ foo{{{ - {{ - {{{ + {{ + {{{ { } foo -, inside { }: +, inside { }: \} literal } x \{ literal { x diff --git a/test/corpus/argument_optional.txt b/test/corpus/argument_optional.txt new file mode 100644 index 0000000..fb1a649 --- /dev/null +++ b/test/corpus/argument_optional.txt @@ -0,0 +1,40 @@ +================================================================================ +optional [argument] +================================================================================ +:ar[gs]! [++opt] [+cmd] {arglist} *:args_f!* +:[count]arge[dit][!] [++opt] [+cmd] {name} .. + + +-------------------------------------------------------------------------------- + +(help_file + (block + (line + (word) + (optional_arg + (word)) + (word) + (optional_arg + (word)) + (optional_arg + (word)) + (argument + (word)) + (tag + (word))) + (line + (word) + (optional_arg + (word)) + (word) + (optional_arg + (word)) + (optional_arg + (word)) + (optional_arg + (word)) + (optional_arg + (word)) + (argument + (word)) + (word)))) diff --git a/test/corpus/codeblock.txt b/test/corpus/codeblock.txt index 0a9c9a9..a138078 100644 --- a/test/corpus/codeblock.txt +++ b/test/corpus/codeblock.txt @@ -398,8 +398,8 @@ codeblock stop and start on same line (line (word) (word) - (word) - (word)) + (optional_arg + (word))) (line (argument (word)))) diff --git a/test/corpus/taglink.txt b/test/corpus/taglink.txt index 4e0440d..8b4c6f9 100644 --- a/test/corpus/taglink.txt +++ b/test/corpus/taglink.txt @@ -56,8 +56,8 @@ Hello |world| hello (taglink (word)) (word) - (word) - (word) + (optional_arg + (word)) (word)) (line (taglink diff --git a/test/corpus/url.txt b/test/corpus/url.txt index ea53720..8e7f4b1 100644 --- a/test/corpus/url.txt +++ b/test/corpus/url.txt @@ -54,11 +54,9 @@ markdown: [https://neovim.io/doc/user/#yay](https://neovim.io/doc/user/#yay). (word)) (line (word) - (word) - (url + (optional_arg (word)) (word) - (word) (url (word)) (word)))) From 2a569e7f93a1f6aead726e73a609d3a168f4c0f1 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Mon, 26 Jun 2023 01:32:58 +0200 Subject: [PATCH 2/3] [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 --- grammar.js | 10 ++++++---- queries/vimdoc/highlights.scm | 2 ++ test/corpus/argument.txt | 6 ++++-- test/corpus/argument_optional.txt | 24 ++++++++---------------- test/corpus/codeblock.txt | 3 +-- test/corpus/taglink.txt | 3 +-- test/corpus/url.txt | 3 +-- 7 files changed, 23 insertions(+), 28 deletions(-) diff --git a/grammar.js b/grammar.js index 6f0aa95..f7281c6 100644 --- a/grammar.js +++ b/grammar.js @@ -57,7 +57,7 @@ module.exports = grammar({ $.taglink, $.codespan, $.argument, - $.optional_arg, + $.optional, $.keycode, $.note, ), @@ -107,6 +107,10 @@ module.exports = grammar({ /ALT-./, ), + // Optional argument: [arg] (no whitespace allowed). + // This is the vimdoc style optional arg, as opposed to {arg}? (LuaLS style). + optional: () => /\[[^\]{\n\t ]+\]/, + // First part (minus tags) of h3 or column_heading. uppercase_name: () => seq( token.immediate(_uppercase_word), // No whitespace before heading. @@ -223,10 +227,8 @@ module.exports = grammar({ taglink: ($) => _word($, prec(1, /[^|\n\t ]+/), '|', '|'), // Inline code (may contain whitespace!): `foo bar` codespan: ($) => _word($, /[^``\n]+/, '`', '`'), - // Argument: {arg} (no whitespace allowed), also {arg}? for LuaLS-style optional args. + // Argument: {arg} (no whitespace allowed), also {arg}? (LuaLS style "optional arg"). argument: ($) => seq(_word($, /[^}\n\t ]+/, '{', '}'), optional(token.immediate('?'))), - // Optional argument: [arg] (no whitespace allowed) - optional_arg: ($) => _word($, /[^\]\n\t ]+/, '[', ']'), }, }); diff --git a/queries/vimdoc/highlights.scm b/queries/vimdoc/highlights.scm index 0bb48cf..2de1b1a 100644 --- a/queries/vimdoc/highlights.scm +++ b/queries/vimdoc/highlights.scm @@ -44,6 +44,8 @@ (argument) @variable.parameter +(optional) @variable.parameter + (keycode) @string.special (url) @string.special.url diff --git a/test/corpus/argument.txt b/test/corpus/argument.txt index 91157c8..a5c5715 100644 --- a/test/corpus/argument.txt +++ b/test/corpus/argument.txt @@ -123,8 +123,10 @@ nvim_buf_detach_event[{buf}] (word)) (line (word) - (optional_arg - (word))))) + (word) + (argument + (word)) + (word)))) ================================================================================ NOT an argument diff --git a/test/corpus/argument_optional.txt b/test/corpus/argument_optional.txt index fb1a649..7b5cc6f 100644 --- a/test/corpus/argument_optional.txt +++ b/test/corpus/argument_optional.txt @@ -11,30 +11,22 @@ optional [argument] (block (line (word) - (optional_arg - (word)) + (optional) (word) - (optional_arg - (word)) - (optional_arg - (word)) + (optional) + (optional) (argument (word)) (tag (word))) (line (word) - (optional_arg - (word)) + (optional) (word) - (optional_arg - (word)) - (optional_arg - (word)) - (optional_arg - (word)) - (optional_arg - (word)) + (optional) + (optional) + (optional) + (optional) (argument (word)) (word)))) diff --git a/test/corpus/codeblock.txt b/test/corpus/codeblock.txt index a138078..f1c655b 100644 --- a/test/corpus/codeblock.txt +++ b/test/corpus/codeblock.txt @@ -398,8 +398,7 @@ codeblock stop and start on same line (line (word) (word) - (optional_arg - (word))) + (optional)) (line (argument (word)))) diff --git a/test/corpus/taglink.txt b/test/corpus/taglink.txt index 8b4c6f9..799489f 100644 --- a/test/corpus/taglink.txt +++ b/test/corpus/taglink.txt @@ -56,8 +56,7 @@ Hello |world| hello (taglink (word)) (word) - (optional_arg - (word)) + (optional) (word)) (line (taglink diff --git a/test/corpus/url.txt b/test/corpus/url.txt index 8e7f4b1..53e0ef2 100644 --- a/test/corpus/url.txt +++ b/test/corpus/url.txt @@ -54,8 +54,7 @@ markdown: [https://neovim.io/doc/user/#yay](https://neovim.io/doc/user/#yay). (word)) (line (word) - (optional_arg - (word)) + (optional) (word) (url (word)) From 3f33af6a2d8616bf5261e5f5db17ebca054c49fa Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Wed, 13 Mar 2024 18:48:26 -0400 Subject: [PATCH 3/3] TEMP --- grammar.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/grammar.js b/grammar.js index f7281c6..bbfb7bb 100644 --- a/grammar.js +++ b/grammar.js @@ -14,7 +14,7 @@ const _uppercase_word = /[A-Z0-9.()][-A-Z0-9.()_]+/; const _li_token = /[-•][ ]+/; module.exports = grammar({ - name: 'vimdoc', + name: 'vimdoc2', extras: () => [/[\t ]/], @@ -91,7 +91,7 @@ module.exports = grammar({ ), note: () => choice( - 'Note:', 'NOTE:', 'Notes:', + 'The', 'the', 'Notes:', 'Warning:', 'WARNING:', 'Deprecated', 'DEPRECATED:' ),