2024-01-21 02:16:07 +08:00
|
|
|
import { CompleteEvent } from "$sb/app_event.ts";
|
2024-02-03 02:19:07 +08:00
|
|
|
import { datastore, events } from "$sb/syscalls.ts";
|
|
|
|
|
2024-01-21 02:16:07 +08:00
|
|
|
import {
|
|
|
|
AttributeCompleteEvent,
|
|
|
|
AttributeCompletion,
|
|
|
|
} from "../index/attributes.ts";
|
|
|
|
import { attributeCompletionsToCMCompletion } from "./snippet.ts";
|
|
|
|
|
|
|
|
export async function templateVariableComplete(completeEvent: CompleteEvent) {
|
2024-02-03 02:19:07 +08:00
|
|
|
// Check if we're in a query, block or template context
|
|
|
|
const fencedParent = completeEvent.parentNodes.find((node) =>
|
|
|
|
node.startsWith("FencedCode:query") ||
|
|
|
|
node.startsWith("FencedCode:template")
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!fencedParent) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const match = /(@|[^\{]\{\{)(\w*)$/.exec(completeEvent.linePrefix);
|
2024-01-21 02:16:07 +08:00
|
|
|
if (!match) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-02-03 02:19:07 +08:00
|
|
|
let allCompletions: any[] = [];
|
|
|
|
|
|
|
|
if (match[1] !== "@") {
|
|
|
|
// Not a variable
|
|
|
|
// Function completions
|
|
|
|
const builtinFunctions = await datastore.listFunctions();
|
|
|
|
allCompletions = builtinFunctions.map((name) => ({
|
|
|
|
label: name,
|
|
|
|
detail: "function",
|
|
|
|
}));
|
2024-01-21 02:16:07 +08:00
|
|
|
|
2024-02-03 02:19:07 +08:00
|
|
|
// Attribute completions
|
|
|
|
const completions = (await events.dispatchEvent(
|
|
|
|
`attribute:complete:_`,
|
|
|
|
{
|
|
|
|
source: "",
|
|
|
|
prefix: match[2],
|
|
|
|
} as AttributeCompleteEvent,
|
|
|
|
)).flat() as AttributeCompletion[];
|
2024-01-21 02:16:07 +08:00
|
|
|
|
2024-02-03 02:19:07 +08:00
|
|
|
allCompletions = allCompletions.concat(
|
|
|
|
attributeCompletionsToCMCompletion(completions),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const allVariables = [...fencedParent.match(/@(\w+)/g) || []];
|
|
|
|
allVariables.push("@page");
|
2024-01-21 02:16:07 +08:00
|
|
|
allCompletions = allCompletions.concat(
|
2024-02-03 02:19:07 +08:00
|
|
|
allVariables.filter((v) => v !== match[0]).map((key) => ({
|
|
|
|
label: key,
|
|
|
|
apply: key.substring(1),
|
|
|
|
detail: "variable",
|
|
|
|
})),
|
2024-01-21 02:16:07 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
2024-02-03 02:19:07 +08:00
|
|
|
from: completeEvent.pos - match[2].length,
|
2024-01-21 02:16:07 +08:00
|
|
|
options: allCompletions,
|
|
|
|
};
|
|
|
|
}
|