From f44c4b61e131284287b24dea6da6324cbe9cb252 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 7 Jul 2020 12:10:14 +0200 Subject: Add a command to compute memory usage statistics --- editors/code/package.json | 10 ++++++++++ editors/code/src/commands.ts | 32 ++++++++++++++++++++++++++++++++ editors/code/src/lsp_ext.ts | 1 + editors/code/src/main.ts | 1 + 4 files changed, 44 insertions(+) (limited to 'editors/code') diff --git a/editors/code/package.json b/editors/code/package.json index 4b47fc9d3..743a2290c 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -61,6 +61,7 @@ "activationEvents": [ "onLanguage:rust", "onCommand:rust-analyzer.analyzerStatus", + "onCommand:rust-analyzer.memoryUsage", "onCommand:rust-analyzer.reloadWorkspace", "workspaceContains:**/Cargo.toml" ], @@ -142,6 +143,11 @@ "title": "Status", "category": "Rust Analyzer" }, + { + "command": "rust-analyzer.memoryUsage", + "title": "Memory Usage (Clears Database)", + "category": "Rust Analyzer" + }, { "command": "rust-analyzer.reloadWorkspace", "title": "Reload workspace", @@ -843,6 +849,10 @@ "command": "rust-analyzer.analyzerStatus", "when": "inRustProject" }, + { + "command": "rust-analyzer.memoryUsage", + "when": "inRustProject" + }, { "command": "rust-analyzer.reloadWorkspace", "when": "inRustProject" diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index 19a9c2a0d..1f3a7cf7e 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts @@ -55,6 +55,38 @@ export function analyzerStatus(ctx: Ctx): Cmd { }; } +export function memoryUsage(ctx: Ctx): Cmd { + const tdcp = new class implements vscode.TextDocumentContentProvider { + readonly uri = vscode.Uri.parse('rust-analyzer-memory://memory'); + readonly eventEmitter = new vscode.EventEmitter(); + + provideTextDocumentContent(_uri: vscode.Uri): vscode.ProviderResult { + if (!vscode.window.activeTextEditor) return ''; + + return ctx.client.sendRequest(ra.memoryUsage, null).then((mem) => { + return 'Per-query memory usage:\n' + mem + '\n(note: database has been cleared)'; + }); + } + + get onDidChange(): vscode.Event { + return this.eventEmitter.event; + } + }(); + + ctx.pushCleanup( + vscode.workspace.registerTextDocumentContentProvider( + 'rust-analyzer-memory', + tdcp, + ), + ); + + return async () => { + tdcp.eventEmitter.fire(tdcp.uri); + const document = await vscode.workspace.openTextDocument(tdcp.uri); + return vscode.window.showTextDocument(document, vscode.ViewColumn.Two, true); + }; +} + export function matchingBrace(ctx: Ctx): Cmd { return async () => { const editor = ctx.activeRustEditor; diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index bf4703239..5f32cb40e 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts @@ -5,6 +5,7 @@ import * as lc from "vscode-languageclient"; export const analyzerStatus = new lc.RequestType("rust-analyzer/analyzerStatus"); +export const memoryUsage = new lc.RequestType("rust-analyzer/memoryUsage"); export type Status = "loading" | "ready" | "invalid" | "needsReload"; export const status = new lc.NotificationType("rust-analyzer/status"); diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 4b990afa1..eda95ae5c 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -96,6 +96,7 @@ async function tryActivate(context: vscode.ExtensionContext) { }); ctx.registerCommand('analyzerStatus', commands.analyzerStatus); + ctx.registerCommand('memoryUsage', commands.memoryUsage); ctx.registerCommand('reloadWorkspace', commands.reloadWorkspace); ctx.registerCommand('matchingBrace', commands.matchingBrace); ctx.registerCommand('joinLines', commands.joinLines); -- cgit v1.2.3