Skip to content
This repository was archived by the owner on Nov 18, 2022. It is now read-only.

Commit f4c9ac5

Browse files
author
Jonas Schievink
committed
Add a command to compute memory usage statistics
1 parent 5b90408 commit f4c9ac5

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

rust-analyzer/editors/code/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"activationEvents": [
6262
"onLanguage:rust",
6363
"onCommand:rust-analyzer.analyzerStatus",
64+
"onCommand:rust-analyzer.memoryUsage",
6465
"onCommand:rust-analyzer.reloadWorkspace",
6566
"workspaceContains:**/Cargo.toml"
6667
],
@@ -142,6 +143,11 @@
142143
"title": "Status",
143144
"category": "Rust Analyzer"
144145
},
146+
{
147+
"command": "rust-analyzer.memoryUsage",
148+
"title": "Memory Usage (Clears Database)",
149+
"category": "Rust Analyzer"
150+
},
145151
{
146152
"command": "rust-analyzer.reloadWorkspace",
147153
"title": "Reload workspace",
@@ -843,6 +849,10 @@
843849
"command": "rust-analyzer.analyzerStatus",
844850
"when": "inRustProject"
845851
},
852+
{
853+
"command": "rust-analyzer.memoryUsage",
854+
"when": "inRustProject"
855+
},
846856
{
847857
"command": "rust-analyzer.reloadWorkspace",
848858
"when": "inRustProject"

rust-analyzer/editors/code/src/commands.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,38 @@ export function analyzerStatus(ctx: Ctx): Cmd {
5555
};
5656
}
5757

58+
export function memoryUsage(ctx: Ctx): Cmd {
59+
const tdcp = new class implements vscode.TextDocumentContentProvider {
60+
readonly uri = vscode.Uri.parse('rust-analyzer-memory://memory');
61+
readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>();
62+
63+
provideTextDocumentContent(_uri: vscode.Uri): vscode.ProviderResult<string> {
64+
if (!vscode.window.activeTextEditor) return '';
65+
66+
return ctx.client.sendRequest(ra.memoryUsage, null).then((mem) => {
67+
return 'Per-query memory usage:\n' + mem + '\n(note: database has been cleared)';
68+
});
69+
}
70+
71+
get onDidChange(): vscode.Event<vscode.Uri> {
72+
return this.eventEmitter.event;
73+
}
74+
}();
75+
76+
ctx.pushCleanup(
77+
vscode.workspace.registerTextDocumentContentProvider(
78+
'rust-analyzer-memory',
79+
tdcp,
80+
),
81+
);
82+
83+
return async () => {
84+
tdcp.eventEmitter.fire(tdcp.uri);
85+
const document = await vscode.workspace.openTextDocument(tdcp.uri);
86+
return vscode.window.showTextDocument(document, vscode.ViewColumn.Two, true);
87+
};
88+
}
89+
5890
export function matchingBrace(ctx: Ctx): Cmd {
5991
return async () => {
6092
const editor = ctx.activeRustEditor;

rust-analyzer/editors/code/src/lsp_ext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import * as lc from "vscode-languageclient";
66

77
export const analyzerStatus = new lc.RequestType<null, string, void>("rust-analyzer/analyzerStatus");
8+
export const memoryUsage = new lc.RequestType<null, string, void>("rust-analyzer/memoryUsage");
89

910
export type Status = "loading" | "ready" | "invalid" | "needsReload";
1011
export const status = new lc.NotificationType<Status>("rust-analyzer/status");

rust-analyzer/editors/code/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ async function tryActivate(context: vscode.ExtensionContext) {
9696
});
9797

9898
ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
99+
ctx.registerCommand('memoryUsage', commands.memoryUsage);
99100
ctx.registerCommand('reloadWorkspace', commands.reloadWorkspace);
100101
ctx.registerCommand('matchingBrace', commands.matchingBrace);
101102
ctx.registerCommand('joinLines', commands.joinLines);

0 commit comments

Comments
 (0)