Skip to content

Commit

Permalink
Added restart Java language server command.
Browse files Browse the repository at this point in the history
- Show progress for restarting the language server

Signed-off-by: AlexXuChen <[email protected]>
Co-authored-by: Roland Grunberg <[email protected]>
Co-authored-by: Jessica He <[email protected]>
  • Loading branch information
3 people committed Apr 27, 2023
1 parent 210ea42 commit ae82808
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ The following commands are available:
- `Java: List All Java Source Paths`: lists all the Java source paths recognized by the Java Language Server workspace.
- `Java: Show Build Job Status`: shows the Java Language Server job status in Visual Studio Code terminal.
- `Java: Go to Super Implementation`: goes to the super implementation for the current selected symbol in editor.
- `Java: Restart Java Language Server`: restarts the Java language server.

Supported VS Code settings
==========================
Expand Down
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,11 @@
"command": "java.clean.sharedIndexes",
"title": "%java.clean.sharedIndexes%",
"category": "Java"
},
{
"command": "java.server.restart",
"title": "%java.server.restart%",
"category": "Java"
}
],
"keybindings": [
Expand Down Expand Up @@ -1381,6 +1386,10 @@
{
"command": "java.server.mode.switch",
"when": "java:serverMode == LightWeight"
},
{
"command": "java.server.restart",
"when": "javaLSReady"
}
],
"view/title": [
Expand Down
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"java.action.showSubtypeHierarchy": "Show Subtype Hierarchy",
"java.action.changeBaseType": "Base on this Type",
"java.project.createModuleInfo.command": "Create module-info.java",
"java.clean.sharedIndexes": "Clean Shared Indexes"
"java.clean.sharedIndexes": "Clean Shared Indexes",
"java.server.restart": "Restart Java Language Server"
}
4 changes: 4 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ export namespace Commands {
* Command to switch between standard mode and lightweight mode.
*/
export const SWITCH_SERVER_MODE = 'java.server.mode.switch';
/**
* Command to restart the language server.
*/
export const RESTART_LANGUAGE_SERVER = 'java.server.restart';

export const LEARN_MORE_ABOUT_REFACTORING = '_java.learnMoreAboutRefactorings';

Expand Down
25 changes: 24 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as fs from 'fs';
import * as fse from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import { CodeActionContext, commands, ConfigurationTarget, Diagnostic, env, EventEmitter, ExtensionContext, extensions, IndentAction, InputBoxOptions, languages, RelativePattern, TextDocument, UIKind, Uri, ViewColumn, window, workspace, WorkspaceConfiguration } from 'vscode';
import { CodeActionContext, commands, ConfigurationTarget, Diagnostic, env, EventEmitter, ExtensionContext, extensions, IndentAction, InputBoxOptions, languages, RelativePattern, TextDocument, UIKind, Uri, ViewColumn, window, workspace, WorkspaceConfiguration, ProgressLocation } from 'vscode';
import { CancellationToken, CodeActionParams, CodeActionRequest, Command, DidChangeConfigurationNotification, ExecuteCommandParams, ExecuteCommandRequest, LanguageClientOptions, RevealOutputChannelOn } from 'vscode-languageclient';
import { LanguageClient } from 'vscode-languageclient/node';
import { apiManager } from './apiManager';
Expand Down Expand Up @@ -329,6 +329,8 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {

context.subscriptions.push(onConfigurationChange(workspacePath, context));

registerRestartJavaLanguageServerCommand(context);

/**
* Command to switch the server mode. Currently it only supports switch from lightweight to standard.
* @param force force to switch server mode without asking
Expand Down Expand Up @@ -965,3 +967,24 @@ function registerOutOfMemoryDetection(storagePath: string) {
showOOMMessage();
});
}

function registerRestartJavaLanguageServerCommand(context: ExtensionContext) {
context.subscriptions.push(commands.registerCommand(Commands.RESTART_LANGUAGE_SERVER, async () => {
switch (getJavaServerMode()) {
case (ServerMode.standard):
// Standard server restart
await standardClient.getClient().restart();
break;
case (ServerMode.lightWeight):
// Syntax server restart
await syntaxClient.getClient().restart();
break;
case (ServerMode.hybrid):
if (syntaxClient.isAlive()) {
await syntaxClient.getClient().restart();
}
await standardClient.getClient().restart();
break;
}
}));
}
2 changes: 1 addition & 1 deletion src/javaServerStarter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { deleteDirectory, ensureExists, getJavaConfiguration, getTimestamp } fro

// eslint-disable-next-line no-var
declare var v8debug;
const DEBUG = (typeof v8debug === 'object') || startedInDebugMode();
export const DEBUG = (typeof v8debug === 'object') || startedInDebugMode();

/**
* Argument that tells the program where to generate the heap dump that is created when an OutOfMemoryError is raised and `HEAP_DUMP` has been passed
Expand Down
4 changes: 2 additions & 2 deletions src/standardLanguageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Commands } from "./commands";
import { ClientStatus } from "./extension.api";
import * as fileEventHandler from './fileEventHandler';
import { gradleCodeActionMetadata, GradleCodeActionProvider } from "./gradle/gradleCodeActionProvider";
import { awaitServerConnection, prepareExecutable } from "./javaServerStarter";
import { awaitServerConnection, prepareExecutable, DEBUG } from "./javaServerStarter";
import { logger } from "./log";
import { checkLombokDependency } from "./lombokSupport";
import { markdownPreviewProvider } from "./markdownPreviewProvider";
Expand Down Expand Up @@ -106,7 +106,7 @@ export class StandardLanguageClient {
}

// Create the language client and start the client.
this.languageClient = new TracingLanguageClient('java', extensionName, serverOptions, clientOptions);
this.languageClient = new TracingLanguageClient('java', extensionName, serverOptions, clientOptions, DEBUG);

this.registerCommandsForStandardServer(context, jdtEventEmitter);
fileEventHandler.registerFileEventHandlers(this.languageClient, context);
Expand Down
3 changes: 2 additions & 1 deletion src/syntaxLanguageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { RequirementsData } from "./requirements";
import { ServerMode } from "./settings";
import { snippetCompletionProvider } from "./snippetCompletionProvider";
import { getJavaConfig } from "./utils";
import { DEBUG } from "./javaServerStarter";

const extensionName = "Language Support for Java (Syntax Server)";

Expand Down Expand Up @@ -55,7 +56,7 @@ export class SyntaxLanguageClient {
}

if (serverOptions) {
this.languageClient = new LanguageClient('java', extensionName, serverOptions, newClientOptions);
this.languageClient = new LanguageClient('java', extensionName, serverOptions, newClientOptions, DEBUG);
}

this.status = ClientStatus.initialized;
Expand Down
1 change: 1 addition & 0 deletions test/lightweight-mode-suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ suite('Java Language Extension - LightWeight', () => {
Commands.SWITCH_SERVER_MODE,
Commands.OPEN_FILE,
Commands.CLEAN_SHARED_INDEXES,
Commands.RESTART_LANGUAGE_SERVER,
].sort();
const foundJavaCommands = commands.filter((value) => {
return JAVA_COMMANDS.indexOf(value)>=0 || value.startsWith('java.');
Expand Down
1 change: 1 addition & 0 deletions test/standard-mode-suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ suite('Java Language Extension - Standard', () => {
Commands.RESOLVE_TYPE_HIERARCHY,
Commands.RESOLVE_WORKSPACE_SYMBOL,
Commands.RUNTIME_VALIDATION_OPEN,
Commands.RESTART_LANGUAGE_SERVER,
Commands.SHOW_JAVA_IMPLEMENTATIONS,
Commands.SHOW_JAVA_REFERENCES,
Commands.SHOW_SERVER_TASK_STATUS,
Expand Down

0 comments on commit ae82808

Please sign in to comment.