-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharkhe-bridge-old.js
More file actions
195 lines (195 loc) · 19.2 KB
/
Copy patharkhe-bridge-old.js
File metadata and controls
195 lines (195 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"use strict";
// ÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇ
// tools/arkhe-bridge/extension.ts
//
// Extensi├│n VS Code ÔÇö bridge unidireccional VS Code ÔåÆ Arkhe.
//
// Comando: arkhe.sendToArkhe (Ctrl+Shift+Alt+A)
// 1. Lee el archivo activo en el editor.
// 2. Resuelve su node_code consultando entries_index.json (o por nombre).
// 3. Escribe arkhe_bridge.json con direction="vscode".
// 4. Si Arkhe no está corriendo y hay executablePath configurado, lo lanza.
// ÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇ
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.activate = activate;
exports.deactivate = deactivate;
const vscode = __importStar(require("vscode"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const child_process_1 = require("child_process");
// Ruta del bridge relativa a la raíz del workspace
const BRIDGE_RELATIVE = 'arkhe_bridge.json';
// ÔöÇÔöÇ Activaci├│n ÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇ
function activate(context) {
const cmd = vscode.commands.registerCommand('arkhe.sendToArkhe', sendToArkhe);
context.subscriptions.push(cmd);
}
// ÔöÇÔöÇ Comando principal ÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇ
async function sendToArkhe() {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showWarningMessage('Arkhe Bridge: no hay archivo activo.');
return;
}
const filePath = editor.document.uri.fsPath;
const workspaceRoot = getWorkspaceRoot();
if (!workspaceRoot) {
vscode.window.showErrorMessage('Arkhe Bridge: no hay workspace abierto.');
return;
}
const bridgePath = path.join(workspaceRoot, BRIDGE_RELATIVE);
// Asegurar que tools/ existe (por si acaso)
fs.mkdirSync(path.dirname(bridgePath), { recursive: true });
// Resolver nodo y modo a partir de la ruta del archivo
const { nodeCode, mode } = resolveNode(workspaceRoot, filePath);
// Escribir bridge
const payload = {
direction: 'vscode',
mode,
node_code: nodeCode,
node_label: '',
tex_file: filePath,
timestamp: Math.floor(Date.now() / 1000)
};
try {
fs.writeFileSync(bridgePath, JSON.stringify(payload, null, 2), 'utf8');
}
catch (err) {
vscode.window.showErrorMessage(`Arkhe Bridge: no se pudo escribir el bridge.\n${err.message}`);
return;
}
// Lanzar Arkhe si no está corriendo
const config = vscode.workspace.getConfiguration('arkhe');
const execPath = config.get('executablePath', '').trim();
const cwd = config.get('workingDirectory', '').trim() || workspaceRoot;
if (isArkheRunning()) {
// Arkhe ya está corriendo; leerá el bridge en el siguiente frame
vscode.window.showInformationMessage(`Arkhe Bridge: enviado  ${nodeCode}`);
}
else if (execPath) {
if (!fs.existsSync(execPath)) {
vscode.window.showErrorMessage(`Arkhe Bridge: ejecutable no encontrado en "${execPath}".`);
return;
}
launchArkhe(execPath, cwd);
vscode.window.showInformationMessage(`Arkhe Bridge: Arkhe lanzado  ${nodeCode}`);
}
else {
// Arkhe no está corriendo y no hay ruta configurada
vscode.window.showWarningMessage('Arkhe Bridge: bridge escrito, pero Arkhe no está corriendo. ' +
'Configura "arkhe.executablePath" para lanzarlo automáticamente.');
}
}
// ÔöÇÔöÇ Helpers ÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇ
// Devuelve la raíz del primer workspace abierto.
function getWorkspaceRoot() {
const folders = vscode.workspace.workspaceFolders;
return (folders && folders.length > 0) ? folders[0].uri.fsPath : null;
}
// Resuelve (nodeCode, mode) a partir de la ruta del archivo activo.
//
// Prioridad:
// 1. Buscar el nombre del archivo en entries_index.json  code exacto.
// 2. Si es .lean, tratar el path relativo a mathlib_src como c├│digo Mathlib.
// 3. Fallback: basename sin extensi├│n, reemplazando '_' por '.'.
function resolveNode(workspaceRoot, filePath) {
const filename = path.basename(filePath);
const ext = path.extname(filePath).toLowerCase();
// ÔöÇÔöÇ 1. entries_index.json ÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇ
const indexPath = path.join(workspaceRoot, 'assets', 'entries', 'entries_index.json');
if (fs.existsSync(indexPath)) {
try {
const index = JSON.parse(fs.readFileSync(indexPath, 'utf8'));
for (const [code, fname] of Object.entries(index)) {
if (fname === filename) {
// El modo lo inferimos del propio código (heurística simple)
const mode = code.startsWith('Mathlib') ? 'Mathlib' : 'MSC2020';
return { nodeCode: code, mode };
}
}
}
catch (_) {
// entries_index.json corrupto; continuar con fallbacks
}
}
//  2. Archivo .lean  Mathlib 
if (ext === '.lean') {
// Intentar resolver relativo a cualquier carpeta "Mathlib" en el path
const normalized = filePath.replace(/\\/g, '/');
const mathlibIdx = normalized.lastIndexOf('/Mathlib/');
if (mathlibIdx !== -1) {
// ".../Mathlib/Algebra/Group/Basic.lean"  "Mathlib.Algebra.Group.Basic"
const relative = normalized.slice(mathlibIdx + 1); // "Mathlib/..."
const nodeCode = relative.replace(/\//g, '.').replace(/\.lean$/, '');
return { nodeCode, mode: 'Mathlib' };
}
// Fallback .lean: basename con _  .
const base = path.basename(filePath, '.lean');
return { nodeCode: base.replace(/_/g, '.'), mode: 'Mathlib' };
}
// ÔöÇÔöÇ 3. Fallback gen├®rico ÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇ
const base = path.basename(filePath, ext);
return { nodeCode: base.replace(/_/g, '.'), mode: 'MSC2020' };
}
// Comprueba si hay un proceso llamado "arkhe" (o "arkhe.exe") corriendo.
function isArkheRunning() {
try {
if (process.platform === 'win32') {
const out = (0, child_process_1.execSync)('tasklist /FI "IMAGENAME eq arkhe.exe" /NH 2>NUL', { encoding: 'utf8', timeout: 3000 });
return out.toLowerCase().includes('arkhe.exe');
}
else {
// Linux / macOS
(0, child_process_1.execSync)('pgrep -x arkhe', { encoding: 'utf8', timeout: 3000 });
return true;
}
}
catch (_) {
return false;
}
}
// Lanza Arkhe como proceso independiente (detached) sin bloquear VS Code.
function launchArkhe(execPath, cwd) {
const child = (0, child_process_1.spawn)(execPath, [], {
cwd,
detached: true,
stdio: 'ignore'
});
child.unref(); // VS Code no espera a que Arkhe termine
}
// ÔöÇÔöÇ Desactivaci├│n ÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇÔöÇ
function deactivate() { }
//# sourceMappingURL=extension.js.map