1
0
silverbullet/plugs/index/attributes.ts

119 lines
3.3 KiB
TypeScript
Raw Normal View History

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";
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
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;
attributeType: string;
2023-08-08 14:35:46 +00:00
builtin?: boolean;
};
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;
}
export async function objectAttributeCompleter(
2023-08-08 14:35:46 +00:00
attributeCompleteEvent: AttributeCompleteEvent,
): Promise<AttributeCompletion[]> {
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
});
return allAttributes.map((value) => {
2023-08-08 14:35:46 +00:00
return {
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) {
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}: `,
detail: `${completion.attributeType} (${completion.source})`,
2023-08-08 14:35:46 +00:00
type: "attribute",
}),
);
}