Skip to content

Commit f358b4c

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

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

editors/code/src/commands/inlay_hints.ts

Lines changed: 12 additions & 10 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();
@@ -77,21 +80,18 @@ export class HintsUpdater {
7780
return;
7881
}
7982

80-
return await this.updateDecorationsFromServer(
81-
document.uri.toString(),
82-
editor
83-
);
83+
return await this.updateDecorationsFromServer(document.uri, editor);
8484
}
8585

8686
private isRustDocument(document: vscode.TextDocument): boolean {
8787
return document && document.languageId === 'rust';
8888
}
8989

9090
private async updateDecorationsFromServer(
91-
documentUri: string,
91+
documentUri: vscode.Uri,
9292
editor: TextEditor
9393
): Promise<void> {
94-
const newHints = await this.queryHints(documentUri);
94+
const newHints = await this.queryHints(documentUri.toString());
9595
if (newHints != null) {
9696
const newDecorations = newHints.map(hint => ({
9797
range: hint.range,
@@ -127,9 +127,11 @@ export class HintsUpdater {
127127
);
128128
}
129129

130-
private getEditorDocumentUri(editor?: vscode.TextEditor): string | null {
130+
private getEditorDocumentUri(
131+
editor?: vscode.TextEditor
132+
): vscode.Uri | null {
131133
if (editor && this.isRustDocument(editor.document)) {
132-
return editor.document.uri.toString();
134+
return editor.document.uri;
133135
}
134136
return null;
135137
}

0 commit comments

Comments
 (0)