2024-07-30 23:33:33 +08:00
|
|
|
import type { CompleteEvent } from "../../plug-api/types.ts";
|
2024-08-07 02:11:38 +08:00
|
|
|
import { events, language } from "@silverbulletmd/silverbullet/syscalls";
|
2024-07-30 23:33:33 +08:00
|
|
|
import type {
|
2023-10-03 20:16:33 +08:00
|
|
|
AttributeCompleteEvent,
|
|
|
|
AttributeCompletion,
|
|
|
|
} from "../index/attributes.ts";
|
|
|
|
|
|
|
|
export async function queryComplete(completeEvent: CompleteEvent) {
|
2024-02-03 02:19:07 +08:00
|
|
|
let querySourceMatch: RegExpExecArray | null = null;
|
|
|
|
|
|
|
|
// Let's check if this is a query block
|
|
|
|
let fencedParent = completeEvent.parentNodes.find((node) =>
|
2023-10-04 23:14:24 +08:00
|
|
|
node.startsWith("FencedCode:query")
|
2023-10-03 20:16:33 +08:00
|
|
|
);
|
2024-02-03 02:19:07 +08:00
|
|
|
if (fencedParent) {
|
|
|
|
// Yep, let's see if we can do source completion
|
2024-02-04 23:36:59 +08:00
|
|
|
querySourceMatch = /^\s*()([\w\-_]*)$/.exec(
|
2024-02-03 02:19:07 +08:00
|
|
|
completeEvent.linePrefix,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Not a query, perhaps a template then?
|
|
|
|
fencedParent = completeEvent.parentNodes.find((node) =>
|
|
|
|
node.startsWith("FencedCode:template")
|
|
|
|
);
|
|
|
|
|
|
|
|
if (fencedParent) {
|
2024-02-04 23:36:59 +08:00
|
|
|
// Match "{{{source" or "{source" (without a { before it, because that would be a variable)
|
|
|
|
querySourceMatch = /([^{]|\{\{)\{(\s*[\w\-_]+)$/.exec(
|
2024-02-03 02:19:07 +08:00
|
|
|
completeEvent.linePrefix,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// No? Then we're out, sorry.
|
|
|
|
return null;
|
|
|
|
}
|
2023-10-03 20:16:33 +08:00
|
|
|
}
|
|
|
|
if (querySourceMatch) {
|
|
|
|
const allEvents = await events.listEvents();
|
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2024-02-04 23:36:59 +08:00
|
|
|
from: completeEvent.pos - querySourceMatch[2].length,
|
2023-10-03 20:16:33 +08:00
|
|
|
options: completionOptions,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-02-04 23:36:59 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function queryAttributeComplete(completeEvent: CompleteEvent) {
|
|
|
|
const fencedParent = completeEvent.parentNodes.find((node) =>
|
|
|
|
node.startsWith("FencedCode:query") ||
|
|
|
|
node.startsWith("FencedCode:template")
|
2023-10-03 20:16:33 +08:00
|
|
|
);
|
2024-02-04 23:36:59 +08:00
|
|
|
if (!fencedParent) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// For this we do need to find the query source, though, so let's look for it
|
|
|
|
let querySourceMatch: RegExpExecArray | null = null;
|
|
|
|
if (fencedParent.startsWith("FencedCode:query")) {
|
|
|
|
querySourceMatch = /^[\n\r\s]*([\w\-_]+)/.exec(
|
|
|
|
fencedParent.slice("FencedCode:query".length),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// We're in a template, so let's just consider the current line and see if we can find the source
|
|
|
|
querySourceMatch = /\{(\s*[\w\-_]+)\s+/.exec(
|
|
|
|
completeEvent.linePrefix,
|
|
|
|
);
|
|
|
|
}
|
2023-10-03 20:16:33 +08:00
|
|
|
const whereMatch =
|
|
|
|
/(where|order\s+by|and|or|select(\s+[\w\s,]+)?)\s+([\w\-_]*)$/
|
|
|
|
.exec(
|
|
|
|
completeEvent.linePrefix,
|
|
|
|
);
|
|
|
|
if (querySourceMatch && whereMatch) {
|
2024-02-04 23:36:59 +08:00
|
|
|
const type = querySourceMatch[1];
|
2023-10-03 20:16:33 +08:00
|
|
|
const attributePrefix = whereMatch[3];
|
|
|
|
const completions = (await events.dispatchEvent(
|
|
|
|
`attribute:complete:${type}`,
|
|
|
|
{
|
|
|
|
source: type,
|
|
|
|
prefix: attributePrefix,
|
|
|
|
} as AttributeCompleteEvent,
|
|
|
|
)).flat() as AttributeCompletion[];
|
|
|
|
return {
|
|
|
|
from: completeEvent.pos - attributePrefix.length,
|
|
|
|
options: attributeCompletionsToCMCompletion(completions),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function attributeCompletionsToCMCompletion(
|
|
|
|
completions: AttributeCompletion[],
|
|
|
|
) {
|
|
|
|
return completions.map(
|
|
|
|
(completion) => ({
|
|
|
|
label: completion.name,
|
|
|
|
detail: `${completion.attributeType} (${completion.source})`,
|
|
|
|
type: "attribute",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2023-10-04 23:14:24 +08:00
|
|
|
|
|
|
|
export async function languageComplete(completeEvent: CompleteEvent) {
|
2024-02-23 16:03:14 +08:00
|
|
|
const languagePrefix = /^(?:```+|~~~+)(\w*)$/.exec(
|
2023-10-04 23:14:24 +08:00
|
|
|
completeEvent.linePrefix,
|
|
|
|
);
|
|
|
|
if (!languagePrefix) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const allLanguages = await language.listLanguages();
|
|
|
|
return {
|
|
|
|
from: completeEvent.pos - languagePrefix[1].length,
|
|
|
|
options: allLanguages.map(
|
|
|
|
(lang) => ({
|
|
|
|
label: lang,
|
|
|
|
type: "language",
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|