about summary refs log tree commit diff
path: root/editors/code/src
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src')
-rw-r--r--editors/code/src/commands/inlay_hints.ts76
-rw-r--r--editors/code/src/extension.ts20
2 files changed, 55 insertions, 41 deletions
diff --git a/editors/code/src/commands/inlay_hints.ts b/editors/code/src/commands/inlay_hints.ts
index 3ba9da48b66..5393a2bc928 100644
--- a/editors/code/src/commands/inlay_hints.ts
+++ b/editors/code/src/commands/inlay_hints.ts
@@ -22,53 +22,56 @@ const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
 export class HintsUpdater {
     private displayHints = true;
 
-    public async loadHints(
-        editor: vscode.TextEditor | undefined
-    ): Promise<void> {
-        if (
-            this.displayHints &&
-            editor !== undefined &&
-            this.isRustDocument(editor.document)
-        ) {
-            await this.updateDecorationsFromServer(
-                editor.document.uri.toString(),
-                editor
-            );
-        }
-    }
-
     public async toggleHintsDisplay(displayHints: boolean): Promise<void> {
         if (this.displayHints !== displayHints) {
             this.displayHints = displayHints;
-
-            if (displayHints) {
-                return this.updateHints();
-            } else {
-                const editor = vscode.window.activeTextEditor;
-                if (editor != null) {
-                    return editor.setDecorations(typeHintDecorationType, []);
-                }
-            }
+            return this.refreshVisibleEditorsHints(
+                displayHints ? undefined : []
+            );
         }
     }
 
-    public async updateHints(cause?: TextDocumentChangeEvent): Promise<void> {
+    public async refreshHintsForVisibleEditors(
+        cause?: TextDocumentChangeEvent
+    ): Promise<void> {
         if (!this.displayHints) {
             return;
         }
-        const editor = vscode.window.activeTextEditor;
-        if (editor == null) {
+        if (
+            cause !== undefined &&
+            (cause.contentChanges.length === 0 ||
+                !this.isRustDocument(cause.document))
+        ) {
             return;
         }
-        const document = cause == null ? editor.document : cause.document;
-        if (!this.isRustDocument(document)) {
-            return;
+        return this.refreshVisibleEditorsHints();
+    }
+
+    private async refreshVisibleEditorsHints(
+        newDecorations?: vscode.DecorationOptions[]
+    ) {
+        const promises: Array<Promise<void>> = [];
+
+        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));
+            }
         }
 
-        return await this.updateDecorationsFromServer(
-            document.uri.toString(),
-            editor
-        );
+        for (const promise of promises) {
+            await promise;
+        }
     }
 
     private isRustDocument(document: vscode.TextDocument): boolean {
@@ -76,11 +79,10 @@ export class HintsUpdater {
     }
 
     private async updateDecorationsFromServer(
-        documentUri: string,
         editor: TextEditor
     ): Promise<void> {
-        const newHints = await this.queryHints(documentUri);
-        if (newHints != null) {
+        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}` } }
diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts
index c6efc2e7e04..39fe6efd889 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(