-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsidebar.ts
342 lines (309 loc) · 13 KB
/
sidebar.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
import * as vscode from 'vscode';
import { ModuleScope, Scope } from '../model/scope';
import { MemoryVariable, ScalarVariable, Variable } from '../model/variable';
import { DisplayStyle, variableDescription, variableBitIndices, memoryRowIndices, variableValue, variableTooltip } from '../model/styling';
import { CXXRTLDebugger } from '../debugger';
import { Observer } from '../debug/observer';
import { Designation, MemoryRangeDesignation, MemoryRowDesignation, ScalarDesignation } from '../model/sample';
import { IWatchItem, globalWatchList } from '../debug/watch';
import { Session } from '../debug/session';
abstract class TreeItem {
// Currently used only for removing watch items, where knowing the index is necessary.
metadata: any;
constructor(
readonly provider: TreeDataProvider
) {}
abstract getTreeItem(): vscode.TreeItem | Thenable<vscode.TreeItem>;
getChildren(): vscode.ProviderResult<TreeItem[]> {
return [];
}
get displayStyle(): DisplayStyle {
return this.provider.displayStyle;
}
getValue<T>(designation: Designation<T>): T | undefined {
return this.provider.getValue(this, designation);
}
}
class BitTreeItem extends TreeItem {
constructor(
provider: TreeDataProvider,
readonly designation: ScalarDesignation | MemoryRowDesignation,
readonly bitIndex: number,
readonly contextValue: string = '',
) {
super(provider);
}
get variable(): Variable {
return this.designation.variable;
}
override getTreeItem(): vscode.TreeItem {
const variable = this.designation.variable;
const treeItem = new vscode.TreeItem(variable.name);
if (this.designation instanceof MemoryRowDesignation) {
treeItem.label += `[${this.designation.index}]`;
}
treeItem.label += `[${this.bitIndex}]`;
treeItem.iconPath = new vscode.ThemeIcon('symbol-boolean');
const value = this.getValue(this.designation);
if (value === undefined) {
treeItem.description = '= ...';
} else {
treeItem.description = ((value & (1n << BigInt(this.bitIndex))) !== 0n)
? '= 1'
: '= 0';
}
treeItem.tooltip = variableTooltip(variable);
treeItem.contextValue = this.contextValue;
return treeItem;
}
getWatchItem(): IWatchItem {
return {
id: this.designation.variable.cxxrtlIdentifier,
row: (this.designation instanceof MemoryRowDesignation)
? this.designation.index : undefined,
bit: this.bitIndex,
};
}
}
class ScalarTreeItem extends TreeItem {
constructor(
provider: TreeDataProvider,
readonly designation: ScalarDesignation | MemoryRowDesignation,
readonly contextValue: string = '',
) {
super(provider);
}
override getTreeItem(): vscode.TreeItem {
const variable = this.designation.variable;
const treeItem = new vscode.TreeItem(variable.name);
if (this.designation instanceof MemoryRowDesignation) {
treeItem.label += `[${this.designation.index}]`;
}
if (variable.width > 1) {
treeItem.collapsibleState = vscode.TreeItemCollapsibleState.Collapsed;
}
treeItem.iconPath = (variable.width === 1)
? new vscode.ThemeIcon('symbol-boolean')
: new vscode.ThemeIcon('symbol-variable');
const value = this.getValue(this.designation);
treeItem.description = variableDescription(this.displayStyle, variable, { scalar: true });
treeItem.description += (treeItem.description !== '') ? ' = ' : '= ';
treeItem.description += variableValue(this.displayStyle, variable, value);
treeItem.tooltip = variableTooltip(variable);
treeItem.command = variable.location?.asOpenCommand();
treeItem.contextValue = this.contextValue;
return treeItem;
}
override getChildren(): TreeItem[] {
const variable = this.designation.variable;
return Array.from(variableBitIndices(this.displayStyle, variable)).map((index) =>
new BitTreeItem(this.provider, this.designation, index, 'canWatch'));
}
getWatchItem(): IWatchItem {
return {
id: this.designation.variable.cxxrtlIdentifier,
row: (this.designation instanceof MemoryRowDesignation)
? this.designation.index : undefined,
};
}
}
class ArrayTreeItem extends TreeItem {
constructor(
provider: TreeDataProvider,
readonly designation: MemoryRangeDesignation,
readonly contextValue: string = '',
) {
super(provider);
}
override getTreeItem(): vscode.TreeItem {
const variable = this.designation.variable;
const treeItem = new vscode.TreeItem(variable.name);
treeItem.collapsibleState = vscode.TreeItemCollapsibleState.Collapsed;
treeItem.iconPath = new vscode.ThemeIcon('symbol-array');
treeItem.description = variableDescription(this.displayStyle, variable);
treeItem.tooltip = variableTooltip(variable);
treeItem.command = variable.location?.asOpenCommand();
treeItem.contextValue = this.contextValue;
return treeItem;
}
override getChildren(): TreeItem[] {
const variable = this.designation.variable;
return Array.from(memoryRowIndices(variable)).map((index) =>
new ScalarTreeItem(this.provider, variable.designation(index), 'canWatch'));
}
getWatchItem(): IWatchItem {
return {
id: this.designation.variable.cxxrtlIdentifier
};
}
}
class ScopeTreeItem extends TreeItem {
constructor(
provider: TreeDataProvider,
readonly scope: Scope,
readonly hasSiblings: boolean,
) {
super(provider);
}
override async getTreeItem(): Promise<vscode.TreeItem> {
if (this.scope.name === '') {
return new vscode.TreeItem('ʜɪᴇʀᴀʀᴄʜʏ', vscode.TreeItemCollapsibleState.Expanded);
} else {
const treeItem = new vscode.TreeItem(this.scope.name);
treeItem.iconPath = new vscode.ThemeIcon('symbol-module');
if (this.scope instanceof ModuleScope) {
treeItem.description = this.scope.moduleFullName.join('.');
}
treeItem.tooltip = new vscode.MarkdownString(this.scope.fullName.join('.'));
treeItem.tooltip.isTrusted = true;
if (this.scope.location) {
treeItem.tooltip.appendMarkdown(`\n\n- ${this.scope.location.asMarkdownLink()}`);
if (this.scope instanceof ModuleScope && this.scope.moduleLocation) {
treeItem.tooltip.appendMarkdown(`\n- ${this.scope.moduleLocation.asMarkdownLink()}`);
}
treeItem.command = this.scope.location.asOpenCommand();
}
const [subScopes, variables] = await Promise.all([this.scope.scopes, this.scope.variables]);
if (subScopes.length > 0 || variables.length > 0) {
treeItem.collapsibleState = this.hasSiblings ?
vscode.TreeItemCollapsibleState.Collapsed :
vscode.TreeItemCollapsibleState.Expanded;
}
return treeItem;
}
}
override async getChildren(): Promise<TreeItem[]> {
const [subScopes, variables] = await Promise.all([this.scope.scopes, this.scope.variables]);
const children = [];
for (const scope of subScopes) {
children.push(new ScopeTreeItem(this.provider, scope, subScopes.length > 1));
}
for (const variable of variables) {
if (variable instanceof ScalarVariable) {
children.push(new ScalarTreeItem(this.provider, variable.designation(),
variable.width > 1 ? 'canWatch|canSetRadix|variable' : 'canWatch|variable'));
}
if (variable instanceof MemoryVariable) {
children.push(new ArrayTreeItem(this.provider, variable.designation(), 'canWatch|canSetRadix|variable'));
}
}
return children;
}
}
class WatchTreeItem extends TreeItem {
constructor(
provider: TreeDataProvider
) {
super(provider);
}
override async getTreeItem(): Promise<vscode.TreeItem> {
if (globalWatchList.get().length > 0) {
return new vscode.TreeItem('ᴡᴀᴛᴄʜ', vscode.TreeItemCollapsibleState.Expanded);
} else {
return new vscode.TreeItem('ᴡᴀᴛᴄʜ (empty)');
}
}
override async getChildren(): Promise<TreeItem[]> {
const children = [];
for (const [index, watchItem] of globalWatchList.get().entries()) {
const variable = await this.provider.getVariable(watchItem.id);
if (variable === null) {
continue;
}
let designation;
if (variable instanceof ScalarVariable) {
designation = variable.designation();
} else if (variable instanceof MemoryVariable) {
if (watchItem.row === undefined) {
designation = variable.designation();
} else {
designation = variable.designation(watchItem.row);
}
}
let treeItem;
if (designation instanceof MemoryRangeDesignation) {
treeItem = new ArrayTreeItem(this.provider, designation, 'inWatchList|canSetRadix');
} else if (designation instanceof ScalarDesignation || designation instanceof MemoryRowDesignation) {
if (watchItem.bit === undefined) {
treeItem = new ScalarTreeItem(this.provider, designation,
designation.variable.width > 1 ? 'inWatchList|canSetRadix' : 'inWatchList');
} else {
treeItem = new BitTreeItem(this.provider, designation, watchItem.bit, 'inWatchList');
}
}
if (treeItem !== undefined) {
treeItem.metadata = { index };
children.push(treeItem);
}
}
return children;
}
}
export class TreeDataProvider implements vscode.TreeDataProvider<TreeItem> {
private _onDidChangeTreeData: vscode.EventEmitter<TreeItem | null> = new vscode.EventEmitter<TreeItem | null>();
readonly onDidChangeTreeData: vscode.Event<TreeItem | null> = this._onDidChangeTreeData.event;
private session: Session | null = null;
private observer: Observer | null = null;
private watchTreeItem: WatchTreeItem | null = null;
private scopeTreeItem: ScopeTreeItem | null = null;
constructor(rtlDebugger: CXXRTLDebugger) {
vscode.workspace.onDidChangeConfiguration((event) => {
if (event.affectsConfiguration('rtlDebugger.displayStyle') ||
event.affectsConfiguration('rtlDebugger.variableOptions')) {
this._onDidChangeTreeData.fire(null);
}
});
rtlDebugger.onDidChangeSession(async (session) => {
this.session = session;
if (session !== null) {
this.observer = new Observer(session, 'sidebar');
this.watchTreeItem = new WatchTreeItem(this);
this.scopeTreeItem = new ScopeTreeItem(this, await session.getRootScope(), false);
} else {
this.observer?.dispose();
this.observer = null;
this.watchTreeItem = null;
this.scopeTreeItem = null;
}
this._onDidChangeTreeData.fire(null);
});
globalWatchList.onDidChange((_items) => {
if (this.watchTreeItem !== null) {
this._onDidChangeTreeData.fire(this.watchTreeItem);
}
});
}
getTreeItem(element: TreeItem): vscode.TreeItem | Thenable<vscode.TreeItem> {
return element.getTreeItem();
}
async getChildren(element?: TreeItem): Promise<TreeItem[] | null | undefined> {
if (element !== undefined) {
return await element.getChildren();
}
const children = [];
if (this.watchTreeItem !== null) {
children.push(this.watchTreeItem);
}
if (this.scopeTreeItem !== null) {
children.push(this.scopeTreeItem);
}
return children;
}
get displayStyle(): DisplayStyle {
const displayStyle = vscode.workspace.getConfiguration('rtlDebugger').get('displayStyle');
return displayStyle as DisplayStyle;
}
getVariable(identifier: string): Promise<Variable | null> {
return this.session!.getVariable(identifier);
}
getValue<T>(element: TreeItem, designation: Designation<T>): T | undefined {
if (this.observer === null) {
return;
}
this.observer.observe(designation, (_value) => {
this._onDidChangeTreeData.fire(element);
return false; // one-shot
});
return this.observer.query(designation);
}
}