@theia/debug 1.53.0-next.5 → 1.53.0-next.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/README.md +62 -62
  2. package/lib/browser/debug-configuration-manager.js +6 -6
  3. package/lib/browser/debug-frontend-application-contribution.d.ts.map +1 -1
  4. package/lib/browser/debug-frontend-application-contribution.js.map +1 -1
  5. package/lib/common/inline-debug-adapter.d.ts +1 -0
  6. package/lib/common/inline-debug-adapter.d.ts.map +1 -1
  7. package/package.json +16 -16
  8. package/src/browser/breakpoint/breakpoint-manager.ts +369 -369
  9. package/src/browser/breakpoint/breakpoint-marker.ts +104 -104
  10. package/src/browser/console/debug-console-contribution.tsx +240 -240
  11. package/src/browser/console/debug-console-items.tsx +384 -384
  12. package/src/browser/console/debug-console-session.ts +205 -205
  13. package/src/browser/debug-call-stack-item-type-key.ts +20 -20
  14. package/src/browser/debug-configuration-manager.ts +591 -591
  15. package/src/browser/debug-configuration-model.ts +100 -100
  16. package/src/browser/debug-contribution.ts +43 -43
  17. package/src/browser/debug-frontend-application-contribution.ts +1551 -1551
  18. package/src/browser/debug-frontend-module.ts +133 -133
  19. package/src/browser/debug-package.spec.ts +20 -20
  20. package/src/browser/debug-preferences.ts +98 -98
  21. package/src/browser/debug-prefix-configuration.ts +195 -195
  22. package/src/browser/debug-resource.ts +59 -59
  23. package/src/browser/debug-schema-updater.ts +149 -149
  24. package/src/browser/debug-session-connection.ts +357 -357
  25. package/src/browser/debug-session-contribution.ts +157 -157
  26. package/src/browser/debug-session-manager.ts +683 -683
  27. package/src/browser/debug-session-options.ts +120 -120
  28. package/src/browser/debug-session.tsx +974 -974
  29. package/src/browser/debug-tab-bar-decorator.ts +57 -57
  30. package/src/browser/debug-watch-manager.ts +93 -93
  31. package/src/browser/disassembly-view/disassembly-view-accessibility-provider.ts +43 -43
  32. package/src/browser/disassembly-view/disassembly-view-breakpoint-renderer.ts +119 -119
  33. package/src/browser/disassembly-view/disassembly-view-contribution.ts +109 -109
  34. package/src/browser/disassembly-view/disassembly-view-instruction-renderer.ts +245 -245
  35. package/src/browser/disassembly-view/disassembly-view-table-delegate.ts +39 -39
  36. package/src/browser/disassembly-view/disassembly-view-utilities.ts +55 -55
  37. package/src/browser/disassembly-view/disassembly-view-widget.ts +463 -463
  38. package/src/browser/editor/debug-breakpoint-widget.tsx +293 -293
  39. package/src/browser/editor/debug-editor-model.ts +529 -529
  40. package/src/browser/editor/debug-editor-service.ts +192 -192
  41. package/src/browser/editor/debug-editor.ts +20 -20
  42. package/src/browser/editor/debug-exception-widget.tsx +122 -122
  43. package/src/browser/editor/debug-expression-provider.ts +78 -78
  44. package/src/browser/editor/debug-hover-source.tsx +105 -105
  45. package/src/browser/editor/debug-hover-widget.ts +298 -298
  46. package/src/browser/editor/debug-inline-value-decorator.ts +373 -373
  47. package/src/browser/model/debug-breakpoint.tsx +151 -151
  48. package/src/browser/model/debug-function-breakpoint.tsx +101 -101
  49. package/src/browser/model/debug-instruction-breakpoint.tsx +68 -68
  50. package/src/browser/model/debug-source-breakpoint.tsx +237 -237
  51. package/src/browser/model/debug-source.ts +93 -93
  52. package/src/browser/model/debug-stack-frame.tsx +177 -177
  53. package/src/browser/model/debug-thread.tsx +292 -292
  54. package/src/browser/preferences/launch-preferences.ts +38 -38
  55. package/src/browser/style/index.css +453 -453
  56. package/src/browser/view/debug-action.tsx +57 -57
  57. package/src/browser/view/debug-breakpoints-source.tsx +53 -53
  58. package/src/browser/view/debug-breakpoints-widget.ts +71 -71
  59. package/src/browser/view/debug-configuration-select.tsx +269 -269
  60. package/src/browser/view/debug-configuration-widget.tsx +121 -121
  61. package/src/browser/view/debug-exception-breakpoint.tsx +68 -68
  62. package/src/browser/view/debug-session-widget.ts +124 -124
  63. package/src/browser/view/debug-stack-frames-source.tsx +75 -75
  64. package/src/browser/view/debug-stack-frames-widget.ts +135 -135
  65. package/src/browser/view/debug-threads-source.tsx +48 -48
  66. package/src/browser/view/debug-threads-widget.ts +126 -126
  67. package/src/browser/view/debug-toolbar-widget.tsx +145 -145
  68. package/src/browser/view/debug-variables-source.ts +43 -43
  69. package/src/browser/view/debug-variables-widget.ts +61 -61
  70. package/src/browser/view/debug-view-model.ts +230 -230
  71. package/src/browser/view/debug-watch-expression.tsx +88 -88
  72. package/src/browser/view/debug-watch-source.ts +41 -41
  73. package/src/browser/view/debug-watch-widget.ts +61 -61
  74. package/src/browser/view/debug-widget.ts +97 -97
  75. package/src/common/debug-adapter-contribution-registry.ts +206 -206
  76. package/src/common/debug-adapter-session.ts +102 -102
  77. package/src/common/debug-common.ts +19 -19
  78. package/src/common/debug-compound.ts +33 -33
  79. package/src/common/debug-configuration.ts +112 -112
  80. package/src/common/debug-model.ts +200 -200
  81. package/src/common/debug-service.ts +184 -184
  82. package/src/common/debug-uri-utils.ts +24 -24
  83. package/src/common/inline-debug-adapter.ts +47 -47
  84. package/src/node/debug-adapter-factory.ts +107 -107
  85. package/src/node/debug-adapter-session-manager.ts +106 -106
  86. package/src/node/debug-backend-module.ts +57 -57
  87. package/src/node/debug-service-impl.ts +119 -119
  88. package/src/node/stream-debug-adapter.ts +126 -126
@@ -1,591 +1,591 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2018 Red Hat, Inc. and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- /*---------------------------------------------------------------------------------------------
18
- * Copyright (c) Microsoft Corporation. All rights reserved.
19
- * Licensed under the MIT License. See License.txt in the project root for license information.
20
- *--------------------------------------------------------------------------------------------*/
21
-
22
- import debounce = require('p-debounce');
23
- import { visit, parse } from 'jsonc-parser';
24
- import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
25
- import URI from '@theia/core/lib/common/uri';
26
- import { Emitter, Event, WaitUntilEvent } from '@theia/core/lib/common/event';
27
- import { EditorManager, EditorWidget } from '@theia/editor/lib/browser';
28
- import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor';
29
- import { LabelProvider, PreferenceScope, PreferenceService, QuickPickValue, StorageService } from '@theia/core/lib/browser';
30
- import { QuickPickService } from '@theia/core/lib/common/quick-pick-service';
31
- import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
32
- import { DebugConfigurationModel } from './debug-configuration-model';
33
- import { DebugSessionOptions, DynamicDebugConfigurationSessionOptions } from './debug-session-options';
34
- import { DebugService } from '../common/debug-service';
35
- import { ContextKey, ContextKeyService } from '@theia/core/lib/browser/context-key-service';
36
- import { DebugConfiguration } from '../common/debug-common';
37
- import { WorkspaceVariableContribution } from '@theia/workspace/lib/browser/workspace-variable-contribution';
38
- import { PreferenceConfigurations } from '@theia/core/lib/browser/preferences/preference-configurations';
39
- import { MonacoTextModelService } from '@theia/monaco/lib/browser/monaco-text-model-service';
40
- import * as monaco from '@theia/monaco-editor-core';
41
- import { ICommandService } from '@theia/monaco-editor-core/esm/vs/platform/commands/common/commands';
42
- import { StandaloneServices } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices';
43
- import { nls } from '@theia/core';
44
- import { DebugCompound } from '../common/debug-compound';
45
-
46
- export interface WillProvideDebugConfiguration extends WaitUntilEvent {
47
- }
48
-
49
- @injectable()
50
- export class DebugConfigurationManager {
51
-
52
- @inject(WorkspaceService)
53
- protected readonly workspaceService: WorkspaceService;
54
- @inject(EditorManager)
55
- protected readonly editorManager: EditorManager;
56
- @inject(DebugService)
57
- protected readonly debug: DebugService;
58
- @inject(QuickPickService)
59
- protected readonly quickPickService: QuickPickService;
60
-
61
- @inject(ContextKeyService)
62
- protected readonly contextKeyService: ContextKeyService;
63
-
64
- @inject(LabelProvider)
65
- protected readonly labelProvider: LabelProvider;
66
-
67
- @inject(MonacoTextModelService)
68
- protected readonly textModelService: MonacoTextModelService;
69
-
70
- @inject(PreferenceService)
71
- protected readonly preferences: PreferenceService;
72
-
73
- @inject(PreferenceConfigurations)
74
- protected readonly preferenceConfigurations: PreferenceConfigurations;
75
-
76
- @inject(WorkspaceVariableContribution)
77
- protected readonly workspaceVariables: WorkspaceVariableContribution;
78
-
79
- protected readonly onDidChangeEmitter = new Emitter<void>();
80
- readonly onDidChange: Event<void> = this.onDidChangeEmitter.event;
81
-
82
- protected readonly onWillProvideDebugConfigurationEmitter = new Emitter<WillProvideDebugConfiguration>();
83
- readonly onWillProvideDebugConfiguration: Event<WillProvideDebugConfiguration> = this.onWillProvideDebugConfigurationEmitter.event;
84
-
85
- protected readonly onWillProvideDynamicDebugConfigurationEmitter = new Emitter<WillProvideDebugConfiguration>();
86
- get onWillProvideDynamicDebugConfiguration(): Event<WillProvideDebugConfiguration> {
87
- return this.onWillProvideDynamicDebugConfigurationEmitter.event;
88
- }
89
-
90
- get onDidChangeConfigurationProviders(): Event<void> {
91
- return this.debug.onDidChangeDebugConfigurationProviders;
92
- }
93
-
94
- protected debugConfigurationTypeKey: ContextKey<string>;
95
-
96
- protected initialized: Promise<void>;
97
-
98
- protected recentDynamicOptionsTracker: DynamicDebugConfigurationSessionOptions[] = [];
99
-
100
- @postConstruct()
101
- protected init(): void {
102
- this.doInit();
103
- }
104
-
105
- protected async doInit(): Promise<void> {
106
- this.debugConfigurationTypeKey = this.contextKeyService.createKey<string>('debugConfigurationType', undefined);
107
- this.initialized = this.preferences.ready.then(() => {
108
- this.workspaceService.onWorkspaceChanged(this.updateModels);
109
- this.preferences.onPreferenceChanged(e => {
110
- if (e.preferenceName === 'launch') {
111
- this.updateModels();
112
- }
113
- });
114
- return this.updateModels();
115
- });
116
- }
117
-
118
- protected readonly models = new Map<string, DebugConfigurationModel>();
119
- protected updateModels = debounce(async () => {
120
- const roots = await this.workspaceService.roots;
121
- const toDelete = new Set(this.models.keys());
122
- for (const rootStat of roots) {
123
- const key = rootStat.resource.toString();
124
- toDelete.delete(key);
125
- if (!this.models.has(key)) {
126
- const model = new DebugConfigurationModel(key, this.preferences);
127
- model.onDidChange(() => this.updateCurrent());
128
- model.onDispose(() => this.models.delete(key));
129
- this.models.set(key, model);
130
- }
131
- }
132
- for (const uri of toDelete) {
133
- const model = this.models.get(uri);
134
- if (model) {
135
- model.dispose();
136
- }
137
- }
138
- this.updateCurrent();
139
- }, 500);
140
-
141
- /**
142
- * All _non-dynamic_ debug configurations.
143
- */
144
- get all(): IterableIterator<DebugSessionOptions> {
145
- return this.getAll();
146
- }
147
- protected *getAll(): IterableIterator<DebugSessionOptions> {
148
- for (const model of this.models.values()) {
149
- for (const configuration of model.configurations) {
150
- yield this.configurationToOptions(configuration, model.workspaceFolderUri);
151
- }
152
- for (const compound of model.compounds) {
153
- yield this.compoundToOptions(compound, model.workspaceFolderUri);
154
- }
155
- }
156
- }
157
-
158
- get supported(): Promise<IterableIterator<DebugSessionOptions>> {
159
- return this.getSupported();
160
- }
161
- protected async getSupported(): Promise<IterableIterator<DebugSessionOptions>> {
162
- await this.initialized;
163
- const debugTypes = await this.debug.debugTypes();
164
- return this.doGetSupported(new Set(debugTypes));
165
- }
166
- protected *doGetSupported(debugTypes: Set<string>): IterableIterator<DebugSessionOptions> {
167
- for (const options of this.getAll()) {
168
- if (options.configuration && debugTypes.has(options.configuration.type)) {
169
- yield options;
170
- }
171
- }
172
- }
173
-
174
- protected _currentOptions: DebugSessionOptions | undefined;
175
- get current(): DebugSessionOptions | undefined {
176
- return this._currentOptions;
177
- }
178
-
179
- async getSelectedConfiguration(): Promise<DebugSessionOptions | undefined> {
180
- if (!DebugSessionOptions.isDynamic(this._currentOptions)) {
181
- return this._currentOptions;
182
- }
183
-
184
- // Refresh a dynamic configuration from the provider.
185
- // This allow providers to update properties before the execution e.g. program
186
- const { providerType, workspaceFolderUri, configuration: { name } } = this._currentOptions;
187
- const configuration = await this.fetchDynamicDebugConfiguration(name, providerType, workspaceFolderUri);
188
-
189
- if (!configuration) {
190
- const message = nls.localize(
191
- 'theia/debug/missingConfiguration',
192
- "Dynamic configuration '{0}:{1}' is missing or not applicable", providerType, name);
193
- throw new Error(message);
194
- }
195
-
196
- return { name, configuration, providerType, workspaceFolderUri };
197
- }
198
-
199
- set current(option: DebugSessionOptions | undefined) {
200
- this.updateCurrent(option);
201
- this.updateRecentlyUsedDynamicConfigurationOptions(option);
202
- }
203
-
204
- protected updateRecentlyUsedDynamicConfigurationOptions(option: DebugSessionOptions | undefined): void {
205
- if (DebugSessionOptions.isDynamic(option)) {
206
- // Removing an item already present in the list
207
- const index = this.recentDynamicOptionsTracker.findIndex(item => this.dynamicOptionsMatch(item, option));
208
- if (index > -1) {
209
- this.recentDynamicOptionsTracker.splice(index, 1);
210
- }
211
- // Adding new item, most recent at the top of the list
212
- const recentMax = 3;
213
- if (this.recentDynamicOptionsTracker.unshift(option) > recentMax) {
214
- // Keep the latest 3 dynamic configuration options to not clutter the dropdown.
215
- this.recentDynamicOptionsTracker.splice(recentMax);
216
- }
217
- }
218
- }
219
-
220
- protected dynamicOptionsMatch(one: DynamicDebugConfigurationSessionOptions, other: DynamicDebugConfigurationSessionOptions): boolean {
221
- return one.providerType !== undefined
222
- && one.configuration.name === other.configuration.name
223
- && one.providerType === other.providerType
224
- && one.workspaceFolderUri === other.workspaceFolderUri;
225
- }
226
-
227
- get recentDynamicOptions(): readonly DynamicDebugConfigurationSessionOptions[] {
228
- return this.recentDynamicOptionsTracker;
229
- }
230
-
231
- protected updateCurrent(options: DebugSessionOptions | undefined = this._currentOptions): void {
232
- if (DebugSessionOptions.isCompound(options)) {
233
- this._currentOptions = options && this.find(options.compound, options.workspaceFolderUri);
234
- } else {
235
- this._currentOptions = options && this.find(options.configuration, options.workspaceFolderUri, options.providerType);
236
- }
237
-
238
- if (!this._currentOptions) {
239
- const model = this.getModel();
240
- if (model) {
241
- const configuration = model.configurations[0];
242
- if (configuration) {
243
- this._currentOptions = this.configurationToOptions(configuration, model.workspaceFolderUri);
244
- }
245
- }
246
- }
247
- this.debugConfigurationTypeKey.set(this.current && this.current.configuration?.type);
248
- this.onDidChangeEmitter.fire(undefined);
249
- }
250
-
251
- /**
252
- * @deprecated since v1.27.0
253
- */
254
- find(name: string, workspaceFolderUri: string): DebugSessionOptions | undefined;
255
- /**
256
- * Find / Resolve DebugSessionOptions from a given target debug configuration
257
- */
258
- find(compound: DebugCompound, workspaceFolderUri?: string): DebugSessionOptions | undefined;
259
- find(configuration: DebugConfiguration, workspaceFolderUri?: string, providerType?: string): DebugSessionOptions | undefined;
260
- find(name: string, workspaceFolderUri?: string, providerType?: string): DebugSessionOptions | undefined;
261
- find(nameOrConfigurationOrCompound: string | DebugConfiguration | DebugCompound, workspaceFolderUri?: string, providerType?: string): DebugSessionOptions | undefined {
262
- if (DebugConfiguration.is(nameOrConfigurationOrCompound) && providerType) {
263
- // providerType is only applicable to dynamic debug configurations and may only be created if we have a configuration given
264
- return this.configurationToOptions(nameOrConfigurationOrCompound, workspaceFolderUri, providerType);
265
- }
266
- const name = typeof nameOrConfigurationOrCompound === 'string' ? nameOrConfigurationOrCompound : nameOrConfigurationOrCompound.name;
267
- const configuration = this.findConfiguration(name, workspaceFolderUri);
268
- if (configuration) {
269
- return this.configurationToOptions(configuration, workspaceFolderUri);
270
- }
271
- const compound = this.findCompound(name, workspaceFolderUri);
272
- if (compound) {
273
- return this.compoundToOptions(compound, workspaceFolderUri);
274
- }
275
- }
276
-
277
- findConfigurations(name: string, workspaceFolderUri?: string): DebugConfiguration[] {
278
- const matches = [];
279
- for (const model of this.models.values()) {
280
- if (model.workspaceFolderUri === workspaceFolderUri) {
281
- for (const configuration of model.configurations) {
282
- if (configuration.name === name) {
283
- matches.push(configuration);
284
- }
285
- }
286
- }
287
- }
288
- return matches;
289
- }
290
-
291
- findConfiguration(name: string, workspaceFolderUri?: string): DebugConfiguration | undefined {
292
- for (const model of this.models.values()) {
293
- if (model.workspaceFolderUri === workspaceFolderUri) {
294
- for (const configuration of model.configurations) {
295
- if (configuration.name === name) {
296
- return configuration;
297
- }
298
- }
299
- }
300
- }
301
- }
302
-
303
- findCompound(name: string, workspaceFolderUri?: string): DebugCompound | undefined {
304
- for (const model of this.models.values()) {
305
- if (model.workspaceFolderUri === workspaceFolderUri) {
306
- for (const compound of model.compounds) {
307
- if (compound.name === name) {
308
- return compound;
309
- }
310
- }
311
- }
312
- }
313
- }
314
-
315
- async openConfiguration(): Promise<void> {
316
- const currentUri = new URI(this.current?.workspaceFolderUri);
317
- const model = this.getModel(currentUri);
318
- if (model) {
319
- await this.doOpen(model);
320
- }
321
- }
322
-
323
- protected configurationToOptions(configuration: DebugConfiguration, workspaceFolderUri?: string, providerType?: string): DebugSessionOptions {
324
- return { name: configuration.name, configuration, providerType, workspaceFolderUri };
325
- }
326
-
327
- protected compoundToOptions(compound: DebugCompound, workspaceFolderUri?: string): DebugSessionOptions {
328
- return { name: compound.name, compound, workspaceFolderUri };
329
- }
330
-
331
- async addConfiguration(): Promise<void> {
332
- let rootUri: URI | undefined = undefined;
333
- if (this.workspaceService.saved && this.workspaceService.tryGetRoots().length > 1) {
334
- rootUri = await this.selectRootUri();
335
- // Do not continue if the user explicitly does not choose a location.
336
- if (!rootUri) {
337
- return;
338
- }
339
- }
340
-
341
- const model = this.getModel(rootUri);
342
- if (!model) {
343
- return;
344
- }
345
- const widget = await this.doOpen(model);
346
- if (!(widget.editor instanceof MonacoEditor)) {
347
- return;
348
- }
349
- const editor = widget.editor.getControl();
350
- const commandService = StandaloneServices.get(ICommandService);
351
- let position: monaco.Position | undefined;
352
- let depthInArray = 0;
353
- let lastProperty = '';
354
- visit(editor.getValue(), {
355
- onObjectProperty: property => {
356
- lastProperty = property;
357
- },
358
- onArrayBegin: offset => {
359
- if (lastProperty === 'configurations' && depthInArray === 0) {
360
- position = editor.getModel()!.getPositionAt(offset + 1);
361
- }
362
- depthInArray++;
363
- },
364
- onArrayEnd: () => {
365
- depthInArray--;
366
- }
367
- });
368
- if (!position) {
369
- return;
370
- }
371
- // Check if there are more characters on a line after a "configurations": [, if yes enter a newline
372
- if (editor.getModel()!.getLineLastNonWhitespaceColumn(position.lineNumber) > position.column) {
373
- editor.setPosition(position);
374
- editor.trigger('debug', 'lineBreakInsert', undefined);
375
- }
376
- // Check if there is already an empty line to insert suggest, if yes just place the cursor
377
- if (editor.getModel()!.getLineLastNonWhitespaceColumn(position.lineNumber + 1) === 0) {
378
- editor.setPosition({ lineNumber: position.lineNumber + 1, column: 1 << 30 });
379
- await commandService.executeCommand('editor.action.deleteLines');
380
- }
381
- editor.setPosition(position);
382
- await commandService.executeCommand('editor.action.insertLineAfter');
383
- await commandService.executeCommand('editor.action.triggerSuggest');
384
- }
385
-
386
- protected async selectRootUri(): Promise<URI | undefined> {
387
- const workspaceRoots = this.workspaceService.tryGetRoots();
388
- const items: QuickPickValue<URI>[] = [];
389
- for (const workspaceRoot of workspaceRoots) {
390
- items.push({
391
- label: this.labelProvider.getName(workspaceRoot.resource),
392
- description: this.labelProvider.getLongName(workspaceRoot.resource),
393
- value: workspaceRoot.resource
394
- });
395
- }
396
- const root = await this.quickPickService.show(items, {
397
- placeholder: nls.localize('theia/debug/addConfigurationPlaceholder', 'Select workspace root to add configuration to'),
398
- });
399
- return root?.value;
400
- }
401
-
402
- protected getModel(uri?: URI): DebugConfigurationModel | undefined {
403
- const workspaceFolderUri = this.workspaceVariables.getWorkspaceRootUri(uri);
404
- if (workspaceFolderUri) {
405
- const key = workspaceFolderUri.toString();
406
- for (const model of this.models.values()) {
407
- if (model.workspaceFolderUri === key) {
408
- return model;
409
- }
410
- }
411
- }
412
- for (const model of this.models.values()) {
413
- if (model.uri) {
414
- return model;
415
- }
416
- }
417
- return this.models.values().next().value;
418
- }
419
-
420
- protected async doOpen(model: DebugConfigurationModel): Promise<EditorWidget> {
421
- const uri = await this.doCreate(model);
422
-
423
- return this.editorManager.open(uri, {
424
- mode: 'activate'
425
- });
426
- }
427
-
428
- protected async doCreate(model: DebugConfigurationModel): Promise<URI> {
429
- const uri = model.uri ?? this.preferences.getConfigUri(PreferenceScope.Folder, model.workspaceFolderUri, 'launch');
430
- if (!uri) { // Since we are requesting information about a known workspace folder, this should never happen.
431
- throw new Error('PreferenceService.getConfigUri has returned undefined when a URI was expected.');
432
- }
433
- const settingsUri = this.preferences.getConfigUri(PreferenceScope.Folder, model.workspaceFolderUri);
434
- // Users may have placed their debug configurations in a `settings.json`, in which case we shouldn't modify the file.
435
- if (settingsUri && !uri.isEqual(settingsUri)) {
436
- await this.ensureContent(uri, model);
437
- }
438
- return uri;
439
- }
440
-
441
- /**
442
- * Checks whether a `launch.json` file contains the minimum necessary content.
443
- * If content not found, provides content and populates the file using Monaco.
444
- */
445
- protected async ensureContent(uri: URI, model: DebugConfigurationModel): Promise<void> {
446
- const textModel = await this.textModelService.createModelReference(uri);
447
- const currentContent = textModel.object.valid ? textModel.object.getText() : '';
448
- try { // Look for the minimal well-formed launch.json content: {configurations: []}
449
- const parsedContent = parse(currentContent);
450
- if (Array.isArray(parsedContent.configurations)) {
451
- return;
452
- }
453
- } catch {
454
- // Just keep going
455
- }
456
- const debugType = await this.selectDebugType();
457
- const configurations = debugType ? await this.provideDebugConfigurations(debugType, model.workspaceFolderUri) : [];
458
- const content = this.getInitialConfigurationContent(configurations);
459
- textModel.object.textEditorModel.setValue(content); // Will clobber anything the user has entered!
460
- await textModel.object.save();
461
- }
462
-
463
- protected async provideDebugConfigurations(debugType: string, workspaceFolderUri: string | undefined): Promise<DebugConfiguration[]> {
464
- await this.fireWillProvideDebugConfiguration();
465
- return this.debug.provideDebugConfigurations(debugType, workspaceFolderUri);
466
- }
467
- protected async fireWillProvideDebugConfiguration(): Promise<void> {
468
- await WaitUntilEvent.fire(this.onWillProvideDebugConfigurationEmitter, {});
469
- }
470
-
471
- async provideDynamicDebugConfigurations(): Promise<Record<string, DynamicDebugConfigurationSessionOptions[]>> {
472
- await this.fireWillProvideDynamicDebugConfiguration();
473
- const roots = this.workspaceService.tryGetRoots();
474
- const promises = roots.map(async root => {
475
- const configsMap = await this.debug.provideDynamicDebugConfigurations!(root.resource.toString());
476
- const optionsMap = Object.fromEntries(Object.entries(configsMap).map(([type, configs]) => {
477
- const options = configs.map(config => ({
478
- name: config.name,
479
- providerType: type,
480
- configuration: config,
481
- workspaceFolderUri: root.resource.toString()
482
- }));
483
- return [type, options];
484
- }));
485
- return optionsMap;
486
- });
487
-
488
- const typesToOptionsRecords = await Promise.all(promises);
489
- const consolidatedTypesToOptions: Record<string, DynamicDebugConfigurationSessionOptions[]> = {};
490
-
491
- for (const typesToOptionsInstance of typesToOptionsRecords) {
492
- for (const [providerType, configurationsOptions] of Object.entries(typesToOptionsInstance)) {
493
- if (!consolidatedTypesToOptions[providerType]) {
494
- consolidatedTypesToOptions[providerType] = [];
495
- }
496
- consolidatedTypesToOptions[providerType].push(...configurationsOptions);
497
- }
498
- }
499
- return consolidatedTypesToOptions;
500
- }
501
-
502
- async fetchDynamicDebugConfiguration(name: string, type: string, folder?: string): Promise<DebugConfiguration | undefined> {
503
- await this.fireWillProvideDynamicDebugConfiguration();
504
- return this.debug.fetchDynamicDebugConfiguration(name, type, folder);
505
- }
506
-
507
- protected async fireWillProvideDynamicDebugConfiguration(): Promise<void> {
508
- await this.initialized;
509
- await WaitUntilEvent.fire(this.onWillProvideDynamicDebugConfigurationEmitter, {});
510
- }
511
-
512
- protected getInitialConfigurationContent(initialConfigurations: DebugConfiguration[]): string {
513
- return `{
514
- // Use IntelliSense to learn about possible attributes.
515
- // Hover to view descriptions of existing attributes.
516
- "version": "0.2.0",
517
- "configurations": ${JSON.stringify(initialConfigurations, undefined, ' ').split('\n').map(line => ' ' + line).join('\n').trim()}
518
- }
519
- `;
520
- }
521
-
522
- protected async selectDebugType(): Promise<string | undefined> {
523
- const widget = this.editorManager.currentEditor;
524
- if (!widget) {
525
- return undefined;
526
- }
527
- const { languageId } = widget.editor.document;
528
- const debuggers = await this.debug.getDebuggersForLanguage(languageId);
529
- if (debuggers.length === 0) {
530
- return undefined;
531
- }
532
- const items: Array<QuickPickValue<string>> = debuggers.map(({ label, type }) => ({ label, value: type }));
533
- const selectedItem = await this.quickPickService.show(items, { placeholder: 'Select Environment' });
534
- return selectedItem?.value;
535
- }
536
-
537
- @inject(StorageService)
538
- protected readonly storage: StorageService;
539
-
540
- async load(): Promise<void> {
541
- await this.initialized;
542
- const data = await this.storage.getData<DebugConfigurationManager.Data>('debug.configurations', {});
543
- this.resolveRecentDynamicOptionsFromData(data.recentDynamicOptions);
544
-
545
- // Between versions v1.26 and v1.27, the expected format of the data changed so that old stored data
546
- // may not contain the configuration key.
547
- if (DebugSessionOptions.isConfiguration(data.current)) {
548
- // ensure options name is reflected from old configurations data
549
- data.current.name = data.current.name ?? data.current.configuration?.name;
550
- this.current = this.find(data.current.configuration, data.current.workspaceFolderUri, data.current.providerType);
551
- } else if (DebugSessionOptions.isCompound(data.current)) {
552
- this.current = this.find(data.current.name, data.current.workspaceFolderUri);
553
- }
554
- }
555
-
556
- protected resolveRecentDynamicOptionsFromData(options?: DynamicDebugConfigurationSessionOptions[]): void {
557
- if (!options || this.recentDynamicOptionsTracker.length !== 0) {
558
- return;
559
- }
560
-
561
- // ensure options name is reflected from old configurations data
562
- const dynamicOptions = options.map(option => {
563
- option.name = option.name ?? option.configuration.name;
564
- return option;
565
- }).filter(DebugSessionOptions.isDynamic);
566
- this.recentDynamicOptionsTracker = dynamicOptions;
567
- }
568
-
569
- save(): void {
570
- const data: DebugConfigurationManager.Data = {};
571
- const { current, recentDynamicOptionsTracker } = this;
572
- if (current) {
573
- data.current = current;
574
- }
575
-
576
- if (this.recentDynamicOptionsTracker.length > 0) {
577
- data.recentDynamicOptions = recentDynamicOptionsTracker;
578
- }
579
-
580
- if (Object.keys(data).length > 0) {
581
- this.storage.setData('debug.configurations', data);
582
- }
583
- }
584
- }
585
-
586
- export namespace DebugConfigurationManager {
587
- export interface Data {
588
- current?: DebugSessionOptions,
589
- recentDynamicOptions?: DynamicDebugConfigurationSessionOptions[]
590
- }
591
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2018 Red Hat, Inc. and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ /*---------------------------------------------------------------------------------------------
18
+ * Copyright (c) Microsoft Corporation. All rights reserved.
19
+ * Licensed under the MIT License. See License.txt in the project root for license information.
20
+ *--------------------------------------------------------------------------------------------*/
21
+
22
+ import debounce = require('p-debounce');
23
+ import { visit, parse } from 'jsonc-parser';
24
+ import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
25
+ import URI from '@theia/core/lib/common/uri';
26
+ import { Emitter, Event, WaitUntilEvent } from '@theia/core/lib/common/event';
27
+ import { EditorManager, EditorWidget } from '@theia/editor/lib/browser';
28
+ import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor';
29
+ import { LabelProvider, PreferenceScope, PreferenceService, QuickPickValue, StorageService } from '@theia/core/lib/browser';
30
+ import { QuickPickService } from '@theia/core/lib/common/quick-pick-service';
31
+ import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
32
+ import { DebugConfigurationModel } from './debug-configuration-model';
33
+ import { DebugSessionOptions, DynamicDebugConfigurationSessionOptions } from './debug-session-options';
34
+ import { DebugService } from '../common/debug-service';
35
+ import { ContextKey, ContextKeyService } from '@theia/core/lib/browser/context-key-service';
36
+ import { DebugConfiguration } from '../common/debug-common';
37
+ import { WorkspaceVariableContribution } from '@theia/workspace/lib/browser/workspace-variable-contribution';
38
+ import { PreferenceConfigurations } from '@theia/core/lib/browser/preferences/preference-configurations';
39
+ import { MonacoTextModelService } from '@theia/monaco/lib/browser/monaco-text-model-service';
40
+ import * as monaco from '@theia/monaco-editor-core';
41
+ import { ICommandService } from '@theia/monaco-editor-core/esm/vs/platform/commands/common/commands';
42
+ import { StandaloneServices } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices';
43
+ import { nls } from '@theia/core';
44
+ import { DebugCompound } from '../common/debug-compound';
45
+
46
+ export interface WillProvideDebugConfiguration extends WaitUntilEvent {
47
+ }
48
+
49
+ @injectable()
50
+ export class DebugConfigurationManager {
51
+
52
+ @inject(WorkspaceService)
53
+ protected readonly workspaceService: WorkspaceService;
54
+ @inject(EditorManager)
55
+ protected readonly editorManager: EditorManager;
56
+ @inject(DebugService)
57
+ protected readonly debug: DebugService;
58
+ @inject(QuickPickService)
59
+ protected readonly quickPickService: QuickPickService;
60
+
61
+ @inject(ContextKeyService)
62
+ protected readonly contextKeyService: ContextKeyService;
63
+
64
+ @inject(LabelProvider)
65
+ protected readonly labelProvider: LabelProvider;
66
+
67
+ @inject(MonacoTextModelService)
68
+ protected readonly textModelService: MonacoTextModelService;
69
+
70
+ @inject(PreferenceService)
71
+ protected readonly preferences: PreferenceService;
72
+
73
+ @inject(PreferenceConfigurations)
74
+ protected readonly preferenceConfigurations: PreferenceConfigurations;
75
+
76
+ @inject(WorkspaceVariableContribution)
77
+ protected readonly workspaceVariables: WorkspaceVariableContribution;
78
+
79
+ protected readonly onDidChangeEmitter = new Emitter<void>();
80
+ readonly onDidChange: Event<void> = this.onDidChangeEmitter.event;
81
+
82
+ protected readonly onWillProvideDebugConfigurationEmitter = new Emitter<WillProvideDebugConfiguration>();
83
+ readonly onWillProvideDebugConfiguration: Event<WillProvideDebugConfiguration> = this.onWillProvideDebugConfigurationEmitter.event;
84
+
85
+ protected readonly onWillProvideDynamicDebugConfigurationEmitter = new Emitter<WillProvideDebugConfiguration>();
86
+ get onWillProvideDynamicDebugConfiguration(): Event<WillProvideDebugConfiguration> {
87
+ return this.onWillProvideDynamicDebugConfigurationEmitter.event;
88
+ }
89
+
90
+ get onDidChangeConfigurationProviders(): Event<void> {
91
+ return this.debug.onDidChangeDebugConfigurationProviders;
92
+ }
93
+
94
+ protected debugConfigurationTypeKey: ContextKey<string>;
95
+
96
+ protected initialized: Promise<void>;
97
+
98
+ protected recentDynamicOptionsTracker: DynamicDebugConfigurationSessionOptions[] = [];
99
+
100
+ @postConstruct()
101
+ protected init(): void {
102
+ this.doInit();
103
+ }
104
+
105
+ protected async doInit(): Promise<void> {
106
+ this.debugConfigurationTypeKey = this.contextKeyService.createKey<string>('debugConfigurationType', undefined);
107
+ this.initialized = this.preferences.ready.then(() => {
108
+ this.workspaceService.onWorkspaceChanged(this.updateModels);
109
+ this.preferences.onPreferenceChanged(e => {
110
+ if (e.preferenceName === 'launch') {
111
+ this.updateModels();
112
+ }
113
+ });
114
+ return this.updateModels();
115
+ });
116
+ }
117
+
118
+ protected readonly models = new Map<string, DebugConfigurationModel>();
119
+ protected updateModels = debounce(async () => {
120
+ const roots = await this.workspaceService.roots;
121
+ const toDelete = new Set(this.models.keys());
122
+ for (const rootStat of roots) {
123
+ const key = rootStat.resource.toString();
124
+ toDelete.delete(key);
125
+ if (!this.models.has(key)) {
126
+ const model = new DebugConfigurationModel(key, this.preferences);
127
+ model.onDidChange(() => this.updateCurrent());
128
+ model.onDispose(() => this.models.delete(key));
129
+ this.models.set(key, model);
130
+ }
131
+ }
132
+ for (const uri of toDelete) {
133
+ const model = this.models.get(uri);
134
+ if (model) {
135
+ model.dispose();
136
+ }
137
+ }
138
+ this.updateCurrent();
139
+ }, 500);
140
+
141
+ /**
142
+ * All _non-dynamic_ debug configurations.
143
+ */
144
+ get all(): IterableIterator<DebugSessionOptions> {
145
+ return this.getAll();
146
+ }
147
+ protected *getAll(): IterableIterator<DebugSessionOptions> {
148
+ for (const model of this.models.values()) {
149
+ for (const configuration of model.configurations) {
150
+ yield this.configurationToOptions(configuration, model.workspaceFolderUri);
151
+ }
152
+ for (const compound of model.compounds) {
153
+ yield this.compoundToOptions(compound, model.workspaceFolderUri);
154
+ }
155
+ }
156
+ }
157
+
158
+ get supported(): Promise<IterableIterator<DebugSessionOptions>> {
159
+ return this.getSupported();
160
+ }
161
+ protected async getSupported(): Promise<IterableIterator<DebugSessionOptions>> {
162
+ await this.initialized;
163
+ const debugTypes = await this.debug.debugTypes();
164
+ return this.doGetSupported(new Set(debugTypes));
165
+ }
166
+ protected *doGetSupported(debugTypes: Set<string>): IterableIterator<DebugSessionOptions> {
167
+ for (const options of this.getAll()) {
168
+ if (options.configuration && debugTypes.has(options.configuration.type)) {
169
+ yield options;
170
+ }
171
+ }
172
+ }
173
+
174
+ protected _currentOptions: DebugSessionOptions | undefined;
175
+ get current(): DebugSessionOptions | undefined {
176
+ return this._currentOptions;
177
+ }
178
+
179
+ async getSelectedConfiguration(): Promise<DebugSessionOptions | undefined> {
180
+ if (!DebugSessionOptions.isDynamic(this._currentOptions)) {
181
+ return this._currentOptions;
182
+ }
183
+
184
+ // Refresh a dynamic configuration from the provider.
185
+ // This allow providers to update properties before the execution e.g. program
186
+ const { providerType, workspaceFolderUri, configuration: { name } } = this._currentOptions;
187
+ const configuration = await this.fetchDynamicDebugConfiguration(name, providerType, workspaceFolderUri);
188
+
189
+ if (!configuration) {
190
+ const message = nls.localize(
191
+ 'theia/debug/missingConfiguration',
192
+ "Dynamic configuration '{0}:{1}' is missing or not applicable", providerType, name);
193
+ throw new Error(message);
194
+ }
195
+
196
+ return { name, configuration, providerType, workspaceFolderUri };
197
+ }
198
+
199
+ set current(option: DebugSessionOptions | undefined) {
200
+ this.updateCurrent(option);
201
+ this.updateRecentlyUsedDynamicConfigurationOptions(option);
202
+ }
203
+
204
+ protected updateRecentlyUsedDynamicConfigurationOptions(option: DebugSessionOptions | undefined): void {
205
+ if (DebugSessionOptions.isDynamic(option)) {
206
+ // Removing an item already present in the list
207
+ const index = this.recentDynamicOptionsTracker.findIndex(item => this.dynamicOptionsMatch(item, option));
208
+ if (index > -1) {
209
+ this.recentDynamicOptionsTracker.splice(index, 1);
210
+ }
211
+ // Adding new item, most recent at the top of the list
212
+ const recentMax = 3;
213
+ if (this.recentDynamicOptionsTracker.unshift(option) > recentMax) {
214
+ // Keep the latest 3 dynamic configuration options to not clutter the dropdown.
215
+ this.recentDynamicOptionsTracker.splice(recentMax);
216
+ }
217
+ }
218
+ }
219
+
220
+ protected dynamicOptionsMatch(one: DynamicDebugConfigurationSessionOptions, other: DynamicDebugConfigurationSessionOptions): boolean {
221
+ return one.providerType !== undefined
222
+ && one.configuration.name === other.configuration.name
223
+ && one.providerType === other.providerType
224
+ && one.workspaceFolderUri === other.workspaceFolderUri;
225
+ }
226
+
227
+ get recentDynamicOptions(): readonly DynamicDebugConfigurationSessionOptions[] {
228
+ return this.recentDynamicOptionsTracker;
229
+ }
230
+
231
+ protected updateCurrent(options: DebugSessionOptions | undefined = this._currentOptions): void {
232
+ if (DebugSessionOptions.isCompound(options)) {
233
+ this._currentOptions = options && this.find(options.compound, options.workspaceFolderUri);
234
+ } else {
235
+ this._currentOptions = options && this.find(options.configuration, options.workspaceFolderUri, options.providerType);
236
+ }
237
+
238
+ if (!this._currentOptions) {
239
+ const model = this.getModel();
240
+ if (model) {
241
+ const configuration = model.configurations[0];
242
+ if (configuration) {
243
+ this._currentOptions = this.configurationToOptions(configuration, model.workspaceFolderUri);
244
+ }
245
+ }
246
+ }
247
+ this.debugConfigurationTypeKey.set(this.current && this.current.configuration?.type);
248
+ this.onDidChangeEmitter.fire(undefined);
249
+ }
250
+
251
+ /**
252
+ * @deprecated since v1.27.0
253
+ */
254
+ find(name: string, workspaceFolderUri: string): DebugSessionOptions | undefined;
255
+ /**
256
+ * Find / Resolve DebugSessionOptions from a given target debug configuration
257
+ */
258
+ find(compound: DebugCompound, workspaceFolderUri?: string): DebugSessionOptions | undefined;
259
+ find(configuration: DebugConfiguration, workspaceFolderUri?: string, providerType?: string): DebugSessionOptions | undefined;
260
+ find(name: string, workspaceFolderUri?: string, providerType?: string): DebugSessionOptions | undefined;
261
+ find(nameOrConfigurationOrCompound: string | DebugConfiguration | DebugCompound, workspaceFolderUri?: string, providerType?: string): DebugSessionOptions | undefined {
262
+ if (DebugConfiguration.is(nameOrConfigurationOrCompound) && providerType) {
263
+ // providerType is only applicable to dynamic debug configurations and may only be created if we have a configuration given
264
+ return this.configurationToOptions(nameOrConfigurationOrCompound, workspaceFolderUri, providerType);
265
+ }
266
+ const name = typeof nameOrConfigurationOrCompound === 'string' ? nameOrConfigurationOrCompound : nameOrConfigurationOrCompound.name;
267
+ const configuration = this.findConfiguration(name, workspaceFolderUri);
268
+ if (configuration) {
269
+ return this.configurationToOptions(configuration, workspaceFolderUri);
270
+ }
271
+ const compound = this.findCompound(name, workspaceFolderUri);
272
+ if (compound) {
273
+ return this.compoundToOptions(compound, workspaceFolderUri);
274
+ }
275
+ }
276
+
277
+ findConfigurations(name: string, workspaceFolderUri?: string): DebugConfiguration[] {
278
+ const matches = [];
279
+ for (const model of this.models.values()) {
280
+ if (model.workspaceFolderUri === workspaceFolderUri) {
281
+ for (const configuration of model.configurations) {
282
+ if (configuration.name === name) {
283
+ matches.push(configuration);
284
+ }
285
+ }
286
+ }
287
+ }
288
+ return matches;
289
+ }
290
+
291
+ findConfiguration(name: string, workspaceFolderUri?: string): DebugConfiguration | undefined {
292
+ for (const model of this.models.values()) {
293
+ if (model.workspaceFolderUri === workspaceFolderUri) {
294
+ for (const configuration of model.configurations) {
295
+ if (configuration.name === name) {
296
+ return configuration;
297
+ }
298
+ }
299
+ }
300
+ }
301
+ }
302
+
303
+ findCompound(name: string, workspaceFolderUri?: string): DebugCompound | undefined {
304
+ for (const model of this.models.values()) {
305
+ if (model.workspaceFolderUri === workspaceFolderUri) {
306
+ for (const compound of model.compounds) {
307
+ if (compound.name === name) {
308
+ return compound;
309
+ }
310
+ }
311
+ }
312
+ }
313
+ }
314
+
315
+ async openConfiguration(): Promise<void> {
316
+ const currentUri = new URI(this.current?.workspaceFolderUri);
317
+ const model = this.getModel(currentUri);
318
+ if (model) {
319
+ await this.doOpen(model);
320
+ }
321
+ }
322
+
323
+ protected configurationToOptions(configuration: DebugConfiguration, workspaceFolderUri?: string, providerType?: string): DebugSessionOptions {
324
+ return { name: configuration.name, configuration, providerType, workspaceFolderUri };
325
+ }
326
+
327
+ protected compoundToOptions(compound: DebugCompound, workspaceFolderUri?: string): DebugSessionOptions {
328
+ return { name: compound.name, compound, workspaceFolderUri };
329
+ }
330
+
331
+ async addConfiguration(): Promise<void> {
332
+ let rootUri: URI | undefined = undefined;
333
+ if (this.workspaceService.saved && this.workspaceService.tryGetRoots().length > 1) {
334
+ rootUri = await this.selectRootUri();
335
+ // Do not continue if the user explicitly does not choose a location.
336
+ if (!rootUri) {
337
+ return;
338
+ }
339
+ }
340
+
341
+ const model = this.getModel(rootUri);
342
+ if (!model) {
343
+ return;
344
+ }
345
+ const widget = await this.doOpen(model);
346
+ if (!(widget.editor instanceof MonacoEditor)) {
347
+ return;
348
+ }
349
+ const editor = widget.editor.getControl();
350
+ const commandService = StandaloneServices.get(ICommandService);
351
+ let position: monaco.Position | undefined;
352
+ let depthInArray = 0;
353
+ let lastProperty = '';
354
+ visit(editor.getValue(), {
355
+ onObjectProperty: property => {
356
+ lastProperty = property;
357
+ },
358
+ onArrayBegin: offset => {
359
+ if (lastProperty === 'configurations' && depthInArray === 0) {
360
+ position = editor.getModel()!.getPositionAt(offset + 1);
361
+ }
362
+ depthInArray++;
363
+ },
364
+ onArrayEnd: () => {
365
+ depthInArray--;
366
+ }
367
+ });
368
+ if (!position) {
369
+ return;
370
+ }
371
+ // Check if there are more characters on a line after a "configurations": [, if yes enter a newline
372
+ if (editor.getModel()!.getLineLastNonWhitespaceColumn(position.lineNumber) > position.column) {
373
+ editor.setPosition(position);
374
+ editor.trigger('debug', 'lineBreakInsert', undefined);
375
+ }
376
+ // Check if there is already an empty line to insert suggest, if yes just place the cursor
377
+ if (editor.getModel()!.getLineLastNonWhitespaceColumn(position.lineNumber + 1) === 0) {
378
+ editor.setPosition({ lineNumber: position.lineNumber + 1, column: 1 << 30 });
379
+ await commandService.executeCommand('editor.action.deleteLines');
380
+ }
381
+ editor.setPosition(position);
382
+ await commandService.executeCommand('editor.action.insertLineAfter');
383
+ await commandService.executeCommand('editor.action.triggerSuggest');
384
+ }
385
+
386
+ protected async selectRootUri(): Promise<URI | undefined> {
387
+ const workspaceRoots = this.workspaceService.tryGetRoots();
388
+ const items: QuickPickValue<URI>[] = [];
389
+ for (const workspaceRoot of workspaceRoots) {
390
+ items.push({
391
+ label: this.labelProvider.getName(workspaceRoot.resource),
392
+ description: this.labelProvider.getLongName(workspaceRoot.resource),
393
+ value: workspaceRoot.resource
394
+ });
395
+ }
396
+ const root = await this.quickPickService.show(items, {
397
+ placeholder: nls.localize('theia/debug/addConfigurationPlaceholder', 'Select workspace root to add configuration to'),
398
+ });
399
+ return root?.value;
400
+ }
401
+
402
+ protected getModel(uri?: URI): DebugConfigurationModel | undefined {
403
+ const workspaceFolderUri = this.workspaceVariables.getWorkspaceRootUri(uri);
404
+ if (workspaceFolderUri) {
405
+ const key = workspaceFolderUri.toString();
406
+ for (const model of this.models.values()) {
407
+ if (model.workspaceFolderUri === key) {
408
+ return model;
409
+ }
410
+ }
411
+ }
412
+ for (const model of this.models.values()) {
413
+ if (model.uri) {
414
+ return model;
415
+ }
416
+ }
417
+ return this.models.values().next().value;
418
+ }
419
+
420
+ protected async doOpen(model: DebugConfigurationModel): Promise<EditorWidget> {
421
+ const uri = await this.doCreate(model);
422
+
423
+ return this.editorManager.open(uri, {
424
+ mode: 'activate'
425
+ });
426
+ }
427
+
428
+ protected async doCreate(model: DebugConfigurationModel): Promise<URI> {
429
+ const uri = model.uri ?? this.preferences.getConfigUri(PreferenceScope.Folder, model.workspaceFolderUri, 'launch');
430
+ if (!uri) { // Since we are requesting information about a known workspace folder, this should never happen.
431
+ throw new Error('PreferenceService.getConfigUri has returned undefined when a URI was expected.');
432
+ }
433
+ const settingsUri = this.preferences.getConfigUri(PreferenceScope.Folder, model.workspaceFolderUri);
434
+ // Users may have placed their debug configurations in a `settings.json`, in which case we shouldn't modify the file.
435
+ if (settingsUri && !uri.isEqual(settingsUri)) {
436
+ await this.ensureContent(uri, model);
437
+ }
438
+ return uri;
439
+ }
440
+
441
+ /**
442
+ * Checks whether a `launch.json` file contains the minimum necessary content.
443
+ * If content not found, provides content and populates the file using Monaco.
444
+ */
445
+ protected async ensureContent(uri: URI, model: DebugConfigurationModel): Promise<void> {
446
+ const textModel = await this.textModelService.createModelReference(uri);
447
+ const currentContent = textModel.object.valid ? textModel.object.getText() : '';
448
+ try { // Look for the minimal well-formed launch.json content: {configurations: []}
449
+ const parsedContent = parse(currentContent);
450
+ if (Array.isArray(parsedContent.configurations)) {
451
+ return;
452
+ }
453
+ } catch {
454
+ // Just keep going
455
+ }
456
+ const debugType = await this.selectDebugType();
457
+ const configurations = debugType ? await this.provideDebugConfigurations(debugType, model.workspaceFolderUri) : [];
458
+ const content = this.getInitialConfigurationContent(configurations);
459
+ textModel.object.textEditorModel.setValue(content); // Will clobber anything the user has entered!
460
+ await textModel.object.save();
461
+ }
462
+
463
+ protected async provideDebugConfigurations(debugType: string, workspaceFolderUri: string | undefined): Promise<DebugConfiguration[]> {
464
+ await this.fireWillProvideDebugConfiguration();
465
+ return this.debug.provideDebugConfigurations(debugType, workspaceFolderUri);
466
+ }
467
+ protected async fireWillProvideDebugConfiguration(): Promise<void> {
468
+ await WaitUntilEvent.fire(this.onWillProvideDebugConfigurationEmitter, {});
469
+ }
470
+
471
+ async provideDynamicDebugConfigurations(): Promise<Record<string, DynamicDebugConfigurationSessionOptions[]>> {
472
+ await this.fireWillProvideDynamicDebugConfiguration();
473
+ const roots = this.workspaceService.tryGetRoots();
474
+ const promises = roots.map(async root => {
475
+ const configsMap = await this.debug.provideDynamicDebugConfigurations!(root.resource.toString());
476
+ const optionsMap = Object.fromEntries(Object.entries(configsMap).map(([type, configs]) => {
477
+ const options = configs.map(config => ({
478
+ name: config.name,
479
+ providerType: type,
480
+ configuration: config,
481
+ workspaceFolderUri: root.resource.toString()
482
+ }));
483
+ return [type, options];
484
+ }));
485
+ return optionsMap;
486
+ });
487
+
488
+ const typesToOptionsRecords = await Promise.all(promises);
489
+ const consolidatedTypesToOptions: Record<string, DynamicDebugConfigurationSessionOptions[]> = {};
490
+
491
+ for (const typesToOptionsInstance of typesToOptionsRecords) {
492
+ for (const [providerType, configurationsOptions] of Object.entries(typesToOptionsInstance)) {
493
+ if (!consolidatedTypesToOptions[providerType]) {
494
+ consolidatedTypesToOptions[providerType] = [];
495
+ }
496
+ consolidatedTypesToOptions[providerType].push(...configurationsOptions);
497
+ }
498
+ }
499
+ return consolidatedTypesToOptions;
500
+ }
501
+
502
+ async fetchDynamicDebugConfiguration(name: string, type: string, folder?: string): Promise<DebugConfiguration | undefined> {
503
+ await this.fireWillProvideDynamicDebugConfiguration();
504
+ return this.debug.fetchDynamicDebugConfiguration(name, type, folder);
505
+ }
506
+
507
+ protected async fireWillProvideDynamicDebugConfiguration(): Promise<void> {
508
+ await this.initialized;
509
+ await WaitUntilEvent.fire(this.onWillProvideDynamicDebugConfigurationEmitter, {});
510
+ }
511
+
512
+ protected getInitialConfigurationContent(initialConfigurations: DebugConfiguration[]): string {
513
+ return `{
514
+ // Use IntelliSense to learn about possible attributes.
515
+ // Hover to view descriptions of existing attributes.
516
+ "version": "0.2.0",
517
+ "configurations": ${JSON.stringify(initialConfigurations, undefined, ' ').split('\n').map(line => ' ' + line).join('\n').trim()}
518
+ }
519
+ `;
520
+ }
521
+
522
+ protected async selectDebugType(): Promise<string | undefined> {
523
+ const widget = this.editorManager.currentEditor;
524
+ if (!widget) {
525
+ return undefined;
526
+ }
527
+ const { languageId } = widget.editor.document;
528
+ const debuggers = await this.debug.getDebuggersForLanguage(languageId);
529
+ if (debuggers.length === 0) {
530
+ return undefined;
531
+ }
532
+ const items: Array<QuickPickValue<string>> = debuggers.map(({ label, type }) => ({ label, value: type }));
533
+ const selectedItem = await this.quickPickService.show(items, { placeholder: 'Select Environment' });
534
+ return selectedItem?.value;
535
+ }
536
+
537
+ @inject(StorageService)
538
+ protected readonly storage: StorageService;
539
+
540
+ async load(): Promise<void> {
541
+ await this.initialized;
542
+ const data = await this.storage.getData<DebugConfigurationManager.Data>('debug.configurations', {});
543
+ this.resolveRecentDynamicOptionsFromData(data.recentDynamicOptions);
544
+
545
+ // Between versions v1.26 and v1.27, the expected format of the data changed so that old stored data
546
+ // may not contain the configuration key.
547
+ if (DebugSessionOptions.isConfiguration(data.current)) {
548
+ // ensure options name is reflected from old configurations data
549
+ data.current.name = data.current.name ?? data.current.configuration?.name;
550
+ this.current = this.find(data.current.configuration, data.current.workspaceFolderUri, data.current.providerType);
551
+ } else if (DebugSessionOptions.isCompound(data.current)) {
552
+ this.current = this.find(data.current.name, data.current.workspaceFolderUri);
553
+ }
554
+ }
555
+
556
+ protected resolveRecentDynamicOptionsFromData(options?: DynamicDebugConfigurationSessionOptions[]): void {
557
+ if (!options || this.recentDynamicOptionsTracker.length !== 0) {
558
+ return;
559
+ }
560
+
561
+ // ensure options name is reflected from old configurations data
562
+ const dynamicOptions = options.map(option => {
563
+ option.name = option.name ?? option.configuration.name;
564
+ return option;
565
+ }).filter(DebugSessionOptions.isDynamic);
566
+ this.recentDynamicOptionsTracker = dynamicOptions;
567
+ }
568
+
569
+ save(): void {
570
+ const data: DebugConfigurationManager.Data = {};
571
+ const { current, recentDynamicOptionsTracker } = this;
572
+ if (current) {
573
+ data.current = current;
574
+ }
575
+
576
+ if (this.recentDynamicOptionsTracker.length > 0) {
577
+ data.recentDynamicOptions = recentDynamicOptionsTracker;
578
+ }
579
+
580
+ if (Object.keys(data).length > 0) {
581
+ this.storage.setData('debug.configurations', data);
582
+ }
583
+ }
584
+ }
585
+
586
+ export namespace DebugConfigurationManager {
587
+ export interface Data {
588
+ current?: DebugSessionOptions,
589
+ recentDynamicOptions?: DynamicDebugConfigurationSessionOptions[]
590
+ }
591
+ }