Add aliases field to template functions

This commit is contained in:
Gregory Schier
2024-09-30 18:28:52 -07:00
parent 9915c57817
commit 6060ddcd87
10 changed files with 44 additions and 145 deletions

View File

@@ -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);

View File

@@ -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,

View File

@@ -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,