Skip to content

Instantly share code, notes, and snippets.

@spoenemann
Last active May 18, 2022 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spoenemann/7f133e37d9363171ecffcd4939bd703a to your computer and use it in GitHub Desktop.
Save spoenemann/7f133e37d9363171ecffcd4939bd703a to your computer and use it in GitHub Desktop.
VS Code language client with stdio + socket
import * as net from 'net';
import * as vscode from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions, StreamInfo } from 'vscode-languageclient';
const DEBUG = false;
const SERVER_PORT = 5008;
export function activate(context: vscode.ExtensionContext) {
const languageClient = DEBUG
? getSocketLanguageClient({ serverPort: SERVER_PORT })
: getStdioLanguageClient();
const disposable = languageClient.start();
context.subscriptions.push(disposable);
}
export function deactivate() {
}
export function getStdioLanguageClient(): LanguageClient {
const commandPath: string = '...'; // Obtain or hard-code path to Java language server executable
const serverOptions: ServerOptions = {
run: {
command: commandPath
},
debug: {
command: commandPath,
args: ['-Xdebug', '-Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n,quiet=y', '-Xmx256m']
}
};
const clientOptions: LanguageClientOptions = {
documentSelector: ['foo'],
};
return new LanguageClient('foo', 'Foo Language', serverOptions, clientOptions);
}
interface DebugOptions {
serverPort: number;
}
export function getSocketLanguageClient({ serverPort }: DebugOptions): LanguageClient {
const serverOptions: ServerOptions = () => {
const socket = net.connect({ port: serverPort });
const result: StreamInfo = {
writer: socket,
reader: socket
};
return Promise.resolve(result);
};
const clientOptions: LanguageClientOptions = {
documentSelector: ['foo'],
};
return new LanguageClient('foo', 'Foo Language', serverOptions, clientOptions);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment