forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugLocationTracker.ts
More file actions
128 lines (108 loc) · 4.39 KB
/
debugLocationTracker.ts
File metadata and controls
128 lines (108 loc) · 4.39 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { DebugAdapterTracker, Event, EventEmitter } from 'vscode';
import { DebugProtocol } from 'vscode-debugprotocol';
import { IDebugLocation } from './types';
// When a python debugging session is active keep track of the current debug location
export class DebugLocationTracker implements DebugAdapterTracker {
private waitingForStackTrace: boolean = false;
private _debugLocation: IDebugLocation | undefined;
private debugLocationUpdatedEvent: EventEmitter<void> = new EventEmitter<void>();
private sessionEndedEmitter: EventEmitter<DebugLocationTracker> = new EventEmitter<DebugLocationTracker>();
constructor(private _sessionId: string) {
this.DebugLocation = undefined;
}
public get sessionId() {
return this._sessionId;
}
public get sessionEnded(): Event<DebugLocationTracker> {
return this.sessionEndedEmitter.event;
}
public get debugLocationUpdated(): Event<void> {
return this.debugLocationUpdatedEvent.event;
}
public get debugLocation(): IDebugLocation | undefined {
return this._debugLocation;
}
// tslint:disable-next-line:no-any
public onDidSendMessage(message: DebugProtocol.ProtocolMessage) {
if (this.isStopEvent(message)) {
// Some type of stop, wait to see our next stack trace to find our location
this.waitingForStackTrace = true;
}
if (this.isContinueEvent(message)) {
// Running, clear the location
this.DebugLocation = undefined;
this.waitingForStackTrace = false;
}
if (this.waitingForStackTrace) {
// If we are waiting for a stack track, check our messages for one
const debugLoc = this.getStackTrace(message);
if (debugLoc) {
this.DebugLocation = debugLoc;
this.waitingForStackTrace = false;
}
}
}
public onWillStopSession() {
this.sessionEndedEmitter.fire(this);
}
// Set our new location and fire our debug event
private set DebugLocation(newLocation: IDebugLocation | undefined) {
const oldLocation = this._debugLocation;
this._debugLocation = newLocation;
if (this._debugLocation !== oldLocation) {
this.debugLocationUpdatedEvent.fire();
}
}
// tslint:disable-next-line:no-any
private isStopEvent(message: DebugProtocol.ProtocolMessage) {
if (message.type === 'event') {
const eventMessage = message as DebugProtocol.Event;
if (eventMessage.event === 'stopped') {
return true;
}
}
return false;
}
// tslint:disable-next-line:no-any
private getStackTrace(message: DebugProtocol.ProtocolMessage): IDebugLocation | undefined {
if (message.type === 'response') {
const responseMessage = message as DebugProtocol.Response;
if (responseMessage.command === 'stackTrace') {
const messageBody = responseMessage.body;
if (messageBody.stackFrames.length > 0) {
const lineNumber = messageBody.stackFrames[0].line;
const fileName = this.normalizeFilePath(messageBody.stackFrames[0].source.path);
const column = messageBody.stackFrames[0].column;
return { lineNumber, fileName, column };
}
}
}
return undefined;
}
private normalizeFilePath(path: string): string {
// Make the path match the os. Debugger seems to return
// invalid path chars on linux/darwin
if (process.platform !== 'win32') {
return path.replace(/\\/g, '/');
}
return path;
}
// tslint:disable-next-line:no-any
private isContinueEvent(message: DebugProtocol.ProtocolMessage): boolean {
if (message.type === 'event') {
const eventMessage = message as DebugProtocol.Event;
if (eventMessage.event === 'continue') {
return true;
}
} else if (message.type === 'response') {
const responseMessage = message as DebugProtocol.Response;
if (responseMessage.command === 'continue') {
return true;
}
}
return false;
}
}