From 2c02aebeb560b6c388a8b94723042b1e25eb49e9 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 5 Aug 2019 00:23:58 +0300 Subject: Query less hints on file open --- editors/code/src/commands/inlay_hints.ts | 52 +++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 18 deletions(-) (limited to 'editors') diff --git a/editors/code/src/commands/inlay_hints.ts b/editors/code/src/commands/inlay_hints.ts index 3ba9da48b..5af3a69bc 100644 --- a/editors/code/src/commands/inlay_hints.ts +++ b/editors/code/src/commands/inlay_hints.ts @@ -21,32 +21,36 @@ const typeHintDecorationType = vscode.window.createTextEditorDecorationType({ export class HintsUpdater { private displayHints = true; + private drawnDecorations = new Map(); - public async loadHints( - editor: vscode.TextEditor | undefined - ): Promise { - if ( - this.displayHints && - editor !== undefined && - this.isRustDocument(editor.document) - ) { - await this.updateDecorationsFromServer( - editor.document.uri.toString(), - editor - ); + public async loadHints(editor?: vscode.TextEditor): Promise { + if (this.displayHints) { + const documentUri = this.getEditorDocumentUri(editor); + if (documentUri !== null) { + const latestDecorations = this.drawnDecorations.get(documentUri); + if (latestDecorations === undefined) { + await this.updateDecorationsFromServer( + documentUri, + editor! + ); + } else { + await editor!.setDecorations(typeHintDecorationType, latestDecorations); + } + } } } public async toggleHintsDisplay(displayHints: boolean): Promise { if (this.displayHints !== displayHints) { this.displayHints = displayHints; + this.drawnDecorations.clear(); if (displayHints) { return this.updateHints(); } else { const editor = vscode.window.activeTextEditor; - if (editor != null) { - return editor.setDecorations(typeHintDecorationType, []); + if (this.getEditorDocumentUri(editor) !== null) { + return editor!.setDecorations(typeHintDecorationType, []); } } } @@ -85,10 +89,15 @@ export class HintsUpdater { range: hint.range, renderOptions: { after: { contentText: `: ${hint.label}` } } })); - return editor.setDecorations( - typeHintDecorationType, - newDecorations - ); + + this.drawnDecorations.set(documentUri, newDecorations); + + if (this.getEditorDocumentUri(vscode.window.activeTextEditor) === documentUri) { + return editor.setDecorations( + typeHintDecorationType, + newDecorations + ); + } } } @@ -106,4 +115,11 @@ export class HintsUpdater { ) ); } + + private getEditorDocumentUri(editor?: vscode.TextEditor): string | null { + if (editor && this.isRustDocument(editor.document)) { + return editor.document.uri.toString(); + } + return null; + } } -- cgit v1.2.3 From 3fb6462d54c498bfd8835dea29ead19d95099625 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 5 Aug 2019 00:47:14 +0300 Subject: Style and test fixes --- editors/code/src/commands/inlay_hints.ts | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'editors') diff --git a/editors/code/src/commands/inlay_hints.ts b/editors/code/src/commands/inlay_hints.ts index 5af3a69bc..11a2cfac5 100644 --- a/editors/code/src/commands/inlay_hints.ts +++ b/editors/code/src/commands/inlay_hints.ts @@ -27,14 +27,19 @@ export class HintsUpdater { if (this.displayHints) { const documentUri = this.getEditorDocumentUri(editor); if (documentUri !== null) { - const latestDecorations = this.drawnDecorations.get(documentUri); + const latestDecorations = this.drawnDecorations.get( + documentUri + ); if (latestDecorations === undefined) { await this.updateDecorationsFromServer( documentUri, editor! ); } else { - await editor!.setDecorations(typeHintDecorationType, latestDecorations); + await editor!.setDecorations( + typeHintDecorationType, + latestDecorations + ); } } } @@ -48,9 +53,12 @@ export class HintsUpdater { if (displayHints) { return this.updateHints(); } else { - const editor = vscode.window.activeTextEditor; - if (this.getEditorDocumentUri(editor) !== null) { - return editor!.setDecorations(typeHintDecorationType, []); + const currentEditor = vscode.window.activeTextEditor; + if (this.getEditorDocumentUri(currentEditor) !== null) { + return currentEditor!.setDecorations( + typeHintDecorationType, + [] + ); } } } @@ -92,7 +100,10 @@ export class HintsUpdater { this.drawnDecorations.set(documentUri, newDecorations); - if (this.getEditorDocumentUri(vscode.window.activeTextEditor) === documentUri) { + if ( + this.getEditorDocumentUri(vscode.window.activeTextEditor) === + documentUri + ) { return editor.setDecorations( typeHintDecorationType, newDecorations -- cgit v1.2.3 From f358b4c0c05b20bd438f5fe3082a53dbe5b90e88 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 5 Aug 2019 11:07:27 +0300 Subject: Use WeakMap to avoid memory leaks --- editors/code/src/commands/inlay_hints.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'editors') diff --git a/editors/code/src/commands/inlay_hints.ts b/editors/code/src/commands/inlay_hints.ts index 11a2cfac5..aad28d564 100644 --- a/editors/code/src/commands/inlay_hints.ts +++ b/editors/code/src/commands/inlay_hints.ts @@ -21,7 +21,10 @@ const typeHintDecorationType = vscode.window.createTextEditorDecorationType({ export class HintsUpdater { private displayHints = true; - private drawnDecorations = new Map(); + private drawnDecorations = new WeakMap< + vscode.Uri, + vscode.DecorationOptions[] + >(); public async loadHints(editor?: vscode.TextEditor): Promise { if (this.displayHints) { @@ -48,7 +51,7 @@ export class HintsUpdater { public async toggleHintsDisplay(displayHints: boolean): Promise { if (this.displayHints !== displayHints) { this.displayHints = displayHints; - this.drawnDecorations.clear(); + this.drawnDecorations = new WeakMap(); if (displayHints) { return this.updateHints(); @@ -77,10 +80,7 @@ export class HintsUpdater { return; } - return await this.updateDecorationsFromServer( - document.uri.toString(), - editor - ); + return await this.updateDecorationsFromServer(document.uri, editor); } private isRustDocument(document: vscode.TextDocument): boolean { @@ -88,10 +88,10 @@ export class HintsUpdater { } private async updateDecorationsFromServer( - documentUri: string, + documentUri: vscode.Uri, editor: TextEditor ): Promise { - const newHints = await this.queryHints(documentUri); + const newHints = await this.queryHints(documentUri.toString()); if (newHints != null) { const newDecorations = newHints.map(hint => ({ range: hint.range, @@ -127,9 +127,11 @@ export class HintsUpdater { ); } - private getEditorDocumentUri(editor?: vscode.TextEditor): string | null { + private getEditorDocumentUri( + editor?: vscode.TextEditor + ): vscode.Uri | null { if (editor && this.isRustDocument(editor.document)) { - return editor.document.uri.toString(); + return editor.document.uri; } return null; } -- cgit v1.2.3 From 777552b6a8e534ef3780ecf30a432df1a48fd25c Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 5 Aug 2019 13:31:00 +0300 Subject: Cache decorations before the first change only --- editors/code/src/commands/inlay_hints.ts | 41 ++++++++++++++++---------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'editors') diff --git a/editors/code/src/commands/inlay_hints.ts b/editors/code/src/commands/inlay_hints.ts index aad28d564..d6d1f61f9 100644 --- a/editors/code/src/commands/inlay_hints.ts +++ b/editors/code/src/commands/inlay_hints.ts @@ -21,8 +21,8 @@ const typeHintDecorationType = vscode.window.createTextEditorDecorationType({ export class HintsUpdater { private displayHints = true; - private drawnDecorations = new WeakMap< - vscode.Uri, + private decorationsSinceLastChange = new Map< + string, vscode.DecorationOptions[] >(); @@ -30,8 +30,8 @@ export class HintsUpdater { if (this.displayHints) { const documentUri = this.getEditorDocumentUri(editor); if (documentUri !== null) { - const latestDecorations = this.drawnDecorations.get( - documentUri + const latestDecorations = this.decorationsSinceLastChange.get( + documentUri.toString() ); if (latestDecorations === undefined) { await this.updateDecorationsFromServer( @@ -51,7 +51,7 @@ export class HintsUpdater { public async toggleHintsDisplay(displayHints: boolean): Promise { if (this.displayHints !== displayHints) { this.displayHints = displayHints; - this.drawnDecorations = new WeakMap(); + this.decorationsSinceLastChange.clear(); if (displayHints) { return this.updateHints(); @@ -72,14 +72,15 @@ export class HintsUpdater { return; } const editor = vscode.window.activeTextEditor; - if (editor == null) { + if (editor === undefined) { return; } - const document = cause == null ? editor.document : cause.document; + const document = cause === undefined ? editor.document : cause.document; if (!this.isRustDocument(document)) { return; } + this.decorationsSinceLastChange.clear(); return await this.updateDecorationsFromServer(document.uri, editor); } @@ -92,23 +93,23 @@ export class HintsUpdater { editor: TextEditor ): Promise { const newHints = await this.queryHints(documentUri.toString()); - if (newHints != null) { + if ( + newHints !== null && + this.getEditorDocumentUri(vscode.window.activeTextEditor) === + documentUri + ) { const newDecorations = newHints.map(hint => ({ range: hint.range, renderOptions: { after: { contentText: `: ${hint.label}` } } })); - - this.drawnDecorations.set(documentUri, newDecorations); - - if ( - this.getEditorDocumentUri(vscode.window.activeTextEditor) === - documentUri - ) { - return editor.setDecorations( - typeHintDecorationType, - newDecorations - ); - } + this.decorationsSinceLastChange.set( + documentUri.toString(), + newDecorations + ); + return editor.setDecorations( + typeHintDecorationType, + newDecorations + ); } } -- cgit v1.2.3 From c5598d9ade92e9ec4474a14229bb34a44a4edad5 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 5 Aug 2019 22:31:12 +0300 Subject: Avoid shared mutable state --- editors/code/src/commands/inlay_hints.ts | 106 ++++++++++++------------------- editors/code/src/extension.ts | 20 ++++-- 2 files changed, 55 insertions(+), 71 deletions(-) (limited to 'editors') diff --git a/editors/code/src/commands/inlay_hints.ts b/editors/code/src/commands/inlay_hints.ts index d6d1f61f9..5393a2bc9 100644 --- a/editors/code/src/commands/inlay_hints.ts +++ b/editors/code/src/commands/inlay_hints.ts @@ -21,67 +21,57 @@ const typeHintDecorationType = vscode.window.createTextEditorDecorationType({ export class HintsUpdater { private displayHints = true; - private decorationsSinceLastChange = new Map< - string, - vscode.DecorationOptions[] - >(); - - public async loadHints(editor?: vscode.TextEditor): Promise { - if (this.displayHints) { - const documentUri = this.getEditorDocumentUri(editor); - if (documentUri !== null) { - const latestDecorations = this.decorationsSinceLastChange.get( - documentUri.toString() - ); - if (latestDecorations === undefined) { - await this.updateDecorationsFromServer( - documentUri, - editor! - ); - } else { - await editor!.setDecorations( - typeHintDecorationType, - latestDecorations - ); - } - } - } - } public async toggleHintsDisplay(displayHints: boolean): Promise { if (this.displayHints !== displayHints) { this.displayHints = displayHints; - this.decorationsSinceLastChange.clear(); - - if (displayHints) { - return this.updateHints(); - } else { - const currentEditor = vscode.window.activeTextEditor; - if (this.getEditorDocumentUri(currentEditor) !== null) { - return currentEditor!.setDecorations( - typeHintDecorationType, - [] - ); - } - } + return this.refreshVisibleEditorsHints( + displayHints ? undefined : [] + ); } } - public async updateHints(cause?: TextDocumentChangeEvent): Promise { + public async refreshHintsForVisibleEditors( + cause?: TextDocumentChangeEvent + ): Promise { if (!this.displayHints) { return; } - const editor = vscode.window.activeTextEditor; - if (editor === undefined) { + if ( + cause !== undefined && + (cause.contentChanges.length === 0 || + !this.isRustDocument(cause.document)) + ) { return; } - const document = cause === undefined ? editor.document : cause.document; - if (!this.isRustDocument(document)) { - return; + return this.refreshVisibleEditorsHints(); + } + + private async refreshVisibleEditorsHints( + newDecorations?: vscode.DecorationOptions[] + ) { + const promises: Array> = []; + + for (const rustEditor of vscode.window.visibleTextEditors.filter( + editor => this.isRustDocument(editor.document) + )) { + if (newDecorations !== undefined) { + promises.push( + Promise.resolve( + rustEditor.setDecorations( + typeHintDecorationType, + newDecorations + ) + ) + ); + } else { + promises.push(this.updateDecorationsFromServer(rustEditor)); + } } - this.decorationsSinceLastChange.clear(); - return await this.updateDecorationsFromServer(document.uri, editor); + for (const promise of promises) { + await promise; + } } private isRustDocument(document: vscode.TextDocument): boolean { @@ -89,23 +79,14 @@ export class HintsUpdater { } private async updateDecorationsFromServer( - documentUri: vscode.Uri, editor: TextEditor ): Promise { - const newHints = await this.queryHints(documentUri.toString()); - if ( - newHints !== null && - this.getEditorDocumentUri(vscode.window.activeTextEditor) === - documentUri - ) { + const newHints = await this.queryHints(editor.document.uri.toString()); + if (newHints !== null) { const newDecorations = newHints.map(hint => ({ range: hint.range, renderOptions: { after: { contentText: `: ${hint.label}` } } })); - this.decorationsSinceLastChange.set( - documentUri.toString(), - newDecorations - ); return editor.setDecorations( typeHintDecorationType, newDecorations @@ -127,13 +108,4 @@ export class HintsUpdater { ) ); } - - private getEditorDocumentUri( - editor?: vscode.TextEditor - ): vscode.Uri | null { - if (editor && this.isRustDocument(editor.document)) { - return editor.document.uri; - } - return null; - } } diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts index c6efc2e7e..39fe6efd8 100644 --- a/editors/code/src/extension.ts +++ b/editors/code/src/extension.ts @@ -151,15 +151,27 @@ export function activate(context: vscode.ExtensionContext) { if (Server.config.displayInlayHints) { const hintsUpdater = new HintsUpdater(); - hintsUpdater.loadHints(vscode.window.activeTextEditor).then(() => { + hintsUpdater.refreshHintsForVisibleEditors().then(() => { + // vscode may ignore top level hintsUpdater.refreshHintsForVisibleEditors() + // so update the hints once when the focus changes to guarantee their presence + let editorChangeDisposable: vscode.Disposable | null = null; + editorChangeDisposable = vscode.window.onDidChangeActiveTextEditor( + _ => { + if (editorChangeDisposable !== null) { + editorChangeDisposable.dispose(); + } + return hintsUpdater.refreshHintsForVisibleEditors(); + } + ); + disposeOnDeactivation( - vscode.window.onDidChangeActiveTextEditor(editor => - hintsUpdater.loadHints(editor) + vscode.window.onDidChangeVisibleTextEditors(_ => + hintsUpdater.refreshHintsForVisibleEditors() ) ); disposeOnDeactivation( vscode.workspace.onDidChangeTextDocument(e => - hintsUpdater.updateHints(e) + hintsUpdater.refreshHintsForVisibleEditors(e) ) ); disposeOnDeactivation( -- cgit v1.2.3