Skip to content

Commit 5fc5db2

Browse files
authored
Support customizing the classpaths and modulepaths (#1005)
* Support customizing the classpaths and modulepaths
1 parent 5f8f688 commit 5fc5db2

File tree

6 files changed

+147
-4
lines changed

6 files changed

+147
-4
lines changed

package.json

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,15 +397,47 @@
397397
"modulePaths": {
398398
"type": "array",
399399
"items": {
400-
"type": "string"
400+
"anyOf": [
401+
{
402+
"enum": [
403+
"$Auto",
404+
"$Runtime",
405+
"$Test",
406+
"!<path>"
407+
],
408+
"enumDescriptions": [
409+
"%java.debugger.launch.modulePaths.auto%",
410+
"%java.debugger.launch.modulePaths.runtime%",
411+
"%java.debugger.launch.modulePaths.test%",
412+
"%java.debugger.launch.modulePaths.exclude%"
413+
]
414+
},
415+
"string"
416+
]
401417
},
402418
"description": "%java.debugger.launch.modulePaths.description%",
403419
"default": []
404420
},
405421
"classPaths": {
406422
"type": "array",
407423
"items": {
408-
"type": "string"
424+
"anyOf": [
425+
{
426+
"enum": [
427+
"$Auto",
428+
"$Runtime",
429+
"$Test",
430+
"!<path>"
431+
],
432+
"enumDescriptions": [
433+
"%java.debugger.launch.classPaths.auto%",
434+
"%java.debugger.launch.classPaths.runtime%",
435+
"%java.debugger.launch.classPaths.test%",
436+
"%java.debugger.launch.classPaths.exclude%"
437+
]
438+
},
439+
"string"
440+
]
409441
},
410442
"description": "%java.debugger.launch.classPaths.description%",
411443
"default": []

package.nls.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44
"java.debugger.launch.args.description": "The command line arguments passed to the program.",
55
"java.debugger.launch.vmArgs.description": "The extra options and system properties for the JVM (e.g. -Xms<size> -Xmx<size> -D<name>=<value>).",
66
"java.debugger.launch.modulePaths.description": "The modulepaths for launching the JVM. If not specified, the debugger will automatically resolve from current project.",
7+
"java.debugger.launch.modulePaths.auto": "Automatically resolve the module paths of current project.",
8+
"java.debugger.launch.modulePaths.runtime": "The module paths within 'runtime' scope of current project.",
9+
"java.debugger.launch.modulePaths.test": "The module paths within 'test' scope of current project.",
10+
"java.debugger.launch.modulePaths.exclude": "The path after '!' will be excluded from the modulePaths.",
711
"java.debugger.launch.classPaths.description": "The classpaths for launching the JVM. If not specified, the debugger will automatically resolve from current project.",
12+
"java.debugger.launch.classPaths.auto": "Automatically resolve the classpaths of current project.",
13+
"java.debugger.launch.classPaths.runtime": "The classpaths within 'runtime' scope of current project.",
14+
"java.debugger.launch.classPaths.test": "The classpaths within 'test' scope of current project.",
15+
"java.debugger.launch.classPaths.exclude": "The path after '!' will be excluded from the classpaths.",
816
"java.debugger.launch.sourcePaths.description": "The extra source directories of the program. The debugger looks for source code from project settings by default. This option allows the debugger to look for source code in extra directories.",
917
"java.debugger.launch.encoding.description": "The file.encoding setting for the JVM. If not specified, 'UTF-8' will be used. Possible values can be found in https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html.",
1018
"java.debugger.launch.cwd.description": "The working directory of the program. Defaults to the current workspace root.",

package.nls.zh.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44
"java.debugger.launch.args.description": "启动应用程序的命令行参数。",
55
"java.debugger.launch.vmArgs.description": "用于启动JVM的额外选项和系统属性(例如-Xms <size> -Xmx <size> -D <name> = <value>)。",
66
"java.debugger.launch.modulePaths.description": "用于启动JVM的模块路径。如果未指定,调试器将自动从当前工程中解析。",
7+
"java.debugger.launch.modulePaths.auto": "自动从当前工程中解析模块路径。",
8+
"java.debugger.launch.modulePaths.runtime": "当前工程中属于 runtime 作用域的模块路径。",
9+
"java.debugger.launch.modulePaths.test": "当前工程中属于 test 作用域的模块路径。",
10+
"java.debugger.launch.modulePaths.exclude": "'!' 之后的路径将会从模块路径中去除。",
711
"java.debugger.launch.classPaths.description": "用于启动JVM的类路径。如果未指定,调试器将自动从当前工程中解析。",
12+
"java.debugger.launch.classPaths.auto": "自动从当前工程中解析类路径。",
13+
"java.debugger.launch.classPaths.runtime": "当前工程中属于 runtime 作用域的类路径。",
14+
"java.debugger.launch.classPaths.test": "当前工程中属于 test 作用域的类路径。",
15+
"java.debugger.launch.classPaths.exclude": "'!' 之后的路径将会从类路径中去除。",
816
"java.debugger.launch.sourcePaths.description": "应用程序的额外源代码目录。调试器默认从工程配置中查找源代码。此选项允许调试器在额外目录中查找源代码。",
917
"java.debugger.launch.encoding.description": "JVM的file.encoding设置。如果未指定,将使用“UTF-8”。可以在http://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html中找到可能的值。",
1018
"java.debugger.launch.cwd.description": "应用程序的工作目录。默认为当前工作空间根目录。",

src/configurationProvider.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as anchor from "./anchor";
1111
import { buildWorkspace } from "./build";
1212
import { populateStepFilters, substituteFilterVariables } from "./classFilter";
1313
import * as commands from "./commands";
14+
import { ClasspathVariable } from "./constants";
1415
import { Type } from "./javaLogger";
1516
import * as lsPlugin from "./languageServerPlugin";
1617
import { addMoreHelpfulVMArgs, getJavaVersion, getShortenApproachForCLI, validateRuntimeCompatibility } from "./launchCommand";
@@ -263,10 +264,16 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
263264
if (progressReporter.isCancelled()) {
264265
return undefined;
265266
}
267+
266268
if (_.isEmpty(config.classPaths) && _.isEmpty(config.modulePaths)) {
267269
const result = <any[]>(await lsPlugin.resolveClasspath(config.mainClass, config.projectName));
268270
config.modulePaths = result[0];
269271
config.classPaths = result[1];
272+
} else {
273+
config.modulePaths = await this.resolvePath(folder, config.modulePaths, config.mainClass,
274+
config.projectName, true /*isModulePath*/);
275+
config.classPaths = await this.resolvePath(folder, config.classPaths, config.mainClass,
276+
config.projectName, false /*isModulePath*/);
270277
}
271278
if (_.isEmpty(config.classPaths) && _.isEmpty(config.modulePaths)) {
272279
throw new utility.UserError({
@@ -381,6 +388,88 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
381388
}
382389
}
383390

391+
private async resolvePath(folder: vscode.WorkspaceFolder | undefined, pathArray: string[], mainClass: string,
392+
projectName: string, isModulePath: boolean): Promise<string[]> {
393+
if (_.isEmpty(pathArray)) {
394+
return [];
395+
}
396+
397+
const pathVariables: string[] = [ClasspathVariable.Auto, ClasspathVariable.Runtime, ClasspathVariable.Test];
398+
const containedVariables: string[] = pathArray.filter((cp: string) => pathVariables.includes(cp));
399+
if (_.isEmpty(containedVariables)) {
400+
return this.filterExcluded(folder, pathArray);
401+
}
402+
403+
const scope: string | undefined = this.mergeScope(containedVariables);
404+
const response: any[] = <any[]> await lsPlugin.resolveClasspath(mainClass, projectName, scope);
405+
const resolvedPaths: string[] = isModulePath ? response?.[0] : response?.[1];
406+
if (!resolvedPaths) {
407+
// tslint:disable-next-line:no-console
408+
console.log("The Java Language Server failed to resolve the classpaths/modulepaths");
409+
}
410+
const paths: string[] = [];
411+
let replaced: boolean = false;
412+
for (const p of pathArray) {
413+
if (pathVariables.includes(p)) {
414+
if (!replaced) {
415+
paths.push(...resolvedPaths);
416+
replaced = true;
417+
}
418+
continue;
419+
}
420+
paths.push(p);
421+
}
422+
return this.filterExcluded(folder, paths);
423+
}
424+
425+
private async filterExcluded(folder: vscode.WorkspaceFolder | undefined, paths: string[]): Promise<string[]> {
426+
const result: string[] = [];
427+
const excludes: Map<string, boolean> = new Map<string, boolean>();
428+
for (const p of paths) {
429+
if (p.startsWith("!")) {
430+
let exclude = p.substr(1);
431+
if (!path.isAbsolute(exclude)) {
432+
exclude = path.join(folder?.uri.fsPath || "", exclude);
433+
}
434+
// use Uri to normalize the fs path
435+
excludes.set(vscode.Uri.file(exclude).fsPath, this.isFile(exclude));
436+
continue;
437+
}
438+
439+
result.push(vscode.Uri.file(p).fsPath);
440+
}
441+
442+
return result.filter((r) => {
443+
for (const [excludedPath, isFile] of excludes.entries()) {
444+
if (isFile && r === excludedPath) {
445+
return false;
446+
}
447+
448+
if (!isFile && r.startsWith(excludedPath)) {
449+
return false;
450+
}
451+
}
452+
453+
return true;
454+
});
455+
}
456+
457+
private mergeScope(scopes: string[]): string | undefined {
458+
if (scopes.includes(ClasspathVariable.Test)) {
459+
return "test";
460+
}
461+
462+
if (scopes.includes(ClasspathVariable.Auto)) {
463+
return undefined;
464+
}
465+
466+
if (scopes.includes(ClasspathVariable.Runtime)) {
467+
return "runtime";
468+
}
469+
470+
return undefined;
471+
}
472+
384473
/**
385474
* Converts an array of arguments to a string as the args and vmArgs.
386475
*/

src/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@
44
export const JAVA_LANGID: string = "java";
55
export const HCR_EVENT = "hotcodereplace";
66
export const USER_NOTIFICATION_EVENT = "usernotification";
7+
8+
export enum ClasspathVariable {
9+
Auto = "$Auto",
10+
Runtime = "$Runtime",
11+
Test = "$Test",
12+
}

src/languageServerPlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export function startDebugSession() {
4646
return commands.executeJavaLanguageServerCommand(commands.JAVA_START_DEBUGSESSION);
4747
}
4848

49-
export function resolveClasspath(mainClass: string, projectName: string) {
50-
return commands.executeJavaLanguageServerCommand(commands.JAVA_RESOLVE_CLASSPATH, mainClass, projectName);
49+
export function resolveClasspath(mainClass: string, projectName: string, scope?: string) {
50+
return commands.executeJavaLanguageServerCommand(commands.JAVA_RESOLVE_CLASSPATH, mainClass, projectName, scope);
5151
}
5252

5353
export function resolveMainClass(workspaceUri?: vscode.Uri): Promise<IMainClassOption[]> {

0 commit comments

Comments
 (0)