Skip to content

Commit

Permalink
add support for stepIn, stepOut, and stepInTarget; see #90793
Browse files Browse the repository at this point in the history
  • Loading branch information
weinand committed May 13, 2020
1 parent ec3c3b7 commit 54ef289
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,4 +1,8 @@

## 0.41.0
* Add support for StepIn and StepOut: StepIn moves execution one character to the right, StepIn to the left
* Add support for StepInTargets: every word in the stopped line is considered one stack frame; StepInTargets returns targets for every character of the stack frame with the given frameId.

## 0.40.0
* Exercise new dynamic debug config API.

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "mock-debug",
"displayName": "Mock Debug",
"version": "0.40.0",
"version": "0.41.0",
"publisher": "andreweinand",
"description": "Starter extension for developing debug adapters for VS Code.",
"author": {
Expand Down
29 changes: 28 additions & 1 deletion src/mockDebug.ts
Expand Up @@ -138,6 +138,9 @@ export class MockDebugSession extends LoggingDebugSession {
// make VS Code send the breakpointLocations request
response.body.supportsBreakpointLocationsRequest = true;

// make VS Code provide "Step in Target" functionality
response.body.supportsStepInTargetsRequest = true;

this.sendResponse(response);

// since this debug adapter can accept configuration requests like 'setBreakpoint' at any time,
Expand Down Expand Up @@ -234,7 +237,13 @@ export class MockDebugSession extends LoggingDebugSession {
const stk = this._runtime.stack(startFrame, endFrame);

response.body = {
stackFrames: stk.frames.map(f => new StackFrame(f.index, f.name, this.createSource(f.file), this.convertDebuggerLineToClient(f.line))),
stackFrames: stk.frames.map(f => {
const sf = new StackFrame(f.index, f.name, this.createSource(f.file), this.convertDebuggerLineToClient(f.line));
if (typeof f.column === 'number') {
sf.column = this.convertDebuggerColumnToClient(f.column);
}
return sf;
}),
totalFrames: stk.count
};
this.sendResponse(response);
Expand Down Expand Up @@ -348,6 +357,24 @@ export class MockDebugSession extends LoggingDebugSession {
this.sendResponse(response);
}

protected stepInTargetsRequest(response: DebugProtocol.StepInTargetsResponse, args: DebugProtocol.StepInTargetsArguments) {
const targets = this._runtime.getStepInTargets(args.frameId);
response.body = {
targets: targets.map(t => { return { id: t.id, label: t.label }} )
};
this.sendResponse(response);
}

protected stepInRequest(response: DebugProtocol.StepInResponse, args: DebugProtocol.StepInArguments): void {
this._runtime.stepIn(args.targetId);
this.sendResponse(response);
}

protected stepOutRequest(response: DebugProtocol.StepOutResponse, args: DebugProtocol.StepOutArguments): void {
this._runtime.stepOut();
this.sendResponse(response);
}

protected evaluateRequest(response: DebugProtocol.EvaluateResponse, args: DebugProtocol.EvaluateArguments): void {

let reply: string | undefined = undefined;
Expand Down
65 changes: 63 additions & 2 deletions src/mockRuntime.ts
Expand Up @@ -27,6 +27,7 @@ export class MockRuntime extends EventEmitter {

// This is the next line that will be 'executed'
private _currentLine = 0;
private _currentColumn: number | undefined;

// maps from sourceFile to array of Mock breakpoints
private _breakPoints = new Map<string, MockBreakpoint[]>();
Expand Down Expand Up @@ -74,6 +75,59 @@ export class MockRuntime extends EventEmitter {
this.run(reverse, event);
}

/**
* "Step into" for Mock debug means: go to next character
*/
public stepIn(targetId: number | undefined) {
if (typeof targetId === 'number') {
this._currentColumn = targetId;
this.sendEvent('stopOnStep');
} else {
if (typeof this._currentColumn === 'number') {
if (this._currentColumn <= this._sourceLines[this._currentLine].length) {
this._currentColumn += 1;
}
} else {
this._currentColumn = 1;
}
this.sendEvent('stopOnStep');
}
}

/**
* "Step out" for Mock debug means: go to previous character
*/
public stepOut() {
if (typeof this._currentColumn === 'number') {
this._currentColumn -= 1;
if (this._currentColumn === 0) {
this._currentColumn = undefined;
}
}
this.sendEvent('stopOnStep');
}

public getStepInTargets(frameId: number): { id: number, label: string}[] {

const line = this._sourceLines[this._currentLine].trim();

// every word of the current line becomes a stack frame.
const words = line.split(/\s+/);

// return nothing if frameId is out of range
if (frameId < 0 || frameId >= words.length) {
return [];
}

// pick the frame for the given frameId
const frame = words[frameId];

const pos = line.indexOf(frame);

// make every character of the frame a potential "step in" target
return frame.split('').map((c, ix) => { return { id: pos + ix, label: c }});
}

/**
* Returns a fake 'stacktrace' where every 'stackframe' is a word from the current line.
*/
Expand All @@ -85,12 +139,16 @@ export class MockRuntime extends EventEmitter {
// every word of the current line becomes a stack frame.
for (let i = startFrame; i < Math.min(endFrame, words.length); i++) {
const name = words[i]; // use a word of the line as the stackframe name
frames.push({
const stackFrame: any = {
index: i,
name: `${name}(${i})`,
file: this._sourceFile,
line: this._currentLine
});
};
if (typeof this._currentColumn === 'number') {
stackFrame.column = this._currentColumn;
}
frames.push(stackFrame);
}
return {
frames: frames,
Expand Down Expand Up @@ -195,16 +253,19 @@ export class MockRuntime extends EventEmitter {
for (let ln = this._currentLine-1; ln >= 0; ln--) {
if (this.fireEventsForLine(ln, stepEvent)) {
this._currentLine = ln;
this._currentColumn = undefined;
return;
}
}
// no more lines: stop at first line
this._currentLine = 0;
this._currentColumn = undefined;
this.sendEvent('stopOnEntry');
} else {
for (let ln = this._currentLine+1; ln < this._sourceLines.length; ln++) {
if (this.fireEventsForLine(ln, stepEvent)) {
this._currentLine = ln;
this._currentColumn = undefined;
return true;
}
}
Expand Down

0 comments on commit 54ef289

Please sign in to comment.