mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-22 01:19:13 +01:00
Add aliases field to template functions
This commit is contained in:
@@ -13,6 +13,7 @@ export type TwigCompletionOptionNamespace = {
|
||||
|
||||
export type TwigCompletionOptionFunction = {
|
||||
args: { name: string }[];
|
||||
aliases: string[];
|
||||
type: 'function';
|
||||
};
|
||||
|
||||
@@ -56,26 +57,30 @@ export function twigCompletion({ options }: TwigCompletionConfig) {
|
||||
}
|
||||
|
||||
const completions: Completion[] = options
|
||||
.map((o): Completion => {
|
||||
.flatMap((o): Completion[] => {
|
||||
const matchSegments = toStartOfName!.text.split('.');
|
||||
const optionSegments = o.name.split('.');
|
||||
|
||||
// If not on the last segment, only complete the namespace
|
||||
if (matchSegments.length < optionSegments.length) {
|
||||
return {
|
||||
label: optionSegments.slice(0, matchSegments.length).join('.'),
|
||||
apply: optionSegments.slice(0, matchSegments.length).join('.'),
|
||||
type: 'namespace',
|
||||
};
|
||||
return [
|
||||
{
|
||||
label: optionSegments.slice(0, matchSegments.length).join('.'),
|
||||
apply: optionSegments.slice(0, matchSegments.length).join('.'),
|
||||
type: 'namespace',
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
// If on the last segment, wrap the entire tag
|
||||
const inner = o.type === 'function' ? `${o.name}()` : o.name;
|
||||
return {
|
||||
label: o.name,
|
||||
apply: openTag + inner + closeTag,
|
||||
type: o.type === 'variable' ? 'variable' : 'function',
|
||||
};
|
||||
return [
|
||||
{
|
||||
label: o.name,
|
||||
apply: openTag + inner + closeTag,
|
||||
type: o.type === 'variable' ? 'variable' : 'function',
|
||||
},
|
||||
];
|
||||
})
|
||||
.filter((v) => v != null);
|
||||
|
||||
|
||||
@@ -50,6 +50,7 @@ export function twig({
|
||||
.join(', ') + (fn.args.length > NUM_ARGS ? ', …' : '');
|
||||
return {
|
||||
name: fn.name,
|
||||
aliases: fn.aliases,
|
||||
type: 'function',
|
||||
args: fn.args.map((a) => ({ name: a.name })),
|
||||
value: null,
|
||||
|
||||
@@ -146,7 +146,9 @@ function templateTags(
|
||||
name = 'response';
|
||||
}
|
||||
|
||||
let option = options.find((v) => v.name === name);
|
||||
let option = options.find(
|
||||
(o) => o.name === name || (o.type === 'function' && o.aliases?.includes(name)),
|
||||
);
|
||||
if (option == null) {
|
||||
option = {
|
||||
invalid: true,
|
||||
|
||||
Reference in New Issue
Block a user