vscode-languageclient#Disposable TypeScript Examples

The following examples show how to use vscode-languageclient#Disposable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: verificationGutterStatusView.ts    From ide-vscode with MIT License 5 votes vote down vote up
private readonly textEditorWatcher?: Disposable;
Example #2
Source File: extension.ts    From sorbet-lsp with MIT License 4 votes vote down vote up
export function activate(context: ExtensionContext) {
	let disposableClient: Disposable;

	const startLanguageServer = () => {
		let cmd = [];

		let vsconfig = vscode.workspace.getConfiguration('sorbet');
		const commandPath = vsconfig.commandPath || 'srb';
		const useBundler = vsconfig.useBundler;
		const useWatchman = vsconfig.useWatchman;
		const bundlerPath = vsconfig.bundlerPath || 'bundle';

		if (useBundler) {
			cmd = cmd.concat([bundlerPath, 'exec', 'srb']);
		} else {
			cmd.push(commandPath);
		}

		cmd = cmd.concat(['tc', '--lsp', '--enable-all-experimental-lsp-features']);

		if (!useWatchman) {
			cmd.push('--disable-watchman');
		}

		const firstWorkspace = (workspace.workspaceFolders && workspace.workspaceFolders[0]) ? workspace.workspaceFolders[0].uri.fsPath : null;

		if (!existsSync(`${firstWorkspace}/sorbet/config`)) {
			vscode.window.showInformationMessage('Sorbet config not found. Sorbet server will not be started');
			return;
		}

		const env = commonOptions(firstWorkspace);

		// The debug options for the server
		// --inspect=6009: runs the server in Node's Inspector mode so VS Code can attach to the server for debugging
		let debugOptions = { execArgv: ['--nolazy', '--inspect=6009'] };

		const serverOptions: ServerOptions = () => {
			return new Promise((resolve) => {
				let child = spawnWithBash(cmd, env);
				child.stderr.on('data', (data: Buffer) => {
					console.log(data.toString());
				});
				child.on('exit', (code, signal) => {
					console.log('Sorbet exited with code', code, signal);
				});
				resolve(child);
			});
		}

		// Options to control the language client
    let clientOptions: LanguageClientOptions = {
      // Register the server for plain text documents
      documentSelector: [{ scheme: 'file', language: 'ruby' }],
      synchronize: {
        // Notify the server about changes to relevant files in the workspace
        fileEvents: workspace.createFileSystemWatcher('{**/*.rb,**/*.gemspec,**/Gemfile}')
      },
      outputChannelName: 'Sorbet Language Server',
      revealOutputChannelOn: RevealOutputChannelOn.Never
    };


    // Create the language client and start the client.
    client = new LanguageClient(
      'sorbetLanguageServer',
      'Sorbet Language Server',
      serverOptions,
      clientOptions
    );

		// Start the client. This will also launch the server
		disposableClient = client.start();
	}

	const restartLanguageServer = function (): Promise<void> {
		return new Promise((resolve) => {
			if (disposableClient) {
				client.stop().then(() => {
					disposableClient.dispose();
					startLanguageServer();
					resolve();
				});
			} else {
				startLanguageServer();
				resolve();
			}
		});
	}

	// Restart command
	var disposableRestart = vscode.commands.registerCommand('sorbet.restart', () => {
		restartLanguageServer().then(() => {
			vscode.window.showInformationMessage('Sorbet server restarted.');
		});
	});
	context.subscriptions.push(disposableRestart);

	startLanguageServer();
}