Autocompletion from grammar using Lezer

I’d like to implement autocompletion ( and possibly syntax highlighting; but autocompletion is my priority right now ) in CodeMirror using Lezer, where autocompletion is generated in accordance with my language grammar. Are there any example implementations of this, or particular parts of the Lezer API you can point me to as a starting point for implementing this ?

My simple example grammar so far looks roughly like this ( this is in the Python Lark EBNF format. This is incomplete; it’s just an example ) –

?start: function_call_retval_number
      | function_call_retval_string

number_arg: function_call_retval_number
          | NUMBER

number_args: number_arg ("," number_arg)+

two_number_args: number_arg "," number_arg

?function_call_retval_number: ADD number_args ")"
                            | ADD2 two_number_args ")"
                            | MULTIPLY number_args ")"

ADD: /add(\s)*\(/

ADD2: /add2(\s)*\(/

MULTIPLY: /multiply(\s)*\(/

I reviewed [Example: Writing a Language Package][CodeMirror Language Package Example] and [Writing a Grammar][Lezer System Guide]; it seems I can easily convert the above to Lezer’s grammar format.

It’s seeming that I’d perhaps want to go in this direction –

  1. Use lezer-generator to generate the parser from my grammar

  2. Implement a [CompletionSource][CodeMirror 6 Reference Manual] that calls [Parser.parse()][Lezer Reference Manual] with the input text.

  3. ?? Eg, if the input text is “add(1, add(”, I don’t see an API method or exception I can use to infer that what’s valid at that point is “NUMBER”, “add”, “add2”, or “multiply”, so that the CompletionSource can build an appropriate return value.

Any feedback, suggestions, and/or examples ?

Syntax-based completion tends to be somewhat ad-hoc—it determines the cursor’s position in the tree, and then uses that to figure out the syntactic context and the completions that are appropriate there. The sql completion and html completion might be useful examples.