Skip to content

Commit 01f7735

Browse files
Auto add --enable-preview vmArgs for java 12 project (#561)
* Auto add --enable-preview vmArgs for java 12 project Signed-off-by: Jinbo Wang <jinbwan@microsoft.com> * make tslint happy Signed-off-by: Jinbo Wang <jinbwan@microsoft.com> * Use new generic api to check project preview flag Signed-off-by: Jinbo Wang <jinbwan@microsoft.com> * Address review comments Signed-off-by: Jinbo Wang <jinbwan@microsoft.com> * Make tslint happy Signed-off-by: Jinbo Wang <jinbwan@microsoft.com>
1 parent 99357f2 commit 01f7735

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/commands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export const JAVA_RESOLVE_MAINMETHOD = "vscode.java.resolveMainMethod";
2727

2828
export const JAVA_INFER_LAUNCH_COMMAND_LENGTH = "vscode.java.inferLaunchCommandLength";
2929

30+
export const JAVA_CHECK_PROJECT_SETTINGS = "vscode.java.checkProjectSettings";
31+
3032
export function executeJavaLanguageServerCommand(...rest) {
3133
// TODO: need to handle error and trace telemetry
3234
return vscode.commands.executeCommand(JAVA_EXECUTE_WORKSPACE_COMMAND, ...rest);

src/configurationProvider.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
186186

187187
// Add the default launch options to the config.
188188
config.cwd = config.cwd || _.get(folder, "uri.fsPath");
189+
// Auto add '--enable-preview' vmArgs if the java project enables COMPILER_PB_ENABLE_PREVIEW_FEATURES flag.
190+
if (await lsPlugin.detectPreviewFlag(config.mainClass, config.projectName)) {
191+
config.vmArgs = (config.vmArgs || "") + " --enable-preview";
192+
}
189193
} else if (config.request === "attach") {
190194
if (!config.hostName || !config.port) {
191195
throw new utility.UserError({

src/languageServerPlugin.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,22 @@ export function validateLaunchConfig(workspaceUri: vscode.Uri, mainClass: string
6161
export function inferLaunchCommandLength(config: vscode.DebugConfiguration): Promise<number> {
6262
return <Promise<number>>commands.executeJavaLanguageServerCommand(commands.JAVA_INFER_LAUNCH_COMMAND_LENGTH, JSON.stringify(config));
6363
}
64+
65+
export function checkProjectSettings(className: string, projectName: string, inheritedOptions: boolean, expectedOptions: {[key: string]: string}):
66+
Promise<boolean> {
67+
return <Promise<boolean>>commands.executeJavaLanguageServerCommand(
68+
commands.JAVA_CHECK_PROJECT_SETTINGS, JSON.stringify({
69+
className,
70+
projectName,
71+
inheritedOptions,
72+
expectedOptions,
73+
}));
74+
}
75+
76+
const COMPILER_PB_ENABLE_PREVIEW_FEATURES: string = "org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures";
77+
export async function detectPreviewFlag(className: string, projectName: string): Promise<boolean> {
78+
const expectedOptions = {
79+
[COMPILER_PB_ENABLE_PREVIEW_FEATURES]: "enabled",
80+
};
81+
return await checkProjectSettings(className, projectName, true, expectedOptions);
82+
}

0 commit comments

Comments
 (0)