2019-12-30 12:05:41 -06:00
|
|
|
import * as vscode from "vscode";
|
2020-03-31 08:05:42 -05:00
|
|
|
|
2020-05-25 05:02:30 -05:00
|
|
|
import { Ctx, Disposable } from "./ctx";
|
|
|
|
import { RustEditor, isRustEditor } from "./util";
|
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
|
2020-05-25 05:02:30 -05:00
|
|
|
export class AstInspector implements vscode.HoverProvider, vscode.DefinitionProvider, 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;
|
|
|
|
|
2020-04-01 19:35:58 -05:00
|
|
|
const buf: [vscode.Range, vscode.Range][] = [];
|
2020-04-01 19:24:30 -05:00
|
|
|
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;
|
|
|
|
|
2020-04-01 19:35:58 -05:00
|
|
|
buf.push([rustRange, this.findAstNodeRange(astLine)]);
|
2020-04-01 19:24:30 -05:00
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
});
|
|
|
|
|
2020-03-31 08:57:03 -05:00
|
|
|
constructor(ctx: Ctx) {
|
2020-05-25 05:02:30 -05:00
|
|
|
ctx.pushCleanup(vscode.languages.registerHoverProvider({ scheme: "rust-analyzer" }, 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 11:36:07 -06:00
|
|
|
private onDidChangeVisibleTextEditors(editors: readonly 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 {
|
2020-05-25 05:02:30 -05:00
|
|
|
return vscode.window.visibleTextEditors.find(
|
|
|
|
(it) => it.document.uri.scheme === "rust-analyzer"
|
|
|
|
);
|
2020-04-01 19:24:30 -05:00
|
|
|
}
|
|
|
|
|
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[]> {
|
2022-05-17 12:31:51 -05:00
|
|
|
if (!this.rustEditor || doc.uri.toString() !== this.rustEditor.document.uri.toString()) {
|
2020-04-01 19:24:30 -05:00
|
|
|
return;
|
2022-05-17 12:31:51 -05:00
|
|
|
}
|
2020-04-01 19:24:30 -05:00
|
|
|
|
|
|
|
const astEditor = this.findAstTextEditor();
|
|
|
|
if (!astEditor) return;
|
|
|
|
|
|
|
|
const rust2AstRanges = this.rust2Ast
|
|
|
|
.get()
|
|
|
|
?.find(([rustRange, _]) => rustRange.contains(pos));
|
|
|
|
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;
|
|
|
|
|
2020-04-01 19:24:45 -05:00
|
|
|
const astFileLine = doc.lineAt(hoverPosition.line);
|
2020-03-31 08:05:42 -05:00
|
|
|
|
2020-04-01 19:24:45 -05:00
|
|
|
const rustFileRange = this.parseRustTextRange(this.rustEditor.document, astFileLine.text);
|
|
|
|
if (!rustFileRange) return;
|
2020-03-31 08:05:42 -05:00
|
|
|
|
2020-04-01 19:24:45 -05:00
|
|
|
this.rustEditor.setDecorations(this.astDecorationType, [rustFileRange]);
|
|
|
|
this.rustEditor.revealRange(rustFileRange);
|
2020-03-31 08:05:42 -05:00
|
|
|
|
2020-04-01 19:24:45 -05:00
|
|
|
const rustSourceCode = this.rustEditor.document.getText(rustFileRange);
|
2020-04-01 19:35:58 -05:00
|
|
|
const astFileRange = this.findAstNodeRange(astFileLine);
|
2020-03-31 08:05:42 -05:00
|
|
|
|
2020-04-01 19:24:45 -05:00
|
|
|
return new vscode.Hover(["```rust\n" + rustSourceCode + "\n```"], astFileRange);
|
2020-03-31 08:05:42 -05:00
|
|
|
}
|
|
|
|
|
2020-04-21 18:04:28 -05:00
|
|
|
private findAstNodeRange(astLine: vscode.TextLine): vscode.Range {
|
2020-03-31 08:05:42 -05:00
|
|
|
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 {
|
2020-05-10 12:43:48 -05:00
|
|
|
const parsedRange = /(\d+)\.\.(\d+)/.exec(astLine);
|
2020-03-31 08:05:42 -05:00
|
|
|
if (!parsedRange) return;
|
|
|
|
|
2020-04-21 18:04:28 -05:00
|
|
|
const [begin, end] = parsedRange.slice(1).map((off) => this.positionAt(doc, +off));
|
2020-03-31 08:05:42 -05:00
|
|
|
|
|
|
|
return new vscode.Range(begin, end);
|
|
|
|
}
|
2020-04-21 18:04:28 -05:00
|
|
|
|
2020-04-21 18:28:44 -05:00
|
|
|
// Memoize the last value, otherwise the CPU is at 100% single core
|
2020-04-21 18:04:28 -05:00
|
|
|
// with quadratic lookups when we build rust2Ast cache
|
2020-04-21 18:28:44 -05:00
|
|
|
cache?: { doc: vscode.TextDocument; offset: number; line: number };
|
2020-04-21 18:04:28 -05:00
|
|
|
|
2020-04-21 18:28:44 -05:00
|
|
|
positionAt(doc: vscode.TextDocument, targetOffset: number): vscode.Position {
|
2020-04-21 18:04:28 -05:00
|
|
|
if (doc.eol === vscode.EndOfLine.LF) {
|
2020-04-21 18:28:44 -05:00
|
|
|
return doc.positionAt(targetOffset);
|
2020-04-21 18:04:28 -05:00
|
|
|
}
|
|
|
|
|
2020-05-10 08:05:09 -05:00
|
|
|
// Dirty workaround for crlf line endings
|
2020-04-21 18:04:28 -05:00
|
|
|
// We are still in this prehistoric era of carriage returns here...
|
|
|
|
|
2020-04-21 18:28:44 -05:00
|
|
|
let line = 0;
|
|
|
|
let offset = 0;
|
2020-04-21 18:04:28 -05:00
|
|
|
|
2020-04-21 18:28:44 -05:00
|
|
|
const cache = this.cache;
|
|
|
|
if (cache?.doc === doc && cache.offset <= targetOffset) {
|
|
|
|
({ line, offset } = cache);
|
2020-04-21 18:04:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
2020-04-21 18:28:44 -05:00
|
|
|
const lineLenWithLf = doc.lineAt(line).text.length + 1;
|
|
|
|
if (offset + lineLenWithLf > targetOffset) {
|
|
|
|
this.cache = { doc, offset, line };
|
|
|
|
return doc.positionAt(targetOffset + line);
|
2020-04-21 18:04:28 -05:00
|
|
|
}
|
2020-04-21 18:28:44 -05:00
|
|
|
offset += lineLenWithLf;
|
|
|
|
line += 1;
|
2020-04-21 18:04:28 -05:00
|
|
|
}
|
|
|
|
}
|
2020-03-31 08:05:42 -05:00
|
|
|
}
|
2020-04-01 19:24:30 -05:00
|
|
|
|
|
|
|
class Lazy<T> {
|
|
|
|
val: undefined | T;
|
|
|
|
|
2020-04-01 19:35:58 -05:00
|
|
|
constructor(private readonly compute: () => undefined | T) {}
|
2020-04-01 19:24:30 -05:00
|
|
|
|
|
|
|
get() {
|
|
|
|
return this.val ?? (this.val = this.compute());
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.val = undefined;
|
|
|
|
}
|
|
|
|
}
|