-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathplugin-debug-service.ts
252 lines (216 loc) · 10.1 KB
/
plugin-debug-service.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
// *****************************************************************************
// 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
// *****************************************************************************
import { DebugService, DebuggerDescription, DebugPath } from '@theia/debug/lib/common/debug-service';
import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable';
import { DebugConfiguration } from '@theia/debug/lib/common/debug-configuration';
import { IJSONSchema, IJSONSchemaSnippet } from '@theia/core/lib/common/json-schema';
import { PluginDebugAdapterContribution } from './plugin-debug-adapter-contribution';
import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
import { WebSocketConnectionProvider } from '@theia/core/lib/browser/messaging/ws-connection-provider';
import { WorkspaceService } from '@theia/workspace/lib/browser';
import { DebuggerContribution } from '../../../common/plugin-protocol';
import { DebugRequestTypes } from '@theia/debug/lib/browser/debug-session-connection';
import * as theia from '@theia/plugin';
/**
* Debug adapter contribution registrator.
*/
export interface PluginDebugAdapterContributionRegistrator {
/**
* Registers [PluginDebugAdapterContribution](#PluginDebugAdapterContribution).
* @param contrib contribution
*/
registerDebugAdapterContribution(contrib: PluginDebugAdapterContribution): Disposable;
/**
* Unregisters [PluginDebugAdapterContribution](#PluginDebugAdapterContribution).
* @param debugType the debug type
*/
unregisterDebugAdapterContribution(debugType: string): void;
}
/**
* Debug service to work with plugin and extension contributions.
*/
@injectable()
export class PluginDebugService implements DebugService, PluginDebugAdapterContributionRegistrator {
protected readonly debuggers: DebuggerContribution[] = [];
protected readonly contributors = new Map<string, PluginDebugAdapterContribution>();
protected readonly toDispose = new DisposableCollection();
// maps session and contribution
protected readonly sessionId2contrib = new Map<string, PluginDebugAdapterContribution>();
protected delegated: DebugService;
@inject(WebSocketConnectionProvider)
protected readonly connectionProvider: WebSocketConnectionProvider;
@inject(WorkspaceService)
protected readonly workspaceService: WorkspaceService;
@postConstruct()
protected init(): void {
this.delegated = this.connectionProvider.createProxy<DebugService>(DebugPath);
this.toDispose.pushAll([
Disposable.create(() => this.delegated.dispose()),
Disposable.create(() => {
for (const sessionId of this.sessionId2contrib.keys()) {
const contrib = this.sessionId2contrib.get(sessionId)!;
contrib.terminateDebugSession(sessionId);
}
this.sessionId2contrib.clear();
})]);
}
registerDebugAdapterContribution(contrib: PluginDebugAdapterContribution): Disposable {
const { type } = contrib;
if (this.contributors.has(type)) {
console.warn(`Debugger with type '${type}' already registered.`);
return Disposable.NULL;
}
this.contributors.set(type, contrib);
return Disposable.create(() => this.unregisterDebugAdapterContribution(type));
}
unregisterDebugAdapterContribution(debugType: string): void {
this.contributors.delete(debugType);
}
async debugTypes(): Promise<string[]> {
const debugTypes = new Set(await this.delegated.debugTypes());
for (const contribution of this.debuggers) {
debugTypes.add(contribution.type);
}
for (const debugType of this.contributors.keys()) {
debugTypes.add(debugType);
}
return [...debugTypes];
}
async provideDebugConfigurations(debugType: keyof DebugRequestTypes, workspaceFolderUri: string | undefined): Promise<theia.DebugConfiguration[]> {
const contributor = this.contributors.get(debugType);
if (contributor) {
return contributor.provideDebugConfigurations && contributor.provideDebugConfigurations(workspaceFolderUri) || [];
} else {
return this.delegated.provideDebugConfigurations(debugType, workspaceFolderUri);
}
}
async provideDynamicDebugConfigurations(): Promise<{ type: string, configurations: DebugConfiguration[] }[]> {
const result: Promise<{ type: string, configurations: theia.DebugConfiguration[] }>[] = [];
for (const [type, contributor] of this.contributors.entries()) {
const typeConfigurations = this.resolveDynamicConfigurationsForType(type, contributor);
result.push(typeConfigurations);
}
return Promise.all(result);
}
protected async resolveDynamicConfigurationsForType(
type: string,
contributor: PluginDebugAdapterContribution): Promise<{ type: string, configurations: DebugConfiguration[] }> {
const configurations = await contributor.provideDebugConfigurations(undefined, true);
for (const configuration of configurations) {
configuration.dynamic = true;
}
return { type, configurations };
}
async resolveDebugConfiguration(config: DebugConfiguration, workspaceFolderUri: string | undefined): Promise<DebugConfiguration> {
let resolved = config;
// we should iterate over all to handle configuration providers for `*`
for (const contributor of this.contributors.values()) {
if (contributor) {
try {
const next = await contributor.resolveDebugConfiguration(resolved, workspaceFolderUri);
if (next) {
resolved = next;
} else {
return resolved;
}
} catch (e) {
console.error(e);
}
}
}
return this.delegated.resolveDebugConfiguration(resolved, workspaceFolderUri);
}
async resolveDebugConfigurationWithSubstitutedVariables(config: DebugConfiguration, workspaceFolderUri: string | undefined): Promise<DebugConfiguration> {
let resolved = config;
// we should iterate over all to handle configuration providers for `*`
for (const contributor of this.contributors.values()) {
if (contributor) {
try {
const next = await contributor.resolveDebugConfigurationWithSubstitutedVariables(resolved, workspaceFolderUri);
if (next) {
resolved = next;
} else {
return resolved;
}
} catch (e) {
console.error(e);
}
}
}
return this.delegated.resolveDebugConfigurationWithSubstitutedVariables(resolved, workspaceFolderUri);
}
registerDebugger(contribution: DebuggerContribution): Disposable {
this.debuggers.push(contribution);
return Disposable.create(() => {
const index = this.debuggers.indexOf(contribution);
if (index !== -1) {
this.debuggers.splice(index, 1);
}
});
}
async getDebuggersForLanguage(language: string): Promise<DebuggerDescription[]> {
const debuggers = await this.delegated.getDebuggersForLanguage(language);
for (const contributor of this.debuggers) {
const languages = contributor.languages;
if (languages && languages.indexOf(language) !== -1) {
const { label, type } = contributor;
debuggers.push({ type, label: label || type });
}
}
return debuggers;
}
async getSchemaAttributes(debugType: string): Promise<IJSONSchema[]> {
let schemas = await this.delegated.getSchemaAttributes(debugType);
for (const contribution of this.debuggers) {
if (contribution.configurationAttributes &&
(contribution.type === debugType || contribution.type === '*' || debugType === '*')) {
schemas = schemas.concat(contribution.configurationAttributes);
}
}
return schemas;
}
async getConfigurationSnippets(): Promise<IJSONSchemaSnippet[]> {
let snippets = await this.delegated.getConfigurationSnippets();
for (const contribution of this.debuggers) {
if (contribution.configurationSnippets) {
snippets = snippets.concat(contribution.configurationSnippets);
}
}
return snippets;
}
async createDebugSession(config: DebugConfiguration): Promise<string> {
const contributor = this.contributors.get(config.type);
if (contributor) {
const sessionId = await contributor.createDebugSession(config);
this.sessionId2contrib.set(sessionId, contributor);
return sessionId;
} else {
return this.delegated.createDebugSession(config);
}
}
async terminateDebugSession(sessionId: string): Promise<void> {
const contributor = this.sessionId2contrib.get(sessionId);
if (contributor) {
this.sessionId2contrib.delete(sessionId);
return contributor.terminateDebugSession(sessionId);
} else {
return this.delegated.terminateDebugSession(sessionId);
}
}
dispose(): void {
this.toDispose.dispose();
}
}