Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

monaco-languageclient config improvement, wrapper+languageclientwrapper improvements #741

Merged
merged 7 commits into from
Sep 23, 2024
42 changes: 18 additions & 24 deletions packages/client/src/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,56 @@

import { MonacoLanguageClient } from './client.js';

export type WebSocketCallOptions = {
export type ConnetionConfigOptions = WebSocketConfigOptionsDirect | WebSocketConfigOptionsParams | WebSocketConfigOptionsUrl | WorkerConfigOptionsParams | WorkerConfigOptionsDirect;

export interface WebSocketCallOptions {
/** Adds handle on languageClient */
onCall: (languageClient?: MonacoLanguageClient) => void;
/** Reports Status Of Language Client */
reportStatus?: boolean;
}

export type LanguageClientRestartOptions = {
retries: number;
timeout: number;
keepWorker?: boolean;
export interface WebSocketConfigOptionsDirect {
$type: 'WebSocketDirect'
webSocket: WebSocket
startOptions?: WebSocketCallOptions;
stopOptions?: WebSocketCallOptions;
}

export type ConnetionConfigOptions = (WebSocketConfigOptionsDirect | WebSocketConfigOptionsParams | WebSocketConfigOptionsUrl | WorkerConfigOptionsParams | WorkerConfigOptionsDirect);

export type WebSocketUrlParams = {
export interface WebSocketUrlParams {
secured: boolean;
host: string;
port?: number;
path?: string;
extraParams?: Record<string, string | number | Array<string | number>>;
}

export type WebSocketUrlString = {
url: string;
}

export type WebSocketConfigOptionsDirect = {
$type: 'WebSocketDirect'
webSocket: WebSocket
export interface WebSocketConfigOptionsParams extends WebSocketUrlParams {
$type: 'WebSocketParams'
startOptions?: WebSocketCallOptions;
stopOptions?: WebSocketCallOptions;
}
kaisalmen marked this conversation as resolved.
Show resolved Hide resolved

export type WebSocketConfigOptionsParams = WebSocketUrlParams & {
$type: 'WebSocketParams'
startOptions?: WebSocketCallOptions;
stopOptions?: WebSocketCallOptions;
export interface WebSocketUrlString {
url: string;
}

export type WebSocketConfigOptionsUrl = WebSocketUrlString & {
export interface WebSocketConfigOptionsUrl extends WebSocketUrlString {
$type: 'WebSocketUrl'
startOptions?: WebSocketCallOptions;
stopOptions?: WebSocketCallOptions;
}

export type WorkerConfigOptionsParams = {
export interface WorkerConfigOptionsParams {
$type: 'WorkerConfig'
url: URL;
type: 'classic' | 'module';
messagePort?: MessagePort;
workerName?: string;
};
}

export type WorkerConfigOptionsDirect = {
export interface WorkerConfigOptionsDirect {
$type: 'WorkerDirect';
worker: Worker;
messagePort?: MessagePort;
};
}
11 changes: 8 additions & 3 deletions packages/wrapper/src/languageClientWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See LICENSE in the package root for license information.
* ------------------------------------------------------------------------------------------ */

import { MonacoLanguageClient, LanguageClientRestartOptions, ConnetionConfigOptions, WorkerConfigOptionsDirect, WorkerConfigOptionsParams } from 'monaco-languageclient';
import { MonacoLanguageClient, ConnetionConfigOptions, WorkerConfigOptionsDirect, WorkerConfigOptionsParams } from 'monaco-languageclient';
import { Logger } from 'monaco-languageclient/tools';
import { BrowserMessageReader, BrowserMessageWriter } from 'vscode-languageserver-protocol/browser.js';
import { CloseAction, ErrorAction, LanguageClientOptions, MessageTransports, State } from 'vscode-languageclient/browser.js';
Expand All @@ -23,10 +23,16 @@ export type LanguageClientConfig = {
restartOptions?: LanguageClientRestartOptions;
}

export type LanguageClientRestartOptions = {
retries: number;
timeout: number;
keepWorker?: boolean;
}

kaisalmen marked this conversation as resolved.
Show resolved Hide resolved
export type LanguageClientError = {
message: string;
error: Error | string;
};
}

export class LanguageClientWrapper {

Expand Down Expand Up @@ -70,7 +76,6 @@ export class LanguageClientWrapper {
return Promise.resolve();
}

// eslint-disable-next-line no-async-promise-executor
return new Promise((resolve, reject) => {
const conConfig = this.languageClientConfig.connection;
const conOptions = conConfig.options;
Expand Down
20 changes: 9 additions & 11 deletions packages/wrapper/src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ export class MonacoEditorLanguageClientWrapper {
logger: this.logger
});

const lccs = wrapperConfig.languageClientConfigs;
if (lccs !== undefined && Object.entries(lccs).length > 0) {

for (const [languageId, lcc] of Object.entries(lccs)) {
const lccEntries = Object.entries(wrapperConfig.languageClientConfigs ?? {});
if (lccEntries.length > 0) {
for (const [languageId, lcc] of lccEntries) {
const lcw = new LanguageClientWrapper({
languageClientConfig: lcc,
logger: this.logger
Expand Down Expand Up @@ -217,24 +216,23 @@ export class MonacoEditorLanguageClientWrapper {
/**
* Disposes all application and editor resources, plus the languageclient (if used).
*/
async dispose(disposeLanguageClients: boolean = true): Promise<void> {
async dispose(disposeLanguageClients: boolean = true) {
this.markStopping();

this.editorApp?.disposeApp();
this.editorApp = undefined;

if (disposeLanguageClients) {
const allPromises: Array<Promise<void>> = [];
for (const lcw of this.languageClientWrappers.values()) {
if (lcw.haveLanguageClient()) {
await lcw.disposeLanguageClient(false);
allPromises.push(lcw.disposeLanguageClient(false));
}
this.editorApp = undefined;
await Promise.resolve('Monaco editor and languageclient completed disposed.');
}
} else {
await Promise.resolve('Monaco editor has been disposed.');
await Promise.all(allPromises);
}
this.initDone = false;

this.initDone = false;
this.markStopped();
}

Expand Down