Wire manifest workbench menus into project shell

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
2026-11-13 16:15:31 +08:00
parent 70a6c17165
commit 26c8a104cd
8 changed files with 1410 additions and 724 deletions

View File

@@ -75,6 +75,7 @@ const pythonCalls = (text, name) => {
const calls = [];
let index = 0;
while ((index = text.indexOf(`${name}(`, index)) >= 0) {
const callIndex = index;
let cursor = index + name.length + 1;
let depth = 1; let quote = "";
for (; cursor < text.length; cursor++) {
@@ -85,7 +86,7 @@ const pythonCalls = (text, name) => {
if (char === ")" || char === "]") depth--;
if (depth === 0) break;
}
calls.push(text.slice(index + name.length + 1, cursor));
calls.push({ body: text.slice(callIndex + name.length + 1, cursor), index: callIndex });
index = cursor + 1;
}
return calls;
@@ -106,16 +107,24 @@ const splitPythonArgs = value => {
};
const parsePythonGui = text => {
const vars = new Map();
const assignments = new Map();
const push = (name, entry) => {
if (!assignments.has(name)) assignments.set(name, []);
assignments.get(name).push(entry);
};
for (const match of text.matchAll(/^\s*(?:self\.)?(\w+)\s*=\s*\[([\s\S]*?)\]\s*$/gm)) {
vars.set(match[1], { type: "list", value: [...match[2].matchAll(/["']([^"']+)["']/g)].map(item => item[1]) });
push(match[1], { pos: match.index, type: "list",
value: [...match[2].matchAll(/["']([^"']+)["']/g)].map(item => item[1]) });
}
for (const match of text.matchAll(/^\s*(?:self\.)?(\w+)\s*=\s*QT_TRANSLATE_NOOP\([^,]+,\s*"([^"]*)"\)/gm)) {
vars.set(match[1], { type: "label", value: match[2] });
push(match[1], { pos: match.index, type: "label", value: match[2] });
}
const resolve = expr => {
const variable = (name, at) => (assignments.get(name) ?? [])
.filter(entry => entry.pos <= at).sort((a, b) => a.pos - b.pos).at(-1) ?? null;
const resolve = (expr, at) => {
const value = expr.trim();
if (vars.has(value)) return vars.get(value);
const found = variable(value.replace(/^self\./, ""), at);
if (found) return found;
if (/^\[/.test(value)) return { type: "list", value: [...value.matchAll(/["']([^"']+)["']/g)].map(item => item[1]) };
const translated = /QT_TRANSLATE_NOOP\([^,]+,\s*"([^"]*)"\)/.exec(value);
if (translated) return { type: "label", value: translated[1] };
@@ -123,27 +132,27 @@ const parsePythonGui = text => {
if (quoted) return { type: "label", value: quoted[1] };
return null;
};
const resolveList = expr => {
if (expr.includes("+")) return splitPythonArgs(expr.replace(/\+/g, ",")).flatMap(resolveList);
const resolved = resolve(expr);
const resolveList = (expr, at) => {
if (expr.includes("+")) return splitPythonArgs(expr.replace(/\+/g, ",")).flatMap(part => resolveList(part, at));
const resolved = resolve(expr, at);
if (!resolved) return [];
return resolved.type === "list" ? resolved.value : [resolved.value];
};
const menus = [];
for (const body of pythonCalls(text, "appendMenu")) {
const args = splitPythonArgs(body);
for (const call of pythonCalls(text, "appendMenu")) {
const args = splitPythonArgs(call.body);
if (args.length < 2) continue;
const labels = resolveList(args[0]);
const items = resolveList(args.at(-1));
const labels = resolveList(args[0], call.index);
const items = resolveList(args.at(-1), call.index);
for (const label of labels) menus.push({ label: label.replace(/^&/, ""), items: items.map(id =>
({ type: id === "Separator" ? "separator" : "command", id })) });
}
const toolbars = [];
for (const body of pythonCalls(text, "appendToolbar")) {
const args = splitPythonArgs(body);
for (const call of pythonCalls(text, "appendToolbar")) {
const args = splitPythonArgs(call.body);
if (args.length < 2) continue;
const names = resolveList(args[0]);
const items = resolveList(args[1]);
const names = resolveList(args[0], call.index);
const items = resolveList(args[1], call.index);
for (const name of names) toolbars.push({ name: name.replace(/^&/, ""), items });
}
return { menus, toolbars };