Skip to content

Commit ef376cb

Browse files
Use WeakMap to avoid memory leaks
1 parent 3fb6462 commit ef376cb

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

editors/code/src/commands/inlay_hints.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
2121

2222
export class HintsUpdater {
2323
private displayHints = true;
24-
private drawnDecorations = new Map<string, vscode.DecorationOptions[]>();
24+
private drawnDecorations = new WeakMap<
25+
vscode.Uri,
26+
vscode.DecorationOptions[]
27+
>();
2528

2629
public async loadHints(editor?: vscode.TextEditor): Promise<void> {
2730
if (this.displayHints) {
@@ -48,7 +51,7 @@ export class HintsUpdater {
4851
public async toggleHintsDisplay(displayHints: boolean): Promise<void> {
4952
if (this.displayHints !== displayHints) {
5053
this.displayHints = displayHints;
51-
this.drawnDecorations.clear();
54+
this.drawnDecorations = new WeakMap();
5255

5356
if (displayHints) {
5457
return this.updateHints();
@@ -78,7 +81,7 @@ export class HintsUpdater {
7881
}
7982

8083
return await this.updateDecorationsFromServer(
81-
document.uri.toString(),
84+
document.uri,
8285
editor
8386
);
8487
}
@@ -88,10 +91,10 @@ export class HintsUpdater {
8891
}
8992

9093
private async updateDecorationsFromServer(
91-
documentUri: string,
94+
documentUri: vscode.Uri,
9295
editor: TextEditor
9396
): Promise<void> {
94-
const newHints = await this.queryHints(documentUri);
97+
const newHints = await this.queryHints(documentUri.toString());
9598
if (newHints != null) {
9699
const newDecorations = newHints.map(hint => ({
97100
range: hint.range,
@@ -127,9 +130,9 @@ export class HintsUpdater {
127130
);
128131
}
129132

130-
private getEditorDocumentUri(editor?: vscode.TextEditor): string | null {
133+
private getEditorDocumentUri(editor?: vscode.TextEditor): vscode.Uri | null {
131134
if (editor && this.isRustDocument(editor.document)) {
132-
return editor.document.uri.toString();
135+
return editor.document.uri;
133136
}
134137
return null;
135138
}

0 commit comments

Comments
 (0)