Skip to content

Commit

Permalink
feat: add LanguageClientRunConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
ls-infra committed Jan 16, 2024
1 parent 43690a7 commit 0b49734
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
18 changes: 18 additions & 0 deletions packages/examples/src/common/client-commons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { initServices, MonacoLanguageClient } from 'monaco-languageclient';
import { CloseAction, ErrorAction, MessageTransports } from 'vscode-languageclient';
import { WebSocketMessageReader, WebSocketMessageWriter, toSocket } from 'vscode-ws-jsonrpc';
import { Uri } from 'vscode';
import { LanguageClientRunConfig } from './model.js';

export const createLanguageClient = (transports: MessageTransports, languageId: string): MonacoLanguageClient => {
return new MonacoLanguageClient({
Expand Down Expand Up @@ -167,3 +168,20 @@ export const createMonacoEditor = async (config: {
} as ExampleJsonEditor;
return Promise.resolve(result);
};

export const runLanguageClient = async (config : LanguageClientRunConfig) => {
const languageId = config.registerConfig.id;
await doInit(config.vscodeApiInit, config.registerConfig);
const editorDom = document.getElementById(config.htmlElementId);
if (editorDom) {
await createMonacoEditor({
htmlElement: editorDom,
content: config.defaultContent,
languageId
});
} else {
console.error(`no dom element for css id: ${config.htmlElementId}`);
}
const url = createUrl(config.clientUrl, config.serverPort, config.serverPath);
initWebSocketAndStartClient(url, languageId);
};
12 changes: 12 additions & 0 deletions packages/examples/src/common/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import * as cp from 'child_process';
import { languages } from 'monaco-editor';
import { ServerOptions } from 'ws';

export enum LanguageCli {
Expand All @@ -20,3 +21,14 @@ export interface LanguageServerRunConfig {
wsServerOptions: ServerOptions,
spawnOptions?: cp.SpawnOptions;
}

export interface LanguageClientRunConfig {
vscodeApiInit : boolean;
clientUrl: string;
serverPath: string;
serverPort: number;
registerConfig: languages.ILanguageExtensionPoint;
defaultContent: string;
/** CSS id selector */
htmlElementId: string;
}
35 changes: 19 additions & 16 deletions packages/examples/src/groovy/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,31 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import '@codingame/monaco-vscode-groovy-default-extension'; // this is for the syntax highlighting
import { createMonacoEditor, createUrl, initWebSocketAndStartClient, doInit } from '../../common/client-commons.js';
import { runLanguageClient } from '../../common/client-commons.js';
import { buildWorkerDefinition } from 'monaco-editor-workers';
import { groovyConfig } from '../config.js';
buildWorkerDefinition('../../node_modules/monaco-editor-workers/dist/workers/', new URL('', window.location.href).href, false);
export const startGroovyClient = async () => {
const languageId = 'groovy';
await doInit(true, {
id: languageId,
extensions: ['.groovy'],
aliases: [languageId],
mimetypes: ['application/json']
});
await createMonacoEditor({
htmlElement: document.getElementById('container')!,
content: `
runLanguageClient(
{
vscodeApiInit: true,
serverPath: groovyConfig.path,
serverPort: groovyConfig.port,
registerConfig: {
id: languageId,
extensions: ['.groovy'],
aliases: [languageId],
mimetypes: ['application/json']
},
defaultContent:
`
package test.org;
import java.io.File ;
File file = new File("E:/Example.txt");
`,
languageId
});

const url = createUrl('localhost', groovyConfig.port, groovyConfig.path);
initWebSocketAndStartClient(url, languageId);
`,
htmlElementId: 'container',
clientUrl: 'localhost'
}
);
};

0 comments on commit 0b49734

Please sign in to comment.