diff --git a/adapter/debugAdapter.js b/adapter/debugAdapter.js index 36d214b..5728e40 100644 --- a/adapter/debugAdapter.js +++ b/adapter/debugAdapter.js @@ -28,6 +28,8 @@ constructor() { this._pendingGlobals = null; this._refTable = new Map(); // ref → { luaRef } this._nextRef = 1000; // IDs únicos para tablas + this._pendingEval = null; + this.programStarted = false; } //initializeRequest(response, args) { @@ -193,6 +195,29 @@ constructor() { return; } + if (json.type === "eval") { + const r = this._pendingEval; + this._pendingEval = null; + + const v = json.payload.result; + + let variablesReference = 0; + + if (v.type === "table" && v.ref > 0) { + const id = this._nextRef++; + this._refTable.set(id, v.ref); + variablesReference = id; + } + + r.body = { + result: v.value, + variablesReference + }; + + this.sendResponse(r); + return; + } + // salida normal this.sendEvent(new OutputEvent(line + "\n", "stdout")); } @@ -207,8 +232,7 @@ constructor() { cwd: cwd, stdio: ['pipe', 'pipe', 'pipe'] }); - - this.sendEvent(new ThreadEvent("started", 1)); + this.programStarted = true; this.stdoutBuffer = ""; @@ -232,6 +256,21 @@ constructor() { this.sendEvent(new TerminatedEvent()); }); + // 3. Ahora que stdout ya está conectado, enviar breakpoints + if (this.breakpoints) { + for (const path in this.breakpoints) { + const lines = this.breakpoints[path]; + + this.sendDebugCommand({ + cmd: "setBreakpoints", + file: path, + lines: lines + }); + } + } + + this.sendEvent(new ThreadEvent("started", 1)); + this.sendResponse(response); } @@ -258,6 +297,36 @@ constructor() { } setBreakPointsRequest(response, args) { + const path = args.source.path; + const clientBreakpoints = args.breakpoints || []; + + const lines = clientBreakpoints.map(bp => bp.line); + + if (!this.breakpoints) + this.breakpoints = {}; + + this.breakpoints[path] = lines; + + // Si el programa YA está corriendo → enviar breakpoints al motor + if (this.programStarted) { + this.sendDebugCommand({ + cmd: "setBreakpoints", + file: path, + lines: lines + }); + } + + response.body = { + breakpoints: lines.map(line => ({ + verified: true, + line: line + })) + }; + + this.sendResponse(response); + } + + /*setBreakPointsRequest(response, args) { const path = args.source.path; // archivo donde se han puesto breakpoints const clientBreakpoints = args.breakpoints || []; @@ -291,7 +360,7 @@ constructor() { }; this.sendResponse(response); - } + }*/ continueRequest(response, args) { const msg = { @@ -344,11 +413,40 @@ constructor() { } disconnectRequest(response, args) { - const msg = { cmd: "continue" }; // por si estaba pausado - this.sendDebugCommand(msg); - //this.process.stdin.write("@@DEBUGCMD@@" + JSON.stringify(msg) + "\n"); + console.log("[ADAPTER] disconnectRequest"); - this.process.kill(); + if (this.process) { + try { + // 1. Cerrar stdin para desbloquear lecturas + this.process.stdin.end(); + } catch (e) {} + + try { + // 2. Intento de kill suave + this.process.kill(); + } catch (e) {} + + try { + // 3. Kill duro por si acaso + this.process.kill('SIGKILL'); + } catch (e) {} + } + + // 4. Notificar a VSCode + this.sendEvent(new TerminatedEvent()); + this.sendResponse(response); + } + + terminateRequest(response, args) { + console.log("[ADAPTER] terminateRequest"); + + if (this.process) { + try { this.process.stdin.end(); } catch (e) {} + try { this.process.kill(); } catch (e) {} + try { this.process.kill('SIGKILL'); } catch (e) {} + } + + this.sendEvent(new TerminatedEvent()); this.sendResponse(response); } @@ -451,6 +549,17 @@ constructor() { this.sendResponse(response); } + evaluateRequest(response, args) { + console.log("[ADAPTER] evaluateRequest:", args.expression); + + this._pendingEval = response; + + this.sendDebugCommand({ + cmd: "eval", + expr: args.expression + }); + } + configurationDoneRequest(response, args) { console.log("[ADAPTER] configurationDoneRequest"); this.sendResponse(response);