Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
Allow to fully override monaco-languageclient clientOptions and conne…
Browse files Browse the repository at this point in the history
…ctionProvider
  • Loading branch information
kaisalmen committed Jan 4, 2024
1 parent e0e6e43 commit 14fa199
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 21 deletions.
5 changes: 3 additions & 2 deletions index.html
Expand Up @@ -44,10 +44,11 @@ <h2>Monaco Editor React</h2>
<br>

<h1>Verification</h1>
Here the bundles are used instead of the direct sources.<br>
For the "Static HTTP Server Alternative" you need to run <b><code>npm run start --workspace packages/examples</code></b> as well.
Here the bundles are used instead of the direct sources.

<h2>Monaco Editor Wrapper Bundle Verification</h2>
For the "Static HTTP Server Alternative" please start <b><code>npm run start:http</code></b> </code></b> beforehand.<br>

<a href="./packages/examples/verify_wrapper.html">JavaScript Diff</a>
(<a href="http://localhost:20002/verify_wrapper.html">Static HTTP Server Alternative</a>)
<br>
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -14,6 +14,7 @@
"test:run": "node --experimental-import-meta-resolve ./node_modules/vitest/vitest.mjs -c vite.config.ts --run",
"release:prepare": "npm run reset:repo && npm ci && npm run lint && npm run build && npm run test:run",
"reset:repo": "git clean -f -X -d",
"start:http": "npm run start:http --workspace packages/examples",
"start:example:server:json": "npm run start:server:json --workspace packages/examples",
"start:example:server:python": "npm run start:server:python --workspace packages/examples"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/package.json
Expand Up @@ -52,7 +52,7 @@
"copy:all": "npm run copy:prepare && npm run copy:workers",
"build:bundle:wrapper": "vite --config vite.bundle-mew.ts build",
"build": "npm run clean && npm run compile && npm run build:bundle:wrapper && npm run copy:all",
"start": "http-server --port 20002 ./",
"start:http": "http-server --port 20002 ./",
"start:server:json": "node --loader ts-node/esm ./src/servers/json-server.ts",
"start:server:python": "node --loader ts-node/esm ./src/servers/python-server.ts"
}
Expand Down
4 changes: 3 additions & 1 deletion packages/examples/src/reactPython.tsx
Expand Up @@ -41,7 +41,9 @@ const userConfig: UserConfig = {
},
reportStatus: true,
}
}, configurationOptions: {
},
clientOptions: {
documentSelector: ['python'],
workspaceFolder: {
index: 0,
name: 'workspace',
Expand Down
2 changes: 2 additions & 0 deletions packages/monaco-editor-wrapper/CHANGELOG.md
Expand Up @@ -5,6 +5,8 @@ All notable changes to npm module [monaco-editor-wrapper](https://www.npmjs.com/
## [3.6.0] - 2024-01-04

- Updated to `monaco-languageclient@7.3.0` and `@codingame/monaco-vscode-api@1.85.0` / `@codingame/monaco-editor-treemended@1.85.0` (=`monaco-editor@0.45.0`).
- How to modify client's capabilities? [#61](https://github.com/TypeFox/monaco-components/issues/61)
- It is now possible to provide and fully override boh monaco-languageclient's clientOptions and connectionProvider

## [3.5.0] - 2023-11-07

Expand Down
34 changes: 17 additions & 17 deletions packages/monaco-editor-wrapper/src/languageClientWrapper.ts
@@ -1,9 +1,8 @@
import { initServices, InitializeServiceConfig, MonacoLanguageClient } from 'monaco-languageclient';
import { initServices, InitializeServiceConfig, MonacoLanguageClient, IConnectionProvider } from 'monaco-languageclient';
import { toSocket, WebSocketMessageReader, WebSocketMessageWriter } from 'vscode-ws-jsonrpc';
import { BrowserMessageReader, BrowserMessageWriter } from 'vscode-languageserver-protocol/browser.js';
import { CloseAction, ErrorAction, MessageTransports, State } from 'vscode-languageclient/lib/common/client.js';
import { CloseAction, ErrorAction, LanguageClientOptions, MessageTransports, State } from 'vscode-languageclient/lib/common/client.js';
import { createUrl } from './utils.js';
import { $ConfigurationOptions } from 'vscode-languageclient/lib/common/configuration.js';
import { Logger } from './logger.js';

export type WebSocketCallOptions = {
Expand Down Expand Up @@ -59,9 +58,8 @@ export type WorkerConfigDirect = LanguageClientConfigBase & {

export type LanguageClientConfig = {
options: WebSocketConfigOptions | WebSocketConfigOptionsUrl | WorkerConfigOptions | WorkerConfigDirect;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
initializationOptions?: any;
configurationOptions?: $ConfigurationOptions
clientOptions?: LanguageClientOptions;
connectionProvider?: IConnectionProvider;
}

export type LanguageClientError = {
Expand Down Expand Up @@ -251,28 +249,30 @@ export class LanguageClientWrapper {
}

private createLanguageClient(transports: MessageTransports): MonacoLanguageClient {
return new MonacoLanguageClient({
const mlcConfig = {
name: this.languageClientConfig?.options.name ?? 'Monaco Wrapper Language Client',
clientOptions: {
// use a language id as a document selector

// allow to fully override the clientOptions
clientOptions: this.languageClientConfig?.clientOptions ?? {
documentSelector: [this.languageId!],
// disable the default error handler
errorHandler: {
error: () => ({ action: ErrorAction.Continue }),
closed: () => ({ action: CloseAction.DoNotRestart })
},
// allow to initialize the language client with user specific options
initializationOptions: this.languageClientConfig?.initializationOptions,

...(this.languageClientConfig?.configurationOptions ?? {})
}
},
// create a language client connection from the JSON RPC connection on demand
connectionProvider: {
// allow to fully override the clonnecetionProvider
connectionProvider: this.languageClientConfig?.connectionProvider ?? {
get: () => {
return Promise.resolve(transports);
}
}
});
};
if (!mlcConfig.clientOptions.documentSelector?.includes(this.languageId)) {
throw new Error(`languageClientWrapper (${this.name}): The language id '${this.languageId}' is not included in the document selector.`);
}

return new MonacoLanguageClient(mlcConfig);
}

private disposeWorker(keepWorker?: boolean) {
Expand Down

0 comments on commit 14fa199

Please sign in to comment.