Elffuss BraidHub

utopia-ia/elffuss-code public

main · 5.7 KiBRawPermalink
1// Comandos de las extensiones con código en Code: salen en F1 (acciones de
2// Monaco), en el menú contextual del editor si la extensión lo pide, y como
3// entradas para la paleta de Code (entries()). Sus atajos se atan con el foco
4// en el editor, salvo los reservados (keys.js). Al quitar la extensión o parar
5// el puente, todo se deshace.
6import { parseKeybinding } from './keys.js';
7
8const s = (v, max = 300) => (typeof v === 'string' ? v.slice(0, max) : '');
9
10// manifiesto (ya con %nls% resuelto) → comandos visibles y atajos
11export function commandsOf(manifest) {
12 const c = manifest && typeof manifest.contributes === 'object' && manifest.contributes ? manifest.contributes : {};
13 const hidden = new Set();
14 const palette = Array.isArray(c.menus?.commandPalette) ? c.menus.commandPalette : [];
15 for (const m of palette) if (m && typeof m.command === 'string' && (m.when === 'false' || m.when === false)) hidden.add(m.command);
16 const contextMenu = new Map();
17 for (const m of Array.isArray(c.menus?.['editor/context']) ? c.menus['editor/context'] : []) {
18 if (m && typeof m.command === 'string') contextMenu.set(m.command, s(m.group, 100) || 'x2');
19 }
20 const list = [];
21 for (const cmd of Array.isArray(c.commands) ? c.commands : []) {
22 if (!cmd || typeof cmd.command !== 'string' || !/^[\w.:/-]{1,200}$/.test(cmd.command)) continue;
23 const title = typeof cmd.title === 'string' ? cmd.title : cmd.title && typeof cmd.title.value === 'string' ? cmd.title.value : cmd.command;
24 const category = typeof cmd.category === 'string' ? cmd.category : cmd.category && typeof cmd.category.value === 'string' ? cmd.category.value : '';
25 list.push({ id: cmd.command, title: s(title), category: s(category, 100), hidden: hidden.has(cmd.command), contextGroup: contextMenu.get(cmd.command) || null });
26 }
27 return { commands: list, keybindings: Array.isArray(c.keybindings) ? c.keybindings.slice(0, 500) : [] };
28}
29
30// La lista viva de la paleta de Code (main.js la expone en
31// window.elffussClaw.commands y la relee en cada repintado). Este módulo
32// nunca importa main.js: solo mete y saca sus propias entradas.
33const paletteList = () => {
34 const l = globalThis.window?.elffussClaw?.commands;
35 return Array.isArray(l) ? l : null;
36};
37
38export class Commands {
39 constructor({ monaco, editor, run, onReport = () => {} }) {
40 this.monaco = monaco;
41 this.editor = editor;
42 this.run = run;
43 this.onReport = onReport;
44 this.byExt = new Map(); // id → { disposables, entries, palette, skipped }
45 this.chords = new Map(); // atajo (número de Monaco) → id de la extensión que lo tiene
46 }
47
48 set(ext) {
49 this.remove(ext.id);
50 const { monaco, editor } = this;
51 const { commands, keybindings } = commandsOf(ext.manifest);
52 const disposables = [], entries = [], palette = [], skipped = [];
53 const name = s(ext.displayName || ext.manifest?.displayName || ext.id, 200);
54 for (const c of commands) {
55 const label = c.category ? `${c.category}: ${c.title}` : c.title;
56 const run = () => this.run(c.id);
57 // F1 y menú contextual (Monaco pinta las etiquetas como texto)
58 disposables.push(editor.addAction({
59 id: 'x2:' + c.id, label,
60 ...(c.contextGroup ? { contextMenuGroupId: 'x2_' + c.contextGroup.replace(/[^\w]/g, '_'), contextMenuOrder: 1 } : {}),
61 run,
62 }));
63 if (c.hidden) continue;
64 entries.push({ id: c.id, label, hint: name, run, ext: ext.id });
65 // en la paleta de Code: se cierra y el comando actúa sobre el editor
66 palette.push({
67 label, hint: name, x2: ext.id,
68 run: () => {
69 const p = document.getElementById('palette');
70 if (p) p.hidden = true;
71 editor.focus();
72 return run();
73 },
74 });
75 }
76 const list = paletteList();
77 if (list) list.push(...palette);
78 // atajos: un comando global de Monaco + su regla de teclado (ambos se deshacen)
79 const registered = new Set();
80 for (const [i, kb] of keybindings.entries()) {
81 const p = parseKeybinding(monaco, kb);
82 // (la tecla que se miró en esta plataforma: «cmd+p» en un Mac, no «ctrl+p»)
83 if (p.skip) { skipped.push({ command: s(kb?.command, 200), key: s(p.key || kb?.key || kb?.mac, 100), why: p.skip, code: p.code || 'invalid' }); continue; }
84 // dos extensiones con el mismo atajo: se queda la primera y se avisa
85 const owner = this.chords.get(p.keybinding);
86 if (owner && owner !== ext.id) { skipped.push({ command: s(kb.command, 200), key: p.key, why: 'conflicts with ' + owner, code: 'conflict', conflict: owner }); continue; }
87 this.chords.set(p.keybinding, ext.id);
88 const id = `x2-key:${ext.id}:${i}`;
89 if (!registered.has(id)) {
90 registered.add(id);
91 disposables.push(monaco.editor.addCommand({ id, run: () => this.run(p.command, p.args) }));
92 }
93 const when = 'editorTextFocus' + (p.when ? ` && (${p.when})` : '');
94 disposables.push(monaco.editor.addKeybindingRules([{ keybinding: p.keybinding, command: id, when }]));
95 }
96 this.byExt.set(ext.id, { disposables, entries, palette, skipped });
97 if (skipped.length) this.onReport(ext.id, skipped);
98 }
99
100 remove(id) {
101 const e = this.byExt.get(id);
102 if (!e) return;
103 for (const d of e.disposables) { try { d?.dispose(); } catch { /* ya fuera */ } }
104 const list = paletteList();
105 if (list) for (const p of e.palette) { const i = list.indexOf(p); if (i >= 0) list.splice(i, 1); }
106 for (const [k, owner] of [...this.chords]) if (owner === id) this.chords.delete(k);
107 this.byExt.delete(id);
108 }
109
110 entries() { return [...this.byExt.values()].flatMap(e => e.entries); }
111 skipped(id) { return this.byExt.get(id)?.skipped || []; }
112 dispose() { for (const id of [...this.byExt.keys()]) this.remove(id); }
113}