]> git.lizzy.rs Git - rust.git/commitdiff
Encapsulate inlay hints activation
authorAleksey Kladov <aleksey.kladov@gmail.com>
Mon, 30 Dec 2019 19:21:25 +0000 (20:21 +0100)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Mon, 30 Dec 2019 19:24:30 +0000 (20:24 +0100)
editors/code/src/inlay_hints.ts
editors/code/src/main.ts

index b975915caedfc449eb13b29b432b60707aac218f..4581e22782f08011d2429753c5b41cc31dc45d83 100644 (file)
@@ -1,6 +1,42 @@
 import * as vscode from 'vscode';
 import * as lc from 'vscode-languageclient';
 import { Server } from './server';
+import { Ctx } from './ctx';
+
+export function activateInlayHints(ctx: Ctx) {
+    const hintsUpdater = new HintsUpdater();
+    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();
+            },
+        );
+
+        ctx.pushCleanup(
+            vscode.window.onDidChangeVisibleTextEditors(_ =>
+                hintsUpdater.refreshHintsForVisibleEditors(),
+            ),
+        );
+        ctx.pushCleanup(
+            vscode.workspace.onDidChangeTextDocument(e =>
+                hintsUpdater.refreshHintsForVisibleEditors(e),
+            ),
+        );
+        ctx.pushCleanup(
+            vscode.workspace.onDidChangeConfiguration(_ =>
+                hintsUpdater.toggleHintsDisplay(
+                    Server.config.displayInlayHints,
+                ),
+            ),
+        );
+    });
+}
 
 interface InlayHintsParams {
     textDocument: lc.TextDocumentIdentifier;
@@ -18,7 +54,7 @@ const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
     },
 });
 
-export class HintsUpdater {
+class HintsUpdater {
     private displayHints = true;
 
     public async toggleHintsDisplay(displayHints: boolean): Promise<void> {
index d6c210579a2cca7d666716243bf5a8ec65c60acf..7e63a9cac23ef1e130ac153807a0f52fbe6898c6 100644 (file)
@@ -2,7 +2,7 @@ import * as vscode from 'vscode';
 import * as lc from 'vscode-languageclient';
 
 import * as commands from './commands';
-import { HintsUpdater } from './inlay_hints';
+import { activateInlayHints } from './inlay_hints';
 import { StatusDisplay } from './status_display';
 import * as events from './events';
 import * as notifications from './notifications';
@@ -37,10 +37,6 @@ export async function activate(context: vscode.ExtensionContext) {
     );
     ctx.pushCleanup(watchStatus);
 
-    function disposeOnDeactivation(disposable: vscode.Disposable) {
-        context.subscriptions.push(disposable);
-    }
-
     // Notifications are events triggered by the language server
     const allNotifications: [string, lc.GenericNotificationHandler][] = [
         [
@@ -71,38 +67,7 @@ export async function activate(context: vscode.ExtensionContext) {
     }
 
     if (Server.config.displayInlayHints) {
-        const hintsUpdater = new HintsUpdater();
-        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.onDidChangeVisibleTextEditors(_ =>
-                    hintsUpdater.refreshHintsForVisibleEditors(),
-                ),
-            );
-            disposeOnDeactivation(
-                vscode.workspace.onDidChangeTextDocument(e =>
-                    hintsUpdater.refreshHintsForVisibleEditors(e),
-                ),
-            );
-            disposeOnDeactivation(
-                vscode.workspace.onDidChangeConfiguration(_ =>
-                    hintsUpdater.toggleHintsDisplay(
-                        Server.config.displayInlayHints,
-                    ),
-                ),
-            );
-        });
+        activateInlayHints(ctx);
     }
 }