From a6509066ae9e67fe0ece44788935ee6fd06049ee 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} | 6 ++-- test/corpus/argument_optional.txt | 40 +++++++++++++++++++++ test/corpus/codeblock.txt | 4 +-- test/corpus/taglink.txt | 4 +-- test/corpus/url.txt | 4 +-- 6 files changed, 51 insertions(+), 12 deletions(-) rename test/corpus/{arguments.txt => argument.txt} (98%) create mode 100644 test/corpus/argument_optional.txt diff --git a/grammar.js b/grammar.js index 36d9970..58dc197 100644 --- a/grammar.js +++ b/grammar.js @@ -64,6 +64,7 @@ module.exports = grammar({ $.taglink, $.codespan, $.argument, + $.optional_arg, $.keycode, $.note, ), @@ -236,8 +237,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 98% rename from test/corpus/arguments.txt rename to test/corpus/argument.txt index 385cc87..2fca01d 100644 --- a/test/corpus/arguments.txt +++ b/test/corpus/argument.txt @@ -123,10 +123,8 @@ nvim_buf_detach_event[{buf}] (word)) (line (word) - (word) - (argument - (word)) - (word)))) + (optional_arg + (word))))) ================================================================================ NOT an argument 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 6746be6..cdd9de6 100644 --- a/test/corpus/codeblock.txt +++ b/test/corpus/codeblock.txt @@ -402,8 +402,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 51568c0..80bb38e 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 345c253851fb200f52aab106e777e81f8cc6b822 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 58dc197..6aba046 100644 --- a/grammar.js +++ b/grammar.js @@ -64,7 +64,7 @@ module.exports = grammar({ $.taglink, $.codespan, $.argument, - $.optional_arg, + $.optional, $.keycode, $.note, ), @@ -114,6 +114,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. @@ -237,10 +241,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 eda14e3..23823c7 100644 --- a/queries/vimdoc/highlights.scm +++ b/queries/vimdoc/highlights.scm @@ -50,6 +50,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 2fca01d..385cc87 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 cdd9de6..741596b 100644 --- a/test/corpus/codeblock.txt +++ b/test/corpus/codeblock.txt @@ -402,8 +402,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 80bb38e..a931686 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 6dd06f89a37b8fdf7ee3961b1b8f96e822a5f7e9 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 6aba046..18da976 100644 --- a/grammar.js +++ b/grammar.js @@ -16,7 +16,7 @@ const _uppercase_word = /[A-Z0-9.()][-A-Z0-9.()_]+/; const _li_token = /[-•][ ]+/; module.exports = grammar({ - name: 'vimdoc', + name: 'vimdoc2', conflicts: $ => [ [$._line_noli, $._column_heading], @@ -98,7 +98,7 @@ module.exports = grammar({ ), note: () => choice( - 'Note:', 'NOTE:', 'Notes:', + 'The', 'the', 'Notes:', 'Warning:', 'WARNING:', 'Deprecated', 'DEPRECATED:' ),