Last active
May 18, 2022 05:25
-
-
Save spoenemann/7f133e37d9363171ecffcd4939bd703a to your computer and use it in GitHub Desktop.
VS Code language client with stdio + socket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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