2018-08-10 12:07:43 +00:00
|
|
|
'use strict';
|
|
|
|
import * as vscode from 'vscode';
|
|
|
|
import {
|
|
|
|
LanguageClient,
|
|
|
|
LanguageClientOptions,
|
|
|
|
ServerOptions,
|
|
|
|
TransportKind,
|
|
|
|
Executable,
|
2018-08-10 18:13:39 +00:00
|
|
|
TextDocumentIdentifier,
|
|
|
|
Range
|
2018-08-10 12:07:43 +00:00
|
|
|
} from 'vscode-languageclient';
|
|
|
|
|
|
|
|
|
|
|
|
let client: LanguageClient;
|
|
|
|
|
|
|
|
let uris = {
|
|
|
|
syntaxTree: vscode.Uri.parse('libsyntax-rust://syntaxtree')
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function activate(context: vscode.ExtensionContext) {
|
2018-08-10 18:13:39 +00:00
|
|
|
let textDocumentContentProvider = new TextDocumentContentProvider()
|
2018-08-10 12:07:43 +00:00
|
|
|
let dispose = (disposable) => {
|
|
|
|
context.subscriptions.push(disposable);
|
|
|
|
}
|
|
|
|
let registerCommand = (name, f) => {
|
|
|
|
dispose(vscode.commands.registerCommand(name, f))
|
|
|
|
}
|
|
|
|
|
|
|
|
registerCommand('libsyntax-rust.syntaxTree', () => openDoc(uris.syntaxTree))
|
2018-08-10 18:13:39 +00:00
|
|
|
registerCommand('libsyntax-rust.extendSelection', async () => {
|
|
|
|
let editor = vscode.window.activeTextEditor
|
|
|
|
if (editor == null || editor.document.languageId != "rust") return
|
|
|
|
let request: ExtendSelectionParams = {
|
|
|
|
textDocument: { uri: editor.document.uri.toString() },
|
|
|
|
selections: editor.selections.map((s) => {
|
|
|
|
let r: Range = { start: s.start, end: s.end }
|
|
|
|
return r;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
let response = await client.sendRequest<ExtendSelectionResult>("m/extendSelection", request)
|
|
|
|
editor.selections = response.selections.map((range) => {
|
|
|
|
return new vscode.Selection(
|
|
|
|
new vscode.Position(range.start.line, range.start.character),
|
|
|
|
new vscode.Position(range.end.line, range.end.character),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-08-10 12:07:43 +00:00
|
|
|
dispose(vscode.workspace.registerTextDocumentContentProvider(
|
|
|
|
'libsyntax-rust',
|
2018-08-10 18:13:39 +00:00
|
|
|
textDocumentContentProvider
|
2018-08-10 12:07:43 +00:00
|
|
|
))
|
|
|
|
startServer()
|
2018-08-10 18:13:39 +00:00
|
|
|
vscode.workspace.onDidChangeTextDocument((event: vscode.TextDocumentChangeEvent) => {
|
|
|
|
let doc = event.document
|
|
|
|
if (doc.languageId != "rust") return
|
|
|
|
// We need to order this after LS updates, but there's no API for that.
|
|
|
|
// Hence, good old setTimeout.
|
|
|
|
setTimeout(() => {
|
|
|
|
textDocumentContentProvider.eventEmitter.fire(uris.syntaxTree)
|
|
|
|
}, 10)
|
|
|
|
}, null, context.subscriptions)
|
2018-08-10 12:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function deactivate(): Thenable<void> {
|
|
|
|
if (!client) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return client.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
function startServer() {
|
|
|
|
let run: Executable = {
|
|
|
|
command: "cargo",
|
2018-08-10 14:49:45 +00:00
|
|
|
args: ["run", "--package", "m"],
|
|
|
|
options: { cwd: "." }
|
2018-08-10 12:07:43 +00:00
|
|
|
}
|
|
|
|
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<vscode.Uri>()
|
|
|
|
public syntaxTree: string = "Not available"
|
|
|
|
|
|
|
|
public provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult<string> {
|
|
|
|
let editor = vscode.window.activeTextEditor;
|
|
|
|
if (editor == null) return ""
|
2018-08-10 18:13:39 +00:00
|
|
|
let request: SyntaxTreeParams = {
|
|
|
|
textDocument: { uri: editor.document.uri.toString() }
|
|
|
|
};
|
|
|
|
return client.sendRequest<SyntaxTreeResult>("m/syntaxTree", request);
|
2018-08-10 12:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get onDidChange(): vscode.Event<vscode.Uri> {
|
|
|
|
return this.eventEmitter.event
|
|
|
|
}
|
|
|
|
}
|
2018-08-10 18:13:39 +00:00
|
|
|
|
|
|
|
interface SyntaxTreeParams {
|
|
|
|
textDocument: TextDocumentIdentifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
type SyntaxTreeResult = string
|
|
|
|
|
|
|
|
interface ExtendSelectionParams {
|
|
|
|
textDocument: TextDocumentIdentifier;
|
|
|
|
selections: Range[];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ExtendSelectionResult {
|
|
|
|
selections: Range[];
|
|
|
|
}
|