forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugger.tsx
More file actions
348 lines (303 loc) · 14 KB
/
debugger.tsx
File metadata and controls
348 lines (303 loc) · 14 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import * as React from "react";
import * as ReactDOM from 'react-dom';
import * as sui from "./sui";
import * as data from "./data";
import * as simulator from "./simulator";
type ISettingsProps = pxt.editor.ISettingsProps;
interface DebuggerVariablesState {
variables?: pxt.Map<Variable>;
frozen?: boolean;
}
interface Variable {
value: any;
prevValue?: any;
children?: pxt.Map<Variable>;
}
interface DebuggerVariablesProps extends ISettingsProps {
}
export class DebuggerVariables extends data.Component<DebuggerVariablesProps, DebuggerVariablesState> {
private static MAX_VARIABLE_CHARS = 20;
private nextVariables: pxt.Map<pxsim.Variables> = {};
constructor(props: DebuggerVariablesProps) {
super(props);
this.state = {
variables: {}
}
}
clear() {
this.nextVariables = {};
this.setState({ variables: {} });
}
set(name: string, value: pxsim.Variables) {
this.nextVariables[name] = value;
}
static renderValue(v: any): string {
let sv = '';
let type = typeof v;
switch (type) {
case "undefined": sv = "undefined"; break;
case "number": sv = v + ""; break;
case "boolean": sv = v + ""; break;
case "string": sv = JSON.stringify(v); break;
case "object":
if (v == null) sv = "null";
else if (v.text) sv = v.text;
else if (v.id && v.preview) return v.preview;
else if (v.id !== undefined) sv = "(object)"
else sv = "(unknown)"
break;
}
return DebuggerVariables.capLength(sv);
}
static capLength(varstr: string) {
let remaining = DebuggerVariables.MAX_VARIABLE_CHARS - 3; // acount for ...
let hasQuotes = false;
if (varstr.indexOf('"') == 0) {
remaining -= 2;
hasQuotes = true;
varstr = varstr.substring(1, varstr.length - 1);
}
if (varstr.length > remaining)
varstr = varstr.substring(0, remaining) + '...';
if (hasQuotes) {
varstr = '"' + varstr + '"'
}
return varstr;
}
update(frozen = false) {
const variables = this.state.variables;
Object.keys(this.nextVariables).forEach(k => {
const v = this.nextVariables[k];
variables[k] = {
value: v,
prevValue: v && !v.id && variables[k] && v !== variables[k].value ?
variables[k].value : undefined
}
})
this.setState({ variables: variables, frozen });
this.nextVariables = {};
}
private toggle(v: Variable) {
// We have to take care of the logic for nested looped variables. Currently they break this implementation.
if (v.children) {
delete v.children;
this.setState({ variables: this.state.variables })
} else {
if (!v.value.id) return;
simulator.driver.variablesAsync(v.value.id)
.then((msg: pxsim.VariablesMessage) => {
if (msg) {
v.children = pxt.Util.mapMap(msg.variables || {},
(k, v) => {
return {
value: msg.variables[k]
}
});
this.setState({ variables: this.state.variables })
}
})
}
}
private variableType(variable: Variable) : string {
let val = variable.value;
if (val == null) return "undefined";
let type = typeof val
switch (type) {
case "string":
case "number":
case "boolean":
return type;
case "object":
if (val.type) return val.type;
if (val.preview) return val.preview;
if (val.text) return val.text;
return "object";
default:
return "unknown";
}
}
private renderVariables(variables: pxt.Map<Variable>, parent?: string, depth?: number): JSX.Element[] {
let r: JSX.Element[] = [];
let varNames = Object.keys(variables);
if (!parent) {
varNames = varNames.sort((var_a, var_b) => {
return this.variableType(variables[var_a]).localeCompare(this.variableType(variables[var_b])) || var_a.toLowerCase().localeCompare(var_b.toLowerCase());
})
}
depth = depth || 0;
let margin = depth*1.5 + 'em';
varNames.forEach(variable => {
const v = variables[variable];
const oldValue = DebuggerVariables.renderValue(v.prevValue);
const newValue = DebuggerVariables.renderValue(v.value);
let type = this.variableType(v);
const onClick = v.value && v.value.id ? () => this.toggle(v) : undefined;
const onMouseOver : any = undefined; // prob a bad idea. Maybe onMouseEnter --> onMouseLeave?
r.push(<div key={(parent || "") + variable} className="item" style={{padding: "0em",}}>
<div role="listitem" className={`ui horizontal label variable ${v.prevValue !== undefined ? "changed" : ""}`} style={{ marginLeft: margin }}
onClick={onClick} onMouseOver={onMouseOver}>
<i className= {`${(v.children ? "down triangle icon" : "right triangle icon") + ((v.value && v.value.hasFields) ? "" : " transparent")}`} ></i>
<span className="varname">{variable + ':'}</span>
<div className="detail">
<span className={`varval ${type}`}>{DebuggerVariables.renderValue(v.value)}</span>
<span className="previousval">{(oldValue !== "undefined" && oldValue !== newValue) ? `${oldValue}` : ''}</span>
</div>
</div>
</div>);
if (v.children)
r = r.concat(this.renderVariables(v.children, variable, depth + 1));
})
return r;
}
renderCore() {
const { variables, frozen } = this.state;
return Object.keys(variables).length == 0 ? <div /> :
<div className={`ui segment debugvariables ${frozen ? "frozen" : ""}`}>
<div className="ui middle aligned list">
{this.renderVariables(variables)}
</div>
</div>;
}
}
export interface DebuggerToolbarProps extends ISettingsProps {
}
export interface DebuggerToolbarState {
isDragging?: boolean;
xPos?: number;
}
export class DebuggerToolbar extends data.Component<DebuggerToolbarProps, DebuggerToolbarState> {
constructor(props: DebuggerToolbarProps) {
super(props);
this.state = {
}
this.toolbarHandleDown = this.toolbarHandleDown.bind(this);
this.restartSimulator = this.restartSimulator.bind(this);
this.dbgPauseResume = this.dbgPauseResume.bind(this);
this.dbgInsertBreakpoint = this.dbgInsertBreakpoint.bind(this);
this.dbgStepOver = this.dbgStepOver.bind(this);
this.dbgStepInto = this.dbgStepInto.bind(this);
this.dbgStepOut = this.dbgStepOut.bind(this);
}
restartSimulator() {
pxt.tickEvent('debugger.restart', undefined, { interactiveConsent: true });
this.props.parent.restartSimulator(true);
}
exitDebugging() {
pxt.tickEvent('debugger.exit', undefined, { interactiveConsent: true });
this.props.parent.toggleDebugging();
}
dbgPauseResume() {
pxt.tickEvent('debugger.pauseresume', undefined, { interactiveConsent: true });
this.props.parent.dbgPauseResume();
}
dbgInsertBreakpoint() {
pxt.tickEvent('debugger.breakpoint', undefined, { interactiveConsent: true });
this.props.parent.dbgInsertBreakpoint();
}
dbgStepOver() {
pxt.tickEvent('debugger.stepover', undefined, { interactiveConsent: true });
this.props.parent.dbgStepOver();
}
dbgStepInto() {
pxt.tickEvent('debugger.stepinto', undefined, { interactiveConsent: true });
this.props.parent.dbgStepInto();
}
dbgStepOut() {
pxt.tickEvent('debugger.stepout', undefined, { interactiveConsent: true });
simulator.dbgStepOut();
}
componentDidUpdate(props: DebuggerToolbarProps, state: DebuggerToolbarState) {
if (this.state.isDragging && !state.isDragging) {
document.addEventListener('mousemove', this.toolbarHandleMove.bind(this));
document.addEventListener('mouseup', this.toolbarHandleUp.bind(this));
} else if (!this.state.isDragging && state.isDragging) {
document.removeEventListener('mousemove', this.toolbarHandleMove.bind(this));
document.removeEventListener('mouseup', this.toolbarHandleUp.bind(this));
}
// Center the component if it hasn't been initialized yet
if (state.xPos == undefined && props.parent.state.debugging) {
this.centerToolbar();
window.addEventListener('resize', this.centerToolbar.bind(this));
}
}
componentWillUnmount() {
document.removeEventListener('mousemove', this.toolbarHandleMove.bind(this));
document.removeEventListener('mouseup', this.toolbarHandleUp.bind(this));
window.removeEventListener('resize', this.centerToolbar.bind(this));
}
private cachedMaxWidth = 0;
toolbarHandleDown(e: React.MouseEvent<any>) {
if (e.button !== 0) return
const menuDOM = this.getMenuDom();
const menuWidth = menuDOM && menuDOM.clientWidth || 0;
this.cachedMaxWidth = window.innerWidth - menuWidth;
this.setState({
isDragging: true,
xPos: Math.min(e.pageX, this.cachedMaxWidth)
})
e.stopPropagation();
e.preventDefault();
}
toolbarHandleMove(e: MouseEvent) {
if (!this.state.isDragging) return;
this.setState({
isDragging: true,
xPos: Math.min(e.pageX, this.cachedMaxWidth)
})
e.stopPropagation();
e.preventDefault();
}
toolbarHandleUp(e: MouseEvent) {
this.setState({ isDragging: false });
e.stopPropagation();
e.preventDefault();
}
getMenuDom() {
const node = ReactDOM.findDOMNode(this);
return node && node.firstElementChild;
}
centerToolbar() {
// Center the toolbar in the middle of the editor view (blocks / JS)
const menuDOM = this.getMenuDom();
const width = menuDOM && menuDOM.clientWidth;
const mainEditor = document.getElementById('maineditor');
const simWidth = window.innerWidth - mainEditor.clientWidth;
this.setState({ xPos: simWidth + (mainEditor.clientWidth - width) / 2 });
}
renderCore() {
const { xPos } = this.state;
const parentState = this.props.parent.state;
const simOpts = pxt.appTarget.simulator;
const simState = parentState.simState;
const isRunning = simState == pxt.editor.SimState.Running;
const isDebugging = parentState.debugging;
if (!isDebugging) return <div />;
const isDebuggerRunning = simulator.driver && simulator.driver.state == pxsim.SimulatorState.Running;
const advancedDebugging = this.props.parent.isJavaScriptActive();
const isValidDebugFile = advancedDebugging || this.props.parent.isBlocksActive();
if (!isValidDebugFile) return <div />;
const restartTooltip = lf("Restart debugging");
const dbgPauseResumeTooltip = isRunning ? lf("Pause execution") : lf("Continue execution");
const dbgStepIntoTooltip = lf("Step into");
const dbgStepOverTooltip = lf("Step over");
const dbgStepOutTooltip = lf("Step out");
return <aside className="debugtoolbar" style={{ left: xPos }} role="complementary" aria-label={lf("Debugger toolbar")}>
{!isDebugging ? undefined :
<div className={`ui compact borderless menu icon`}>
<div role="button" className={`ui item link dbg-btn dbg-handle`} key={'toolbarhandle'}
title={lf("Debugger buttons")}
onMouseDown={this.toolbarHandleDown}>
<sui.Icon key='iconkey' icon={`icon ellipsis vertical`} />
<sui.Icon key='iconkey2' icon={`xicon bug`} />
</div>
<sui.Item key='dbgpauseresume' className={`dbg-btn dbg-pause-resume ${isDebuggerRunning ? "pause" : "play"}`} icon={`${isDebuggerRunning ? "pause blue" : "step forward green"}`} title={dbgPauseResumeTooltip} onClick={this.dbgPauseResume} />
<sui.Item key='dbgbreakpoint' className={`dbg-btn dbg-breakpoint`} icon="circle red" title={lf("Insert debugger breakpoint")} onClick={this.dbgInsertBreakpoint} />
{!advancedDebugging ? <sui.Item key='dbgstep' className={`dbg-btn dbg-step`} icon={`arrow right ${isDebuggerRunning ? "disabled" : "blue"}`} title={dbgStepIntoTooltip} onClick={this.dbgStepInto} /> : undefined}
{advancedDebugging ? <sui.Item key='dbgstepover' className={`dbg-btn dbg-step-over`} icon={`xicon stepover ${isDebuggerRunning ? "disabled" : "blue"}`} title={dbgStepOverTooltip} onClick={this.dbgStepOver} /> : undefined}
{advancedDebugging ? <sui.Item key='dbgstepinto' className={`dbg-btn dbg-step-into`} icon={`xicon stepinto ${isDebuggerRunning ? "disabled" : ""}`} title={dbgStepIntoTooltip} onClick={this.dbgStepInto} /> : undefined}
{advancedDebugging ? <sui.Item key='dbgstepout' className={`dbg-btn dbg-step-out`} icon={`xicon stepout ${isDebuggerRunning ? "disabled" : ""}`} title={dbgStepOutTooltip} onClick={this.dbgStepOut} /> : undefined}
<sui.Item key='dbgrestart' className={`dbg-btn dbg-restart right`} icon={`refresh green`} title={restartTooltip} onClick={this.restartSimulator} />
</div>}
</aside>;
}
}