-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
breakpoint-manager.ts
359 lines (316 loc) · 14.5 KB
/
breakpoint-manager.ts
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
349
350
351
352
353
354
355
356
357
358
359
// *****************************************************************************
// Copyright (C) 2018 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************
import * as deepEqual from 'fast-deep-equal';
import { injectable, inject } from '@theia/core/shared/inversify';
import { Emitter } from '@theia/core/lib/common';
import { StorageService } from '@theia/core/lib/browser';
import { Marker } from '@theia/markers/lib/common/marker';
import { MarkerManager } from '@theia/markers/lib/browser/marker-manager';
import URI from '@theia/core/lib/common/uri';
import { SourceBreakpoint, BREAKPOINT_KIND, ExceptionBreakpoint, FunctionBreakpoint, BaseBreakpoint, InstructionBreakpoint } from './breakpoint-marker';
export interface BreakpointsChangeEvent<T extends BaseBreakpoint> {
uri: URI
added: T[]
removed: T[]
changed: T[]
}
export type SourceBreakpointsChangeEvent = BreakpointsChangeEvent<SourceBreakpoint>;
export type FunctionBreakpointsChangeEvent = BreakpointsChangeEvent<FunctionBreakpoint>;
export type InstructionBreakpointsChangeEvent = BreakpointsChangeEvent<InstructionBreakpoint>;
@injectable()
export class BreakpointManager extends MarkerManager<SourceBreakpoint> {
static EXCEPTION_URI = new URI('debug:exception://');
static FUNCTION_URI = new URI('debug:function://');
static INSTRUCTION_URI = new URI('debug:instruction://');
protected readonly owner = 'breakpoint';
@inject(StorageService)
protected readonly storage: StorageService;
getKind(): string {
return BREAKPOINT_KIND;
}
protected readonly onDidChangeBreakpointsEmitter = new Emitter<SourceBreakpointsChangeEvent>();
readonly onDidChangeBreakpoints = this.onDidChangeBreakpointsEmitter.event;
protected readonly onDidChangeFunctionBreakpointsEmitter = new Emitter<FunctionBreakpointsChangeEvent>();
readonly onDidChangeFunctionBreakpoints = this.onDidChangeFunctionBreakpointsEmitter.event;
protected readonly onDidChangeInstructionBreakpointsEmitter = new Emitter<InstructionBreakpointsChangeEvent>();
readonly onDidChangeInstructionBreakpoints = this.onDidChangeInstructionBreakpointsEmitter.event;
override setMarkers(uri: URI, owner: string, newMarkers: SourceBreakpoint[]): Marker<SourceBreakpoint>[] {
const result = this.findMarkers({ uri, owner });
const added: SourceBreakpoint[] = [];
const removed: SourceBreakpoint[] = [];
const changed: SourceBreakpoint[] = [];
const oldMarkers = new Map(result.map(({ data }) => [data.id, data]));
const ids = new Set<string>();
let didChangeMarkers = false;
for (const newMarker of newMarkers) {
ids.add(newMarker.id);
const oldMarker = oldMarkers.get(newMarker.id);
if (!oldMarker) {
added.push(newMarker);
} else {
// We emit all existing markers as 'changed', but we only fire an event if something really did change.
didChangeMarkers ||= !!added.length || !deepEqual(oldMarker, newMarker);
changed.push(newMarker);
}
}
for (const [id, data] of oldMarkers.entries()) {
if (!ids.has(id)) {
removed.push(data);
}
}
if (added.length || removed.length || didChangeMarkers) {
super.setMarkers(uri, owner, newMarkers);
this.onDidChangeBreakpointsEmitter.fire({ uri, added, removed, changed });
}
return result;
}
getLineBreakpoints(uri: URI, line: number): SourceBreakpoint[] {
return this.findMarkers({
uri,
dataFilter: breakpoint => breakpoint.raw.line === line
}).map(({ data }) => data);
}
getInlineBreakpoint(uri: URI, line: number, column: number): SourceBreakpoint | undefined {
const marker = this.findMarkers({
uri,
dataFilter: breakpoint => breakpoint.raw.line === line && breakpoint.raw.column === column
})[0];
return marker && marker.data;
}
getBreakpoints(uri?: URI): SourceBreakpoint[] {
return this.findMarkers({ uri }).map(marker => marker.data);
}
setBreakpoints(uri: URI, breakpoints: SourceBreakpoint[]): void {
this.setMarkers(uri, this.owner, breakpoints.sort((a, b) => (a.raw.line - b.raw.line) || ((a.raw.column || 0) - (b.raw.column || 0))));
}
addBreakpoint(breakpoint: SourceBreakpoint): boolean {
const uri = new URI(breakpoint.uri);
const breakpoints = this.getBreakpoints(uri);
const newBreakpoints = breakpoints.filter(({ raw }) => !(raw.line === breakpoint.raw.line && raw.column === breakpoint.raw.column));
if (breakpoints.length === newBreakpoints.length) {
newBreakpoints.push(breakpoint);
this.setBreakpoints(uri, newBreakpoints);
return true;
}
return false;
}
enableAllBreakpoints(enabled: boolean): void {
for (const uriString of this.getUris()) {
let didChange = false;
const uri = new URI(uriString);
const markers = this.findMarkers({ uri });
for (const marker of markers) {
if (marker.data.enabled !== enabled) {
marker.data.enabled = enabled;
didChange = true;
}
}
if (didChange) {
this.fireOnDidChangeMarkers(uri);
}
}
let didChangeFunction = false;
for (const breakpoint of (this.getFunctionBreakpoints() as BaseBreakpoint[]).concat(this.getInstructionBreakpoints())) {
if (breakpoint.enabled !== enabled) {
breakpoint.enabled = enabled;
didChangeFunction = true;
}
}
if (didChangeFunction) {
this.fireOnDidChangeMarkers(BreakpointManager.FUNCTION_URI);
}
}
protected _breakpointsEnabled = true;
get breakpointsEnabled(): boolean {
return this._breakpointsEnabled;
}
set breakpointsEnabled(breakpointsEnabled: boolean) {
if (this._breakpointsEnabled !== breakpointsEnabled) {
this._breakpointsEnabled = breakpointsEnabled;
for (const uri of this.getUris()) {
this.fireOnDidChangeMarkers(new URI(uri));
}
this.fireOnDidChangeMarkers(BreakpointManager.FUNCTION_URI);
}
}
protected readonly exceptionBreakpoints = new Map<string, ExceptionBreakpoint>();
getExceptionBreakpoint(filter: string): ExceptionBreakpoint | undefined {
return this.exceptionBreakpoints.get(filter);
}
getExceptionBreakpoints(): IterableIterator<ExceptionBreakpoint> {
return this.exceptionBreakpoints.values();
}
setExceptionBreakpoints(exceptionBreakpoints: ExceptionBreakpoint[]): void {
const toRemove = new Set(this.exceptionBreakpoints.keys());
for (const exceptionBreakpoint of exceptionBreakpoints) {
const filter = exceptionBreakpoint.raw.filter;
toRemove.delete(filter);
this.exceptionBreakpoints.set(filter, exceptionBreakpoint);
}
for (const filter of toRemove) {
this.exceptionBreakpoints.delete(filter);
}
if (toRemove.size || exceptionBreakpoints.length) {
this.fireOnDidChangeMarkers(BreakpointManager.EXCEPTION_URI);
}
}
toggleExceptionBreakpoint(filter: string): void {
const breakpoint = this.getExceptionBreakpoint(filter);
if (breakpoint) {
breakpoint.enabled = !breakpoint.enabled;
this.fireOnDidChangeMarkers(BreakpointManager.EXCEPTION_URI);
}
}
protected functionBreakpoints: FunctionBreakpoint[] = [];
getFunctionBreakpoints(): FunctionBreakpoint[] {
return this.functionBreakpoints;
}
setFunctionBreakpoints(functionBreakpoints: FunctionBreakpoint[]): void {
const oldBreakpoints = new Map(this.functionBreakpoints.map(b => [b.id, b] as [string, FunctionBreakpoint]));
this.functionBreakpoints = functionBreakpoints;
this.fireOnDidChangeMarkers(BreakpointManager.FUNCTION_URI);
const added: FunctionBreakpoint[] = [];
const removed: FunctionBreakpoint[] = [];
const changed: FunctionBreakpoint[] = [];
const ids = new Set<string>();
for (const newBreakpoint of functionBreakpoints) {
ids.add(newBreakpoint.id);
if (oldBreakpoints.has(newBreakpoint.id)) {
changed.push(newBreakpoint);
} else {
added.push(newBreakpoint);
}
}
for (const [id, breakpoint] of oldBreakpoints.entries()) {
if (!ids.has(id)) {
removed.push(breakpoint);
}
}
this.onDidChangeFunctionBreakpointsEmitter.fire({ uri: BreakpointManager.FUNCTION_URI, added, removed, changed });
}
protected instructionBreakpoints: InstructionBreakpoint[] = [];
getInstructionBreakpoints(): ReadonlyArray<InstructionBreakpoint> {
return Object.freeze(this.instructionBreakpoints.slice());
}
hasBreakpoints(): boolean {
return Boolean(this.getUris().next().value || this.functionBreakpoints.length || this.instructionBreakpoints.length);
}
protected setInstructionBreakpoints(newBreakpoints: InstructionBreakpoint[]): void {
const oldBreakpoints = new Map(this.instructionBreakpoints.map(breakpoint => [breakpoint.id, breakpoint]));
const currentBreakpoints = new Map(newBreakpoints.map(breakpoint => [breakpoint.id, breakpoint]));
const added = [];
const changed = [];
for (const [id, breakpoint] of currentBreakpoints.entries()) {
const old = oldBreakpoints.get(id);
if (old) {
changed.push(old);
} else {
added.push(breakpoint);
}
oldBreakpoints.delete(id);
}
const removed = Array.from(oldBreakpoints.values());
this.instructionBreakpoints = Array.from(currentBreakpoints.values());
this.fireOnDidChangeMarkers(BreakpointManager.INSTRUCTION_URI);
this.onDidChangeInstructionBreakpointsEmitter.fire({ uri: BreakpointManager.INSTRUCTION_URI, added, removed, changed });
}
addInstructionBreakpoint(address: string, offset: number, condition?: string, hitCondition?: string): void {
this.setInstructionBreakpoints(this.instructionBreakpoints.concat(InstructionBreakpoint.create({
instructionReference: address,
offset,
condition,
hitCondition,
})));
}
updateInstructionBreakpoint(id: string, options: Partial<Pick<InstructionBreakpoint, 'condition' | 'hitCondition' | 'enabled'>>): void {
const breakpoint = this.instructionBreakpoints.find(candidate => id === candidate.id);
if (breakpoint) {
Object.assign(breakpoint, options);
this.fireOnDidChangeMarkers(BreakpointManager.INSTRUCTION_URI);
this.onDidChangeInstructionBreakpointsEmitter.fire({ uri: BreakpointManager.INSTRUCTION_URI, changed: [breakpoint], added: [], removed: [] });
}
}
removeInstructionBreakpoint(address?: string): void {
if (!address) {
this.clearInstructionBreakpoints();
}
const breakpointIndex = this.instructionBreakpoints.findIndex(breakpoint => breakpoint.instructionReference === address);
if (breakpointIndex !== -1) {
const removed = this.instructionBreakpoints.splice(breakpointIndex, 1);
this.fireOnDidChangeMarkers(BreakpointManager.INSTRUCTION_URI);
this.onDidChangeInstructionBreakpointsEmitter.fire({ uri: BreakpointManager.INSTRUCTION_URI, added: [], changed: [], removed });
}
}
clearInstructionBreakpoints(): void {
this.setInstructionBreakpoints([]);
}
removeBreakpoints(): void {
this.cleanAllMarkers();
this.setFunctionBreakpoints([]);
this.setInstructionBreakpoints([]);
}
async load(): Promise<void> {
const data = await this.storage.getData<BreakpointManager.Data>('breakpoints', {
breakpointsEnabled: true,
breakpoints: {}
});
this._breakpointsEnabled = data.breakpointsEnabled;
// eslint-disable-next-line guard-for-in
for (const uri in data.breakpoints) {
this.setBreakpoints(new URI(uri), data.breakpoints[uri]);
}
if (data.functionBreakpoints) {
this.setFunctionBreakpoints(data.functionBreakpoints);
}
if (data.exceptionBreakpoints) {
this.setExceptionBreakpoints(data.exceptionBreakpoints);
}
if (data.instructionBreakpoints) {
this.setInstructionBreakpoints(data.instructionBreakpoints);
}
}
save(): void {
const data: BreakpointManager.Data = {
breakpointsEnabled: this._breakpointsEnabled,
breakpoints: {}
};
const uris = this.getUris();
for (const uri of uris) {
data.breakpoints[uri] = this.findMarkers({ uri: new URI(uri) }).map(marker => marker.data);
}
if (this.functionBreakpoints.length) {
data.functionBreakpoints = this.functionBreakpoints;
}
if (this.exceptionBreakpoints.size) {
data.exceptionBreakpoints = [...this.exceptionBreakpoints.values()];
}
if (this.instructionBreakpoints.length) {
data.instructionBreakpoints = this.instructionBreakpoints;
}
this.storage.setData('breakpoints', data);
}
}
export namespace BreakpointManager {
export interface Data {
breakpointsEnabled: boolean;
breakpoints: {
[uri: string]: SourceBreakpoint[];
}
exceptionBreakpoints?: ExceptionBreakpoint[];
functionBreakpoints?: FunctionBreakpoint[];
instructionBreakpoints?: InstructionBreakpoint[];
}
}