forked from microsoft/vscode-java-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigurationProvider.ts
More file actions
260 lines (242 loc) · 12 KB
/
configurationProvider.ts
File metadata and controls
260 lines (242 loc) · 12 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as vscode from "vscode";
import TelemetryReporter from "vscode-extension-telemetry";
import * as commands from "./commands";
export class JavaDebugConfigurationProvider implements vscode.DebugConfigurationProvider {
private isUserSettingsDirty: boolean = true;
constructor(private _reporter: TelemetryReporter) {
vscode.workspace.onDidChangeConfiguration((event) => {
if (vscode.debug.activeDebugSession) {
this.isUserSettingsDirty = false;
return updateDebugSettings();
} else {
this.isUserSettingsDirty = true;
}
});
}
// Returns an initial debug configurations based on contextual information.
public provideDebugConfigurations(folder: vscode.WorkspaceFolder | undefined, token?: vscode.CancellationToken):
vscode.ProviderResult<vscode.DebugConfiguration[]> {
return <Thenable<vscode.DebugConfiguration[]>>this.provideDebugConfigurationsAsync(folder);
}
// Try to add all missing attributes to the debug configuration being launched.
public resolveDebugConfiguration(folder: vscode.WorkspaceFolder | undefined, config: vscode.DebugConfiguration, token?: vscode.CancellationToken):
vscode.ProviderResult<vscode.DebugConfiguration> {
return this.heuristicallyResolveDebugConfiguration(folder, config);
}
private provideDebugConfigurationsAsync(folder: vscode.WorkspaceFolder | undefined, token?: vscode.CancellationToken) {
return vscode.window.withProgress({location: vscode.ProgressLocation.Window}, (p) => {
return new Promise((resolve, reject) => {
p.report({message: "Auto generating configuration..."});
resolveMainClass(folder.uri).then((res: any[]) => {
let cache;
cache = {};
const launchConfigs = res.map((item) => {
return {
type: "java",
name: this.constructLaunchConfigName(item.mainClass, item.projectName, cache),
request: "launch",
// tslint:disable-next-line
cwd: "${workspaceFolder}",
mainClass: item.mainClass,
projectName: item.projectName,
args: "",
};
});
resolve([...launchConfigs, {
type: "java",
name: "Debug (Attach)",
request: "attach",
hostName: "localhost",
port: 0,
}]);
}, (ex) => {
p.report({message: `failed to generate configuration. ${ex}`});
reject(ex);
});
});
});
}
private constructLaunchConfigName(mainClass: string, projectName: string, cache: {}) {
const prefix = "Debug (Launch)-";
let name = prefix + mainClass.substr(mainClass.lastIndexOf(".") + 1);
if (projectName !== undefined) {
name += `<${projectName}>`;
}
if (cache[name] === undefined) {
cache[name] = 0;
return name;
} else {
cache[name] += 1;
return `${name}(${cache[name]})`;
}
}
private async heuristicallyResolveDebugConfiguration(folder: vscode.WorkspaceFolder | undefined, config: vscode.DebugConfiguration) {
try {
if (this.isUserSettingsDirty) {
this.isUserSettingsDirty = false;
await updateDebugSettings();
}
try {
const buildResult = await vscode.commands.executeCommand(commands.JAVA_BUILD_WORKSPACE);
console.log(buildResult);
} catch (err) {
vscode.window.showErrorMessage("Build failed, please fix build error first.");
return config;
}
if (Object.keys(config).length === 0) { // No launch.json in current workspace.
// check whether it is opened as a folder
if (folder !== undefined) {
// for opened with folder, return directly.
return config;
}
// Generate config in memory for single file
config.type = "java";
config.name = "Java Debug";
config.request = "launch";
}
// Workaround bug https://github.com/Microsoft/vscode-java-debug/issues/145
config.stopOnEntry = false;
if (config.request === "launch") {
if (!config.mainClass) {
const res = <any[]>(await resolveMainClass(folder.uri));
if (res.length === 0) {
vscode.window.showErrorMessage(
"Cannot resolve main class automatically, please specify the mainClass " +
"(e.g. [mymodule/]com.xyz.MainClass) in the launch.json.");
return;
}
const pickItems = res.map((item) => {
let name = item.mainClass;
let details = `main class: ${item.mainClass}`;
if (item.projectName !== undefined) {
name += `<${item.projectName}>`;
details += ` | project name: ${item.projectName}`;
}
return {
description: details,
label: name,
item,
};
}).sort ((a, b): number => {
return a.label > b.label ? 1 : -1;
});
const selection = await vscode.window.showQuickPick(pickItems, { placeHolder: "Select main class<project name>" });
if (selection) {
config.mainClass = selection.item.mainClass;
config.projectName = selection.item.projectName;
} else {
vscode.window.showErrorMessage("Please specify the mainClass (e.g. [mymodule/]com.xyz.MainClass) in the launch.json.");
this.log("usageError", "Please specify the mainClass (e.g. [mymodule/]com.xyz.MainClass) in the launch.json.");
return undefined;
}
}
if (this.isEmptyArray(config.classPaths) && this.isEmptyArray(config.modulePaths)) {
const result = <any[]>(await resolveClasspath(config.mainClass, config.projectName));
config.modulePaths = result[0];
config.classPaths = result[1];
}
if (this.isEmptyArray(config.classPaths) && this.isEmptyArray(config.modulePaths)) {
const hintMessage = "Cannot resolve the modulepaths/classpaths automatically, please specify the value in the launch.json.";
vscode.window.showErrorMessage(hintMessage);
this.log("usageError", hintMessage);
return undefined;
}
} else if (config.request === "attach") {
if (!config.hostName || !config.port) {
vscode.window.showErrorMessage("Please specify the host name and the port of the remote debuggee in the launch.json.");
this.log("usageError", "Please specify the host name and the port of the remote debuggee in the launch.json.");
return undefined;
}
} else {
const ans = await vscode.window.showErrorMessage(
// tslint:disable-next-line:max-line-length
"Request type \"" + config.request + "\" is not supported. Only \"launch\" and \"attach\" are supported.", "Open launch.json");
if (ans === "Open launch.json") {
await vscode.commands.executeCommand(commands.VSCODE_ADD_DEBUGCONFIGURATION);
}
this.log("usageError", "Illegal request type in launch.json");
return undefined;
}
const debugServerPort = await startDebugSession();
if (debugServerPort) {
config.debugServer = debugServerPort;
return config;
} else {
this.log("exception", "Failed to start debug server.");
// Information for diagnostic:
console.log("Cannot find a port for debugging session");
return undefined;
}
} catch (ex) {
const errorMessage = (ex && ex.message) || ex;
vscode.window.showErrorMessage(String(errorMessage));
if (this._reporter) {
const exception = (ex && ex.data && ex.data.cause)
|| { stackTrace: [], detailMessage: String((ex && ex.message) || ex || "Unknown exception") };
const properties = {
message: "",
stackTrace: "",
};
if (exception && typeof exception === "object") {
properties.message = exception.detailMessage;
properties.stackTrace = (Array.isArray(exception.stackTrace) && JSON.stringify(exception.stackTrace))
|| String(exception.stackTrace);
} else {
properties.message = String(exception);
}
this._reporter.sendTelemetryEvent("exception", properties);
}
return undefined;
}
}
private log(type: string, message: string) {
if (this._reporter) {
this._reporter.sendTelemetryEvent(type, { message });
}
}
private isEmptyArray(configItems: any): boolean {
return !Array.isArray(configItems) || !configItems.length;
}
}
function startDebugSession() {
return commands.executeJavaLanguageServerCommand(commands.JAVA_START_DEBUGSESSION);
}
function resolveClasspath(mainClass, projectName) {
return commands.executeJavaLanguageServerCommand(commands.JAVA_RESOLVE_CLASSPATH, mainClass, projectName);
}
function resolveMainClass(workspaceUri: vscode.Uri) {
return commands.executeJavaLanguageServerCommand(commands.JAVA_RESOLVE_MAINCLASS, workspaceUri.toString());
}
async function updateDebugSettings() {
const debugSettingsRoot: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("java.debug");
if (!debugSettingsRoot) {
return;
}
const logLevel = convertLogLevel(debugSettingsRoot.logLevel || "");
if (debugSettingsRoot.settings && Object.keys(debugSettingsRoot.settings).length) {
try {
console.log("settings:", await commands.executeJavaLanguageServerCommand(commands.JAVA_UPDATE_DEBUG_SETTINGS, JSON.stringify(
{ ...debugSettingsRoot.settings, logLevel })));
} catch (err) {
// log a warning message and continue, since update settings failure should not block debug session
console.log("Cannot update debug settings.", err)
}
}
}
function convertLogLevel(commonLogLevel: string) {
// convert common log level to java log level
switch (commonLogLevel.toLowerCase()) {
case "verbose":
return "FINE";
case "warn":
return "WARNING";
case "error":
return "SEVERE";
case "info":
return "INFO";
default:
return "FINE";
}
}