Skip to content

Commit

Permalink
added xml.restart.language.server command
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Chen committed Jul 20, 2022
1 parent 2032a08 commit 03faf4c
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 35 deletions.
38 changes: 19 additions & 19 deletions package-lock.json

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

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"fs-extra": "^8.1.0",
"glob": "^7.1.4",
"path-exists": "^4.0.0",
"vscode-languageclient": "^7.0.0",
"vscode-languageclient": "^8.0.1",
"yauzl": "^2.10.0"
},
"contributes": {
Expand Down Expand Up @@ -613,6 +613,11 @@
"command": "xml.command.bind.grammar",
"title": "Bind to grammar/schema file",
"category": "XML"
},
{
"command": "xml.restart.language.server",
"title": "Restart Language Server",
"category": "XML"
}
],
"menus": {
Expand Down
26 changes: 18 additions & 8 deletions src/client/clientErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from "fs-extra";
import { commands, ConfigurationTarget, ExtensionContext, window, workspace } from "vscode";
import { CloseAction, ErrorAction, ErrorHandler, Message } from "vscode-languageclient";
import { CloseAction, CloseHandlerResult, ErrorAction, ErrorHandler, ErrorHandlerResult, Message } from "vscode-languageclient";
import { ClientCommandConstants } from "../commands/commandConstants";
import { HEAP_DUMP_LOCATION } from "../server/java/jvmArguments";
import { Telemetry } from "../telemetry";
Expand Down Expand Up @@ -28,11 +28,13 @@ export class ClientErrorHandler implements ErrorHandler {
this.heapDumpFolder = getHeapDumpFolderFromSettings() || context.globalStorageUri.fsPath;
}

error(_error: Error, _message: Message, _count: number): ErrorAction {
return ErrorAction.Continue;
error(_error: Error, _message: Message, _count: number): ErrorHandlerResult {
return {
action: ErrorAction.Continue
}
}

closed(): CloseAction {
closed(): CloseHandlerResult{
this.restarts.push(Date.now());
const heapProfileGlob = new glob.GlobSync(`${this.heapDumpFolder}/java_*.hprof`);
if (heapProfileGlob.found.length) {
Expand All @@ -44,18 +46,26 @@ export class ClientErrorHandler implements ErrorHandler {
cleanUpHeapDumps(this.context);
Telemetry.sendTelemetry(Telemetry.JAVA_OOM_EVT, { 'jvm.xmx': getXmxFromSettings() });
showOOMMessage();
return CloseAction.DoNotRestart;
return {
action: CloseAction.DoNotRestart
}
}
if (this.restarts.length < 5) {
return CloseAction.Restart;
return {
action: CloseAction.Restart
}
} else {
const diff = this.restarts[this.restarts.length - 1] - this.restarts[0];
if (diff <= 10 * 60 * 1000) {
window.showErrorMessage(`The ${this.name} language server crashed 5 times in the last 10 minutes. The server will not be restarted.`);
return CloseAction.DoNotRestart;
return {
action: CloseAction.DoNotRestart
}
}
this.restarts.shift();
return CloseAction.Restart;
return {
action: CloseAction.Restart
}
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/client/xmlClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ export async function startLanguageClient(context: ExtensionContext, executable:
return Telemetry.sendTelemetry(e.name, e.properties);
});

context.subscriptions.push(languageClient.start());
await languageClient.onReady();
await languageClient.start();

// ---

Expand Down Expand Up @@ -145,8 +144,7 @@ function getLanguageClientOptions(
},
actionableNotificationSupport: true,
openSettingsCommandSupport: true,
bindingWizardSupport: true,
shouldLanguageServerExitOnShutdown: true
bindingWizardSupport: true
}
},
errorHandler: new ClientErrorHandler('XML', context),
Expand All @@ -157,8 +155,9 @@ function getLanguageClientOptions(
middleware: {
workspace: {
didChangeConfiguration: () => {
languageClient.sendNotification(DidChangeConfigurationNotification.type, { settings: getXMLSettings(requirementsData.java_home, logfile, externalXmlSettings) });
const result = languageClient.sendNotification(DidChangeConfigurationNotification.type, { settings: getXMLSettings(requirementsData.java_home, logfile, externalXmlSettings) });
onConfigurationChange();
return result;
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/commands/commandConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ export namespace ClientCommandConstants {
* Client command to execute an XML command on XML Language Server side.
*/
export const EXECUTE_WORKSPACE_COMMAND = 'xml.workspace.executeCommand';

/**
* Command to restart connection to language server.
*/
export const RESTART_LANGUAGE_SERVER = 'xml.restart.language.server';
}

/**
Expand Down
17 changes: 15 additions & 2 deletions src/commands/registerCommands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from 'path';
import { commands, ExtensionContext, OpenDialogOptions, Position, QuickPickItem, Uri, window, workspace, WorkspaceEdit, Disposable, ConfigurationTarget, TextDocument, WorkspaceFolder } from "vscode";
import { commands, ExtensionContext, OpenDialogOptions, Position, QuickPickItem, Uri, window, workspace, WorkspaceEdit, ConfigurationTarget, TextDocument } from "vscode";
import { CancellationToken, ExecuteCommandParams, ExecuteCommandRequest, ReferencesRequest, TextDocumentIdentifier, TextDocumentEdit } from "vscode-languageclient";
import { LanguageClient } from 'vscode-languageclient/node';
import { markdownPreviewProvider } from "../markdownPreviewProvider";
Expand Down Expand Up @@ -28,6 +28,7 @@ export async function registerClientServerCommands(context: ExtensionContext, la
registerCodeLensReferencesCommands(context, languageClient);
registerValidationCommands(context);
registerAssociationCommands(context, languageClient);
registerRestartLanguageServerCommand(context, languageClient);

// Register client command to execute custom XML Language Server command
context.subscriptions.push(commands.registerCommand(ClientCommandConstants.EXECUTE_WORKSPACE_COMMAND, (command, ...rest) => {
Expand Down Expand Up @@ -322,4 +323,16 @@ async function checkCanBindGrammar(documentURI: Uri) {
}

return result
}
}

/**
* Register command to restart the connection to the language server
*
* @param context the extension context
* @param languageClient the language client
*/
function registerRestartLanguageServerCommand(context: ExtensionContext, languageClient: LanguageClient) {
context.subscriptions.push(commands.registerCommand(ClientCommandConstants.RESTART_LANGUAGE_SERVER, async () => {
languageClient.restart();
}));
}
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ export async function activate(context: ExtensionContext): Promise<XMLExtensionA
export async function deactivate(): Promise<void> {
if (languageClient) {
await languageClient.stop();
languageClient = undefined;
}
}

0 comments on commit 03faf4c

Please sign in to comment.