Attribute indexing and code completion
This commit is contained in:
+87
-13
@@ -2,23 +2,97 @@ import { events } from "$sb/plugos-syscall/mod.ts";
|
||||
import { CompleteEvent } from "$sb/app_event.ts";
|
||||
import { buildHandebarOptions } from "./util.ts";
|
||||
import type { PageMeta } from "../../web/types.ts";
|
||||
import { index } from "$sb/silverbullet-syscall/mod.ts";
|
||||
|
||||
const builtinAttributes: Record<string, Record<string, string>> = {
|
||||
page: {
|
||||
name: "string",
|
||||
lastModified: "number",
|
||||
perm: "rw|ro",
|
||||
contentType: "string",
|
||||
size: "number",
|
||||
tags: "array",
|
||||
},
|
||||
task: {
|
||||
name: "string",
|
||||
done: "boolean",
|
||||
page: "string",
|
||||
deadline: "string",
|
||||
pos: "number",
|
||||
tags: "array",
|
||||
},
|
||||
item: {
|
||||
name: "string",
|
||||
page: "string",
|
||||
pos: "number",
|
||||
tags: "array",
|
||||
},
|
||||
tag: {
|
||||
name: "string",
|
||||
freq: "number",
|
||||
},
|
||||
};
|
||||
|
||||
export async function queryComplete(completeEvent: CompleteEvent) {
|
||||
const match = /#query ([\w\-_]*)$/.exec(completeEvent.linePrefix);
|
||||
if (!match) {
|
||||
return null;
|
||||
const querySourceMatch = /#query\s+([\w\-_]*)$/.exec(
|
||||
completeEvent.linePrefix,
|
||||
);
|
||||
if (querySourceMatch) {
|
||||
const allEvents = await events.listEvents();
|
||||
|
||||
return {
|
||||
from: completeEvent.pos - querySourceMatch[1].length,
|
||||
options: allEvents
|
||||
.filter((eventName) => eventName.startsWith("query:"))
|
||||
.map((source) => ({
|
||||
label: source.substring("query:".length),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
const allEvents = await events.listEvents();
|
||||
|
||||
return {
|
||||
from: completeEvent.pos - match[1].length,
|
||||
options: allEvents
|
||||
.filter((eventName) => eventName.startsWith("query:"))
|
||||
.map((source) => ({
|
||||
label: source.substring("query:".length),
|
||||
})),
|
||||
};
|
||||
if (completeEvent.parentNodes.includes("DirectiveStart")) {
|
||||
const querySourceMatch = /#query\s+([\w\-_]+)/.exec(
|
||||
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];
|
||||
// console.log("Type", type);
|
||||
// console.log("Where", attributePrefix);
|
||||
const allAttributes = await index.queryPrefix(
|
||||
`attr:${type}:`,
|
||||
);
|
||||
const customAttributesCompletions = allAttributes.map((attr) => {
|
||||
const [_prefix, _context, name] = attr.key.split(":");
|
||||
return {
|
||||
label: name,
|
||||
detail: attr.value.type,
|
||||
type: "attribute",
|
||||
};
|
||||
});
|
||||
const builtinAttributesCompletions = builtinAttributes[type]
|
||||
? Object.entries(
|
||||
builtinAttributes[type],
|
||||
).map(([name, type]) => ({
|
||||
label: name,
|
||||
detail: type,
|
||||
type: "attribute",
|
||||
}))
|
||||
: [];
|
||||
return {
|
||||
from: completeEvent.pos - attributePrefix.length,
|
||||
options: [
|
||||
...customAttributesCompletions,
|
||||
...builtinAttributesCompletions,
|
||||
],
|
||||
};
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function handlebarHelperComplete(completeEvent: CompleteEvent) {
|
||||
|
||||
Reference in New Issue
Block a user