2023-08-01 19:35:19 +00:00
|
|
|
import type { CompleteEvent } from "$sb/app_event.ts";
|
2023-08-28 15:12:15 +00:00
|
|
|
import { events } from "$sb/syscalls.ts";
|
2023-10-03 12:16:33 +00:00
|
|
|
import { queryObjects } from "./api.ts";
|
|
|
|
import { ObjectValue, QueryExpression } from "$sb/types.ts";
|
|
|
|
import { builtinPseudoPage } from "./builtins.ts";
|
2023-08-01 19:35:19 +00:00
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
export type AttributeObject = ObjectValue<{
|
|
|
|
name: string;
|
|
|
|
attributeType: string;
|
|
|
|
tag: string;
|
|
|
|
page: string;
|
|
|
|
}>;
|
2023-08-01 19:35:19 +00:00
|
|
|
|
2023-08-08 14:35:46 +00:00
|
|
|
export type AttributeCompleteEvent = {
|
|
|
|
source: string;
|
|
|
|
prefix: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type AttributeCompletion = {
|
|
|
|
name: string;
|
|
|
|
source: string;
|
2023-10-03 12:16:33 +00:00
|
|
|
attributeType: string;
|
2023-08-08 14:35:46 +00:00
|
|
|
builtin?: boolean;
|
|
|
|
};
|
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
export function determineType(v: any): string {
|
2023-08-01 19:35:19 +00:00
|
|
|
const t = typeof v;
|
|
|
|
if (t === "object") {
|
|
|
|
if (Array.isArray(v)) {
|
|
|
|
return "array";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
export async function objectAttributeCompleter(
|
2023-08-08 14:35:46 +00:00
|
|
|
attributeCompleteEvent: AttributeCompleteEvent,
|
|
|
|
): Promise<AttributeCompletion[]> {
|
2023-10-03 12:16:33 +00:00
|
|
|
const attributeFilter: QueryExpression | undefined =
|
|
|
|
attributeCompleteEvent.source === ""
|
|
|
|
? undefined
|
|
|
|
: ["=", ["attr", "tag"], ["string", attributeCompleteEvent.source]];
|
|
|
|
const allAttributes = await queryObjects<AttributeObject>("attribute", {
|
|
|
|
filter: attributeFilter,
|
2023-08-08 14:35:46 +00:00
|
|
|
});
|
2023-10-03 12:16:33 +00:00
|
|
|
return allAttributes.map((value) => {
|
2023-08-08 14:35:46 +00:00
|
|
|
return {
|
2023-10-03 12:16:33 +00:00
|
|
|
name: value.name,
|
|
|
|
source: value.tag,
|
|
|
|
attributeType: value.attributeType,
|
|
|
|
builtin: value.page === builtinPseudoPage,
|
|
|
|
} as AttributeCompletion;
|
2023-08-08 14:35:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-01 19:35:19 +00:00
|
|
|
export async function attributeComplete(completeEvent: CompleteEvent) {
|
2023-09-01 14:57:29 +00:00
|
|
|
if (/([\-\*]\s+\[)([^\]]+)$/.test(completeEvent.linePrefix)) {
|
|
|
|
// Don't match task states, which look similar
|
|
|
|
return null;
|
|
|
|
}
|
2023-08-04 19:35:58 +00:00
|
|
|
const inlineAttributeMatch = /([^\[\{}]|^)\[(\w+)$/.exec(
|
2023-08-01 19:35:19 +00:00
|
|
|
completeEvent.linePrefix,
|
|
|
|
);
|
|
|
|
if (inlineAttributeMatch) {
|
|
|
|
// console.log("Parents", completeEvent.parentNodes);
|
|
|
|
let type = "page";
|
|
|
|
if (completeEvent.parentNodes.includes("Task")) {
|
|
|
|
type = "task";
|
|
|
|
} else if (completeEvent.parentNodes.includes("ListItem")) {
|
|
|
|
type = "item";
|
|
|
|
}
|
2023-08-08 14:35:46 +00:00
|
|
|
const completions = (await events.dispatchEvent(
|
|
|
|
`attribute:complete:${type}`,
|
|
|
|
{
|
|
|
|
source: type,
|
|
|
|
prefix: inlineAttributeMatch[2],
|
|
|
|
} as AttributeCompleteEvent,
|
|
|
|
)).flat() as AttributeCompletion[];
|
2023-08-01 19:35:19 +00:00
|
|
|
return {
|
|
|
|
from: completeEvent.pos - inlineAttributeMatch[2].length,
|
2023-08-08 14:35:46 +00:00
|
|
|
options: attributeCompletionsToCMCompletion(
|
|
|
|
completions.filter((completion) => !completion.builtin),
|
|
|
|
),
|
2023-08-01 19:35:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
const attributeMatch = /^(\w+)$/.exec(completeEvent.linePrefix);
|
|
|
|
if (attributeMatch) {
|
|
|
|
if (completeEvent.parentNodes.includes("FrontMatterCode")) {
|
2023-08-08 14:35:46 +00:00
|
|
|
const completions = (await events.dispatchEvent(
|
|
|
|
`attribute:complete:page`,
|
|
|
|
{
|
|
|
|
source: "page",
|
|
|
|
prefix: attributeMatch[1],
|
|
|
|
} as AttributeCompleteEvent,
|
|
|
|
)).flat() as AttributeCompletion[];
|
2023-08-01 19:35:19 +00:00
|
|
|
return {
|
|
|
|
from: completeEvent.pos - attributeMatch[1].length,
|
2023-08-08 14:35:46 +00:00
|
|
|
options: attributeCompletionsToCMCompletion(
|
|
|
|
completions.filter((completion) => !completion.builtin),
|
|
|
|
),
|
2023-08-01 19:35:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2023-08-08 14:35:46 +00:00
|
|
|
|
|
|
|
export function attributeCompletionsToCMCompletion(
|
|
|
|
completions: AttributeCompletion[],
|
|
|
|
) {
|
|
|
|
return completions.map(
|
|
|
|
(completion) => ({
|
|
|
|
label: completion.name,
|
|
|
|
apply: `${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",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|