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]>
  • Loading branch information
Alexander Chen committed Jul 21, 2022
1 parent ce9c5a7 commit 4619154
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 26 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
51 changes: 45 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { workspace, extensions, ExtensionContext, window, commands, ViewColumn,
import { ExecuteCommandParams, ExecuteCommandRequest, LanguageClientOptions, RevealOutputChannelOn, ErrorHandler, Message, ErrorAction, CloseAction, DidChangeConfigurationNotification, CancellationToken, CodeActionRequest, CodeActionParams, Command } from 'vscode-languageclient';
import { LanguageClient } from 'vscode-languageclient/node';
import { collectJavaExtensions, isContributedPartUpdated } from './plugin';
import { HEAP_DUMP_LOCATION, prepareExecutable } from './javaServerStarter';
import { DEBUG, HEAP_DUMP_LOCATION, prepareExecutable } from './javaServerStarter';
import * as requirements from './requirements';
import { initialize as initializeRecommendation } from './recommendation';
import { Commands } from './commands';
Expand Down 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,41 @@ 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.stop();
if (DEBUG) {
await new Promise((resolve) => setTimeout(resolve, 2000));
}
standardClient.start();
break;
case (ServerMode.LIGHTWEIGHT):
// Syntax server restart
await syntaxClient.stop();
if (DEBUG) {
await new Promise((resolve) => setTimeout(resolve, 2000));
}
syntaxClient.start();
break;
case (ServerMode.HYBRID):
if (syntaxClient.isAlive()) {
await syntaxClient.stop();
if (DEBUG) {
await new Promise((resolve) => setTimeout(resolve, 2000));
}
syntaxClient.start();
}
await standardClient.stop();
if (DEBUG) {
await new Promise((resolve) => setTimeout(resolve, 2000));
}
standardClient.start();
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

0 comments on commit 4619154

Please sign in to comment.