-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathdebug-console-items.tsx
388 lines (344 loc) · 13.4 KB
/
debug-console-items.tsx
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// *****************************************************************************
// 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-only WITH Classpath-exception-2.0
// *****************************************************************************
import * as React from '@theia/core/shared/react';
import { DebugProtocol } from '@vscode/debugprotocol/lib/debugProtocol';
import { SingleTextInputDialog } from '@theia/core/lib/browser';
import { ConsoleItem, CompositeConsoleItem } from '@theia/console/lib/browser/console-session';
import { DebugSession } from '../debug-session';
import { Severity } from '@theia/core/lib/common/severity';
import * as monaco from '@theia/monaco-editor-core';
import { nls } from '@theia/core';
export type DebugSessionProvider = () => DebugSession | undefined;
export class ExpressionContainer implements CompositeConsoleItem {
private static readonly BASE_CHUNK_SIZE = 100;
protected readonly sessionProvider: DebugSessionProvider;
protected get session(): DebugSession | undefined {
return this.sessionProvider();
}
protected variablesReference: number;
protected namedVariables: number | undefined;
protected indexedVariables: number | undefined;
protected readonly startOfVariables: number;
constructor(options: ExpressionContainer.Options) {
this.sessionProvider = options.session;
this.variablesReference = options.variablesReference || 0;
this.namedVariables = options.namedVariables;
this.indexedVariables = options.indexedVariables;
this.startOfVariables = options.startOfVariables || 0;
}
render(): React.ReactNode {
return undefined;
}
get hasElements(): boolean {
return !!this.variablesReference;
}
protected elements: Promise<ExpressionContainer[]> | undefined;
async getElements(): Promise<IterableIterator<ExpressionContainer>> {
if (!this.hasElements || !this.session) {
return [][Symbol.iterator]();
}
if (!this.elements) {
this.elements = this.doResolve();
}
return (await this.elements)[Symbol.iterator]();
}
protected async doResolve(): Promise<ExpressionContainer[]> {
const result: ExpressionContainer[] = [];
if (this.namedVariables) {
await this.fetch(result, 'named');
}
if (this.indexedVariables) {
let chunkSize = ExpressionContainer.BASE_CHUNK_SIZE;
while (this.indexedVariables > chunkSize * ExpressionContainer.BASE_CHUNK_SIZE) {
chunkSize *= ExpressionContainer.BASE_CHUNK_SIZE;
}
if (this.indexedVariables > chunkSize) {
const numberOfChunks = Math.ceil(this.indexedVariables / chunkSize);
for (let i = 0; i < numberOfChunks; i++) {
const start = this.startOfVariables + i * chunkSize;
const count = Math.min(chunkSize, this.indexedVariables - i * chunkSize);
const { variablesReference } = this;
result.push(new DebugVirtualVariable({
session: this.sessionProvider,
variablesReference,
namedVariables: 0,
indexedVariables: count,
startOfVariables: start,
name: `[${start}..${start + count - 1}]`
}));
}
return result;
}
}
await this.fetch(result, 'indexed', this.startOfVariables, this.indexedVariables);
return result;
}
protected fetch(result: ConsoleItem[], filter: 'named'): Promise<void>;
protected fetch(result: ConsoleItem[], filter: 'indexed', start: number, count?: number): Promise<void>;
protected async fetch(result: ConsoleItem[], filter: 'indexed' | 'named', start?: number, count?: number): Promise<void> {
try {
const { variablesReference } = this;
const response = await this.session!.sendRequest('variables', { variablesReference, filter, start, count });
const { variables } = response.body;
const names = new Set<string>();
for (const variable of variables) {
if (!names.has(variable.name)) {
result.push(new DebugVariable(this.sessionProvider, variable, this));
names.add(variable.name);
}
}
} catch (e) {
result.push({
severity: Severity.Error,
visible: !!e.message,
render: () => e.message
});
}
}
}
export namespace ExpressionContainer {
export interface Options {
session: DebugSessionProvider,
variablesReference?: number
namedVariables?: number
indexedVariables?: number
startOfVariables?: number
}
}
export class DebugVariable extends ExpressionContainer {
static booleanRegex = /^true|false$/i;
static stringRegex = /^(['"]).*\1$/;
constructor(
session: DebugSessionProvider,
protected readonly variable: DebugProtocol.Variable,
readonly parent: ExpressionContainer
) {
super({
session,
variablesReference: variable.variablesReference,
namedVariables: variable.namedVariables,
indexedVariables: variable.indexedVariables
});
}
get name(): string {
return this.variable.name;
}
protected _type: string | undefined;
get type(): string | undefined {
return this._type || this.variable.type;
}
protected _value: string | undefined;
get value(): string {
return this._value || this.variable.value;
}
get readOnly(): boolean {
return this.variable.presentationHint?.attributes?.includes('readOnly') ?? false;
}
override render(): React.ReactNode {
const { type, value, name } = this;
return <div className={this.variableClassName}>
<span title={type || name} className='name' ref={this.setNameRef}>{name}{!!value && ': '}</span>
<span title={value} ref={this.setValueRef}>{value}</span>
</div>;
}
protected get variableClassName(): string {
const { type, value } = this;
const classNames = ['theia-debug-console-variable'];
if (type === 'number' || type === 'boolean' || type === 'string') {
classNames.push(type);
} else if (!isNaN(+value)) {
classNames.push('number');
} else if (DebugVariable.booleanRegex.test(value)) {
classNames.push('boolean');
} else if (DebugVariable.stringRegex.test(value)) {
classNames.push('string');
}
return classNames.join(' ');
}
get supportSetVariable(): boolean {
return !!this.session && !!this.session.capabilities.supportsSetVariable;
}
async setValue(value: string): Promise<void> {
if (!this.session) {
return;
}
const { name, parent } = this;
const variablesReference = parent['variablesReference'];
try {
const response = await this.session.sendRequest('setVariable', { variablesReference, name, value });
this._value = response.body.value;
this._type = response.body.type;
this.variablesReference = response.body.variablesReference || 0;
this.namedVariables = response.body.namedVariables;
this.indexedVariables = response.body.indexedVariables;
this.elements = undefined;
this.session['fireDidChange']();
} catch (error) {
console.error('setValue failed:', error);
}
}
get supportCopyValue(): boolean {
return !!this.valueRef && document.queryCommandSupported('copy');
}
copyValue(): void {
const selection = document.getSelection();
if (this.valueRef && selection) {
selection.selectAllChildren(this.valueRef);
document.execCommand('copy');
}
}
protected valueRef: HTMLSpanElement | undefined;
protected setValueRef = (valueRef: HTMLSpanElement | null) => this.valueRef = valueRef || undefined;
get supportCopyAsExpression(): boolean {
return !!this.nameRef && document.queryCommandSupported('copy');
}
copyAsExpression(): void {
const selection = document.getSelection();
if (this.nameRef && selection) {
selection.selectAllChildren(this.nameRef);
document.execCommand('copy');
}
}
protected nameRef: HTMLSpanElement | undefined;
protected setNameRef = (nameRef: HTMLSpanElement | null) => this.nameRef = nameRef || undefined;
async open(): Promise<void> {
if (!this.supportSetVariable || this.readOnly) {
return;
}
const input = new SingleTextInputDialog({
title: nls.localize('theia/debug/debugVariableInput', 'Set {0} Value', this.name),
initialValue: this.value,
placeholder: nls.localizeByDefault('Value')
});
const newValue = await input.open();
if (newValue) {
await this.setValue(newValue);
}
}
}
export class DebugVirtualVariable extends ExpressionContainer {
constructor(
protected readonly options: VirtualVariableItem.Options
) {
super(options);
}
override render(): React.ReactNode {
return this.options.name;
}
}
export namespace VirtualVariableItem {
export interface Options extends ExpressionContainer.Options {
name: string
}
}
export class ExpressionItem extends ExpressionContainer {
severity?: Severity;
static notAvailable = 'not available';
protected _value = ExpressionItem.notAvailable;
get value(): string {
return this._value;
}
protected _type: string | undefined;
get type(): string | undefined {
return this._type;
}
protected _available = false;
get available(): boolean {
return this._available;
}
constructor(
protected _expression: string,
session: DebugSessionProvider
) {
super({ session });
}
get expression(): string {
return this._expression;
}
override render(): React.ReactNode {
const valueClassNames: string[] = [];
if (!this._available) {
valueClassNames.push(ConsoleItem.errorClassName);
valueClassNames.push('theia-debug-console-unavailable');
}
return <div className={'theia-debug-console-expression'}>
<div>{this._expression}</div>
<div className={valueClassNames.join(' ')}>{this._value}</div>
</div>;
}
async evaluate(context: string = 'repl'): Promise<void> {
const session = this.session;
if (session) {
try {
const body = await session.evaluate(this._expression, context);
this.setResult(body);
} catch (err) {
this.setResult(undefined, err.message);
}
} else {
this.setResult(undefined, 'Please start a debug session to evaluate');
}
}
protected setResult(body?: DebugProtocol.EvaluateResponse['body'], error: string = ExpressionItem.notAvailable): void {
if (body) {
this._value = body.result;
this._type = body.type;
this._available = true;
this.variablesReference = body.variablesReference;
this.namedVariables = body.namedVariables;
this.indexedVariables = body.indexedVariables;
this.severity = Severity.Log;
} else {
this._value = error;
this._type = undefined;
this._available = false;
this.variablesReference = 0;
this.namedVariables = undefined;
this.indexedVariables = undefined;
this.severity = Severity.Error;
}
this.elements = undefined;
}
}
export class DebugScope extends ExpressionContainer {
constructor(
protected readonly raw: DebugProtocol.Scope,
session: DebugSessionProvider
) {
super({
session,
variablesReference: raw.variablesReference,
namedVariables: raw.namedVariables,
indexedVariables: raw.indexedVariables
});
}
override render(): React.ReactNode {
return this.name;
}
get expensive(): boolean {
return this.raw.expensive;
}
get range(): monaco.Range | undefined {
const { line, column, endLine, endColumn } = this.raw;
if (line !== undefined && column !== undefined && endLine !== undefined && endColumn !== undefined) {
return new monaco.Range(line, column, endLine, endColumn);
}
return undefined;
}
get name(): string {
return this.raw.name;
}
}