From d7c5a6f3081c2e7266620779d3c32067f947b959 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 10 Aug 2018 15:07:43 +0300 Subject: Start lang server --- codeless/src/extension.ts | 88 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 codeless/src/extension.ts (limited to 'codeless/src/extension.ts') diff --git a/codeless/src/extension.ts b/codeless/src/extension.ts new file mode 100644 index 000000000..712d93e04 --- /dev/null +++ b/codeless/src/extension.ts @@ -0,0 +1,88 @@ +'use strict'; +import * as vscode from 'vscode'; +import { + LanguageClient, + LanguageClientOptions, + ServerOptions, + TransportKind, + Executable, + TextDocumentIdentifier +} from 'vscode-languageclient'; + + +let client: LanguageClient; + +let uris = { + syntaxTree: vscode.Uri.parse('libsyntax-rust://syntaxtree') +} + + +export function activate(context: vscode.ExtensionContext) { + let dispose = (disposable) => { + context.subscriptions.push(disposable); + } + let registerCommand = (name, f) => { + dispose(vscode.commands.registerCommand(name, f)) + } + + registerCommand('libsyntax-rust.syntaxTree', () => openDoc(uris.syntaxTree)) + dispose(vscode.workspace.registerTextDocumentContentProvider( + 'libsyntax-rust', + new TextDocumentContentProvider() + )) + startServer() +} + +export function deactivate(): Thenable { + if (!client) { + return undefined; + } + return client.stop(); +} + +function startServer() { + let run: Executable = { + command: "cargo", + args: ["run"], + options: { + cwd: "./server" + } + } + let serverOptions: ServerOptions = { + run, + debug: run + }; + + let clientOptions: LanguageClientOptions = { + documentSelector: [{ scheme: 'file', language: 'rust' }], + }; + + client = new LanguageClient( + 'm', + 'm languge server', + serverOptions, + clientOptions, + ); + client.start(); +} + +async function openDoc(uri: vscode.Uri) { + let document = await vscode.workspace.openTextDocument(uri) + return vscode.window.showTextDocument(document, vscode.ViewColumn.Two, true) +} + +class TextDocumentContentProvider implements vscode.TextDocumentContentProvider { + public eventEmitter = new vscode.EventEmitter() + public syntaxTree: string = "Not available" + + public provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult { + let editor = vscode.window.activeTextEditor; + if (editor == null) return "" + let textDocument: TextDocumentIdentifier = { uri: editor.document.uri.toString() }; + return client.sendRequest("m/syntaxTree", { textDocument }) + } + + get onDidChange(): vscode.Event { + return this.eventEmitter.event + } +} -- cgit v1.2.3