Skip to content

Commit 20cd68c

Browse files
Respond LightWeight server mode properly (#838)
* Respond LightWeight server mode properly Signed-off-by: Jinbo Wang <[email protected]> * make tslint happy Signed-off-by: Jinbo Wang <[email protected]> * Refine server mode waiting logic Signed-off-by: Jinbo Wang <[email protected]> * Check server status before switching Signed-off-by: Jinbo Wang <[email protected]>
1 parent cc40399 commit 20cd68c

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

src/configurationProvider.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
9595
mainClass: "${file}",
9696
};
9797
try {
98+
const isOnStandardMode = await utility.waitForStandardMode();
99+
if (!isOnStandardMode) {
100+
resolve([defaultLaunchConfig]);
101+
return ;
102+
}
103+
98104
const mainClasses = await lsPlugin.resolveMainClass(folder ? folder.uri : undefined);
99105
let cache;
100106
cache = {};
@@ -148,6 +154,11 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
148154

149155
private async resolveAndValidateDebugConfiguration(folder: vscode.WorkspaceFolder | undefined, config: vscode.DebugConfiguration) {
150156
try {
157+
const isOnStandardMode = await utility.waitForStandardMode();
158+
if (!isOnStandardMode) {
159+
return undefined;
160+
}
161+
151162
if (this.isUserSettingsDirty) {
152163
this.isUserSettingsDirty = false;
153164
await updateDebugSettings();

src/debugCodeLensProvider.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { instrumentOperationAsVsCodeCommand } from "vscode-extension-telemetry-w
99
import { JAVA_LANGID } from "./constants";
1010
import { initializeHoverProvider } from "./hoverProvider";
1111
import { IMainMethod, isOnClasspath, resolveMainMethod } from "./languageServerPlugin";
12-
import { getJavaExtensionAPI, isJavaExtEnabled } from "./utility";
12+
import { getJavaExtensionAPI, isJavaExtEnabled, ServerMode } from "./utility";
1313

1414
const JAVA_RUN_CODELENS_COMMAND = "java.debug.runCodeLens";
1515
const JAVA_DEBUG_CODELENS_COMMAND = "java.debug.debugCodeLens";
@@ -19,8 +19,16 @@ const ENABLE_CODE_LENS_VARIABLE = "enableRunDebugCodeLens";
1919
export function initializeCodeLensProvider(context: vscode.ExtensionContext): void {
2020
// delay registering codelens provider until the Java extension is activated.
2121
if (isActivatedByJavaFile() && isJavaExtEnabled()) {
22-
getJavaExtensionAPI().then(() => {
23-
context.subscriptions.push(new DebugCodeLensContainer());
22+
getJavaExtensionAPI().then((api) => {
23+
if (api && (api.serverMode === ServerMode.LIGHTWEIGHT || api.serverMode === ServerMode.HYBRID)) {
24+
api.onDidServerModeChange((mode: string) => {
25+
if (mode === ServerMode.STANDARD) {
26+
context.subscriptions.push(new DebugCodeLensContainer());
27+
}
28+
});
29+
} else {
30+
context.subscriptions.push(new DebugCodeLensContainer());
31+
}
2432
});
2533
} else {
2634
context.subscriptions.push(new DebugCodeLensContainer());

src/extension.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,11 @@ async function applyHCR(hcrStatusBar: NotificationBar) {
204204
async function runJavaFile(uri: vscode.Uri, noDebug: boolean) {
205205
const alreadyActivated: boolean = utility.isJavaExtActivated();
206206
try {
207-
// Wait for Java Language Support extension being activated.
208-
await utility.getJavaExtensionAPI();
207+
// Wait for Java Language Support extension being on Standard mode.
208+
const isOnStandardMode = await utility.waitForStandardMode();
209+
if (!isOnStandardMode) {
210+
return;
211+
}
209212
} catch (ex) {
210213
if (ex instanceof utility.JavaExtensionNotEnabledError) {
211214
utility.guideToInstallJavaExtension();

src/utility.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,51 @@ export function getLauncherScriptPath() {
178178
const ext = vscode.extensions.getExtension(DEBUGGER_EXTENSION_ID);
179179
return path.join(ext.extensionPath, "scripts", "launcher.bat");
180180
}
181+
182+
export enum ServerMode {
183+
STANDARD = "Standard",
184+
LIGHTWEIGHT = "LightWeight",
185+
HYBRID = "Hybrid",
186+
}
187+
188+
/**
189+
* Wait for Java Language Support extension being on Standard mode,
190+
* and return true if the final status is on Standard mode.
191+
*/
192+
export async function waitForStandardMode(): Promise<boolean> {
193+
const api = await getJavaExtensionAPI();
194+
if (api && api.serverMode === ServerMode.LIGHTWEIGHT) {
195+
const answer = await vscode.window.showInformationMessage("Run/Debug feature requires Java language server to run in Standard mode. "
196+
+ "Do you want to switch it to Standard mode now?", "Yes", "Cancel");
197+
if (answer === "Yes") {
198+
return vscode.window.withProgress<boolean>({ location: vscode.ProgressLocation.Window }, async (progress) => {
199+
if (api.serverMode === ServerMode.STANDARD) {
200+
return true;
201+
}
202+
203+
progress.report({ message: "Switching to Standard mode..." });
204+
return new Promise<boolean>((resolve) => {
205+
api.onDidServerModeChange((mode: string) => {
206+
if (mode === ServerMode.STANDARD) {
207+
resolve(true);
208+
}
209+
});
210+
211+
vscode.commands.executeCommand("java.server.mode.switch", ServerMode.STANDARD, true);
212+
});
213+
});
214+
}
215+
216+
return false;
217+
} else if (api && api.serverMode === ServerMode.HYBRID) {
218+
return new Promise<boolean>((resolve) => {
219+
api.onDidServerModeChange((mode: string) => {
220+
if (mode === ServerMode.STANDARD) {
221+
resolve(true);
222+
}
223+
});
224+
});
225+
}
226+
227+
return true;
228+
}

0 commit comments

Comments
 (0)