Global anchor completion

pull/555/head
Zef Hemel 2023-11-02 16:19:33 +01:00
parent 74387cb5b1
commit 3f1aa45b5d
1 changed files with 11 additions and 8 deletions

View File

@ -1,7 +1,7 @@
import { collectNodesOfType } from "$sb/lib/tree.ts"; import { collectNodesOfType } from "$sb/lib/tree.ts";
import type { CompleteEvent, IndexTreeEvent } from "$sb/app_event.ts"; import type { CompleteEvent, IndexTreeEvent } from "$sb/app_event.ts";
import { removeQueries } from "$sb/lib/query.ts"; import { removeQueries } from "$sb/lib/query.ts";
import { ObjectValue } from "$sb/types.ts"; import { ObjectValue, QueryExpression } from "$sb/types.ts";
import { indexObjects, queryObjects } from "./api.ts"; import { indexObjects, queryObjects } from "./api.ts";
type AnchorObject = ObjectValue<{ type AnchorObject = ObjectValue<{
@ -34,17 +34,20 @@ export async function anchorComplete(completeEvent: CompleteEvent) {
return null; return null;
} }
let [pageRef, anchorRef] = match[1].split("@"); const pageRef = match[1].split("@")[0];
let filter: QueryExpression | undefined = ["=", ["attr", "page"], [
"string",
pageRef,
]];
if (!pageRef) { if (!pageRef) {
pageRef = completeEvent.pageName; // "bare" anchor, match any page for completion purposes
filter = undefined;
} }
const allAnchors = await queryObjects<AnchorObject>("anchor", { const allAnchors = await queryObjects<AnchorObject>("anchor", { filter });
filter: ["=", ["attr", "page"], ["string", pageRef]],
});
return { return {
from: completeEvent.pos - anchorRef.length, from: completeEvent.pos - match[1].length,
options: allAnchors.map((a) => ({ options: allAnchors.map((a) => ({
label: a.name, label: a.page === completeEvent.pageName ? `@${a.name}` : a.ref,
type: "anchor", type: "anchor",
})), })),
}; };