Skip to content

Commit

Permalink
LanguageClient allows to specify MessageTransport directly
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisalmen committed Sep 5, 2024
1 parent ce32c0f commit 62b8561
Show file tree
Hide file tree
Showing 20 changed files with 251 additions and 319 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ lib
node_modules
*.tsbuildinfo
.angular
__screenshots__
16 changes: 6 additions & 10 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,22 @@

import { BaseLanguageClient, MessageTransports, LanguageClientOptions } from 'vscode-languageclient/browser.js';

export interface IConnectionProvider {
get(encoding: string): Promise<MessageTransports>;
}

export type MonacoLanguageClientOptions = {
name: string;
id?: string;
clientOptions: LanguageClientOptions;
connectionProvider: IConnectionProvider;
messageTransports: MessageTransports;
}

export class MonacoLanguageClient extends BaseLanguageClient {
protected readonly connectionProvider: IConnectionProvider;
protected readonly messageTransports: MessageTransports;

constructor({ id, name, clientOptions, connectionProvider }: MonacoLanguageClientOptions) {
constructor({ id, name, clientOptions, messageTransports }: MonacoLanguageClientOptions) {
super(id ?? name.toLowerCase(), name, clientOptions);
this.connectionProvider = connectionProvider;
this.messageTransports = messageTransports;
}

protected override createMessageTransports(encoding: string): Promise<MessageTransports> {
return this.connectionProvider.get(encoding);
protected override createMessageTransports(_encoding: string): Promise<MessageTransports> {
return Promise.resolve(this.messageTransports);
}
}
2 changes: 1 addition & 1 deletion packages/client/src/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type LanguageClientRestartOptions = {

export type LanguageClientConfigType = 'WebSocket' | 'WebSocketUrl' | 'WebSocketDirect' | 'WorkerConfig' | 'Worker';

export type LanguageClientConfigOptions = (WebSocketConfigOptionsDirect | WebSocketConfigOptionsParams | WebSocketConfigOptionsUrl | WorkerConfigOptions | WorkerConfigDirect) & {
export type ConnetionConfigOptions = (WebSocketConfigOptionsDirect | WebSocketConfigOptionsParams | WebSocketConfigOptionsUrl | WorkerConfigOptions | WorkerConfigDirect) & {
restartOptions?: LanguageClientRestartOptions;
}

Expand Down
6 changes: 2 additions & 4 deletions packages/examples/src/bare/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const initWebSocketAndStartClient = (url: string): WebSocket => {
return webSocket;
};

export const createLanguageClient = (transports: MessageTransports): MonacoLanguageClient => {
export const createLanguageClient = (messageTransports: MessageTransports): MonacoLanguageClient => {
return new MonacoLanguageClient({
name: 'Sample Language Client',
clientOptions: {
Expand All @@ -86,8 +86,6 @@ export const createLanguageClient = (transports: MessageTransports): MonacoLangu
}
},
// create a language client connection from the JSON RPC connection on demand
connectionProvider: {
get: async () => (transports)
}
messageTransports
});
};
10 changes: 6 additions & 4 deletions packages/examples/src/eclipse.jdt.ls/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ export const runEclipseJdtLsClient = () => {
languageClientConfigs: {
java: {
languageId: 'java',
options: {
$type: 'WebSocketUrl',
url: 'ws://localhost:30003/jdtls'
connection: {
configOptions: {
$type: 'WebSocketUrl',
url: 'ws://localhost:30003/jdtls'
}
},
clientOptions: {
languageClientOptions: {
documentSelector: ['java'],
workspaceFolder: {
index: 0,
Expand Down
8 changes: 5 additions & 3 deletions packages/examples/src/groovy/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ const userConfig: UserConfig = {
languageClientConfigs: {
groovy: {
languageId: 'groovy',
options: {
$type: 'WebSocketUrl',
url: `ws://localhost:${groovyConfig.port}${groovyConfig.path}`
connection: {
configOptions: {
$type: 'WebSocketUrl',
url: `ws://localhost:${groovyConfig.port}${groovyConfig.path}`
}
}
}
}
Expand Down
28 changes: 15 additions & 13 deletions packages/examples/src/json/client/wrapperWs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,22 @@ export const jsonClientUserConfig: UserConfig = {
languageClientConfigs: {
json: {
languageId: 'json',
options: {
$type: 'WebSocketUrl',
url: 'ws://localhost:30000/sampleServer',
startOptions: {
onCall: () => {
console.log('Connected to socket.');
connection: {
configOptions: {
$type: 'WebSocketUrl',
url: 'ws://localhost:30000/sampleServer',
startOptions: {
onCall: () => {
console.log('Connected to socket.');
},
reportStatus: true
},
reportStatus: true
},
stopOptions: {
onCall: () => {
console.log('Disconnected from socket.');
},
reportStatus: true
stopOptions: {
onCall: () => {
console.log('Disconnected from socket.');
},
reportStatus: true
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ export const setupLangiumClientClassic = async (): Promise<UserConfig> => {
languageClientConfigs: {
langium: {
languageId: 'langium',
options: {
$type: 'WorkerDirect',
worker: langiumWorker
connection: {
configOptions: {
$type: 'WorkerDirect',
worker: langiumWorker
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ export const setupLangiumClientExtended = async (): Promise<UserConfig> => {
languageClientConfigs: {
langium: {
languageId: 'langium',
options: {
$type: 'WorkerDirect',
worker: langiumWorker
},
connectionProvider: {
get: async () => ({ reader, writer })
connection: {
configOptions: {
$type: 'WorkerDirect',
worker: langiumWorker
},
messageTransports: { reader, writer }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
import getKeybindingsServiceOverride from '@codingame/monaco-vscode-keybindings-service-override';
import getLifecycleServiceOverride from '@codingame/monaco-vscode-lifecycle-service-override';
import getLocalizationServiceOverride from '@codingame/monaco-vscode-localization-service-override';
import { IConnectionProvider } from 'monaco-languageclient';
import { createDefaultLocaleConfiguration } from 'monaco-languageclient/vscode/services';
import { LanguageClientConfig, UserConfig } from 'monaco-editor-wrapper';
// cannot be imported with assert as json contains comments
import statemachineLanguageConfig from './language-configuration.json?raw';
import responseStatemachineTm from '../syntaxes/statemachine.tmLanguage.json?raw';
import { MessageTransports } from 'vscode-languageclient';

export const createLangiumGlobalConfig = async (params: {
languageServerId: string,
useLanguageClient: boolean,
text?: string,
worker?: Worker,
messagePort?: MessagePort,
connectionProvider?: IConnectionProvider
messageTransports?: MessageTransports
}): Promise<UserConfig> => {
const extensionFilesOrContents = new Map<string, string | URL>();
extensionFilesOrContents.set(`/${params.languageServerId}-statemachine-configuration.json`, statemachineLanguageConfig);
Expand All @@ -36,12 +36,14 @@ export const createLangiumGlobalConfig = async (params: {
const languageClientConfigs: Record<string, LanguageClientConfig> | undefined = params.useLanguageClient && params.worker ? {
statemachine: {
languageId: 'statemachine',
options: {
$type: 'WorkerDirect',
worker: params.worker,
messagePort: params.messagePort,
},
connectionProvider: params.connectionProvider
connection: {
configOptions: {
$type: 'WorkerDirect',
worker: params.worker,
messagePort: params.messagePort,
},
messageTransports: params.messageTransports
}
}
} : undefined;

Expand Down
4 changes: 1 addition & 3 deletions packages/examples/src/langium/statemachine/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ const startEditor = async () => {
useLanguageClient: true,
worker: stateMachineWorkerPort,
messagePort: channel.port1,
connectionProvider: {
get: async () => ({ reader, writer })
}
messageTransports: { reader, writer }
});
await wrapper.initAndStart(langiumGlobalConfig, document.getElementById('monaco-editor-root'));

Expand Down
36 changes: 18 additions & 18 deletions packages/examples/src/python/client/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,32 @@ export const createUserConfig = (workspaceRoot: string, code: string, codeUri: s
python: {
languageId: 'python',
name: 'Python Language Server Example',
options: {
$type: 'WebSocketDirect',
webSocket: webSocket,
startOptions: {
onCall: (languageClient?: MonacoLanguageClient) => {
setTimeout(() => {
['pyright.restartserver', 'pyright.organizeimports'].forEach((cmdName) => {
vscode.commands.registerCommand(cmdName, (...args: unknown[]) => {
languageClient?.sendRequest('workspace/executeCommand', { command: cmdName, arguments: args });
connection: {
configOptions: {
$type: 'WebSocketDirect',
webSocket: webSocket,
startOptions: {
onCall: (languageClient?: MonacoLanguageClient) => {
setTimeout(() => {
['pyright.restartserver', 'pyright.organizeimports'].forEach((cmdName) => {
vscode.commands.registerCommand(cmdName, (...args: unknown[]) => {
languageClient?.sendRequest('workspace/executeCommand', { command: cmdName, arguments: args });
});
});
});
}, 250);
},
reportStatus: true,
}
}, 250);
},
reportStatus: true,
}
},
messageTransports: { reader, writer }
},
clientOptions: {
languageClientOptions: {
documentSelector: ['python'],
workspaceFolder: {
index: 0,
name: 'workspace',
uri: vscode.Uri.parse(workspaceRoot)
},
},
connectionProvider: {
get: async () => ({ reader, writer })
}
}
},
Expand Down
56 changes: 30 additions & 26 deletions packages/examples/src/ts/wrapperAdvanced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,40 +56,44 @@ export const runMultipleLanguageClientsExample = async () => {
json: {
languageId: 'json',
name: 'JSON Client',
options: {
$type: 'WebSocketParams',
host: 'localhost',
port: 30000,
path: 'sampleServer',
secured: false
connection: {
configOptions: {
$type: 'WebSocketParams',
host: 'localhost',
port: 30000,
path: 'sampleServer',
secured: false
}
}
},
python: {
languageId: 'python',
name: 'Python Client',
options: {
$type: 'WebSocketParams',
host: 'localhost',
port: 30001,
path: 'pyright',
secured: false,
extraParams: {
authorization: 'UserAuth'
},
startOptions: {
onCall: (languageClient?: MonacoLanguageClient) => {
setTimeout(() => {
['pyright.restartserver', 'pyright.organizeimports'].forEach((cmdName) => {
vscode.commands.registerCommand(cmdName, (...args: unknown[]) => {
languageClient?.sendRequest('workspace/executeCommand', { command: cmdName, arguments: args });
});
});
}, 250);
connection: {
configOptions: {
$type: 'WebSocketParams',
host: 'localhost',
port: 30001,
path: 'pyright',
secured: false,
extraParams: {
authorization: 'UserAuth'
},
reportStatus: true,
startOptions: {
onCall: (languageClient?: MonacoLanguageClient) => {
setTimeout(() => {
['pyright.restartserver', 'pyright.organizeimports'].forEach((cmdName) => {
vscode.commands.registerCommand(cmdName, (...args: unknown[]) => {
languageClient?.sendRequest('workspace/executeCommand', { command: cmdName, arguments: args });
});
});
}, 250);
},
reportStatus: true,
}
}
},
clientOptions: {
languageClientOptions: {
documentSelector: ['python'],
workspaceFolder: {
index: 0,
Expand Down
Loading

0 comments on commit 62b8561

Please sign in to comment.