2019-12-30 12:05:41 -06:00
|
|
|
import * as vscode from 'vscode';
|
2020-02-24 16:57:14 -06:00
|
|
|
import * as ra from '../rust-analyzer-api';
|
2019-12-30 12:05:41 -06:00
|
|
|
|
2020-03-31 08:05:42 -05:00
|
|
|
import { Ctx, Cmd, Disposable } from '../ctx';
|
|
|
|
import { isRustDocument, RustEditor, isRustEditor, sleep } from '../util';
|
|
|
|
|
|
|
|
const AST_FILE_SCHEME = "rust-analyzer";
|
2019-12-30 12:05:41 -06:00
|
|
|
|
|
|
|
// 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 12:12:49 -06:00
|
|
|
const tdcp = new TextDocumentContentProvider(ctx);
|
2019-12-30 12:05:41 -06:00
|
|
|
|
2020-03-31 08:57:03 -05:00
|
|
|
void new AstInspector(ctx);
|
|
|
|
|
2020-03-31 08:05:42 -05:00
|
|
|
ctx.pushCleanup(vscode.workspace.registerTextDocumentContentProvider(AST_FILE_SCHEME, tdcp));
|
2020-04-01 21:38:52 -05:00
|
|
|
ctx.pushCleanup(vscode.languages.setLanguageConfiguration("ra_syntax_tree", {
|
|
|
|
brackets: [["[", ")"]],
|
|
|
|
}));
|
2019-12-30 12:05:41 -06:00
|
|
|
|
|
|
|
return async () => {
|
|
|
|
const editor = vscode.window.activeTextEditor;
|
2020-03-31 08:05:42 -05:00
|
|
|
const rangeEnabled = !!editor && !editor.selection.isEmpty;
|
2019-12-30 12:05:41 -06:00
|
|
|
|
|
|
|
const uri = rangeEnabled
|
2019-12-30 12:12:49 -06:00
|
|
|
? vscode.Uri.parse(`${tdcp.uri.toString()}?range=true`)
|
|
|
|
: tdcp.uri;
|
2019-12-30 12:05:41 -06:00
|
|
|
|
|
|
|
const document = await vscode.workspace.openTextDocument(uri);
|
|
|
|
|
2019-12-30 12:12:49 -06:00
|
|
|
tdcp.eventEmitter.fire(uri);
|
2019-12-30 12:05:41 -06:00
|
|
|
|
2020-03-31 08:05:42 -05:00
|
|
|
void await vscode.window.showTextDocument(document, {
|
|
|
|
viewColumn: vscode.ViewColumn.Two,
|
|
|
|
preserveFocus: true
|
|
|
|
});
|
2019-12-30 12:05:41 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-31 08:57:03 -05:00
|
|
|
class TextDocumentContentProvider implements vscode.TextDocumentContentProvider {
|
2020-04-01 21:38:52 -05:00
|
|
|
readonly uri = vscode.Uri.parse('rust-analyzer://syntaxtree/tree.rast');
|
2020-03-31 08:05:42 -05:00
|
|
|
readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>();
|
2020-03-31 08:57:03 -05:00
|
|
|
|
2019-12-30 12:05:41 -06:00
|
|
|
|
2020-02-17 07:23:23 -06:00
|
|
|
constructor(private readonly ctx: Ctx) {
|
2020-03-31 08:57:03 -05:00
|
|
|
vscode.workspace.onDidChangeTextDocument(this.onDidChangeTextDocument, this, ctx.subscriptions);
|
|
|
|
vscode.window.onDidChangeActiveTextEditor(this.onDidChangeActiveTextEditor, this, ctx.subscriptions);
|
2019-12-30 12:05:41 -06:00
|
|
|
}
|
|
|
|
|
2020-03-31 08:05:42 -05:00
|
|
|
private onDidChangeTextDocument(event: vscode.TextDocumentChangeEvent) {
|
|
|
|
if (isRustDocument(event.document)) {
|
|
|
|
// We need to order this after language server updates, but there's no API for that.
|
|
|
|
// Hence, good old sleep().
|
|
|
|
void sleep(10).then(() => this.eventEmitter.fire(this.uri));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private onDidChangeActiveTextEditor(editor: vscode.TextEditor | undefined) {
|
|
|
|
if (editor && isRustEditor(editor)) {
|
|
|
|
this.eventEmitter.fire(this.uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
provideTextDocumentContent(uri: vscode.Uri, ct: vscode.CancellationToken): vscode.ProviderResult<string> {
|
|
|
|
const rustEditor = this.ctx.activeRustEditor;
|
|
|
|
if (!rustEditor) return '';
|
2019-12-30 12:05:41 -06:00
|
|
|
|
|
|
|
// When the range based query is enabled we take the range of the selection
|
2020-03-31 08:05:42 -05:00
|
|
|
const range = uri.query === 'range=true' && !rustEditor.selection.isEmpty
|
|
|
|
? this.ctx.client.code2ProtocolConverter.asRange(rustEditor.selection)
|
2020-02-24 16:57:14 -06:00
|
|
|
: null;
|
2019-12-30 12:05:41 -06:00
|
|
|
|
2020-03-31 08:05:42 -05:00
|
|
|
const params = { textDocument: { uri: rustEditor.document.uri.toString() }, range, };
|
|
|
|
return this.ctx.client.sendRequest(ra.syntaxTree, params, ct);
|
2019-12-30 12:05:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
get onDidChange(): vscode.Event<vscode.Uri> {
|
|
|
|
return this.eventEmitter.event;
|
|
|
|
}
|
|
|
|
}
|
2020-03-31 08:05:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
// FIXME: consider implementing this via the Tree View API?
|
|
|
|
// https://code.visualstudio.com/api/extension-guides/tree-view
|
|
|
|
class AstInspector implements vscode.HoverProvider, Disposable {
|
2020-04-01 19:24:30 -05:00
|
|
|
private readonly astDecorationType = vscode.window.createTextEditorDecorationType({
|
2020-04-01 17:20:08 -05:00
|
|
|
borderColor: new vscode.ThemeColor('rust_analyzer.syntaxTreeBorder'),
|
|
|
|
borderStyle: "solid",
|
|
|
|
borderWidth: "2px",
|
|
|
|
|
2020-03-31 08:05:42 -05:00
|
|
|
});
|
|
|
|
private rustEditor: undefined | RustEditor;
|
|
|
|
|
2020-04-01 19:24:30 -05:00
|
|
|
// Lazy rust token range -> syntax tree file range.
|
|
|
|
private readonly rust2Ast = new Lazy(() => {
|
|
|
|
const astEditor = this.findAstTextEditor();
|
|
|
|
if (!this.rustEditor || !astEditor) return undefined;
|
|
|
|
|
|
|
|
console.time("Build goto def index");
|
|
|
|
let buf: [vscode.Range, vscode.Range][] = [];
|
|
|
|
for (let i = 0; i < astEditor.document.lineCount; ++i) {
|
|
|
|
const astLine = astEditor.document.lineAt(i);
|
|
|
|
|
|
|
|
// Heuristically look for nodes with quoted text (which are token nodes)
|
|
|
|
const isTokenNode = astLine.text.lastIndexOf('"') >= 0;
|
|
|
|
if (!isTokenNode) continue;
|
|
|
|
|
|
|
|
const rustRange = this.parseRustTextRange(this.rustEditor.document, astLine.text);
|
|
|
|
if (!rustRange) continue;
|
|
|
|
|
|
|
|
buf.push([rustRange, this.findAstRange(astLine)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.timeEnd("Build goto def index");
|
|
|
|
return buf;
|
|
|
|
});
|
|
|
|
|
2020-03-31 08:57:03 -05:00
|
|
|
constructor(ctx: Ctx) {
|
|
|
|
ctx.pushCleanup(vscode.languages.registerHoverProvider({ scheme: AST_FILE_SCHEME }, this));
|
2020-04-01 19:24:30 -05:00
|
|
|
ctx.pushCleanup(vscode.languages.registerDefinitionProvider({ language: "rust" }, this));
|
2020-03-31 08:57:03 -05:00
|
|
|
vscode.workspace.onDidCloseTextDocument(this.onDidCloseTextDocument, this, ctx.subscriptions);
|
2020-04-01 19:24:30 -05:00
|
|
|
vscode.workspace.onDidChangeTextDocument(this.onDidChangeTextDocument, this, ctx.subscriptions);
|
2020-03-31 08:57:03 -05:00
|
|
|
vscode.window.onDidChangeVisibleTextEditors(this.onDidChangeVisibleTextEditors, this, ctx.subscriptions);
|
|
|
|
|
|
|
|
ctx.pushCleanup(this);
|
2020-03-31 08:05:42 -05:00
|
|
|
}
|
|
|
|
dispose() {
|
|
|
|
this.setRustEditor(undefined);
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:24:30 -05:00
|
|
|
private onDidChangeTextDocument(event: vscode.TextDocumentChangeEvent) {
|
|
|
|
if (this.rustEditor && event.document.uri.toString() === this.rustEditor.document.uri.toString()) {
|
|
|
|
this.rust2Ast.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 08:05:42 -05:00
|
|
|
private onDidCloseTextDocument(doc: vscode.TextDocument) {
|
2020-03-31 11:06:07 -05:00
|
|
|
if (this.rustEditor && doc.uri.toString() === this.rustEditor.document.uri.toString()) {
|
2020-03-31 08:05:42 -05:00
|
|
|
this.setRustEditor(undefined);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private onDidChangeVisibleTextEditors(editors: vscode.TextEditor[]) {
|
2020-04-01 19:24:30 -05:00
|
|
|
if (!this.findAstTextEditor()) {
|
2020-03-31 08:05:42 -05:00
|
|
|
this.setRustEditor(undefined);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setRustEditor(editors.find(isRustEditor));
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:24:30 -05:00
|
|
|
private findAstTextEditor(): undefined | vscode.TextEditor {
|
|
|
|
return vscode.window.visibleTextEditors.find(it => it.document.uri.scheme === AST_FILE_SCHEME);
|
|
|
|
}
|
|
|
|
|
2020-03-31 08:05:42 -05:00
|
|
|
private setRustEditor(newRustEditor: undefined | RustEditor) {
|
2020-04-01 19:24:30 -05:00
|
|
|
if (this.rustEditor && this.rustEditor !== newRustEditor) {
|
|
|
|
this.rustEditor.setDecorations(this.astDecorationType, []);
|
|
|
|
this.rust2Ast.reset();
|
2020-03-31 08:05:42 -05:00
|
|
|
}
|
|
|
|
this.rustEditor = newRustEditor;
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:24:30 -05:00
|
|
|
// additional positional params are omitted
|
|
|
|
provideDefinition(doc: vscode.TextDocument, pos: vscode.Position): vscode.ProviderResult<vscode.DefinitionLink[]> {
|
|
|
|
if (!this.rustEditor || doc.uri.toString() !== this.rustEditor.document.uri.toString()) return;
|
|
|
|
|
|
|
|
const astEditor = this.findAstTextEditor();
|
|
|
|
if (!astEditor) return;
|
|
|
|
|
|
|
|
console.time("Goto def");
|
|
|
|
const rust2AstRanges = this.rust2Ast.get()?.find(([rustRange, _]) => rustRange.contains(pos));
|
|
|
|
console.timeEnd("Goto def");
|
|
|
|
if (!rust2AstRanges) return;
|
|
|
|
|
|
|
|
const [rustFileRange, astFileRange] = rust2AstRanges;
|
|
|
|
|
|
|
|
astEditor.revealRange(astFileRange);
|
|
|
|
astEditor.selection = new vscode.Selection(astFileRange.start, astFileRange.end);
|
|
|
|
|
|
|
|
return [{
|
|
|
|
targetRange: astFileRange,
|
|
|
|
targetUri: astEditor.document.uri,
|
|
|
|
originSelectionRange: rustFileRange,
|
|
|
|
targetSelectionRange: astFileRange,
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
// additional positional params are omitted
|
2020-03-31 08:05:42 -05:00
|
|
|
provideHover(doc: vscode.TextDocument, hoverPosition: vscode.Position): vscode.ProviderResult<vscode.Hover> {
|
|
|
|
if (!this.rustEditor) return;
|
|
|
|
|
|
|
|
const astTextLine = doc.lineAt(hoverPosition.line);
|
|
|
|
|
|
|
|
const rustTextRange = this.parseRustTextRange(this.rustEditor.document, astTextLine.text);
|
|
|
|
if (!rustTextRange) return;
|
|
|
|
|
2020-04-01 19:24:30 -05:00
|
|
|
this.rustEditor.setDecorations(this.astDecorationType, [rustTextRange]);
|
2020-03-31 12:28:10 -05:00
|
|
|
this.rustEditor.revealRange(rustTextRange);
|
2020-03-31 08:05:42 -05:00
|
|
|
|
|
|
|
const rustSourceCode = this.rustEditor.document.getText(rustTextRange);
|
|
|
|
const astTextRange = this.findAstRange(astTextLine);
|
|
|
|
|
|
|
|
return new vscode.Hover(["```rust\n" + rustSourceCode + "\n```"], astTextRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
private findAstRange(astLine: vscode.TextLine) {
|
|
|
|
const lineOffset = astLine.range.start;
|
|
|
|
const begin = lineOffset.translate(undefined, astLine.firstNonWhitespaceCharacterIndex);
|
|
|
|
const end = lineOffset.translate(undefined, astLine.text.trimEnd().length);
|
|
|
|
return new vscode.Range(begin, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
private parseRustTextRange(doc: vscode.TextDocument, astLine: string): undefined | vscode.Range {
|
|
|
|
const parsedRange = /\[(\d+); (\d+)\)/.exec(astLine);
|
|
|
|
if (!parsedRange) return;
|
|
|
|
|
2020-03-31 12:28:10 -05:00
|
|
|
const [begin, end] = parsedRange.slice(1).map(off => doc.positionAt(+off));
|
2020-03-31 08:05:42 -05:00
|
|
|
|
|
|
|
return new vscode.Range(begin, end);
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 19:24:30 -05:00
|
|
|
|
|
|
|
class Lazy<T> {
|
|
|
|
val: undefined | T;
|
|
|
|
|
|
|
|
constructor(private readonly compute: () => undefined | T) {}
|
|
|
|
|
|
|
|
get() {
|
|
|
|
return this.val ?? (this.val = this.compute());
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.val = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|