Skip to content

Commit 0749848

Browse files
authored
feat: Automatically add --enable-preview to vmargs when necessary (#701)
1 parent 93cc43f commit 0749848

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/constants/commands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export namespace JavaTestRunnerDelegateCommands {
1313
export const SEARCH_TEST_LOCATION: string = 'vscode.java.test.search.location';
1414
export const RESOLVE_RUNTIME_CLASSPATH: string = 'vscode.java.test.runtime.classpath';
1515
export const GET_PROJECT_INFO: string = 'vscode.java.test.project.info';
16+
// This is a command from the Java Debugger
17+
export const JAVA_CHECK_PROJECT_SETTINGS: string = 'vscode.java.checkProjectSettings';
1618
}
1719

1820
export namespace JavaTestRunnerCommands {

src/runners/runnerExecutor.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { IExecutionConfig } from '../runConfigs';
99
import { testReportProvider } from '../testReportProvider';
1010
import { testResultManager } from '../testResultManager';
1111
import { testStatusBarProvider } from '../testStatusBarProvider';
12+
import { shouldEnablePreviewFlag } from '../utils/commandUtils';
1213
import { loadRunConfig } from '../utils/configUtils';
1314
import { resolve } from '../utils/settingUtils';
1415
import { ITestRunner } from './ITestRunner';
@@ -47,6 +48,15 @@ class RunnerExecutor {
4748
continue;
4849
}
4950

51+
// Auto add '--enable-preview' vmArgs if the java project enables COMPILER_PB_ENABLE_PREVIEW_FEATURES flag.
52+
if (await shouldEnablePreviewFlag('', tests[0].project)) {
53+
if (config.vmargs) {
54+
config.vmargs.push('--enable-preview');
55+
} else {
56+
config.vmargs = ['--enable-preview'];
57+
}
58+
}
59+
5060
await window.withProgress(
5161
{ location: ProgressLocation.Notification, cancellable: true },
5262
async (progress: Progress<any>, token: CancellationToken): Promise<void> => {

src/utils/commandUtils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ export async function resolveRuntimeClassPath(paths: string[]): Promise<string[]
3838
JavaTestRunnerDelegateCommands.RESOLVE_RUNTIME_CLASSPATH, paths) || []);
3939
}
4040

41+
export async function checkProjectSettings(className: string, projectName: string, inheritedOptions: boolean, expectedOptions: {[key: string]: string}): Promise<boolean> {
42+
return await executeJavaLanguageServerCommand<boolean>(
43+
JavaTestRunnerDelegateCommands.JAVA_CHECK_PROJECT_SETTINGS, JSON.stringify({
44+
className,
45+
projectName,
46+
inheritedOptions,
47+
expectedOptions,
48+
})) || false;
49+
}
50+
51+
const COMPILER_PB_ENABLE_PREVIEW_FEATURES: string = 'org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures';
52+
export async function shouldEnablePreviewFlag(className: string, projectName: string): Promise<boolean> {
53+
const expectedOptions: { [x: string]: string; } = {
54+
[COMPILER_PB_ENABLE_PREVIEW_FEATURES]: 'enabled',
55+
};
56+
return await checkProjectSettings(className, projectName, true, expectedOptions);
57+
}
58+
4159
async function executeJavaLanguageServerCommand<T>(...rest: any[]): Promise<T | undefined> {
4260
try {
4361
return await commands.executeCommand<T>(JavaLanguageServerCommands.EXECUTE_WORKSPACE_COMMAND, ...rest);

0 commit comments

Comments
 (0)