-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
debug-configuration-manager.ts
559 lines (496 loc) · 24.4 KB
/
debug-configuration-manager.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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// *****************************************************************************
// Copyright (C) 2018 Red Hat, Inc. 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
// *****************************************************************************
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import debounce = require('p-debounce');
import { visit, parse } from 'jsonc-parser';
import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
import URI from '@theia/core/lib/common/uri';
import { Emitter, Event, WaitUntilEvent } from '@theia/core/lib/common/event';
import { EditorManager, EditorWidget } from '@theia/editor/lib/browser';
import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor';
import { LabelProvider, PreferenceScope, PreferenceService, QuickPickValue, StorageService } from '@theia/core/lib/browser';
import { QuickPickService } from '@theia/core/lib/common/quick-pick-service';
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
import { DebugConfigurationModel } from './debug-configuration-model';
import { DebugSessionOptions, DynamicDebugConfigurationSessionOptions } from './debug-session-options';
import { DebugService } from '../common/debug-service';
import { ContextKey, ContextKeyService } from '@theia/core/lib/browser/context-key-service';
import { DebugConfiguration } from '../common/debug-common';
import { WorkspaceVariableContribution } from '@theia/workspace/lib/browser/workspace-variable-contribution';
import { PreferenceConfigurations } from '@theia/core/lib/browser/preferences/preference-configurations';
import { MonacoTextModelService } from '@theia/monaco/lib/browser/monaco-text-model-service';
import * as monaco from '@theia/monaco-editor-core';
import { ICommandService } from '@theia/monaco-editor-core/esm/vs/platform/commands/common/commands';
import { StandaloneServices } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices';
import { nls } from '@theia/core';
import { DebugCompound } from '../common/debug-compound';
export interface WillProvideDebugConfiguration extends WaitUntilEvent {
}
@injectable()
export class DebugConfigurationManager {
@inject(WorkspaceService)
protected readonly workspaceService: WorkspaceService;
@inject(EditorManager)
protected readonly editorManager: EditorManager;
@inject(DebugService)
protected readonly debug: DebugService;
@inject(QuickPickService)
protected readonly quickPickService: QuickPickService;
@inject(ContextKeyService)
protected readonly contextKeyService: ContextKeyService;
@inject(LabelProvider)
protected readonly labelProvider: LabelProvider;
@inject(MonacoTextModelService)
protected readonly textModelService: MonacoTextModelService;
@inject(PreferenceService)
protected readonly preferences: PreferenceService;
@inject(PreferenceConfigurations)
protected readonly preferenceConfigurations: PreferenceConfigurations;
@inject(WorkspaceVariableContribution)
protected readonly workspaceVariables: WorkspaceVariableContribution;
protected readonly onDidChangeEmitter = new Emitter<void>();
readonly onDidChange: Event<void> = this.onDidChangeEmitter.event;
protected readonly onWillProvideDebugConfigurationEmitter = new Emitter<WillProvideDebugConfiguration>();
readonly onWillProvideDebugConfiguration: Event<WillProvideDebugConfiguration> = this.onWillProvideDebugConfigurationEmitter.event;
protected readonly onWillProvideDynamicDebugConfigurationEmitter = new Emitter<WillProvideDebugConfiguration>();
get onWillProvideDynamicDebugConfiguration(): Event<WillProvideDebugConfiguration> {
return this.onWillProvideDynamicDebugConfigurationEmitter.event;
}
get onDidChangeConfigurationProviders(): Event<void> {
return this.debug.onDidChangeDebugConfigurationProviders;
}
protected debugConfigurationTypeKey: ContextKey<string>;
protected initialized: Promise<void>;
protected recentDynamicOptionsTracker: DynamicDebugConfigurationSessionOptions[] = [];
@postConstruct()
protected async init(): Promise<void> {
this.debugConfigurationTypeKey = this.contextKeyService.createKey<string>('debugConfigurationType', undefined);
this.initialized = this.preferences.ready.then(() => {
this.preferences.onPreferenceChanged(e => {
if (e.preferenceName === 'launch') {
this.updateModels();
}
});
return this.updateModels();
});
}
protected readonly models = new Map<string, DebugConfigurationModel>();
protected updateModels = debounce(async () => {
const roots = await this.workspaceService.roots;
const toDelete = new Set(this.models.keys());
for (const rootStat of roots) {
const key = rootStat.resource.toString();
toDelete.delete(key);
if (!this.models.has(key)) {
const model = new DebugConfigurationModel(key, this.preferences);
model.onDidChange(() => this.updateCurrent());
model.onDispose(() => this.models.delete(key));
this.models.set(key, model);
}
}
for (const uri of toDelete) {
const model = this.models.get(uri);
if (model) {
model.dispose();
}
}
this.updateCurrent();
}, 500);
/**
* All _non-dynamic_ debug configurations.
*/
get all(): IterableIterator<DebugSessionOptions> {
return this.getAll();
}
protected *getAll(): IterableIterator<DebugSessionOptions> {
for (const model of this.models.values()) {
for (const configuration of model.configurations) {
yield this.configurationToOptions(configuration, model.workspaceFolderUri);
}
for (const compound of model.compounds) {
yield this.compoundToOptions(compound, model.workspaceFolderUri);
}
}
}
get supported(): Promise<IterableIterator<DebugSessionOptions>> {
return this.getSupported();
}
protected async getSupported(): Promise<IterableIterator<DebugSessionOptions>> {
await this.initialized;
const debugTypes = await this.debug.debugTypes();
return this.doGetSupported(new Set(debugTypes));
}
protected *doGetSupported(debugTypes: Set<string>): IterableIterator<DebugSessionOptions> {
for (const options of this.getAll()) {
if (options.configuration && debugTypes.has(options.configuration.type)) {
yield options;
}
}
}
protected _currentOptions: DebugSessionOptions | undefined;
get current(): DebugSessionOptions | undefined {
return this._currentOptions;
}
async getSelectedConfiguration(): Promise<DebugSessionOptions | undefined> {
if (!DebugSessionOptions.isDynamic(this._currentOptions)) {
return this._currentOptions;
}
// Refresh a dynamic configuration from the provider.
// This allow providers to update properties before the execution e.g. program
const { providerType, configuration: { name } } = this._currentOptions;
const configuration = await this.fetchDynamicDebugConfiguration(name, providerType);
if (!configuration) {
const message = nls.localize(
'theia/debug/missingConfiguration',
"Dynamic configuration '{0}:{1}' is missing or not applicable", providerType, name);
throw new Error(message);
}
return { name, configuration, providerType };
}
set current(option: DebugSessionOptions | undefined) {
this.updateCurrent(option);
this.updateRecentlyUsedDynamicConfigurationOptions(option);
}
protected updateRecentlyUsedDynamicConfigurationOptions(option: DebugSessionOptions | undefined): void {
if (DebugSessionOptions.isDynamic(option)) {
// Removing an item already present in the list
const index = this.recentDynamicOptionsTracker.findIndex(item => this.dynamicOptionsMatch(item, option));
if (index > -1) {
this.recentDynamicOptionsTracker.splice(index, 1);
}
// Adding new item, most recent at the top of the list
const recentMax = 3;
if (this.recentDynamicOptionsTracker.unshift(option) > recentMax) {
// Keep the latest 3 dynamic configuration options to not clutter the dropdown.
this.recentDynamicOptionsTracker.splice(recentMax);
}
}
}
protected dynamicOptionsMatch(one: DynamicDebugConfigurationSessionOptions, other: DynamicDebugConfigurationSessionOptions): boolean {
return one.providerType !== undefined
&& one.configuration.name === other.configuration.name
&& one.providerType === other.providerType;
}
get recentDynamicOptions(): readonly DynamicDebugConfigurationSessionOptions[] {
return this.recentDynamicOptionsTracker;
}
protected updateCurrent(options: DebugSessionOptions | undefined = this._currentOptions): void {
if (DebugSessionOptions.isCompound(options)) {
this._currentOptions = options && this.find(options.compound, options.workspaceFolderUri);
} else {
this._currentOptions = options && this.find(options.configuration, options.workspaceFolderUri, options.providerType);
}
if (!this._currentOptions) {
const model = this.getModel();
if (model) {
const configuration = model.configurations[0];
if (configuration) {
this._currentOptions = this.configurationToOptions(configuration, model.workspaceFolderUri);
}
}
}
this.debugConfigurationTypeKey.set(this.current && this.current.configuration?.type);
this.onDidChangeEmitter.fire(undefined);
}
/**
* @deprecated since v1.27.0
*/
find(name: string, workspaceFolderUri: string): DebugSessionOptions | undefined;
/**
* Find / Resolve DebugSessionOptions from a given target debug configuration
*/
find(compound: DebugCompound, workspaceFolderUri?: string): DebugSessionOptions | undefined;
find(configuration: DebugConfiguration, workspaceFolderUri?: string, providerType?: string): DebugSessionOptions | undefined;
find(name: string, workspaceFolderUri?: string, providerType?: string): DebugSessionOptions | undefined;
find(nameOrConfigurationOrCompound: string | DebugConfiguration | DebugCompound, workspaceFolderUri?: string, providerType?: string): DebugSessionOptions | undefined {
if (DebugConfiguration.is(nameOrConfigurationOrCompound) && providerType) {
// providerType is only applicable to dynamic debug configurations and may only be created if we have a configuration given
return this.configurationToOptions(nameOrConfigurationOrCompound, workspaceFolderUri, providerType);
}
const name = typeof nameOrConfigurationOrCompound === 'string' ? nameOrConfigurationOrCompound : nameOrConfigurationOrCompound.name;
const configuration = this.findConfiguration(name, workspaceFolderUri);
if (configuration) {
return this.configurationToOptions(configuration, workspaceFolderUri);
}
const compound = this.findCompound(name, workspaceFolderUri);
if (compound) {
return this.compoundToOptions(compound, workspaceFolderUri);
}
}
findConfigurations(name: string, workspaceFolderUri?: string): DebugConfiguration[] {
const matches = [];
for (const model of this.models.values()) {
if (model.workspaceFolderUri === workspaceFolderUri) {
for (const configuration of model.configurations) {
if (configuration.name === name) {
matches.push(configuration);
}
}
}
}
return matches;
}
findConfiguration(name: string, workspaceFolderUri?: string): DebugConfiguration | undefined {
for (const model of this.models.values()) {
if (model.workspaceFolderUri === workspaceFolderUri) {
for (const configuration of model.configurations) {
if (configuration.name === name) {
return configuration;
}
}
}
}
}
findCompound(name: string, workspaceFolderUri?: string): DebugCompound | undefined {
for (const model of this.models.values()) {
if (model.workspaceFolderUri === workspaceFolderUri) {
for (const compound of model.compounds) {
if (compound.name === name) {
return compound;
}
}
}
}
}
async openConfiguration(): Promise<void> {
const model = this.getModel();
if (model) {
await this.doOpen(model);
}
}
protected configurationToOptions(configuration: DebugConfiguration, workspaceFolderUri?: string, providerType?: string): DebugSessionOptions {
return { name: configuration.name, configuration, providerType, workspaceFolderUri };
}
protected compoundToOptions(compound: DebugCompound, workspaceFolderUri?: string): DebugSessionOptions {
return { name: compound.name, compound, workspaceFolderUri };
}
async addConfiguration(): Promise<void> {
let rootUri: URI | undefined = undefined;
if (this.workspaceService.saved && this.workspaceService.tryGetRoots().length > 1) {
rootUri = await this.selectRootUri();
// Do not continue if the user explicitly does not choose a location.
if (!rootUri) {
return;
}
}
const model = this.getModel(rootUri);
if (!model) {
return;
}
const widget = await this.doOpen(model);
if (!(widget.editor instanceof MonacoEditor)) {
return;
}
const editor = widget.editor.getControl();
const commandService = StandaloneServices.get(ICommandService);
let position: monaco.Position | undefined;
let depthInArray = 0;
let lastProperty = '';
visit(editor.getValue(), {
onObjectProperty: property => {
lastProperty = property;
},
onArrayBegin: offset => {
if (lastProperty === 'configurations' && depthInArray === 0) {
position = editor.getModel()!.getPositionAt(offset + 1);
}
depthInArray++;
},
onArrayEnd: () => {
depthInArray--;
}
});
if (!position) {
return;
}
// Check if there are more characters on a line after a "configurations": [, if yes enter a newline
if (editor.getModel()!.getLineLastNonWhitespaceColumn(position.lineNumber) > position.column) {
editor.setPosition(position);
editor.trigger('debug', 'lineBreakInsert', undefined);
}
// Check if there is already an empty line to insert suggest, if yes just place the cursor
if (editor.getModel()!.getLineLastNonWhitespaceColumn(position.lineNumber + 1) === 0) {
editor.setPosition({ lineNumber: position.lineNumber + 1, column: 1 << 30 });
await commandService.executeCommand('editor.action.deleteLines');
}
editor.setPosition(position);
await commandService.executeCommand('editor.action.insertLineAfter');
await commandService.executeCommand('editor.action.triggerSuggest');
}
protected async selectRootUri(): Promise<URI | undefined> {
const workspaceRoots = this.workspaceService.tryGetRoots();
const items: QuickPickValue<URI>[] = [];
for (const workspaceRoot of workspaceRoots) {
items.push({
label: this.labelProvider.getName(workspaceRoot.resource),
description: this.labelProvider.getLongName(workspaceRoot.resource),
value: workspaceRoot.resource
});
}
const root = await this.quickPickService.show(items, {
placeholder: nls.localize('theia/debug/addConfigurationPlaceholder', 'Select workspace root to add configuration to'),
ignoreFocusOut: true
});
return root?.value;
}
protected getModel(uri?: URI): DebugConfigurationModel | undefined {
const workspaceFolderUri = this.workspaceVariables.getWorkspaceRootUri(uri);
if (workspaceFolderUri) {
const key = workspaceFolderUri.toString();
for (const model of this.models.values()) {
if (model.workspaceFolderUri === key) {
return model;
}
}
}
for (const model of this.models.values()) {
if (model.uri) {
return model;
}
}
return this.models.values().next().value;
}
protected async doOpen(model: DebugConfigurationModel): Promise<EditorWidget> {
const uri = await this.doCreate(model);
return this.editorManager.open(uri, {
mode: 'activate'
});
}
protected async doCreate(model: DebugConfigurationModel): Promise<URI> {
const uri = model.uri ?? this.preferences.getConfigUri(PreferenceScope.Folder, model.workspaceFolderUri, 'launch');
if (!uri) { // Since we are requesting information about a known workspace folder, this should never happen.
throw new Error('PreferenceService.getConfigUri has returned undefined when a URI was expected.');
}
const settingsUri = this.preferences.getConfigUri(PreferenceScope.Folder, model.workspaceFolderUri);
// Users may have placed their debug configurations in a `settings.json`, in which case we shouldn't modify the file.
if (settingsUri && !uri.isEqual(settingsUri)) {
await this.ensureContent(uri, model);
}
return uri;
}
/**
* Checks whether a `launch.json` file contains the minimum necessary content.
* If content not found, provides content and populates the file using Monaco.
*/
protected async ensureContent(uri: URI, model: DebugConfigurationModel): Promise<void> {
const textModel = await this.textModelService.createModelReference(uri);
const currentContent = textModel.object.valid ? textModel.object.getText() : '';
try { // Look for the minimal well-formed launch.json content: {configurations: []}
const parsedContent = parse(currentContent);
if (Array.isArray(parsedContent.configurations)) {
return;
}
} catch {
// Just keep going
}
const debugType = await this.selectDebugType();
const configurations = debugType ? await this.provideDebugConfigurations(debugType, model.workspaceFolderUri) : [];
const content = this.getInitialConfigurationContent(configurations);
textModel.object.textEditorModel.setValue(content); // Will clobber anything the user has entered!
await textModel.object.save();
}
protected async provideDebugConfigurations(debugType: string, workspaceFolderUri: string | undefined): Promise<DebugConfiguration[]> {
await this.fireWillProvideDebugConfiguration();
return this.debug.provideDebugConfigurations(debugType, workspaceFolderUri);
}
protected async fireWillProvideDebugConfiguration(): Promise<void> {
await WaitUntilEvent.fire(this.onWillProvideDebugConfigurationEmitter, {});
}
async provideDynamicDebugConfigurations(): Promise<Record<string, DebugConfiguration[]>> {
await this.fireWillProvideDynamicDebugConfiguration();
return this.debug.provideDynamicDebugConfigurations!();
}
async fetchDynamicDebugConfiguration(name: string, type: string): Promise<DebugConfiguration | undefined> {
await this.fireWillProvideDynamicDebugConfiguration();
return this.debug.fetchDynamicDebugConfiguration(name, type);
}
protected async fireWillProvideDynamicDebugConfiguration(): Promise<void> {
await this.initialized;
await WaitUntilEvent.fire(this.onWillProvideDynamicDebugConfigurationEmitter, {});
}
protected getInitialConfigurationContent(initialConfigurations: DebugConfiguration[]): string {
return `{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
"version": "0.2.0",
"configurations": ${JSON.stringify(initialConfigurations, undefined, ' ').split('\n').map(line => ' ' + line).join('\n').trim()}
}
`;
}
protected async selectDebugType(): Promise<string | undefined> {
const widget = this.editorManager.currentEditor;
if (!widget) {
return undefined;
}
const { languageId } = widget.editor.document;
const debuggers = await this.debug.getDebuggersForLanguage(languageId);
if (debuggers.length === 0) {
return undefined;
}
const items: Array<QuickPickValue<string>> = debuggers.map(({ label, type }) => ({ label, value: type }));
const selectedItem = await this.quickPickService.show(items, { placeholder: 'Select Environment' });
return selectedItem?.value;
}
@inject(StorageService)
protected readonly storage: StorageService;
async load(): Promise<void> {
await this.initialized;
const data = await this.storage.getData<DebugConfigurationManager.Data>('debug.configurations', {});
this.resolveRecentDynamicOptionsFromData(data.recentDynamicOptions);
// Between versions v1.26 and v1.27, the expected format of the data changed so that old stored data
// may not contain the configuration key.
if (DebugSessionOptions.isConfiguration(data.current)) {
// ensure options name is reflected from old configurations data
data.current.name = data.current.name ?? data.current.configuration?.name;
this.current = this.find(data.current.configuration, data.current.workspaceFolderUri, data.current.providerType);
} else if (DebugSessionOptions.isCompound(data.current)) {
this.current = this.find(data.current.name, data.current.workspaceFolderUri);
}
}
protected resolveRecentDynamicOptionsFromData(options?: DynamicDebugConfigurationSessionOptions[]): void {
if (!options || this.recentDynamicOptionsTracker.length !== 0) {
return;
}
// ensure options name is reflected from old configurations data
const dynamicOptions = options.map(option => {
option.name = option.name ?? option.configuration.name;
return option;
}).filter(DebugSessionOptions.isDynamic);
this.recentDynamicOptionsTracker = dynamicOptions;
}
save(): void {
const data: DebugConfigurationManager.Data = {};
const { current, recentDynamicOptionsTracker } = this;
if (current) {
data.current = current;
}
if (this.recentDynamicOptionsTracker.length > 0) {
data.recentDynamicOptions = recentDynamicOptionsTracker;
}
if (Object.keys(data).length > 0) {
this.storage.setData('debug.configurations', data);
}
}
}
export namespace DebugConfigurationManager {
export interface Data {
current?: DebugSessionOptions,
recentDynamicOptions?: DynamicDebugConfigurationSessionOptions[]
}
}