Skip to content

Commit

Permalink
Added restart java language server command
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Chen <[email protected]>

Signed-off-by: Alexander Chen <[email protected]>
  • Loading branch information
Alexander Chen committed Jul 21, 2022
1 parent ce9c5a7 commit 5d41d75
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,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
60 changes: 42 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,11 @@
"command": "java.action.changeBaseType",
"title": "%java.action.changeBaseType%",
"category": "Java"
},
{
"command": "java.language.server.restart",
"title": "%java.language.server.restart%",
"category": "Java"
}
],
"keybindings": [
Expand Down
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
"java.action.showClassHierarchy": "Show Class Hierarchy",
"java.action.showSupertypeHierarchy": "Show Supertype Hierarchy",
"java.action.showSubtypeHierarchy": "Show Subtype Hierarchy",
"java.action.changeBaseType": "Base on this Type"
"java.action.changeBaseType": "Base on this Type",
"java.language.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 @@ -259,6 +259,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 client connection to language server.
*/
export const RESTART_LANGUAGE_SERVER = 'java.language.server.restart';

export const LEARN_MORE_ABOUT_REFACTORING = '_java.learnMoreAboutRefactorings';

Expand Down
33 changes: 28 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
context.subscriptions.push(commands.registerCommand(Commands.NOT_COVERED_EXECUTION, async () => {
markdownPreviewProvider.show(context.asAbsolutePath(path.join('document', `_java.notCoveredExecution.md`)), 'Not Covered Maven Plugin Execution', "", context);
}));
registerRestartJavaLanguageServerCommand(context);

storagePath = context.storagePath;
context.subscriptions.push(commands.registerCommand(Commands.MEATDATA_FILES_GENERATION, async () => {
Expand Down Expand Up @@ -271,7 +272,7 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
range: client.code2ProtocolConverter.asRange(range),
context: client.code2ProtocolConverter.asCodeActionContext(context)
};
const showAt = getJavaConfiguration().get<string>("quickfix.showAt");
const showAt = getJavaConfiguration().get<string>("quickfix.showAt");
if (showAt === 'line' && range.start.line === range.end.line && range.start.character === range.end.character) {
const textLine = document.lineAt(params.range.start.line);
if (textLine !== null) {
Expand Down Expand Up @@ -508,7 +509,7 @@ async function workspaceContainsBuildFiles(): Promise<boolean> {
const inclusionPatterns: string[] = getBuildFilePatterns();
const inclusionPatternsFromNegatedExclusion: string[] = getInclusionPatternsFromNegatedExclusion();
if (inclusionPatterns.length > 0 && inclusionPatternsFromNegatedExclusion.length > 0 &&
(await workspace.findFiles(convertToGlob(inclusionPatterns, inclusionPatternsFromNegatedExclusion), null, 1 /*maxResults*/)).length > 0) {
(await workspace.findFiles(convertToGlob(inclusionPatterns, inclusionPatternsFromNegatedExclusion), null, 1 /*maxResults*/)).length > 0) {
return true;
}

Expand Down Expand Up @@ -545,7 +546,7 @@ async function ensureNoBuildToolConflicts(context: ExtensionContext, clientOptio
clientOptions.initializationOptions.settings.java.import.maven.enabled = false;
context.workspaceState.update(ACTIVE_BUILD_TOOL_STATE, "gradle");
} else {
throw new Error ("Unknown build tool: " + activeBuildTool); // unreachable
throw new Error("Unknown build tool: " + activeBuildTool); // unreachable
}
}

Expand Down Expand Up @@ -801,7 +802,7 @@ function openLogFile(logFile, openingFailureWarning: string, column: ViewColumn
if (!doc) {
return false;
}
return window.showTextDocument(doc, {viewColumn: column, preview: false})
return window.showTextDocument(doc, { viewColumn: column, preview: false })
.then(editor => !!editor);
}, () => false)
.then(didOpen => {
Expand Down Expand Up @@ -1048,7 +1049,7 @@ async function cleanJavaWorkspaceStorage() {
}
}
});
}
}
}

function registerOutOfMemoryDetection(storagePath: string) {
Expand All @@ -1065,3 +1066,25 @@ function registerOutOfMemoryDetection(storagePath: string) {
showOOMMessage();
});
}

function registerRestartJavaLanguageServerCommand(context: ExtensionContext) {
context.subscriptions.push(commands.registerCommand(Commands.RESTART_LANGUAGE_SERVER, async () => {
// Can be replaced with `await languageClient.restart()` with vscode-languageclient ^8.0.1,
switch (getJavaServerMode()) {
case (ServerMode.STANDARD):
// Standard server restart
await standardClient.restart();
break;
case (ServerMode.LIGHTWEIGHT):
// Syntax server restart
await syntaxClient.restart();
break;
case (ServerMode.HYBRID):
if (syntaxClient.isAlive()) {
await syntaxClient.restart();
}
await standardClient.restart();
break;
}
}));
}
2 changes: 1 addition & 1 deletion src/javaServerStarter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { workspace, ExtensionContext, window } from 'vscode';
import { addLombokParam, isLombokSupportEnabled } from './lombokSupport';

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
8 changes: 8 additions & 0 deletions src/standardLanguageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,14 @@ export class StandardLanguageClient {
return Promise.resolve();
}

public async restart(): Promise<void> {
await this.languageClient.stop();
if (this.languageClient.isInDebugMode) {
await new Promise((resolve) => setTimeout(resolve, 2000))
}
this.languageClient.start()
}

public getClient(): LanguageClient {
return this.languageClient;
}
Expand Down
14 changes: 12 additions & 2 deletions src/syntaxLanguageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,28 @@ export class SyntaxLanguageClient {
}
}

public stop(): Promise<void> {
public stop(cleanup: boolean = true): Promise<void> {
this.status = ClientStatus.Stopping;
if (this.languageClient) {
try {
return this.languageClient.stop();
} finally {
this.languageClient = null;
if (cleanup) {
this.languageClient = null;
}
}
}
return Promise.resolve();
}

public async restart(): Promise<void> {
await this.languageClient.stop();
if (this.languageClient.isInDebugMode) {
await new Promise((resolve) => setTimeout(resolve, 2000))
}
this.languageClient.start()
}

public isAlive(): boolean {
return !!this.languageClient && this.status !== ClientStatus.Stopping;
}
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 @@ -23,6 +23,7 @@ suite('Java Language Extension - LightWeight', () => {
Commands.OPEN_FORMATTER,
Commands.CLEAN_WORKSPACE,
Commands.SWITCH_SERVER_MODE,
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 @@ -63,6 +63,7 @@ suite('Java Language Extension - Standard', () => {
Commands.BUILD_PROJECT,
Commands.REMOVE_FROM_SOURCEPATH_CMD,
Commands.RENAME_COMMAND,
Commands.RESTART_LANGUAGE_SERVER,
Commands.SHOW_JAVA_IMPLEMENTATIONS,
Commands.SHOW_JAVA_REFERENCES,
Commands.SHOW_SERVER_TASK_STATUS,
Expand Down

0 comments on commit 5d41d75

Please sign in to comment.