2023-08-28 15:12:15 +00:00
|
|
|
import { events } from "$sb/syscalls.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
import { CompleteEvent } from "$sb/app_event.ts";
|
2023-07-24 09:09:17 +00:00
|
|
|
import { buildHandebarOptions } from "./util.ts";
|
2023-08-28 15:12:15 +00:00
|
|
|
import type {
|
2023-08-08 14:35:46 +00:00
|
|
|
AttributeCompleteEvent,
|
|
|
|
AttributeCompletion,
|
2023-08-28 15:12:15 +00:00
|
|
|
} from "../index/attributes.ts";
|
2023-10-03 12:16:33 +00:00
|
|
|
import { PageMeta } from "$sb/types.ts";
|
2022-07-04 13:07:27 +00:00
|
|
|
|
2022-12-21 13:55:24 +00:00
|
|
|
export async function queryComplete(completeEvent: CompleteEvent) {
|
2023-08-01 19:35:19 +00:00
|
|
|
const querySourceMatch = /#query\s+([\w\-_]*)$/.exec(
|
|
|
|
completeEvent.linePrefix,
|
|
|
|
);
|
|
|
|
if (querySourceMatch) {
|
|
|
|
const allEvents = await events.listEvents();
|
2022-07-04 13:07:27 +00:00
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
const completionOptions = allEvents
|
|
|
|
.filter((eventName) =>
|
|
|
|
eventName.startsWith("query:") && !eventName.includes("*")
|
|
|
|
)
|
|
|
|
.map((source) => ({
|
|
|
|
label: source.substring("query:".length),
|
|
|
|
}));
|
|
|
|
|
|
|
|
const allObjectTypes: string[] = (await events.dispatchEvent("query_", {}))
|
|
|
|
.flat();
|
|
|
|
|
|
|
|
for (const type of allObjectTypes) {
|
|
|
|
completionOptions.push({
|
|
|
|
label: type,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-01 19:35:19 +00:00
|
|
|
return {
|
|
|
|
from: completeEvent.pos - querySourceMatch[1].length,
|
2023-10-03 12:16:33 +00:00
|
|
|
options: completionOptions,
|
2023-08-01 19:35:19 +00:00
|
|
|
};
|
|
|
|
}
|
2022-07-04 13:07:27 +00:00
|
|
|
|
2023-08-01 19:35:19 +00:00
|
|
|
if (completeEvent.parentNodes.includes("DirectiveStart")) {
|
2023-10-03 12:16:33 +00:00
|
|
|
const querySourceMatch = /#query\s+([\w\-_\/]+)/.exec(
|
2023-08-01 19:35:19 +00:00
|
|
|
completeEvent.linePrefix,
|
|
|
|
);
|
|
|
|
const whereMatch =
|
|
|
|
/(where|order\s+by|and|select(\s+[\w\s,]+)?)\s+([\w\-_]*)$/.exec(
|
|
|
|
completeEvent.linePrefix,
|
|
|
|
);
|
|
|
|
if (querySourceMatch && whereMatch) {
|
|
|
|
const type = querySourceMatch[1];
|
|
|
|
const attributePrefix = whereMatch[3];
|
2023-08-08 14:35:46 +00:00
|
|
|
const completions = (await events.dispatchEvent(
|
|
|
|
`attribute:complete:${type}`,
|
|
|
|
{
|
|
|
|
source: type,
|
|
|
|
prefix: attributePrefix,
|
|
|
|
} as AttributeCompleteEvent,
|
|
|
|
)).flat() as AttributeCompletion[];
|
2023-08-01 19:35:19 +00:00
|
|
|
return {
|
|
|
|
from: completeEvent.pos - attributePrefix.length,
|
2023-08-08 14:35:46 +00:00
|
|
|
options: attributeCompletionsToCMCompletion(completions),
|
2023-08-01 19:35:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2022-07-04 13:07:27 +00:00
|
|
|
}
|
2023-07-02 09:25:32 +00:00
|
|
|
|
2023-08-02 06:35:24 +00:00
|
|
|
export async function templateVariableComplete(completeEvent: CompleteEvent) {
|
2023-07-02 09:25:32 +00:00
|
|
|
const match = /\{\{([\w@]*)$/.exec(completeEvent.linePrefix);
|
|
|
|
if (!match) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const handlebarOptions = buildHandebarOptions({ name: "" } as PageMeta);
|
2023-08-02 06:35:24 +00:00
|
|
|
let allCompletions: any[] = Object.keys(handlebarOptions.helpers).map(
|
|
|
|
(name) => ({ label: name, detail: "helper" }),
|
|
|
|
);
|
|
|
|
allCompletions = allCompletions.concat(
|
|
|
|
Object.keys(handlebarOptions.data).map((key) => ({
|
|
|
|
label: `@${key}`,
|
|
|
|
detail: "global variable",
|
|
|
|
})),
|
|
|
|
);
|
|
|
|
|
2023-08-08 14:35:46 +00:00
|
|
|
const completions = (await events.dispatchEvent(
|
2023-10-03 12:16:33 +00:00
|
|
|
`attribute:complete:_`,
|
2023-08-08 14:35:46 +00:00
|
|
|
{
|
2023-10-03 12:16:33 +00:00
|
|
|
source: "",
|
2023-08-08 14:35:46 +00:00
|
|
|
prefix: match[1],
|
|
|
|
} as AttributeCompleteEvent,
|
|
|
|
)).flat() as AttributeCompletion[];
|
|
|
|
|
2023-08-02 06:35:24 +00:00
|
|
|
allCompletions = allCompletions.concat(
|
2023-08-08 14:35:46 +00:00
|
|
|
attributeCompletionsToCMCompletion(completions),
|
2023-07-02 09:25:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
from: completeEvent.pos - match[1].length,
|
2023-08-02 06:35:24 +00:00
|
|
|
options: allCompletions,
|
2023-07-02 09:25:32 +00:00
|
|
|
};
|
|
|
|
}
|
2023-08-08 14:35:46 +00:00
|
|
|
|
|
|
|
export function attributeCompletionsToCMCompletion(
|
|
|
|
completions: AttributeCompletion[],
|
|
|
|
) {
|
|
|
|
return completions.map(
|
|
|
|
(completion) => ({
|
|
|
|
label: completion.name,
|
2023-10-03 12:16:33 +00:00
|
|
|
detail: `${completion.attributeType} (${completion.source})`,
|
2023-08-08 14:35:46 +00:00
|
|
|
type: "attribute",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|