This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
cpp-debug-frontend-contribution.ts
315 lines (300 loc) · 15.5 KB
/
cpp-debug-frontend-contribution.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
/********************************************************************************
* Copyright (C) 2019 Ericsson 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 { AbstractViewContribution, FrontendApplicationContribution, Widget } from '@theia/core/lib/browser';
import { ColorContribution } from '@theia/core/lib/browser/color-application-contribution';
import { ColorRegistry } from '@theia/core/lib/browser/color-registry';
import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
import { TabBarToolbarContribution, TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { Command, CommandRegistry, MenuModelRegistry } from '@theia/core/lib/common';
import { Color } from '@theia/core/lib/common/color';
import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
import { DebugScope, DebugVariable } from '@theia/debug/lib/browser/console/debug-console-items';
import { DebugFrontendApplicationContribution } from '@theia/debug/lib/browser/debug-frontend-application-contribution';
import { DebugVariablesWidget } from '@theia/debug/lib/browser/view/debug-variables-widget';
import * as Long from 'long';
import { MemoryEditableTableWidget } from './editable-widget/memory-editable-table-widget';
import { MemoryTableWidget } from './memory-widget/memory-table-widget';
import { MemoryWidget } from './memory-widget/memory-widget';
import { RegisterTableWidget } from './register-widget/register-table-widget';
import { RegisterWidget } from './register-widget/register-widget-types';
import {
CreateNewMemoryViewCommand, CreateNewRegisterViewCommand, FollowPointerDebugCommand, FollowPointerTableCommand, MemoryCommand,
RegisterSetVariableCommand, ResetModifiedCellCommand, ToggleDiffSelectWidgetVisibilityCommand, ViewVariableInMemoryCommand, ViewVariableInRegisterViewCommand
} from './utils/memory-commands';
import { MemoryWidgetManager } from './utils/memory-widget-manager';
import { VariableRange } from './utils/memory-widget-variable-utils';
import { MemoryDockPanel } from './wrapper-widgets/memory-dock-panel';
import { MemoryLayoutWidget } from './wrapper-widgets/memory-layout-widget';
const ONE_HALF_OPACITY = 0.5;
@injectable()
export class DebugFrontendContribution extends AbstractViewContribution<MemoryLayoutWidget>
implements FrontendApplicationContribution,
TabBarToolbarContribution,
ColorContribution {
@inject(DebugFrontendApplicationContribution) protected readonly debugContribution: DebugFrontendApplicationContribution;
@inject(MemoryWidgetManager) protected readonly memoryWidgetManager: MemoryWidgetManager;
@inject(FrontendApplicationStateService) protected readonly stateService: FrontendApplicationStateService;
constructor() {
super({
widgetId: MemoryLayoutWidget.ID,
widgetName: MemoryLayoutWidget.LABEL,
defaultWidgetOptions: {
area: 'right',
},
toggleCommandId: MemoryCommand.id,
});
}
@postConstruct()
init(): void {
this.stateService.reachedState('initialized_layout').then(() => {
// Close leftover widgets from previous sessions.
this.memoryWidgetManager.availableWidgets.forEach(widget => {
if (!(widget.parent instanceof MemoryDockPanel)) {
widget.close();
}
});
});
}
async initializeLayout(): Promise<void> {
await this.openView({ activate: false });
}
// eslint-disable-next-line max-lines-per-function
registerCommands(registry: CommandRegistry): void {
super.registerCommands(registry);
registry.registerCommand(ViewVariableInMemoryCommand, {
execute: async () => {
let currentLevel = this.debugContribution.selectedVariable;
if (currentLevel) {
let { name } = currentLevel;
while (currentLevel['parent'] instanceof DebugVariable) { // TODO: THIS IS A PROTECTED FIELD THAT WE SHOULDN'T ACCESS.
const separator = name.startsWith('[') ? '' : '.';
currentLevel = currentLevel['parent'];
if (name.startsWith(`*${currentLevel.name}.`)) { // Theia has added a layer of pointer dereferencing
name = name.replace(`*${currentLevel.name}.`, `(*${currentLevel.name})->`);
} else if (name.startsWith(`*${currentLevel.name}`)) {
// that's fine, it's what you clicked on and probably what you want to see.
} else {
name = `${currentLevel.name}${separator}${name}`;
}
}
name = `&(${name})`;
await this.openMemoryWidgetAt(name);
}
},
isVisible: () => {
let { selectedVariable: currentLevel } = this.debugContribution;
if (!currentLevel) {
return false;
}
while (currentLevel['parent'] instanceof DebugVariable) { // TODO: THIS IS A PROTECTED FIELD THAT WE SHOULDN'T ACCESS.
currentLevel = currentLevel['parent'];
}
return currentLevel['parent'] instanceof DebugScope && currentLevel['parent']['raw'].name === 'Local';
},
});
registry.registerCommand(ViewVariableInRegisterViewCommand, {
execute: async () => {
const name = this.debugContribution.selectedVariable?.name;
if (name) {
await this.openRegisterWidgetWithReg(name);
}
},
isVisible: () => {
let { selectedVariable: currentLevel } = this.debugContribution;
if (!currentLevel) {
return false;
}
while (currentLevel['parent'] instanceof DebugVariable) { // TODO: THIS IS A PROTECTED FIELD THAT WE SHOULDN'T ACCESS.
currentLevel = currentLevel['parent'];
}
return currentLevel['parent'] instanceof DebugScope && currentLevel['parent']?.['raw']?.name === 'Registers';
},
});
registry.registerCommand(FollowPointerDebugCommand, {
isVisible: () => !!this.isPointer(this.debugContribution.selectedVariable?.type),
isEnabled: () => !!this.isPointer(this.debugContribution.selectedVariable?.type),
execute: async () => {
const name = this.debugContribution.selectedVariable?.name;
if (name) {
await this.openMemoryWidgetAt(name);
}
},
});
registry.registerCommand(ResetModifiedCellCommand, {
isEnabled: (widgetToActOn, address: Long) => Long.isLong(address) && widgetToActOn instanceof MemoryEditableTableWidget,
isVisible: (widgetToActOn, address: Long) => Long.isLong(address) && widgetToActOn instanceof MemoryEditableTableWidget,
execute: (widgetToActOn: MemoryEditableTableWidget, address: Long) => widgetToActOn.resetModifiedValue(address),
});
registry.registerCommand(FollowPointerTableCommand, {
isEnabled: (widgetToActOn, address, variable?: VariableRange) => widgetToActOn instanceof MemoryTableWidget &&
this.isPointer(variable?.type),
isVisible: (widgetToActOn, address, variable?: VariableRange) => widgetToActOn instanceof MemoryTableWidget &&
this.isPointer(variable?.type),
execute: (widgetToActOn: MemoryTableWidget, address, variable: VariableRange) => {
if (variable?.name) {
widgetToActOn.optionsWidget.setAddressAndGo(variable.name);
}
},
});
registry.registerCommand(CreateNewMemoryViewCommand, {
isEnabled: w => this.withWidget(() => true, w),
isVisible: w => this.withWidget(() => true, w),
execute: () => this.memoryWidgetManager.createNewMemoryWidget(),
});
registry.registerCommand(CreateNewRegisterViewCommand, {
isEnabled: w => this.withWidget(() => true, w),
isVisible: w => this.withWidget(() => true, w),
execute: () => this.memoryWidgetManager.createNewMemoryWidget('register'),
});
registry.registerCommand(RegisterSetVariableCommand, {
isEnabled: (widgetToActOn, dVar: DebugVariable) => widgetToActOn instanceof RegisterTableWidget &&
dVar && dVar.supportSetVariable,
isVisible: (widgetToActOn, dVar: DebugVariable) => widgetToActOn instanceof RegisterTableWidget &&
dVar && dVar.supportSetVariable,
execute: (widgetToActOn: RegisterTableWidget, dVar: DebugVariable) => dVar && widgetToActOn.handleSetValue(dVar),
});
registry.registerCommand(ToggleDiffSelectWidgetVisibilityCommand, {
isVisible: widget => this.withWidget(() => this.memoryWidgetManager.canCompare, widget),
execute: (widget: MemoryLayoutWidget) => {
widget.toggleComparisonVisibility();
},
});
}
protected isPointer(type?: string): boolean {
return !!type?.includes('*');
}
/**
* @param {string} addressReference Should be the exact string to be used in the address bar. I.e. it must resolve to an address value.
*/
protected async openMemoryWidgetAt(addressReference: string): Promise<MemoryWidget> {
await this.openView({ activate: false });
const newWidget = await this.memoryWidgetManager.createNewMemoryWidget();
await this.shell.activateWidget(newWidget.id);
if (newWidget) {
newWidget.optionsWidget.setAddressAndGo(addressReference);
}
return newWidget;
}
protected async openRegisterWidgetWithReg(name: string): Promise<MemoryWidget> {
await this.openView({ activate: false });
const newWidget = await this.memoryWidgetManager.createNewMemoryWidget<RegisterWidget>('register');
await this.shell.activateWidget(newWidget.id);
if (newWidget) {
newWidget.optionsWidget.setRegAndUpdate(name);
}
return newWidget;
}
protected withWidget(fn: (widget: MemoryLayoutWidget) => boolean, widget: Widget | undefined = this.tryGetWidget()): boolean {
if (widget instanceof MemoryLayoutWidget && widget.id === MemoryLayoutWidget.ID) {
return fn(widget);
}
return false;
}
registerMenus(registry: MenuModelRegistry): void {
super.registerMenus(registry);
const registerMenuActions = (menuPath: string[], ...commands: Command[]): void => {
for (const [index, command] of commands.entries()) {
registry.registerMenuAction(menuPath, {
commandId: command.id,
label: command.label,
icon: command.iconClass,
order: String.fromCharCode('a'.charCodeAt(0) + index),
});
}
};
registry.registerMenuAction(
DebugVariablesWidget.WATCH_MENU,
{ commandId: ViewVariableInMemoryCommand.id, label: ViewVariableInMemoryCommand.label },
);
registry.registerMenuAction(
DebugVariablesWidget.WATCH_MENU,
{ commandId: FollowPointerDebugCommand.id, label: FollowPointerDebugCommand.label },
);
registry.registerMenuAction(
DebugVariablesWidget.WATCH_MENU,
{ commandId: ViewVariableInRegisterViewCommand.id, label: ViewVariableInRegisterViewCommand.label },
);
registry.registerMenuAction(
MemoryEditableTableWidget.CONTEXT_MENU,
{ commandId: ResetModifiedCellCommand.id, label: ResetModifiedCellCommand.label },
);
registry.registerMenuAction(
MemoryTableWidget.CONTEXT_MENU,
{ commandId: FollowPointerTableCommand.id, label: FollowPointerTableCommand.label },
);
registerMenuActions(
RegisterTableWidget.CONTEXT_MENU,
RegisterSetVariableCommand,
);
}
registerToolbarItems(toolbarRegistry: TabBarToolbarRegistry): void {
toolbarRegistry.registerItem({
id: CreateNewMemoryViewCommand.id,
command: CreateNewMemoryViewCommand.id,
tooltip: CreateNewMemoryViewCommand.label,
priority: -2,
});
toolbarRegistry.registerItem({
id: CreateNewRegisterViewCommand.id,
command: CreateNewRegisterViewCommand.id,
tooltip: CreateNewRegisterViewCommand.label,
priority: -1,
});
toolbarRegistry.registerItem({
id: ToggleDiffSelectWidgetVisibilityCommand.id,
command: ToggleDiffSelectWidgetVisibilityCommand.id,
tooltip: 'Toggle Comparison Widget Visibility',
priority: -3,
onDidChange: this.memoryWidgetManager.onChanged,
});
}
registerColors(colorRegistry: ColorRegistry): void {
colorRegistry.register(
{
id: 'diffEditor.removedTextBackground.light',
defaults: {
dark: Color.transparent('diffEditor.removedTextBackground', ONE_HALF_OPACITY),
light: Color.transparent('diffEditor.removedTextBackground', ONE_HALF_OPACITY),
},
description: 'An even lighter version of the diff editor colors for contexts when contrast is an issue.',
},
{
id: 'diffEditor.insertedTextBackground.light',
defaults: {
dark: Color.transparent('diffEditor.insertedTextBackground', ONE_HALF_OPACITY),
light: Color.transparent('diffEditor.insertedTextBackground', ONE_HALF_OPACITY),
},
description: 'An even lighter version of the diff editor colors for contexts when contrast is an issue.',
},
{
id: 'editorLightBulbAutoFix.foreground.light',
defaults: {
dark: Color.transparent('editorLightBulbAutoFix.foreground', ONE_HALF_OPACITY),
light: Color.transparent('editorLightBulbAutoFix.foreground', ONE_HALF_OPACITY),
},
description: 'A lighter version of a blue focused editor state background.',
},
{
id: 'editor.foreground.light',
defaults: {
dark: Color.transparent('editor.foreground', ONE_HALF_OPACITY),
light: Color.transparent('editor.foreground', ONE_HALF_OPACITY),
},
description: 'A lighter version of the text in the editor.',
},
);
}
}