2019-12-30 18:05:41 +00:00
|
|
|
import * as vscode from 'vscode';
|
|
|
|
import * as lc from 'vscode-languageclient';
|
|
|
|
|
|
|
|
import { Ctx, Cmd } from '../ctx';
|
|
|
|
|
|
|
|
// Opens the virtual file that will show the syntax tree
|
|
|
|
//
|
|
|
|
// The contents of the file come from the `TextDocumentContentProvider`
|
|
|
|
export function syntaxTree(ctx: Ctx): Cmd {
|
2019-12-30 18:12:49 +00:00
|
|
|
const tdcp = new TextDocumentContentProvider(ctx);
|
2019-12-30 18:05:41 +00:00
|
|
|
|
|
|
|
ctx.pushCleanup(
|
|
|
|
vscode.workspace.registerTextDocumentContentProvider(
|
|
|
|
'rust-analyzer',
|
2019-12-30 18:12:49 +00:00
|
|
|
tdcp,
|
2019-12-30 18:05:41 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
vscode.workspace.onDidChangeTextDocument(
|
|
|
|
(event: vscode.TextDocumentChangeEvent) => {
|
|
|
|
const doc = event.document;
|
|
|
|
if (doc.languageId !== 'rust') return;
|
2019-12-30 18:12:49 +00:00
|
|
|
afterLs(() => tdcp.eventEmitter.fire(tdcp.uri));
|
2019-12-30 18:05:41 +00:00
|
|
|
},
|
2020-02-05 20:39:47 +00:00
|
|
|
null,
|
2019-12-30 18:05:41 +00:00
|
|
|
ctx.subscriptions,
|
|
|
|
);
|
|
|
|
|
|
|
|
vscode.window.onDidChangeActiveTextEditor(
|
|
|
|
(editor: vscode.TextEditor | undefined) => {
|
|
|
|
if (!editor || editor.document.languageId !== 'rust') return;
|
2019-12-30 18:12:49 +00:00
|
|
|
tdcp.eventEmitter.fire(tdcp.uri);
|
2019-12-30 18:05:41 +00:00
|
|
|
},
|
2020-02-05 20:39:47 +00:00
|
|
|
null,
|
2019-12-30 18:05:41 +00:00
|
|
|
ctx.subscriptions,
|
|
|
|
);
|
|
|
|
|
|
|
|
return async () => {
|
|
|
|
const editor = vscode.window.activeTextEditor;
|
|
|
|
const rangeEnabled = !!(editor && !editor.selection.isEmpty);
|
|
|
|
|
|
|
|
const uri = rangeEnabled
|
2019-12-30 18:12:49 +00:00
|
|
|
? vscode.Uri.parse(`${tdcp.uri.toString()}?range=true`)
|
|
|
|
: tdcp.uri;
|
2019-12-30 18:05:41 +00:00
|
|
|
|
|
|
|
const document = await vscode.workspace.openTextDocument(uri);
|
|
|
|
|
2019-12-30 18:12:49 +00:00
|
|
|
tdcp.eventEmitter.fire(uri);
|
2019-12-30 18:05:41 +00:00
|
|
|
|
|
|
|
return vscode.window.showTextDocument(
|
|
|
|
document,
|
|
|
|
vscode.ViewColumn.Two,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to order this after LS updates, but there's no API for that.
|
|
|
|
// Hence, good old setTimeout.
|
2020-02-02 21:23:01 +00:00
|
|
|
function afterLs(f: () => void) {
|
2019-12-30 18:05:41 +00:00
|
|
|
setTimeout(f, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface SyntaxTreeParams {
|
|
|
|
textDocument: lc.TextDocumentIdentifier;
|
|
|
|
range?: lc.Range;
|
|
|
|
}
|
|
|
|
|
2019-12-30 18:12:49 +00:00
|
|
|
class TextDocumentContentProvider
|
2019-12-30 18:05:41 +00:00
|
|
|
implements vscode.TextDocumentContentProvider {
|
2019-12-30 18:30:30 +00:00
|
|
|
private ctx: Ctx;
|
2019-12-30 18:05:41 +00:00
|
|
|
uri = vscode.Uri.parse('rust-analyzer://syntaxtree');
|
|
|
|
eventEmitter = new vscode.EventEmitter<vscode.Uri>();
|
|
|
|
|
|
|
|
constructor(ctx: Ctx) {
|
|
|
|
this.ctx = ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult<string> {
|
|
|
|
const editor = vscode.window.activeTextEditor;
|
2019-12-31 17:55:34 +00:00
|
|
|
const client = this.ctx.client;
|
2019-12-31 17:50:32 +00:00
|
|
|
if (!editor || !client) return '';
|
2019-12-30 18:05:41 +00:00
|
|
|
|
|
|
|
let range: lc.Range | undefined;
|
|
|
|
|
|
|
|
// When the range based query is enabled we take the range of the selection
|
|
|
|
if (uri.query === 'range=true') {
|
|
|
|
range = editor.selection.isEmpty
|
|
|
|
? undefined
|
2019-12-31 17:50:32 +00:00
|
|
|
: client.code2ProtocolConverter.asRange(editor.selection);
|
2019-12-30 18:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const request: SyntaxTreeParams = {
|
|
|
|
textDocument: { uri: editor.document.uri.toString() },
|
|
|
|
range,
|
|
|
|
};
|
2019-12-31 17:50:32 +00:00
|
|
|
return client.sendRequest<string>(
|
2019-12-30 18:05:41 +00:00
|
|
|
'rust-analyzer/syntaxTree',
|
|
|
|
request,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
get onDidChange(): vscode.Event<vscode.Uri> {
|
|
|
|
return this.eventEmitter.event;
|
|
|
|
}
|
|
|
|
}
|