forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonacoDebugger.ts
More file actions
171 lines (135 loc) · 5.19 KB
/
monacoDebugger.ts
File metadata and controls
171 lines (135 loc) · 5.19 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
/// <reference path="../../localtypings/monaco.d.ts" />
/// <reference path="../../built/pxteditor.d.ts" />
import * as pkg from "./package";
export class MonacoBreakpoint implements monaco.IDisposable {
protected active: boolean;
protected decoration: string;
protected range: monaco.Range;
constructor(public readonly source: pxtc.Breakpoint, protected readonly editor: monaco.editor.IStandaloneCodeEditor) {
this.active = false;
const model = this.editor.getModel();
const start = model.getPositionAt(this.source.start);
const end = model.getPositionAt(this.source.start + this.source.length);
if (end.lineNumber === start.lineNumber) {
this.range = new monaco.Range(start.lineNumber, start.column, end.lineNumber, end.column);
}
else {
this.range = new monaco.Range(start.lineNumber, start.column, start.lineNumber, model.getLineMaxColumn(start.lineNumber));
}
this.updateDecoration();
}
public simulatorID() {
return this.source.id;
}
public isActive() {
return this.active;
}
public setActive(active: boolean) {
if (this.active === active) return;
this.active = active;
this.updateDecoration();
}
public toggle() {
this.setActive(!this.active);
}
public distanceFromLine(line: number) {
return Math.abs(this.range.startLineNumber - line);
}
public dispose() {
if (this.decoration) {
this.editor.deltaDecorations([this.decoration], []);
this.decoration = undefined;
}
}
updateDecoration() {
const glyphClass = this.active ? "monaco-breakpoint active" : "monaco-breakpoint";
const glyphHover = this.active ? pxt.U.lf("Remove breakpoint") : pxt.U.lf("Add breakpoint");
const dec = this.editor.deltaDecorations(this.decoration ? [this.decoration] : [], [
{
range: this.range,
options: {
glyphMarginClassName: glyphClass,
glyphMarginHoverMessage: glyphHover
}
}
]);
this.decoration = dec[0];
}
}
export class BreakpointCollection implements monaco.IDisposable {
protected fileToBreakpoint: pxt.Map<pxtc.Breakpoint[]>;
protected loadedBreakpoints: MonacoBreakpoint[];
protected activeBreakpoints: number[];
constructor(allBreakpoints: pxtc.Breakpoint[]) {
this.fileToBreakpoint = {};
this.activeBreakpoints = [];
for (const bp of allBreakpoints) {
if (!this.fileToBreakpoint[bp.fileName]) this.fileToBreakpoint[bp.fileName] = [];
this.fileToBreakpoint[bp.fileName].push(bp);
}
}
loadBreakpointsForFile(file: pkg.File, editor: monaco.editor.IStandaloneCodeEditor) {
if (this.loadedBreakpoints) this.loadedBreakpoints.forEach(bp => bp.dispose());
if (!file) return;
const fileBreakpoints = this.fileToBreakpoint[file.getTypeScriptName()];
if (fileBreakpoints) {
this.loadedBreakpoints = fileBreakpoints.map(bp => {
const mbp = new MonacoBreakpoint(bp, editor);
if (this.activeBreakpoints.indexOf(bp.id) != -1) mbp.setActive(true);
return mbp
});
}
}
toggleBreakpointAt(lineNo: number) {
const bp = this.getBreakpointForLine(lineNo);
if (bp) {
bp.toggle();
if (bp.isActive()) {
this.activeBreakpoints.push(bp.source.id);
}
else {
this.activeBreakpoints = this.activeBreakpoints.filter(id => id != bp.source.id);
}
}
}
refreshDecorations() {
if (this.loadedBreakpoints) this.loadedBreakpoints.forEach(bp => bp.updateDecoration());
}
clearDecorations() {
if (this.loadedBreakpoints) this.loadedBreakpoints.forEach(bp => bp.dispose());
}
getActiveBreakpoints() {
return this.activeBreakpoints;
}
dispose() {
if (this.loadedBreakpoints) {
this.loadedBreakpoints.forEach(bp => bp.dispose());
this.loadedBreakpoints = undefined;
}
this.activeBreakpoints = undefined;
this.fileToBreakpoint = undefined;
}
getLocationOfBreakpoint(id: number): pxtc.LocationInfo {
for (const file of Object.keys(this.fileToBreakpoint)) {
const bps = this.fileToBreakpoint[file];
for (const bp of bps) {
if (bp.id === id) return bp;
}
}
return undefined;
}
protected getBreakpointForLine(lineNo: number) {
if (!this.loadedBreakpoints || !this.loadedBreakpoints.length) return undefined;
let closestBreakpoint: MonacoBreakpoint;
let closestDistance: number;
for (const bp of this.loadedBreakpoints) {
const distance = bp.distanceFromLine(lineNo);
if (closestDistance === undefined || distance < closestDistance) {
closestBreakpoint = bp;
closestDistance = distance;
}
}
if (closestDistance < 5) return closestBreakpoint;
return undefined;
}
}