1 import * as vscode from 'vscode';
2 import * as ra from '../rust-analyzer-api';
4 import { Ctx, Cmd, Disposable } from '../ctx';
5 import { isRustDocument, RustEditor, isRustEditor, sleep } from '../util';
7 const AST_FILE_SCHEME = "rust-analyzer";
9 // Opens the virtual file that will show the syntax tree
11 // The contents of the file come from the `TextDocumentContentProvider`
12 export function syntaxTree(ctx: Ctx): Cmd {
13 const tdcp = new TextDocumentContentProvider(ctx);
15 void new AstInspector(ctx);
17 ctx.pushCleanup(vscode.workspace.registerTextDocumentContentProvider(AST_FILE_SCHEME, tdcp));
20 const editor = vscode.window.activeTextEditor;
21 const rangeEnabled = !!editor && !editor.selection.isEmpty;
23 const uri = rangeEnabled
24 ? vscode.Uri.parse(`${tdcp.uri.toString()}?range=true`)
27 const document = await vscode.workspace.openTextDocument(uri);
29 tdcp.eventEmitter.fire(uri);
31 void await vscode.window.showTextDocument(document, {
32 viewColumn: vscode.ViewColumn.Two,
38 class TextDocumentContentProvider implements vscode.TextDocumentContentProvider {
39 readonly uri = vscode.Uri.parse('rust-analyzer://syntaxtree');
40 readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>();
43 constructor(private readonly ctx: Ctx) {
44 vscode.workspace.onDidChangeTextDocument(this.onDidChangeTextDocument, this, ctx.subscriptions);
45 vscode.window.onDidChangeActiveTextEditor(this.onDidChangeActiveTextEditor, this, ctx.subscriptions);
48 private onDidChangeTextDocument(event: vscode.TextDocumentChangeEvent) {
49 if (isRustDocument(event.document)) {
50 // We need to order this after language server updates, but there's no API for that.
51 // Hence, good old sleep().
52 void sleep(10).then(() => this.eventEmitter.fire(this.uri));
55 private onDidChangeActiveTextEditor(editor: vscode.TextEditor | undefined) {
56 if (editor && isRustEditor(editor)) {
57 this.eventEmitter.fire(this.uri);
61 provideTextDocumentContent(uri: vscode.Uri, ct: vscode.CancellationToken): vscode.ProviderResult<string> {
62 const rustEditor = this.ctx.activeRustEditor;
63 if (!rustEditor) return '';
65 // When the range based query is enabled we take the range of the selection
66 const range = uri.query === 'range=true' && !rustEditor.selection.isEmpty
67 ? this.ctx.client.code2ProtocolConverter.asRange(rustEditor.selection)
70 const params = { textDocument: { uri: rustEditor.document.uri.toString() }, range, };
71 return this.ctx.client.sendRequest(ra.syntaxTree, params, ct);
74 get onDidChange(): vscode.Event<vscode.Uri> {
75 return this.eventEmitter.event;
80 // FIXME: consider implementing this via the Tree View API?
81 // https://code.visualstudio.com/api/extension-guides/tree-view
82 class AstInspector implements vscode.HoverProvider, Disposable {
83 private static readonly astDecorationType = vscode.window.createTextEditorDecorationType({
84 borderColor: new vscode.ThemeColor('rust_analyzer.syntaxTreeBorder'),
89 private rustEditor: undefined | RustEditor;
91 constructor(ctx: Ctx) {
92 ctx.pushCleanup(vscode.languages.registerHoverProvider({ scheme: AST_FILE_SCHEME }, this));
93 vscode.workspace.onDidCloseTextDocument(this.onDidCloseTextDocument, this, ctx.subscriptions);
94 vscode.window.onDidChangeVisibleTextEditors(this.onDidChangeVisibleTextEditors, this, ctx.subscriptions);
96 ctx.pushCleanup(this);
99 this.setRustEditor(undefined);
102 private onDidCloseTextDocument(doc: vscode.TextDocument) {
103 if (this.rustEditor && doc.uri.toString() === this.rustEditor.document.uri.toString()) {
104 this.setRustEditor(undefined);
108 private onDidChangeVisibleTextEditors(editors: vscode.TextEditor[]) {
109 if (editors.every(suspect => suspect.document.uri.scheme !== AST_FILE_SCHEME)) {
110 this.setRustEditor(undefined);
113 this.setRustEditor(editors.find(isRustEditor));
116 private setRustEditor(newRustEditor: undefined | RustEditor) {
117 if (newRustEditor !== this.rustEditor) {
118 this.rustEditor?.setDecorations(AstInspector.astDecorationType, []);
120 this.rustEditor = newRustEditor;
123 provideHover(doc: vscode.TextDocument, hoverPosition: vscode.Position): vscode.ProviderResult<vscode.Hover> {
124 if (!this.rustEditor) return;
126 const astTextLine = doc.lineAt(hoverPosition.line);
128 const rustTextRange = this.parseRustTextRange(this.rustEditor.document, astTextLine.text);
129 if (!rustTextRange) return;
131 this.rustEditor.setDecorations(AstInspector.astDecorationType, [rustTextRange]);
132 this.rustEditor.revealRange(rustTextRange);
134 const rustSourceCode = this.rustEditor.document.getText(rustTextRange);
135 const astTextRange = this.findAstRange(astTextLine);
137 return new vscode.Hover(["```rust\n" + rustSourceCode + "\n```"], astTextRange);
140 private findAstRange(astLine: vscode.TextLine) {
141 const lineOffset = astLine.range.start;
142 const begin = lineOffset.translate(undefined, astLine.firstNonWhitespaceCharacterIndex);
143 const end = lineOffset.translate(undefined, astLine.text.trimEnd().length);
144 return new vscode.Range(begin, end);
147 private parseRustTextRange(doc: vscode.TextDocument, astLine: string): undefined | vscode.Range {
148 const parsedRange = /\[(\d+); (\d+)\)/.exec(astLine);
149 if (!parsedRange) return;
151 const [begin, end] = parsedRange.slice(1).map(off => doc.positionAt(+off));
153 return new vscode.Range(begin, end);