- Ja arreplega els breakpoints, els executa i espoden controlar des de vscode

This commit is contained in:
2026-03-30 06:42:03 +02:00
parent eb123a60cb
commit 1b9fc95f27
2 changed files with 134 additions and 24 deletions

View File

@@ -2,8 +2,11 @@ const {
LoggingDebugSession, LoggingDebugSession,
InitializedEvent, InitializedEvent,
TerminatedEvent, TerminatedEvent,
OutputEvent OutputEvent,
StoppedEvent,
Thread
} = require('vscode-debugadapter'); } = require('vscode-debugadapter');
const { spawn } = require('child_process'); const { spawn } = require('child_process');
class MyLuaDebugSession extends LoggingDebugSession { class MyLuaDebugSession extends LoggingDebugSession {
@@ -16,37 +19,62 @@ class MyLuaDebugSession extends LoggingDebugSession {
initializeRequest(response, args) { initializeRequest(response, args) {
this.sendEvent(new InitializedEvent()); this.sendEvent(new InitializedEvent());
response.body = {
supportsConfigurationDoneRequest: true
};
this.sendResponse(response); this.sendResponse(response);
} }
handleLine(line) {
if (line.startsWith("@@DEBUG@@")) {
const json = JSON.parse(line.substring(9));
if (json.type === "luaError") {
const e = new OutputEvent(json.message + "\n", "stderr");
e.body.source = { path: json.file };
e.body.line = json.line;
this.sendEvent(e);
return;
}
if (json.type === "break") {
this.currentFile = json.file;
this.currentLine = json.line;
this.sendEvent(new StoppedEvent("breakpoint", 1));
return;
}
return;
}
// salida normal
this.sendEvent(new OutputEvent(line + "\n", "stdout"));
}
launchRequest(response, args) { launchRequest(response, args) {
const program = args.program; const program = args.program;
const cwd = args.cwd || process.cwd(); const cwd = args.cwd || process.cwd();
this.process = spawn(program, [], { this.process = spawn(program, [], {
cwd: cwd, cwd: cwd,
stdio: ['ignore', 'pipe', 'pipe'] stdio: ['pipe', 'pipe', 'pipe']
}); });
this.stdoutBuffer = "";
this.process.stdout.on('data', data => { this.process.stdout.on('data', data => {
const text = data.toString(); this.stdoutBuffer += data.toString();
if (text.startsWith("@@DEBUG@@")) { let idx;
const json = JSON.parse(text.substring(9)); while ((idx = this.stdoutBuffer.indexOf("\n")) >= 0) {
const line = this.stdoutBuffer.slice(0, idx).trim();
this.stdoutBuffer = this.stdoutBuffer.slice(idx + 1);
if (json.type === "luaError") { this.handleLine(line);
const e = new OutputEvent(json.message + "\n", "stderr");
e.body.source = { path: json.file };
e.body.line = json.line;
this.sendEvent(e);
} }
return;
}
// salida normal
this.sendEvent(new OutputEvent(text, "stdout"));
}); });
this.process.stderr.on('data', data => { this.process.stderr.on('data', data => {
@@ -61,17 +89,87 @@ class MyLuaDebugSession extends LoggingDebugSession {
} }
setBreakPointsRequest(response, args) { setBreakPointsRequest(response, args) {
const path = args.source.path; const path = args.source.path; // archivo donde se han puesto breakpoints
const bps = args.breakpoints.map(bp => bp.line); const clientBreakpoints = args.breakpoints || [];
// Guardamos los breakpoints internamente // Extraemos solo las líneas
this.breakpoints[path] = bps; const lines = clientBreakpoints.map(bp => bp.line);
// Enviar confirmación a VSCode // Guardamos internamente los breakpoints
response.body = { if (!this.breakpoints)
breakpoints: bps.map(line => ({ verified: true, line })) this.breakpoints = {};
this.breakpoints[path] = lines;
// Enviamos los breakpoints a tu aplicación por stdin
const msg = {
cmd: "setBreakpoints",
file: path,
lines: lines
}; };
this.process.stdin.write(
"@@DEBUGCMD@@" + JSON.stringify(msg) + "\n"
);
// VSCode necesita una respuesta con los breakpoints "verificados"
response.body = {
breakpoints: lines.map(line => ({
verified: true,
line: line
}))
};
this.sendResponse(response);
}
continueRequest(response, args) {
const msg = {
cmd: "continue"
};
this.process.stdin.write(
"@@DEBUGCMD@@" + JSON.stringify(msg) + "\n"
);
this.sendResponse(response);
}
disconnectRequest(response, args) {
const msg = { cmd: "continue" }; // por si estaba pausado
this.process.stdin.write("@@DEBUGCMD@@" + JSON.stringify(msg) + "\n");
this.process.kill();
this.sendResponse(response);
}
threadsRequest(response) {
// VSCode exige al menos un thread
response.body = {
threads: [
new Thread(1, "Main Thread")
]
};
this.sendResponse(response);
}
stackTraceRequest(response, args) {
response.body = {
stackFrames: [
{
id: 1,
name: "Lua",
source: { path: this.currentFile },
line: this.currentLine,
column: 1
}
],
totalFrames: 1
};
this.sendResponse(response);
}
configurationDoneRequest(response, args) {
this.sendResponse(response); this.sendResponse(response);
} }

View File

@@ -11,6 +11,9 @@
"activationEvents": ["onDebug"], "activationEvents": ["onDebug"],
"main": "./extension.js", "main": "./extension.js",
"contributes": { "contributes": {
"breakpoints": [
{ "language": "lua" }
],
"debuggers": [ "debuggers": [
{ {
"type": "myLuaApp", "type": "myLuaApp",
@@ -18,6 +21,15 @@
"program": "./adapter/debugAdapter.js", "program": "./adapter/debugAdapter.js",
"runtime": "node", "runtime": "node",
"languages": ["lua"], "languages": ["lua"],
"request": "launch",
"initialConfigurations": [
{
"type": "myLuaApp",
"request": "launch",
"name": "Debug My Lua App",
"program": "${workspaceFolder}/miapp"
}
],
"configurationAttributes": { "configurationAttributes": {
"launch": { "launch": {
"properties": { "properties": {