mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-02-22 10:37:45 +01:00
39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import type { CompletionContext } from '@codemirror/autocomplete';
|
|
|
|
const openTag = '${[ ';
|
|
const closeTag = ' ]}';
|
|
|
|
const variables = [
|
|
{ name: 'DOMAIN' },
|
|
{ name: 'BASE_URL' },
|
|
{ name: 'TOKEN' },
|
|
{ name: 'PROJECT_ID' },
|
|
{ name: 'DUMMY' },
|
|
{ name: 'DUMMY_2' },
|
|
];
|
|
|
|
export function myCompletions(context: CompletionContext) {
|
|
const toStartOfName = context.explicit ? context.matchBefore(/\w*/) : context.matchBefore(/\w+/);
|
|
const toStartOfVariable = context.matchBefore(/\$\{?\[?\s*\w*/);
|
|
const toMatch = toStartOfVariable ?? toStartOfName ?? null;
|
|
|
|
if (toMatch === null) {
|
|
return null;
|
|
}
|
|
|
|
// Match a minimum of two characters when typing a variable ${[...]} to prevent it
|
|
// from opening on "$"
|
|
if (toStartOfVariable !== null && toMatch.to - toMatch.from < 2 && !context.explicit) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
from: toMatch.from,
|
|
options: variables.map((v) => ({
|
|
label: toStartOfVariable ? `${openTag}${v.name}${closeTag}` : v.name,
|
|
apply: `${openTag}${v.name}${closeTag}`,
|
|
type: 'variable',
|
|
})),
|
|
};
|
|
}
|