/
index.ts
165 lines (142 loc) · 3.92 KB
/
index.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { IThemeManager, ToolbarButton } from '@jupyterlab/apputils';
import { nullTranslator, ITranslator } from '@jupyterlab/translation';
import { CommandRegistry } from '@lumino/commands';
import { Panel, Widget } from '@lumino/widgets';
import { IDebugger } from '../../tokens';
import { VariablesBodyGrid } from './grid';
import { VariablesHeader } from './header';
import { ScopeSwitcher } from './scope';
import { VariablesBodyTree } from './tree';
/**
* A Panel to show a variable explorer.
*/
export class Variables extends Panel {
/**
* Instantiate a new Variables Panel.
*
* @param options The instantiation options for a Variables Panel.
*/
constructor(options: Variables.IOptions) {
super();
const { model, service, commands, themeManager } = options;
const translator = options.translator || nullTranslator;
const trans = translator.load('jupyterlab');
this._header = new VariablesHeader(translator);
this._tree = new VariablesBodyTree({ model, service });
this._table = new VariablesBodyGrid({ model, commands, themeManager });
this._table.hide();
const onViewChange = (): void => {
if (this._table.isHidden) {
this._tree.hide();
this._table.show();
this.node.setAttribute('data-jp-table', 'true');
} else {
this._tree.show();
this._table.hide();
this.node.removeAttribute('data-jp-table');
}
this.update();
};
this._header.toolbar.addItem(
'scope-switcher',
new ScopeSwitcher({
translator,
model,
tree: this._tree,
grid: this._table
})
);
this._header.toolbar.addItem(
'view-VariableSwitch',
new ToolbarButton({
iconClass: 'jp-ToggleSwitch',
onClick: onViewChange,
tooltip: trans.__('Table / Tree View')
})
);
this.addWidget(this._header);
this.addWidget(this._tree);
this.addWidget(this._table);
this.addClass('jp-DebuggerVariables');
}
/**
* Set the variable filter for both the tree and table views.
*/
set filter(filter: Set<string>) {
this._tree.filter = filter;
this._table.filter = filter;
}
/**
* A message handler invoked on a `'resize'` message.
*
* @param msg The Lumino message to process.
*/
protected onResize(msg: Widget.ResizeMessage): void {
super.onResize(msg);
this._resizeBody(msg);
}
/**
* Resize the body.
*
* @param msg The resize message.
*/
private _resizeBody(msg: Widget.ResizeMessage): void {
const height = msg.height - this._header.node.offsetHeight;
this._tree.node.style.height = `${height}px`;
}
private _header: VariablesHeader;
private _tree: VariablesBodyTree;
private _table: VariablesBodyGrid;
}
/**
* Convert a variable to a primitive type.
*
* @param variable The variable.
*/
export const convertType = (variable: IDebugger.IVariable): string | number => {
const { type, value } = variable;
switch (type) {
case 'int':
return parseInt(value, 10);
case 'float':
return parseFloat(value);
case 'bool':
return value;
case 'str':
return value.slice(1, value.length - 1);
default:
return type ?? value;
}
};
/**
* A namespace for Variables `statics`.
*/
export namespace Variables {
/**
* Instantiation options for `Variables`.
*/
export interface IOptions extends Panel.IOptions {
/**
* The variables model.
*/
model: IDebugger.Model.IVariables;
/**
* The debugger service.
*/
service: IDebugger;
/**
* The commands registry.
*/
commands: CommandRegistry;
/**
* An optional application theme manager to detect theme changes.
*/
themeManager?: IThemeManager | null;
/**
* The application language translator
*/
translator?: ITranslator;
}
}