Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/debug/common/debugStorage.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Disposable } from '../../../../base/common/lifecycle.js';
7
import { ISettableObservable, observableValue } from '../../../../base/common/observable.js';
8
import { URI } from '../../../../base/common/uri.js';
9
import { ILogService } from '../../../../platform/log/common/log.js';
10
import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
11
import { IUriIdentityService } from '../../../../platform/uriIdentity/common/uriIdentity.js';
12
import { IDebugModel, IEvaluate, IExpression } from './debug.js';
13
import { Breakpoint, DataBreakpoint, ExceptionBreakpoint, Expression, FunctionBreakpoint } from './debugModel.js';
14
import { ITextFileService } from '../../../services/textfile/common/textfiles.js';
15
import { mapValues } from '../../../../base/common/objects.js';
16
17
const DEBUG_BREAKPOINTS_KEY = 'debug.breakpoint';
18
const DEBUG_FUNCTION_BREAKPOINTS_KEY = 'debug.functionbreakpoint';
19
const DEBUG_DATA_BREAKPOINTS_KEY = 'debug.databreakpoint';
20
const DEBUG_EXCEPTION_BREAKPOINTS_KEY = 'debug.exceptionbreakpoint';
21
const DEBUG_WATCH_EXPRESSIONS_KEY = 'debug.watchexpressions';
22
const DEBUG_CHOSEN_ENVIRONMENTS_KEY = 'debug.chosenenvironment';
23
const DEBUG_UX_STATE_KEY = 'debug.uxstate';
24
25
export interface IChosenEnvironment {
26
type: string;
27
dynamicLabel?: string;
28
}
29
30
export class DebugStorage extends Disposable {
31
public readonly breakpoints: ISettableObservable<Breakpoint[]>;
32
public readonly functionBreakpoints: ISettableObservable<FunctionBreakpoint[]>;
33
public readonly exceptionBreakpoints: ISettableObservable<ExceptionBreakpoint[]>;
34
public readonly dataBreakpoints: ISettableObservable<DataBreakpoint[]>;
35
public readonly watchExpressions: ISettableObservable<Expression[]>;
36
37
constructor(
38
@IStorageService private readonly storageService: IStorageService,
39
@ITextFileService private readonly textFileService: ITextFileService,
40
@IUriIdentityService private readonly uriIdentityService: IUriIdentityService,
41
@ILogService private readonly logService: ILogService
42
) {
43
super();
44
this.breakpoints = observableValue(this, this.loadBreakpoints());
45
this.functionBreakpoints = observableValue(this, this.loadFunctionBreakpoints());
46
this.exceptionBreakpoints = observableValue(this, this.loadExceptionBreakpoints());
47
this.dataBreakpoints = observableValue(this, this.loadDataBreakpoints());
48
this.watchExpressions = observableValue(this, this.loadWatchExpressions());
49
50
this._register(storageService.onDidChangeValue(StorageScope.WORKSPACE, undefined, this._store)(e => {
51
if (e.external) {
52
switch (e.key) {
53
case DEBUG_BREAKPOINTS_KEY:
54
return this.breakpoints.set(this.loadBreakpoints(), undefined);
55
case DEBUG_FUNCTION_BREAKPOINTS_KEY:
56
return this.functionBreakpoints.set(this.loadFunctionBreakpoints(), undefined);
57
case DEBUG_EXCEPTION_BREAKPOINTS_KEY:
58
return this.exceptionBreakpoints.set(this.loadExceptionBreakpoints(), undefined);
59
case DEBUG_DATA_BREAKPOINTS_KEY:
60
return this.dataBreakpoints.set(this.loadDataBreakpoints(), undefined);
61
case DEBUG_WATCH_EXPRESSIONS_KEY:
62
return this.watchExpressions.set(this.loadWatchExpressions(), undefined);
63
}
64
}
65
}));
66
}
67
68
loadDebugUxState(): 'simple' | 'default' {
69
return this.storageService.get(DEBUG_UX_STATE_KEY, StorageScope.WORKSPACE, 'default') as 'simple' | 'default';
70
}
71
72
storeDebugUxState(value: 'simple' | 'default'): void {
73
this.storageService.store(DEBUG_UX_STATE_KEY, value, StorageScope.WORKSPACE, StorageTarget.MACHINE);
74
}
75
76
private loadBreakpoints(): Breakpoint[] {
77
let result: Breakpoint[] | undefined;
78
try {
79
result = JSON.parse(this.storageService.get(DEBUG_BREAKPOINTS_KEY, StorageScope.WORKSPACE, '[]')).map((breakpoint: ReturnType<Breakpoint['toJSON']>) => {
80
breakpoint.uri = URI.revive(breakpoint.uri);
81
return new Breakpoint(breakpoint, this.textFileService, this.uriIdentityService, this.logService, breakpoint.id);
82
});
83
} catch (e) { }
84
85
return result || [];
86
}
87
88
private loadFunctionBreakpoints(): FunctionBreakpoint[] {
89
let result: FunctionBreakpoint[] | undefined;
90
try {
91
result = JSON.parse(this.storageService.get(DEBUG_FUNCTION_BREAKPOINTS_KEY, StorageScope.WORKSPACE, '[]')).map((fb: ReturnType<FunctionBreakpoint['toJSON']>) => {
92
return new FunctionBreakpoint(fb, fb.id);
93
});
94
} catch (e) { }
95
96
return result || [];
97
}
98
99
private loadExceptionBreakpoints(): ExceptionBreakpoint[] {
100
let result: ExceptionBreakpoint[] | undefined;
101
try {
102
result = JSON.parse(this.storageService.get(DEBUG_EXCEPTION_BREAKPOINTS_KEY, StorageScope.WORKSPACE, '[]')).map((exBreakpoint: ReturnType<ExceptionBreakpoint['toJSON']>) => {
103
return new ExceptionBreakpoint(exBreakpoint, exBreakpoint.id);
104
});
105
} catch (e) { }
106
107
return result || [];
108
}
109
110
private loadDataBreakpoints(): DataBreakpoint[] {
111
let result: DataBreakpoint[] | undefined;
112
try {
113
result = JSON.parse(this.storageService.get(DEBUG_DATA_BREAKPOINTS_KEY, StorageScope.WORKSPACE, '[]')).map((dbp: ReturnType<DataBreakpoint['toJSON']>) => {
114
return new DataBreakpoint(dbp, dbp.id);
115
});
116
} catch (e) { }
117
118
return result || [];
119
}
120
121
private loadWatchExpressions(): Expression[] {
122
let result: Expression[] | undefined;
123
try {
124
result = JSON.parse(this.storageService.get(DEBUG_WATCH_EXPRESSIONS_KEY, StorageScope.WORKSPACE, '[]')).map((watchStoredData: { name: string; id: string }) => {
125
return new Expression(watchStoredData.name, watchStoredData.id);
126
});
127
} catch (e) { }
128
129
return result || [];
130
}
131
132
loadChosenEnvironments(): Record<string, IChosenEnvironment> {
133
const obj = JSON.parse(this.storageService.get(DEBUG_CHOSEN_ENVIRONMENTS_KEY, StorageScope.WORKSPACE, '{}'));
134
// back compat from when this was a string map:
135
return mapValues(obj, (value): IChosenEnvironment => typeof value === 'string' ? { type: value } : value);
136
}
137
138
storeChosenEnvironments(environments: Record<string, IChosenEnvironment>): void {
139
this.storageService.store(DEBUG_CHOSEN_ENVIRONMENTS_KEY, JSON.stringify(environments), StorageScope.WORKSPACE, StorageTarget.MACHINE);
140
}
141
142
storeWatchExpressions(watchExpressions: (IExpression & IEvaluate)[]): void {
143
if (watchExpressions.length) {
144
this.storageService.store(DEBUG_WATCH_EXPRESSIONS_KEY, JSON.stringify(watchExpressions.map(we => ({ name: we.name, id: we.getId() }))), StorageScope.WORKSPACE, StorageTarget.MACHINE);
145
} else {
146
this.storageService.remove(DEBUG_WATCH_EXPRESSIONS_KEY, StorageScope.WORKSPACE);
147
}
148
}
149
150
storeBreakpoints(debugModel: IDebugModel): void {
151
const breakpoints = debugModel.getBreakpoints();
152
if (breakpoints.length) {
153
this.storageService.store(DEBUG_BREAKPOINTS_KEY, JSON.stringify(breakpoints), StorageScope.WORKSPACE, StorageTarget.MACHINE);
154
} else {
155
this.storageService.remove(DEBUG_BREAKPOINTS_KEY, StorageScope.WORKSPACE);
156
}
157
158
const functionBreakpoints = debugModel.getFunctionBreakpoints();
159
if (functionBreakpoints.length) {
160
this.storageService.store(DEBUG_FUNCTION_BREAKPOINTS_KEY, JSON.stringify(functionBreakpoints), StorageScope.WORKSPACE, StorageTarget.MACHINE);
161
} else {
162
this.storageService.remove(DEBUG_FUNCTION_BREAKPOINTS_KEY, StorageScope.WORKSPACE);
163
}
164
165
const dataBreakpoints = debugModel.getDataBreakpoints().filter(dbp => dbp.canPersist);
166
if (dataBreakpoints.length) {
167
this.storageService.store(DEBUG_DATA_BREAKPOINTS_KEY, JSON.stringify(dataBreakpoints), StorageScope.WORKSPACE, StorageTarget.MACHINE);
168
} else {
169
this.storageService.remove(DEBUG_DATA_BREAKPOINTS_KEY, StorageScope.WORKSPACE);
170
}
171
172
const exceptionBreakpoints = debugModel.getExceptionBreakpoints();
173
if (exceptionBreakpoints.length) {
174
this.storageService.store(DEBUG_EXCEPTION_BREAKPOINTS_KEY, JSON.stringify(exceptionBreakpoints), StorageScope.WORKSPACE, StorageTarget.MACHINE);
175
} else {
176
this.storageService.remove(DEBUG_EXCEPTION_BREAKPOINTS_KEY, StorageScope.WORKSPACE);
177
}
178
}
179
}
180
181