From 11a2adbea6cb10d3d777a389474ee32d32a1d2e2 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Fri, 17 Jan 2025 19:10:46 +0100 Subject: [PATCH] Lua script block prioritization --- common/space_lua.ts | 7 ++++++- plugs/index/script.ts | 11 ++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/common/space_lua.ts b/common/space_lua.ts index 4e48eda0..66c76a20 100644 --- a/common/space_lua.ts +++ b/common/space_lua.ts @@ -28,7 +28,12 @@ export class SpaceLuaEnvironment { ) { const allScripts: ScriptObject[] = await system.invokeFunction( "index.queryObjects", - ["space-lua", {}], + ["space-lua", { + orderBy: [{ + expr: ["attr", "priority"], + desc: true, + }], + }], ); try { this.env = buildLuaEnv(system, scriptEnv); diff --git a/plugs/index/script.ts b/plugs/index/script.ts index d0c14970..5ad54208 100644 --- a/plugs/index/script.ts +++ b/plugs/index/script.ts @@ -5,6 +5,7 @@ import { indexObjects } from "./api.ts"; import { space } from "@silverbulletmd/silverbullet/syscalls"; export type ScriptObject = ObjectValue<{ script: string; + priority?: number; }>; export async function indexSpaceScript({ name, tree }: IndexTreeEvent) { @@ -50,10 +51,14 @@ export async function indexSpaceLua({ name, tree }: IndexTreeEvent) { return; } const codeText = codeTextNode.children![0].text!; + // Parse out "-- priority: " + const priority = codeText.match(/--\s*priority:\s*(-?\d+)/)?.[1]; + allScripts.push({ ref: `${name}@${t.from!}`, tag: "space-lua", script: codeText, + priority: priority !== undefined ? +priority : undefined, }); }); await indexObjects(name, allScripts); @@ -65,9 +70,13 @@ export async function indexSpaceLuaFile(name: string) { } console.log("Indexing space lua file", name); const data = await space.readFile(name); + const code = new TextDecoder().decode(data); + // Parse out "-- priority: " + const priority = code.match(/--\s*priority:\s*(-?\d+)/)?.[1]; await indexObjects(name, [{ ref: `${name}`, tag: "space-lua", - script: new TextDecoder().decode(data), + script: code, + priority: priority !== undefined ? +priority : undefined, }]); }