| 1 |
import type { CompletionSource } from "@codemirror/autocomplete"; |
| 2 |
|
| 3 |
import bqlGrammar from "./bql-grammar"; |
| 4 |
|
| 5 |
const { columns, functions, keywords } = bqlGrammar; |
| 6 |
|
| 7 |
const completions = [...columns, ...functions.map((f) => `${f}(`), ...keywords]; |
| 8 |
const allCompletionOptions = completions.map((label) => ({ label })); |
| 9 |
|
| 10 |
const commands = [ |
| 11 |
"balances", |
| 12 |
"errors", |
| 13 |
"explain", |
| 14 |
"help", |
| 15 |
"lex", |
| 16 |
"parse", |
| 17 |
"print", |
| 18 |
"runcustom", |
| 19 |
"select", |
| 20 |
"tokenize", |
| 21 |
]; |
| 22 |
const firstWordCompletions = commands.map((label) => ({ label })); |
| 23 |
|
| 24 |
export const bqlCompletion: CompletionSource = (context) => { |
| 25 |
const token = context.matchBefore(/\w+/); |
| 26 |
if (!token) { |
| 27 |
return null; |
| 28 |
} |
| 29 |
if (token.from === 0) { |
| 30 |
return { from: token.from, options: firstWordCompletions }; |
| 31 |
} |
| 32 |
return { from: token.from, options: allCompletionOptions }; |
| 33 |
}; |