From 271ec6b990523c79f93468a5b0ab5e1aceab50f6 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 21 May 2021 23:59:52 +0200 Subject: Add a "Debug ItemTree" LSP request --- editors/code/package.json | 5 +++++ editors/code/src/commands.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++ editors/code/src/lsp_ext.ts | 6 ++++++ editors/code/src/main.ts | 1 + 4 files changed, 62 insertions(+) (limited to 'editors/code') diff --git a/editors/code/package.json b/editors/code/package.json index 1743b374c..17d9281ff 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -109,6 +109,11 @@ "title": "View Hir", "category": "Rust Analyzer" }, + { + "command": "rust-analyzer.viewItemTree", + "title": "Debug ItemTree", + "category": "Rust Analyzer" + }, { "command": "rust-analyzer.viewCrateGraph", "title": "View Crate Graph", diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index 8ab259af2..8f672e68d 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts @@ -429,6 +429,56 @@ export function viewHir(ctx: Ctx): Cmd { }; } +export function viewItemTree(ctx: Ctx): Cmd { + const tdcp = new class implements vscode.TextDocumentContentProvider { + readonly uri = vscode.Uri.parse('rust-analyzer://viewItemTree/itemtree.txt'); + readonly eventEmitter = new vscode.EventEmitter(); + constructor() { + vscode.workspace.onDidChangeTextDocument(this.onDidChangeTextDocument, this, ctx.subscriptions); + vscode.window.onDidChangeActiveTextEditor(this.onDidChangeActiveTextEditor, this, ctx.subscriptions); + } + + 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 { + const rustEditor = ctx.activeRustEditor; + const client = ctx.client; + if (!rustEditor || !client) return ''; + + const params = { + textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(rustEditor.document), + }; + return client.sendRequest(ra.viewItemTree, params, ct); + } + + get onDidChange(): vscode.Event { + return this.eventEmitter.event; + } + }; + + ctx.pushCleanup(vscode.workspace.registerTextDocumentContentProvider('rust-analyzer', tdcp)); + + return async () => { + const document = await vscode.workspace.openTextDocument(tdcp.uri); + tdcp.eventEmitter.fire(tdcp.uri); + void await vscode.window.showTextDocument(document, { + viewColumn: vscode.ViewColumn.Two, + preserveFocus: true + }); + }; +} + export function viewCrateGraph(ctx: Ctx): Cmd { return async () => { const panel = vscode.window.createWebviewPanel("rust-analyzer.crate-graph", "rust-analyzer crate graph", vscode.ViewColumn.Two); diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index aa745a65c..6d5c2ea72 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts @@ -27,6 +27,12 @@ export const syntaxTree = new lc.RequestType("ru export const viewHir = new lc.RequestType("rust-analyzer/viewHir"); +export interface ViewItemTreeParams { + textDocument: lc.TextDocumentIdentifier; +} + +export const viewItemTree = new lc.RequestType("rust-analyzer/viewItemTree"); + export const viewCrateGraph = new lc.RequestType0("rust-analyzer/viewCrateGraph"); export interface ExpandMacroParams { diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 516322d03..92c797d47 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -106,6 +106,7 @@ async function tryActivate(context: vscode.ExtensionContext) { ctx.registerCommand('parentModule', commands.parentModule); ctx.registerCommand('syntaxTree', commands.syntaxTree); ctx.registerCommand('viewHir', commands.viewHir); + ctx.registerCommand('viewItemTree', commands.viewItemTree); ctx.registerCommand('viewCrateGraph', commands.viewCrateGraph); ctx.registerCommand('expandMacro', commands.expandMacro); ctx.registerCommand('run', commands.run); -- cgit v1.2.3