Skip to content

Add support for websockets - treat language server as WS server #2

New issue

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

By clicking “No 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? No Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@types/node": "^18.19.15",
"@types/semver": "^7.5.6",
"@types/which": "^3.0.3",
"@types/ws": "^8.18.1",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"commander": "^12.0.0",
Expand All @@ -74,7 +75,9 @@
"vscode-languageserver-protocol": "^3.17.5",
"vscode-languageserver-textdocument": "1.0.11",
"vscode-uri": "^3.0.8",
"vscode-ws-jsonrpc": "^3.4.0",
"which": "^4.0.0",
"why-is-node-running": "^2.2.2"
"why-is-node-running": "^2.2.2",
"ws": "^8.18.1"
}
}
33 changes: 25 additions & 8 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/

import { readFileSync } from 'node:fs';
// import { readFileSync } from 'node:fs';
import { Command } from 'commander';
import lsp from 'vscode-languageserver';
import lsp from 'vscode-languageserver/node.js';
import { WebSocketMessageReader, WebSocketMessageWriter, toSocket } from 'vscode-ws-jsonrpc';
import { createLspConnection } from './lsp-connection.js';
import * as ws from 'ws';

const DEFAULT_LOG_LEVEL = lsp.MessageType.Info;
const { version } = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), { encoding: 'utf8' }));
// const { version } = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), { encoding: 'utf8' }));

const program = new Command('typescript-language-server')
.version(version)
.requiredOption('--stdio', 'use stdio')
// .version(version)
// .requiredOption('--stdio', 'use stdio')
.option('--log-level <logLevel>', 'A number indicating the log level (4 = log, 3 = info, 2 = warn, 1 = error). Defaults to `2`.')
.parse(process.argv);

Expand All @@ -30,6 +32,21 @@ if (options.logLevel) {
}
}

createLspConnection({
showMessageLevel: logLevel as lsp.MessageType,
}).listen();
const port = 9090;
const wss = new ws.WebSocketServer({ port });

let connection: lsp.Connection | undefined = undefined;

wss.on('connection', (socket) => {
if (connection) {
connection.dispose();
}
const sock = toSocket(socket);
const reader = new WebSocketMessageReader(sock);
const writer = new WebSocketMessageWriter(sock);
connection = lsp.createConnection(lsp.ProposedFeatures.all, reader, writer);

createLspConnection({
showMessageLevel: logLevel as lsp.MessageType,
}, connection).listen();
});
3 changes: 1 addition & 2 deletions src/lsp-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export interface LspConnectionOptions {
showMessageLevel: lsp.MessageType;
}

export function createLspConnection(options: LspConnectionOptions): lsp.Connection {
const connection = lsp.createConnection(lsp.ProposedFeatures.all);
export function createLspConnection(options: LspConnectionOptions, connection: lsp.Connection): lsp.Connection {
const lspClient = new LspClientImpl(connection);
const logger = new LspClientLogger(lspClient, options.showMessageLevel);
const server: LspServer = new LspServer({
Expand Down
Loading