Skip to content

Commit

Permalink
feat: add runLanguageClient
Browse files Browse the repository at this point in the history
  • Loading branch information
ls-infra committed Jan 16, 2024
1 parent 0b49734 commit 0c865d1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
18 changes: 0 additions & 18 deletions packages/examples/src/common/client-commons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ 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 @@ -168,20 +167,3 @@ 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);
};
23 changes: 23 additions & 0 deletions packages/examples/src/common/language-client-runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) 2018-2022 TypeFox GmbH (http://www.typefox.io). All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import { createMonacoEditor, createUrl, doInit, initWebSocketAndStartClient } from './client-commons.js';
import { LanguageClientRunConfig } from './model.js';
/** LSP client runner */
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
});
const url = createUrl(config.clientUrl, config.serverPort, config.serverPath);
initWebSocketAndStartClient(url, languageId);
} else {
console.error(`no dom element for css id: ${config.htmlElementId}`);
}
};
2 changes: 1 addition & 1 deletion packages/examples/src/common/language-server-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import express from 'express';
import { getLocalDirectory } from '../utils/fs-utils.js';
import { upgradeWsServer } from './server-commons.js';
import { LanguageServerRunConfig } from './model.js';

/** LSP server runner */
export const runLanguageServer = (
languageServerRunConfig: LanguageServerRunConfig
) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/src/groovy/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* 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 { runLanguageClient } from '../../common/client-commons.js';
import { runLanguageClient } from '../../common/language-client-runner.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);
Expand Down
1 change: 1 addition & 0 deletions packages/examples/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* ------------------------------------------------------------------------------------------ */
/* client side export only */
export * from './common/client-commons.js';
export * from './common/language-client-runner.js';
export * from './browser/main.js';
export * from './json/client/main.js';
export * from './langium/statemachineClient.js';
Expand Down

0 comments on commit 0c865d1

Please sign in to comment.